
    Ed                        d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7m8Z8 d dl9m:Z: d Z;d Z<d Z=d@dZ>d Z?d Z@d ZAdAdZBdAd ZCd! ZDdBd#ZEd$ ZFdBd%ZGd& ZHdCd(ZId) ZJdBd*ZKd+ ZLd, ZMd- ZNdBd.ZOdAd/ZPdAd0ZQd1 ZRdAd2ZSd3 ZTd4 ZUe:rB eV eWe8e;e<e=e?e@eBeCeDeEeFeGeIeKeMe>eNeOePeQeLeReSf                    \  Z;Z<Z=Z?Z@ZBZCZDZEZFZGZIZKZMZ>ZNZOZPZQZLZRZSeBe;feCe;fe7gZXeIeBe;feCe;fe;gfZYeNeEe;feNeEeHe;fe7gZZe@eHfe7gZ[e@e?e@eKe@eMe@e;fZ\e@e?eGe@e?eIfeBeDeIe@feZeXeFeYe@eFeFe[fe7gZ]d5 fd6Z^dDd7Z_d8`                                Za eb eV ecea eV eW ed            je        ea                                                  Zfd9 Zgd'ahd@d:Zid; Zjd< Zkd= Zld> Zmd? Znd'S )E    )defaultdictAdd)Expr)Factors	gcd_termsfactor_terms
expand_mul)Mul)piI)Pow)S)orderedDummy)sympify	bottom_up)binomial)coshsinhtanhcothsechcschHyperbolicFunction)cossintancotseccscsqrtTrigonometricFunction)perfect_power)factor)greedy)identitydebug)SYMPY_DEBUGc                 r    |                                                                                                  S )zSimplification of rational polynomials, trying to simplify
    the expression, e.g. combine things like 3*x + 2*x, etc....
    )normalr(   expandrvs    1lib/python3.11/site-packages/sympy/simplify/fu.pyTR0r3      s*     99;;&&(((    c                 (    d }t          | |          S )zReplace sec, csc with 1/cos, 1/sin

    Examples
    ========

    >>> from sympy.simplify.fu import TR1, sec, csc
    >>> from sympy.abc import x
    >>> TR1(2*csc(x) + sec(x))
    1/cos(x) + 2/sin(x)
    c                     t          | t                    r)| j        d         }t          j        t          |          z  S t          | t                    r)| j        d         }t          j        t          |          z  S | S Nr   )
isinstancer#   argsr   Oner   r$   r    r1   as     r2   fzTR1.<locals>.f4   se    b# 	 
A5Q<C   	 
A5Q<	r4   r   r1   r=   s     r2   TR1r?   (   s#       Rr4   c                 (    d }t          | |          S )a@  Replace tan and cot with sin/cos and cos/sin

    Examples
    ========

    >>> from sympy.simplify.fu import TR2
    >>> from sympy.abc import x
    >>> from sympy import tan, cot, sin, cos
    >>> TR2(tan(x))
    sin(x)/cos(x)
    >>> TR2(cot(x))
    cos(x)/sin(x)
    >>> TR2(tan(tan(x) - sin(x)/cos(x)))
    0

    c                 
   t          | t                    r,| j        d         }t          |          t	          |          z  S t          | t
                    r,| j        d         }t	          |          t          |          z  S | S r7   )r8   r!   r9   r    r   r"   r;   s     r2   r=   zTR2.<locals>.fR   sm    b# 	!
Aq66#a&&= C   	!
Aq66#a&&= 	r4   r   r>   s     r2   TR2rB   @   s#    $   Rr4   Fc                 .    fd}t          | |          S )a  Converts ratios involving sin and cos as follows::
        sin(x)/cos(x) -> tan(x)
        sin(x)/(cos(x) + 1) -> tan(x/2) if half=True

    Examples
    ========

    >>> from sympy.simplify.fu import TR2i
    >>> from sympy.abc import x, a
    >>> from sympy import sin, cos
    >>> TR2i(sin(x)/cos(x))
    tan(x)

    Powers of the numerator and denominator are also recognized

    >>> TR2i(sin(x)**2/(cos(x) + 1)**2, half=True)
    tan(x/2)**2

    The transformation does not take place unless assumptions allow
    (i.e. the base must be positive or the exponent must be an integer
    for both numerator and denominator)

    >>> TR2i(sin(x)**a/(cos(x) + 1)**a)
    sin(x)**a/(cos(x) + 1)**a

    c                 f  	
 | j         s| S |                                 \  		j        sj        r| S fd
	                                		
fdt	          	                                          D             }	s| S                                 
fdt	                                                    D             }s| S 
fd} |	|            ||           g }	D ]M}t          |t                    rt          |j	        d         d          }|v rS|         	|         k    rA|
                    t          |j	        d                   	|         z             d x	|<   |<   r^d|z   }|v rU|         	|         k    rC|
                    t          |j	        d         d	z            	|         z             d x	|<   |<   t          |t                    rut          |j	        d         d          }|v rS|         	|         k    rA|
                    t          |j	        d                   	|          z             d x	|<   |<   vr|j        r|j	        d         t          j        u rt          |j	        d         t                    rt          |j	        d         j	        d         d          }|v rj|         	|         k    rX|         j        s|j        rD|
                    t          |j	        d         d	z            	|          z             d x	|<   |<   O|rxt#          |d
 	                                D             z    t#          d                                 D              z  } | t#          d |D              t#          d |D              z  z  } | S )Nc                     |j         s| j        oS| j        t          t          fv p>o<| j        o5t          | j                  dk    ot          d | j        D                       S )N   c              3   l   K   | ]/}t          d  t          j        |          D                       V  0dS )c              3   j   K   | ].}t          |t                    p|j        o|j        t          u V  /d S N)r8   r   is_Powbase).0ais     r2   	<genexpr>z8TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>.<genexpr>   sR       , , #2s++Kry/KRW^ , , , , , ,r4   N)anyr   	make_argsrL   r<   s     r2   rN   z.TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>   sf       = =01  , ,-**, , , , , = = = = = =r4   )	
is_integeris_positivefuncr    r   is_Addlenr9   rO   )kehalfs     r2   okzTR2i.<locals>.f.<locals>.ok   s     . ?3*$ > *=*=AFq *=  = =56V= = = = =@r4   c                 b    g | ]+} ||                   |                     |          f,S  pop)rL   rW   nrZ   s     r2   
<listcomp>z#TR2i.<locals>.f.<locals>.<listcomp>   :    JJJ1bbAaDkkJ!QUU1XXJJJr4   c                 b    g | ]+} ||                   |                     |          f,S r\   r]   )rL   rW   drZ   s     r2   r`   z#TR2i.<locals>.f.<locals>.<listcomp>   ra   r4   c                    g }| D ]^}|j         rUt          |j                  dk    r=rt          |          nt	          |          }||k    r|                    ||f           _|r}t          |          D ]\  }\  }}| |= |||<   t          |                                 }|D ]<}| |         ||         z   } ||          r|| |<   %|                    ||f           =~d S d S N   )	rU   rV   r9   r(   r	   append	enumerater   as_powers_dict)	rc   ddonenewkrW   knewivrY   rZ   s	          r2   	factorizez"TR2i.<locals>.f.<locals>.factorize   s$   D / /8 /AFa /(,A6!999,q//Dqy /QI... $-dOO # #LAy4!"DGGDz0022 - -A!tAwAr!Qxx - !aV,,,,DD r4   r   Fevaluaterf   rF   c                 "    g | ]\  }}|||z  S r\   r\   rL   brX   s      r2   r`   z#TR2i.<locals>.f.<locals>.<listcomp>   s%    <<<TQ!<AqD<<<r4   c                 "    g | ]\  }}|||z  S r\   r\   rs   s      r2   r`   z#TR2i.<locals>.f.<locals>.<listcomp>   s%    666tq!A6ad666r4   c                     g | ]
\  }}||z  S r\   r\   rs   s      r2   r`   z#TR2i.<locals>.f.<locals>.<listcomp>   s     ///A1///r4   c                     g | ]
\  }}||z  S r\   r\   rs   s      r2   r`   z#TR2i.<locals>.f.<locals>.<listcomp>   s     6N6N6N1q!t6N6N6Nr4   )is_Mulas_numer_denomis_Atomri   listkeysr8   r    r   r9   rg   r!   rU   r   r:   rR   rS   r   items)r1   ndonerj   ro   trW   r<   a1rc   r_   rZ   rY   s           @@@r2   r=   zTR2i.<locals>.fz   s(   y 	I  ""19 		 	I	@ 	@ 	@ 	@ 	@ JJJJJQVVXXJJJ 	IJJJJJQVVXXJJJ 	I	 	 	 	 	 	& 		!U	!U  	' 	'A!S!! 'q	E2226 ,adadl ,HHS^^QqT1222"&&AaD1Q44 ,QBQw ,1R5AaD= ,#afQik"2"2QqT!9:::'++!quAs## 'q	E2226 'adadl 'HHS^^adU2333"&&AaD1Q4 '!( 'qvayAE'9 'qvay#..'q	q)E:::6 'adadl '! ''HHS1--!u4555"&&AaD1Q4 	Pq<<QWWYY<<<<>66qwwyy66678B#/////06N6N6N6N6N1OOOB	r4   r   )r1   rY   r=   s    ` r2   TR2ir   ^   s4    8S S S S Sj Rr4   c                 :    ddl m fd}t          | |          S )aR  Induced formula: example sin(-a) = -sin(a)

    Examples
    ========

    >>> from sympy.simplify.fu import TR3
    >>> from sympy.abc import x, y
    >>> from sympy import pi
    >>> from sympy import cos
    >>> TR3(cos(y - x*(y - x)))
    cos(x*(x - y) + y)
    >>> cos(pi/2 + x)
    -sin(x)
    >>> cos(30*pi/2 + x)
    -cos(x)

    r   signsimpc                 T   t          | t                    s| S |                      | j        d                             } t          | t                    s| S | j        d         t          j        dz  z
  j        t          j        dz  | j        d         z
  j        cxu rdu rn nt          t          t          t          t          t          t          t          t          t          t          t          i} |t          |                    t          j        dz  | j        d         z
            } | S )Nr      rF   T)r8   r&   rT   r9   r   PirS   r   r    r!   r"   r#   r$   type)r1   fmapr   s     r2   r=   zTR3.<locals>.f   s    "344 	IWWXXbgaj))**"344 	IGAJa,a"'!*1D0Q 	5 	5 	5 	5UY 	5 	5 	5 	5 	5c3S#sCc3ODd2hhQ 344B	r4   )sympy.simplify.simplifyr   r   )r1   r=   r   s     @r2   TR3r      sB    $ 100000	 	 	 	 	 Rr4   c                     | S )a  Identify values of special angles.

        a=  0   pi/6        pi/4        pi/3        pi/2
    ----------------------------------------------------
    sin(a)  0   1/2         sqrt(2)/2   sqrt(3)/2   1
    cos(a)  1   sqrt(3)/2   sqrt(2)/2   1/2         0
    tan(a)  0   sqt(3)/3    1           sqrt(3)     --

    Examples
    ========

    >>> from sympy import pi
    >>> from sympy import cos, sin, tan, cot
    >>> for s in (0, pi/6, pi/4, pi/3, pi/2):
    ...    print('%s %s %s %s' % (cos(s), sin(s), tan(s), cot(s)))
    ...
    1 0 0 zoo
    sqrt(3)/2 1/2 sqrt(3)/3 sqrt(3)
    sqrt(2)/2 sqrt(2)/2 1 1
    1/2 sqrt(3)/2 sqrt(3) sqrt(3)/3
    0 1 zoo 0
    r\   r0   s    r2   TR4r      s	    0 Ir4   c                 >    fd}t          | |          S )a+  Helper for TR5 and TR6 to replace f**2 with h(g**2)

    Options
    =======

    max :   controls size of exponent that can appear on f
            e.g. if max=4 then f**4 will be changed to h(g**2)**2.
    pow :   controls whether the exponent must be a perfect power of 2
            e.g. if pow=True (and max >= 6) then f**6 will not be changed
            but f**8 will be changed to h(g**2)**4

    >>> from sympy.simplify.fu import _TR56 as T
    >>> from sympy.abc import x
    >>> from sympy import sin, cos
    >>> h = lambda x: 1 - x
    >>> T(sin(x)**3, sin, cos, h, 4, False)
    (1 - cos(x)**2)*sin(x)
    >>> T(sin(x)**6, sin, cos, h, 6, False)
    (1 - cos(x)**2)**3
    >>> T(sin(x)**6, sin, cos, h, 6, True)
    sin(x)**6
    >>> T(sin(x)**8, sin, cos, h, 10, True)
    (1 - cos(x)**2)**4
    c                    | j         r| j        j        k    s| S | j        j        s| S | j        dk     dk    r| S | j        k    dk    r| S | j        dk    r| S | j        dk    r'  | j        j        d                   dz            S | j        dz  dk    rP| j        dz  } | j        j        d                     | j        j        d                   dz            |z  z  S | j        dk    rd}n;s| j        dz  r| S | j        dz  }n"t          | j                  }|s| S | j        dz  }  | j        j        d                   dz            |z  S )Nr   Trf   rF   r   )rJ   rK   rT   expis_realr9   r'   )r1   rX   pr=   ghmaxpows      r2   _fz_TR56.<locals>._f1  s   
 	 	bgla/ 	Iv~ 	IFQJ4 	IFSLT! 	I6Q; 	I6Q; 	/1QQrw|A''*+++vzQ FAIqa))!!AAbgl1o,>,>,A*B*BA*EEE1 
 6A: IFAI!"&)) IFAI1QQrw|A''*++Q..r4   r   )r1   r=   r   r   r   r   r   s    ````` r2   _TR56r     sG    4!/ !/ !/ !/ !/ !/ !/ !/ !/F Rr4   r   c                 B    t          | t          t          d ||          S )a  Replacement of sin**2 with 1 - cos(x)**2.

    See _TR56 docstring for advanced use of ``max`` and ``pow``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR5
    >>> from sympy.abc import x
    >>> from sympy import sin
    >>> TR5(sin(x)**2)
    1 - cos(x)**2
    >>> TR5(sin(x)**-2)  # unchanged
    sin(x)**(-2)
    >>> TR5(sin(x)**4)
    (1 - cos(x)**2)**2
    c                     d| z
  S re   r\   xs    r2   <lambda>zTR5.<locals>.<lambda>i  
    Q r4   r   r   )r   r    r   r1   r   r   s      r2   TR5r   W  !    $ S#CSAAAAr4   c                 B    t          | t          t          d ||          S )a  Replacement of cos**2 with 1 - sin(x)**2.

    See _TR56 docstring for advanced use of ``max`` and ``pow``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR6
    >>> from sympy.abc import x
    >>> from sympy import cos
    >>> TR6(cos(x)**2)
    1 - sin(x)**2
    >>> TR6(cos(x)**-2)  #unchanged
    cos(x)**(-2)
    >>> TR6(cos(x)**4)
    (1 - sin(x)**2)**2
    c                     d| z
  S re   r\   r   s    r2   r   zTR6.<locals>.<lambda>~  r   r4   r   )r   r   r    r   s      r2   TR6r   l  r   r4   c                 (    d }t          | |          S )a  Lowering the degree of cos(x)**2.

    Examples
    ========

    >>> from sympy.simplify.fu import TR7
    >>> from sympy.abc import x
    >>> from sympy import cos
    >>> TR7(cos(x)**2)
    cos(2*x)/2 + 1/2
    >>> TR7(cos(x)**2 + 1)
    cos(2*x)/2 + 3/2

    c                     | j         r | j        j        t          k    r| j        dk    s| S dt          d| j        j        d         z            z   dz  S )NrF   rf   r   )rJ   rK   rT   r   r   r9   r0   s    r2   r=   zTR7.<locals>.f  sS    	 	bglc1 	bfk 	IC"',q/)***A--r4   r   r>   s     r2   TR7r     s#     . . .
 Rr4   Tc                 .    fd}t          | |          S )aq  Converting products of ``cos`` and/or ``sin`` to a sum or
    difference of ``cos`` and or ``sin`` terms.

    Examples
    ========

    >>> from sympy.simplify.fu import TR8
    >>> from sympy import cos, sin
    >>> TR8(cos(2)*cos(3))
    cos(5)/2 + cos(1)/2
    >>> TR8(cos(2)*sin(3))
    sin(5)/2 + sin(1)/2
    >>> TR8(sin(2)*sin(3))
    -cos(5)/2 + cos(1)/2
    c                 ^	   | j         s;| j        r2| j        j        t          t
          fv r| j        j        s| j        j        s| S rd | 	                                D             \  }}t          |d          }t          |d          }||k    s||k    rpt          ||z            } | j         rW| j        d         j        rEt          | j                  dk    r-| j        d         j        rt!          |                                  } | S t          g t
          g d g i}t%          t!          j        |                     D ]}|j        t          t
          fv r4|t)          |                                       |j        d                    K|j        rx|j        j        rl|j        dk    ra|j        j        t          t
          fv rG|t)          |j                                               |j        j        d         g|j        z             |d                              |           |t                   }|t
                   }|r|s(t          |          dk    st          |          dk    s| S |d          }t1          t          |          t          |                    }t3          |          D ]e}	|                                }
|                                }|                    t          |
|z             t          |
|z
            z   dz             ft          |          dk    rv|                                }
|                                }|                    t	          |
|z             t	          |
|z
            z   dz             t          |          dk    v|r4|                    t	          |                                                     t          |          dk    rw|                                }
|                                }|                    t	          |
|z              t	          |
|z
            z   dz             t          |          dk    w|r4|                    t          |                                                     t          t7          t!          |                     S )Nc                 ,    g | ]}t          |          S r\   r
   rL   rm   s     r2   r`   z"TR8.<locals>.f.<locals>.<listcomp>  s    ???aJqMM???r4   Ffirstr   rF   rf   )rx   rJ   rK   rT   r   r    r   rR   rS   ry   TR8r   r9   is_RationalrV   rU   r   as_coeff_Mulr   rP   r   rg   
is_Integerextendminranger^   r   )r1   r_   rc   newnnewdr9   r<   csrm   r   a2r   s               r2   r=   zTR8.<locals>.f  s9   I	I	 GLS#J&	 V		 #%'"5		
 I 		??2+<+<+>+>???DAqq&&&Dq&&&Dqy 1DAI 1tDy))9 1!7 1BG)1.0gaj.?1boo//0BIRb$+r**++ 		% 		%Av#s# %T!WW$$QVAY////( %qu/ %AEAI %FKC:-% T!&\\"))16;q>*:15*@AAAAT
!!!$$$$II 	a 	3q66A: 	Q! 	IDzAAq 	9 	9ABBKKR"WBG4a78888!ffqj 	9BBKKR"WBG4a7888 !ffqj 	9  	&KKAEEGG%%%!ffqj 	:BBKK#b2g,,R"W5q8999 !ffqj 	:  	&KKAEEGG%%%:c4j))***r4   r   r1   r   r=   s    ` r2   r   r     s/    "5+ 5+ 5+ 5+ 5+n Rr4   c                 (    d }t          | |          S )ac  Sum of ``cos`` or ``sin`` terms as a product of ``cos`` or ``sin``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR9
    >>> from sympy import cos, sin
    >>> TR9(cos(1) + cos(2))
    2*cos(1/2)*cos(3/2)
    >>> TR9(cos(1) + 2*sin(1) + 2*sin(2))
    cos(1) + 4*sin(3/2)*cos(1/2)

    If no change is made by TR9, no re-arrangement of the
    expression will be made. For example, though factoring
    of common term is attempted, if the factored expression
    was not changed, the original expression will be returned:

    >>> TR9(cos(3) + cos(3)*cos(2))
    cos(3) + cos(2)*cos(3)

    c                 B    | j         s| S dfd	t          |           S )NTc                    | j         s| S t          t          | j                            }t	          |          dk    rd}t          t	          |                    D ]_}||         }|t          |dz   t	          |                    D ]1}||         }|||z   } |          }	|	|k    r|	||<   d ||<   d} n2`|r%t          d |D              } | j         r |           } | S t          | }
|
s| S |
\  }}}}}}|ru||k    r4||z  dz  t          ||z   dz            z  t          ||z
  dz            z  S |dk     r||}}d|z  t          ||z   dz            z  t          ||z
  dz            z  S ||k    r4||z  dz  t          ||z   dz            z  t          ||z
  dz            z  S |dk     r||}}d|z  t          ||z   dz            z  t          ||z
  dz            z  S )NrF   Frf   Tc                     g | ]}||S r\   r\   rL   r   s     r2   r`   z.TR9.<locals>.f.<locals>.do.<locals>.<listcomp>#      777bB7r777r4   r   
rU   r{   r   r9   rV   r   r   
trig_splitr   r    )r1   r   r9   hitrm   rM   jajwasnewsplitgcdn1n2r<   rt   iscosdos                    r2   r   zTR9.<locals>.f.<locals>.do  sn    9 	(())D4yyA~ s4yy)) " "AaB ! "1q5#d))44 
" 
"!!W %$ 2g bgg#: "&)DG&*DG"&C!E	"
  $77D7778By $RVV	 %E 	',$CRAu  ;8 Br6!8CQ	NN23Aqy>>AA6  aqA#vc1q5!)nn,S!a%^^;;8 Br6!8CQ	NN23Aqy>>AA6  aqAuS!a%^^+CQ	NN::r4   T)rU   process_common_addends)r1   r   s    @r2   r=   zTR9.<locals>.f  sC    y 	I<	; <	; <	; <	; <	; <	;| &b"---r4   r   r>   s     r2   TR9r     s'    .B. B. B.H Rr4   c                 .    fd}t          | |          S )a  Separate sums in ``cos`` and ``sin``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR10
    >>> from sympy.abc import a, b, c
    >>> from sympy import cos, sin
    >>> TR10(cos(a + b))
    -sin(a)*sin(b) + cos(a)*cos(b)
    >>> TR10(sin(a + b))
    sin(a)*cos(b) + sin(b)*cos(a)
    >>> TR10(sin(a + b + c))
    (-sin(a)*sin(b) + cos(a)*cos(b))*sin(c) +     (sin(a)*cos(b) + sin(b)*cos(a))*cos(c)
    c                    | j         t          t          fvr| S | j         }| j        d         }|j        rr"t          t          |j                            }nt          |j                  }|                                }t          j	        |          }|j        r|t          k    r]t          |          t          t          |          d          z  t          |          t          t          |          d          z  z   S t          |          t          t          |          d          z  t          |          t          t          |          d          z  z
  S |t          k    r?t          |          t          |          z  t          |          t          |          z  z   S t          |          t          |          z  t          |          t          |          z  z
  S | S )Nr   Fr   )rT   r   r    r9   rU   r{   r   r^   r   
_from_argsTR10)r1   r=   argr9   r<   rt   r   s         r2   r=   zTR10.<locals>.fT  s   73*$ 	IGgaj: 	9 &GCH--..CH~~

At$$Ax 98 9q66$s1vvU";";";;AtCFF%88889 9 q66$s1vvU";";";;AtCFF%88889 9 8 9q66#a&&=3q66#a&&=88q66#a&&=3q66#a&&=88	r4   r   r   s    ` r2   r   r   B  s.    $    6 Rr4   c                 R    t           t                       d }t          | |          S )a  Sum of products to function of sum.

    Examples
    ========

    >>> from sympy.simplify.fu import TR10i
    >>> from sympy import cos, sin, sqrt
    >>> from sympy.abc import x

    >>> TR10i(cos(1)*cos(3) + sin(1)*sin(3))
    cos(2)
    >>> TR10i(cos(1)*sin(3) + sin(1)*cos(3) + cos(3))
    cos(3) + sin(4)
    >>> TR10i(sqrt(2)*cos(x)*x + sqrt(6)*sin(x)*x)
    2*sqrt(2)*x*sin(x + pi/6)

    Nc           	         | j         s| S dfd	t          | d           } | j         rt          t                    }| j        D ]}d}|j        rO|j        D ]G}|j        r>|j        t          j	        u r+|j
        j        r||                             |           d} nH|s%|t          j                                     |           g }|D ]}t          |z  t          fD ]}||v rt!          t#          ||                             D ]}||         |         t!          t#          ||                             D ]y}||         |         t%          ||         |         ||         |         z             }	 |	          }
|
|	k    r-|                    |
           d ||         |<   d ||         |<    nz׌|r)t%          |d |                                D             z    } n |           } n| j         | S )NTc                    | j         s| S t          t          | j                            }t	          |          dk    rd}t          t	          |                    D ]_}||         }|t          |dz   t	          |                    D ]1}||         }|||z   } |          }	|	|k    r|	||<   d ||<   d} n2`|r%t          d |D              } | j         r |           } | S t          |ddi}
|
s| S |
\  }}}}}}|r5||z  }||k    r|t          ||z
            z  S |t          ||z             z  S ||z  }||k    r|t          ||z             z  S |t          ||z
            z  S )NrF   Frf   Tc                     g | ]}||S r\   r\   r   s     r2   r`   z0TR10i.<locals>.f.<locals>.do.<locals>.<listcomp>  r   r4   twor   )r1   r   r9   r   rm   rM   r   r   r   r   r   r   r   r   r<   rt   samer   s                    r2   r   zTR10i.<locals>.f.<locals>.do  s    9 	(())D4yyA~ s4yy)) " "AaB ! "1q5#d))44 
" 
"!!W %$ 2g bgg#: "&)DG&*DG"&C!E	"
  $77D7778By $RVV	 /$//E 	&+#CRAt  	&f8 *s1q5zz>)3q1u::~%f8 *s1q5zz>)3q1u::~%r4   c                 D    t          t          | j                            S rI   )tupler   free_symbolsr   s    r2   r   z"TR10i.<locals>.f.<locals>.<lambda>  s    eGAN$;$;<< r4   r   rf   c                 4    g | ]}t          d  |D              S )c                     g | ]}||S r\   r\   r   s     r2   r`   z/TR10i.<locals>.f.<locals>.<listcomp>.<listcomp>  s    (>(>(>2(>(>(>(>r4   r   )rL   rn   s     r2   r`   z$TR10i.<locals>.f.<locals>.<listcomp>  s<     #- #- #- $'(>(>a(>(>(>#? #- #- #-r4   r   )rU   r   r   r{   r9   rx   rJ   r   r   HalfrK   r   rg   r:   _ROOT3	_invROOT3r   rV   r   values)r1   byradr<   r   rM   r9   rt   rm   r   r   r   r   s              @r2   r=   zTR10i.<locals>.f  s   y 	I6	& 6	& 6	& 6	& 6	& 6	&p $<<> >
 i &	%%EW 
+ 
+8 "f " "9 "16)9 " " 2"!"I,,Q///"#C!E +!%L''*** D * * (I. * *AEz *!&s58}}!5!5 * *A$Qx{ ) (%*3uQx==%9%9 	* 	*#(8A; !-$,&)%(1+a*C&D&D&(bgg#&#: !*$(KK$4$4$426E!HQK26E!HQK$)E	!**  4 #- #-"\\^^#- #- #- - / RVVM i &	P 	r4   )_ROOT2_rootsr   r>   s     r2   TR10ir   r  s:    &  i i iV Rr4   Nc                 .    fd}t          | |          S )an  Function of double angle to product. The ``base`` argument can be used
    to indicate what is the un-doubled argument, e.g. if 3*pi/7 is the base
    then cosine and sine functions with argument 6*pi/7 will be replaced.

    Examples
    ========

    >>> from sympy.simplify.fu import TR11
    >>> from sympy import cos, sin, pi
    >>> from sympy.abc import x
    >>> TR11(sin(2*x))
    2*sin(x)*cos(x)
    >>> TR11(cos(2*x))
    -sin(x)**2 + cos(x)**2
    >>> TR11(sin(4*x))
    4*(-sin(x)**2 + cos(x)**2)*sin(x)*cos(x)
    >>> TR11(sin(4*x/3))
    4*(-sin(x/3)**2 + cos(x/3)**2)*sin(x/3)*cos(x/3)

    If the arguments are simply integers, no change is made
    unless a base is provided:

    >>> TR11(cos(2))
    cos(2)
    >>> TR11(cos(4), 2)
    -sin(2)**2 + cos(2)**2

    There is a subtle issue here in that autosimplification will convert
    some higher angles to lower angles

    >>> cos(6*pi/7) + cos(3*pi/7)
    -cos(pi/7) + cos(3*pi/7)

    The 6*pi/7 angle is now pi/7 but can be targeted with TR11 by supplying
    the 3*pi/7 base:

    >>> TR11(_, 3*pi/7)
    -sin(3*pi/7)**2 + cos(3*pi/7)**2 + cos(3*pi/7)

    c                    | j         t          t          fvr| S r| j         } |dz            }t          j        }|j        r|                                \  }}|j         t          t          fvr| S | j        d         |j        d         k    r@t                    }t                    }|t          u r|dz  |dz  z
  |z  S d|z  |z  |z  S | S | j        d         j        s| j        d                             d          \  }}|j	        dz  dk    rq|j	        dz  |z  |j
        z  }t          t          |                    }t          t          |                    }| j         t          k    r	d|z  |z  } n|dz  |dz  z
  } | S )NrF   r   T)rational)rT   r   r    r   r:   rx   r   r9   	is_Numberr   qTR11)	r1   r=   r   cor   r   mr   rK   s	           r2   r=   zTR11.<locals>.f   s   73*$ 	I 	%A$q&		ABx )((Avc3Z' 	wqzQVAY& $IIII8 $qD1a4K++Q3q58OI% 	% 71:**D*99DAqsQw!| %c1fQhqslSNNSNN7c> %1QBBA1B	r4   r   )r1   rK   r=   s    ` r2   r   r     s0    T! ! ! ! !F Rr4   c                 (    d }t          | |          S )a  
    Helper for TR11 to find half-arguments for sin in factors of
    num/den that appear in cos or sin factors in the den/num.

    Examples
    ========

    >>> from sympy.simplify.fu import TR11, _TR11
    >>> from sympy import cos, sin
    >>> from sympy.abc import x
    >>> TR11(sin(x/3)/(cos(x/6)))
    sin(x/3)/cos(x/6)
    >>> _TR11(sin(x/3)/(cos(x/6)))
    2*sin(x/6)
    >>> TR11(sin(x/6)/(sin(x/3)))
    sin(x/6)/sin(x/3)
    >>> _TR11(sin(x/6)/(sin(x/3)))
    1/(2*cos(x/6))

    c                     t          | t                    s| S d }t          ||                                           \  }}d } || ||          }  || ||          } | S )Nc                 4   t          t                    }t          j        |           D ]n}|                                \  }}|j        rN|dk    rH|j        t          t          fv r3|t          |                   
                    |j        d                    o|S r7   )r   setr   rP   as_base_expr   rT   r   r    r   addr9   )flatr9   firt   rX   s        r2   sincos_argsz%_TR11.<locals>.f.<locals>.sincos_args_  s     s##DmD)) 5 5~~''1< 5AE 5v#s+ 5T!WW))!&)444Kr4   c                     |t                    D ]a}|dz  }||t                   v rt          }n||t                    v rt           }n6t          | |          } ||                             |           b| S NrF   )r    r   r   remove)r1   num_argsden_argsnargrY   rT   s         r2   handle_matchz&_TR11.<locals>.f.<locals>.handle_matchk  s     ! 	, 	,Av8C=( DDXc]* DD"d^^%%d++++Ir4   )r8   r   mapry   )r1   r   r   r   r  s        r2   r=   z_TR11.<locals>.f[  s    "d## 	I
	 
	 
	 !b.?.?.A.ABB(	 	 	  \"h11\"h11	r4   r   r>   s     r2   _TR11r  F  s$    *# # #J Rr4   c                 .    fd}t          | |          S )zSeparate sums in ``tan``.

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy import tan
    >>> from sympy.simplify.fu import TR12
    >>> TR12(tan(x + y))
    (tan(x) + tan(y))/(-tan(x)*tan(y) + 1)
    c                    | j         t          k    s| S | j        d         }|j        rr"t	          t          |j                            }nt	          |j                  }|                                }t          j        |          }|j        rt          t          |          d          }nt          |          }t          |          |z   dt          |          |z  z
  z  S | S )Nr   Fr   rf   )
rT   r!   r9   rU   r{   r   r^   r   r   TR12)r1   r   r9   r<   rt   tbr   s         r2   r=   zTR12.<locals>.f  s    w#~ 	Igaj: 	1 &GCH--..CH~~

At$$Ax #a&&...VVFFRK!c!ffRi-00	r4   r   r   s    ` r2   r  r    s.        & Rr4   c                 (    d }t          | |          S )aK  Combine tan arguments as
    (tan(y) + tan(x))/(tan(x)*tan(y) - 1) -> -tan(x + y).

    Examples
    ========

    >>> from sympy.simplify.fu import TR12i
    >>> from sympy import tan
    >>> from sympy.abc import a, b, c
    >>> ta, tb, tc = [tan(i) for i in (a, b, c)]
    >>> TR12i((ta + tb)/(-ta*tb + 1))
    tan(a + b)
    >>> TR12i((ta + tb)/(ta*tb - 1))
    -tan(a + b)
    >>> TR12i((-ta - tb)/(ta*tb - 1))
    tan(a + b)
    >>> eq = (ta + tb)/(-ta*tb + 1)**2*(-3*ta - 3*tc)/(2*(ta*tc - 1))
    >>> TR12i(eq.expand())
    -3*tan(a + b)*tan(a + c)/(2*(tan(a) + tan(b) - 1))
    c                 \   | j         s| j        s	| j        s| S |                                 \  }}|j        r|j        s| S i }d }t          t          j        |                    }t          |          D ]1\  }} ||          }|r2|\  }	}
t          d |
j        D              }t          j        ||<   |	||<   E|j         r@t          |          }|j        r)|                    |j                   t          j        ||<   |j        r|j        j        s|j        j        r ||j                  }|r5|\  }	}
t          d |
j        D              }|j        ||<   |	|j        z  ||<   t          |          }|j        r)|                    |j                   t          j        ||<   3|s| S d }t          t          j        t%          |                              }d}t          |          D ]\  }} ||          }|s ||           }|rt          j        ||<   n|j         r@t          |          }|j        r)|                    |j                   t          j        ||<   x|j        rz|j        j        s|j        j        rb ||j                  }|rt          j        ||<   nPt          |          }|j        r)|                    |j                   t          j        ||<   t          j        ||<   d}t          d |D              }||         }|                    t          j                  }||r|||<   n|                    |           ||xx         t-          |           z  cc<   |r9t          | t          | z  t          d |                                D              z  } | S )	Nc                     t          |           }|rU|\  }}}|t          j        u rC|j        r>t	          |j                  dk    r(t          d |j        D                       r||fS d S d S d S d S d S )NrF   c              3   @   K   | ]}t          |t                    V  d S rI   )r8   r!   )rL   r   s     r2   rN   z/TR12i.<locals>.f.<locals>.ok.<locals>.<genexpr>  s,      AABJr3//AAAAAAr4   )as_f_sign_1r   NegativeOnerx   rV   r9   all)dir   r   r=   r   s        r2   rZ   zTR12i.<locals>.f.<locals>.ok  s    BA  1a%  !(  s16{{a7G  AA!&AAAAA a4K	                  r4   c                 (    g | ]}|j         d          S r   r9   rL   _s     r2   r`   z$TR12i.<locals>.f.<locals>.<listcomp>  s    444!&)444r4   c                 (    g | ]}|j         d          S r  r  r  s     r2   r`   z$TR12i.<locals>.f.<locals>.<listcomp>  s    888AafQi888r4   c                     | j         rPt          | j                  dk    r:| j        \  }}t          |t                    rt          |t                    r
||fS d S d S d S d S r   )rU   rV   r9   r8   r!   )nir<   rt   s      r2   rZ   zTR12i.<locals>.f.<locals>.ok  s    y  S\\Q.  w1a%%  *Q*<*<  a4K              r4   FTc                 (    g | ]}|j         d          S r  r  r  s     r2   r`   z$TR12i.<locals>.f.<locals>.<listcomp>  s    +++AafQi+++r4   c                 P    g | ]#\  }}t          d  |j        D              dz
  |z  $S )c                 ,    g | ]}t          |          S r\   )r!   rQ   s     r2   r`   z/TR12i.<locals>.f.<locals>.<listcomp>.<listcomp>  s+     8( 8( 8(A8( 8( 8(r4   rf   )r   r9   )rL   rm   rX   s      r2   r`   z$TR12i.<locals>.f.<locals>.<listcomp>  se     1J 1J 1J59Q 36 8( 8( !8( 8( 8( 3)+,3-/021 1J 1J 1Jr4   )rU   rx   rJ   ry   r9   r{   r   rP   rh   r   r   r:   r(   r   r   rR   rK   rS   r	   r  extract_additivelyr^   r!   r}   )r1   r_   rc   dokrZ   d_argsrm   r  r   r   r   r   n_argsr   r  edneweds                    r2   r=   zTR12i.<locals>.f  s   	 	RY 	") 	I  ""1v 	QV 	I	  	  	  cmA&&''v&& 	* 	*EAr2A 144QV4445Aq	y *BZZ9 &MM"'*** !F1I * 1 *RW5H *BrwKK 	*DAq888889AVCF !26	F1IIBy *bg...$%Eq	 	I	  	  	 
 cmLOO4455v&& %	! %	!EAr2A "BsGG ! !F1IIy !#BZZ9 ."MM"'222()F1I  !F-!131D!BrwKK %()F1II!'B!y 2 &bg 6 6 6,-Eq	$ Eq	C+++++,AQB))!%00E  "CFFGGAJJJ1III#a&& IIII 	Kfc6l*3 1J 1J=@YY[[1J 1J 1J ,K KB 	r4   r   r>   s     r2   TR12ir"    s'    *a a aF Rr4   c                 (    d }t          | |          S )a  Change products of ``tan`` or ``cot``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR13
    >>> from sympy import tan, cot
    >>> TR13(tan(3)*tan(2))
    -tan(2)/tan(5) - tan(3)/tan(5) + 1
    >>> TR13(cot(3)*cot(2))
    cot(2)*cot(5) + 1 + cot(3)*cot(5)
    c           	      j   | j         s| S t          g t          g d g i}t          t	          j        |                     D ]f}|j        t          t          fv r4|t          |                                       |j	        d                    K|d                              |           g|t                   }|t                   }t          |          dk     rt          |          dk     r| S |d          }t          |          dk    r|                                }|                                }|                    dt          |          t          ||z             z  t          |          t          ||z             z  z   z
             t          |          dk    |r4|                    t          |                                                     t          |          dk    r|                                }|                                }|                    dt          |          t          ||z             z  z   t          |          t          ||z             z  z              t          |          dk    |r4|                    t          |                                                     t	          | S )Nr   rF   rf   )rx   r!   r"   r   r   rP   rT   r   rg   r9   rV   r^   )r1   r9   r<   r   r   t1t2s          r2   r=   zTR13.<locals>.f/  sL   y 	I Rb$+r**++ 	% 	%Av#s# %T!WW$$QVAY////T
!!!$$$$IIq66A: 	#a&&1* 	IDz!ffqj 	KBBKKSWWSb\\1CGGCRLL4HHIJJJ !ffqj 	K  	&KKAEEGG%%%!ffqj 	IBBKKCGGCRLL003r773rBw<<3GGHHH !ffqj 	I  	&KKAEEGG%%%Dzr4   r   r>   s     r2   TR13r'  !  s#      < Rr4   c                 0    dfd	t          |           S )a  Returns cos(x)*cos(2*x)*...*cos(2**(k-1)*x) -> sin(2**k*x)/(2**k*sin(x))

    Examples
    ========

    >>> from sympy.simplify.fu import TRmorrie, TR8, TR3
    >>> from sympy.abc import x
    >>> from sympy import Mul, cos, pi
    >>> TRmorrie(cos(x)*cos(2*x))
    sin(4*x)/(4*sin(x))
    >>> TRmorrie(7*Mul(*[cos(x) for x in range(10)]))
    7*sin(12)*sin(16)*cos(5)*cos(7)*cos(9)/(64*sin(1)*sin(3))

    Sometimes autosimplification will cause a power to be
    not recognized. e.g. in the following, cos(4*pi/7) automatically
    simplifies to -cos(3*pi/7) so only 2 of the 3 terms are
    recognized:

    >>> TRmorrie(cos(pi/7)*cos(2*pi/7)*cos(4*pi/7))
    -sin(3*pi/7)*cos(3*pi/7)/(4*sin(pi/7))

    A touch by TR8 resolves the expression to a Rational

    >>> TR8(_)
    -1/8

    In this case, if eq is unsimplified, the answer is obtained
    directly:

    >>> eq = cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9)
    >>> TRmorrie(eq)
    1/16

    But if angles are made canonical with TR3 then the answer
    is not simplified without further work:

    >>> TR3(eq)
    sin(pi/18)*cos(pi/9)*cos(2*pi/9)/2
    >>> TRmorrie(_)
    sin(pi/18)*sin(4*pi/9)/(8*sin(pi/9))
    >>> TR8(_)
    cos(7*pi/18)/(16*sin(pi/9))
    >>> TR3(_)
    1/16

    The original expression would have resolve to 1/16 directly with TR8,
    however:

    >>> TR8(eq)
    1/16

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Morrie%27s_law

    Tc                 `   | j         s| S |r0|                                 \  }} |d           |d          z  S t          t                    i }g }| j        D ]}|                                \  }}|j        rXt          |t                    rC|j        d         	                                \  }	}
|
         
                    |	           |||<   x|
                    |           g }D ]}
|
         }|                                 |r~d}|d         x}}||v r|dz  }|dz  }||v |dk    rt          d|z  |z  |
z            d|z  z  t          ||
z            z  }d }g }t          |          D ]N}|dz  }t          |
|z  d          }|
                    |           t          ||         |p||                   }Ot          |          D ]W}|                                }t          |
|z  d          }||xx         |z  cc<   ||         s|                    |           X|
                    ||z             nCt          |                    d          |
z            }|
                    |||         z             |~|rt#          ||z   fdD             z    } | S )Nr   rf   rF   Frp   c                 N    g | ]!}|         D ]}t          ||z  d           "S )Frp   )r   )rL   r<   rW   r9   s      r2   r`   z'TRmorrie.<locals>.f.<locals>.<listcomp>  sX     &I &I &I-.Q&I &I;<AaC%(((&I &I &I &Ir4   )rx   ry   r   r{   r9   r   r   r8   r   r   rg   sortr    r   r   r^   r   r   )r1   r   r_   rc   cossotherr   rt   rX   r   r<   r   rW   cccinewargtakeccsrm   keyr9   r=   s                       @r2   r=   zTRmorrie.<locals>.f  s    y 	I 	#$$&&DAq1Q7711Q77?"4   	  	 A==??DAq|  
1c 2 2  q	..00AQr"""QQ 	- 	-AQAFFHHH -A$RAg FA!GB Ag  q5 - Ab^^AqD0RT:FDC"1XX A Aa!!B$777

2"49d.?d3i@@"1XX ) ) WWYY!!B$777S			T)			#Cy )HHRLLLJJvt|,,,,AEE!HHQJALLDG,,,5  -8  	KsU{ &I &I &I &I26&I &I &I I KB 	r4   r   r   r>   s    @r2   TRmorrier4  P  s5    v7 7 7 7 7 7r Rr4   c                 .    fd}t          | |          S )a  Convert factored powers of sin and cos identities into simpler
    expressions.

    Examples
    ========

    >>> from sympy.simplify.fu import TR14
    >>> from sympy.abc import x, y
    >>> from sympy import cos, sin
    >>> TR14((cos(x) - 1)*(cos(x) + 1))
    -sin(x)**2
    >>> TR14((sin(x) - 1)*(sin(x) + 1))
    -cos(x)**2
    >>> p1 = (cos(x) + 1)*(cos(x) - 1)
    >>> p2 = (cos(y) - 1)*2*(cos(y) + 1)
    >>> p3 = (3*(cos(y) - 1))*(3*(cos(y) + 1))
    >>> TR14(p1*p2*p3*(x - 1))
    -18*(x - 1)*sin(x)**2*sin(y)**4

    c           	      ~   | j         s| S rZ|                                 \  }}|t          j        ur5t	          |d          }t	          |d          }||k    s||k    r||z  } | S g }g }| j        D ]}|j        r>|                                \  }}	|	j        s|j	        s|
                    |           D|}nt          j        }	t          |          }
|
r|
d         j        t          t          fvr=|	t          j        u r|
                    |           n|
                    ||	z             |
\  }}}|
                    ||	j        |	|||f           t!          t#          |                    }t%          |          }t!          t'          d                    x}\  }}}	}}}|rw|                    d          |r8|d         |	         j        r[|	         j        rM|         |         k    r9|         |         k    r&|                    d          t+          |	         |	                   }|	         |k    r5fd|D             }||	xx         |z  cc<   |                    d|           n@|	         |k    r4fd|D             }||	xx         |z  cc<   |                    d|           t/          |         t                    rt          }nt          }|
                    |          |         z   ||         j        d                   dz  z  |z             nǉ|	         |	         k    r|         |         k    r|         |         k    r|                    d          |	         }t/          |         t                    rt          }nt          }|
                    |          |         z   ||         j        d                   dz  z  |z             S|
                    |         |	         z             |wt%          |          |k    r	t1          | } | S )	NFr   rf      r   c                      g | ]
}|         S r\   r\   )rL   rm   Bs     r2   r`   z#TR14.<locals>.f.<locals>.<listcomp>      &:&:&:qt&:&:&:r4   c                      g | ]
}|         S r\   r\   )rL   rm   As     r2   r`   z#TR14.<locals>.f.<locals>.<listcomp>  r:  r4   rF   )rx   ry   r   r:   TR14r9   rJ   r   rR   rS   rg   r  rT   r   r    r   r{   r   rV   r   r^   r   insertr8   r   )r1   r_   rc   r   r   r-  processr<   rt   rX   r   r   r=   sinotherr|   r   r1  remr<  r9  r   s                      @@r2   r=   zTR14.<locals>.f  st   y 	I 		 $$&&DAq~ AU+++AU+++19 #	 #dB	 	: 	:Ax }}1  LLOOOEAA !	#s3 : 'LLOOOOLLA&&&HAq"NNAq{Aq"a89999 ww''(( U &*%((^^3"1aB ,	%AA '%AJQ4> $%adn $%tqt| %R5AbE> % 'AA#&qtQqT??D  !tt| 7&:&:&:&:T&:&:&: #A$ 'q# 6 6 6 6!"1 7&:&:&:&:T&:&:&: #A$ 'q# 6 6 6)!A$44 ($'$'!LL1Q4%!*QQqty|__a5G*G$)NOOO$qTQqT\ %tqt| 	%R5AbE> % 'AA#$Q4D)!A$44 ($'$'!LL1Q4%!*QQqty|__a5G*G$)NOOO$ LL1qt$$$Y  ,	%\ u:: 	eB	r4   r   r   s    ` r2   r=  r=    s4    ,^ ^ ^ ^ ^@ Rr4   c                 2    fd}t          | |          S )a  Convert sin(x)**-2 to 1 + cot(x)**2.

    See _TR56 docstring for advanced use of ``max`` and ``pow``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR15
    >>> from sympy.abc import x
    >>> from sympy import sin
    >>> TR15(1 - 1/sin(x)**2)
    -cot(x)**2

    c                 (   t          | t                    rt          | j        t                    s| S | j        }|dz  dk    r"t          | j        |dz   z            | j        z  S d| z  }t          |t          t          d           }||k    r|} | S )NrF   rf   c                     d| z   S re   r\   r   s    r2   r   z!TR15.<locals>.f.<locals>.<lambda>Y  
    !a% r4   r   )r8   r   rK   r    r   TR15r   r"   r1   rX   iar<   r   r   s       r2   r=   zTR15.<locals>.fP      2s## 	
27C(@(@ 	IFq5A: 	2!a%())"'11rT"c3ScBBB7 	B	r4   r   r1   r   r   r=   s    `` r2   rG  rG  @  4           Rr4   c                 2    fd}t          | |          S )a  Convert cos(x)**-2 to 1 + tan(x)**2.

    See _TR56 docstring for advanced use of ``max`` and ``pow``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR16
    >>> from sympy.abc import x
    >>> from sympy import cos
    >>> TR16(1 - 1/cos(x)**2)
    -tan(x)**2

    c                 (   t          | t                    rt          | j        t                    s| S | j        }|dz  dk    r"t          | j        |dz   z            | j        z  S d| z  }t          |t          t          d           }||k    r|} | S )NrF   rf   c                     d| z   S re   r\   r   s    r2   r   z!TR16.<locals>.f.<locals>.<lambda>z  rF  r4   r   )r8   r   rK   r   r   rG  r   r!   rH  s       r2   r=   zTR16.<locals>.fq  rJ  r4   r   rK  s    `` r2   TR16rP  a  rL  r4   c                 (    d }t          | |          S )aD  Convert f(x)**-i to g(x)**i where either ``i`` is an integer
    or the base is positive and f, g are: tan, cot; sin, csc; or cos, sec.

    Examples
    ========

    >>> from sympy.simplify.fu import TR111
    >>> from sympy.abc import x
    >>> from sympy import tan
    >>> TR111(1 - 1/tan(x)**2)
    1 - cot(x)**2

    c                    t          | t                    r$| j        j        s| j        j        r| j        j        s| S t          | j        t                    r(t          | j        j	        d                   | j         z  S t          | j        t                    r(t          | j        j	        d                   | j         z  S t          | j        t                    r(t          | j        j	        d                   | j         z  S | S r7   )r8   r   rK   rS   r   rR   is_negativer!   r"   r9   r    r$   r   r#   r0   s    r2   r=   zTR111.<locals>.f  s    r3	W 	$&F$5	:<&:L	 Ibgs## 	1rw|A''"&00%% 	1rw|A''"&00%% 	1rw|A''"&00	r4   r   r>   s     r2   TR111rT    s#       Rr4   c                 2    fd}t          | |          S )ah  Convert tan(x)**2 to sec(x)**2 - 1 and cot(x)**2 to csc(x)**2 - 1.

    See _TR56 docstring for advanced use of ``max`` and ``pow``.

    Examples
    ========

    >>> from sympy.simplify.fu import TR22
    >>> from sympy.abc import x
    >>> from sympy import tan, cot
    >>> TR22(1 + tan(x)**2)
    sec(x)**2
    >>> TR22(1 + cot(x)**2)
    csc(x)**2

    c                     t          | t                    r| j        j        t          t
          fv s| S t          | t
          t          d           } t          | t          t          d           } | S )Nc                     | dz
  S re   r\   r   s    r2   r   z!TR22.<locals>.f.<locals>.<lambda>  
    1q5 r4   r   c                     | dz
  S re   r\   r   s    r2   r   z!TR22.<locals>.f.<locals>.<lambda>  rX  r4   )	r8   r   rK   rT   r"   r!   r   r#   r$   r   s    r2   r=   zTR22.<locals>.f  sk    2s## 	c
(B 	I2sCcsCCC2sCcsCCC	r4   r   rK  s    `` r2   TR22rZ    s4    $      Rr4   c                 (    d }t          | |          S )a  Convert sin(x)**n and cos(x)**n with positive n to sums.

    Examples
    ========

    >>> from sympy.simplify.fu import TRpower
    >>> from sympy.abc import x
    >>> from sympy import cos, sin
    >>> TRpower(sin(x)**6)
    -15*cos(2*x)/32 + 3*cos(4*x)/16 - cos(6*x)/32 + 5/16
    >>> TRpower(sin(x)**3*cos(2*x)**4)
    (3*sin(x)/4 - sin(3*x)/4)*(cos(4*x)/2 + cos(8*x)/8 + 3/8)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Power-reduction_formulae

    c                    t          | t                    r!t          | j        t          t          f          s| S |                                 \  }|j        d         j        rj        rj	        rIt          |t                    r4ddz
  z  t          fdt          dz   dz            D              z  } nj	        r^t          |t                    rIddz
  z  t          j        dz
  dz  z  z  t          fdt          dz   dz            D              z  } nj        rEt          |t                    r0ddz
  z  t          fdt          dz            D              z  } n^j        rWt          |t                    rBddz
  z  t          j        dz  z  z  t          fdt          dz            D              z  } j        r| d z  t          dz            z  z  } | S )Nr   rF   rf   c                 b    g | ]+}t          |          t          d |z  z
  z            z  ,S rF   r   r   rL   rW   r_   r   s     r2   r`   z&TRpower.<locals>.f.<locals>.<listcomp>  sJ     $/ $/ $/ %-QNN3AaC{3C3C$C $/ $/ $/r4   c                     g | ];}t          |          t          j        |z  z  t          d |z  z
  z            z  <S r^  )r   r   r  r    r`  s     r2   r`   z&TRpower.<locals>.f.<locals>.<listcomp>  sc     =Q =Q =Q:; >Fa^^M1$>%%(!ac'1%5%5>6 =Q =Q =Qr4   c                 b    g | ]+}t          |          t          d |z  z
  z            z  ,S r^  r_  r`  s     r2   r`   z&TRpower.<locals>.f.<locals>.<listcomp>  sJ     $) $) $) %-QNN3AaC{3C3C$C $) $) $)r4   c                     g | ];}t          |          t          j        |z  z  t          d |z  z
  z            z  <S r^  )r   r   r  r   r`  s     r2   r`   z&TRpower.<locals>.f.<locals>.<listcomp>  sc     9K 9K 9K:; :B!QM1$:%%(!ac'1%5%5:6 9K 9K 9Kr4   )r8   r   rK   r    r   r   r9   r   rS   is_oddr   r   r   r  is_evenr   )r1   rt   r_   r   s     @@r2   r=   zTRpower.<locals>.f  s   2s## 	
27S#J(G(G 	I~~1F1I< 	/AM 	/x LJq#.. L1Xc $/ $/ $/ $/ $/"AE19--$/ $/ $/ 0 0 LjC00 L1XamqsAg66s =Q =Q =Q =Q =Q?Da!eQY?O?O=Q =Q =Q 8R R Lz!S11 L1Xc $) $) $) $) $)"1Q3ZZ$) $) $) * * Lz!S11 L1Xamac223 9K 9K 9K 9K 9K?DQqSzz9K 9K 9K 4L Ly /a1"ghq!A#....	r4   r   r>   s     r2   TRpowerrf    s#    *  , Rr4   c                 P    t          |                     t                              S )zReturn count of trigonometric functions in expression.

    Examples
    ========

    >>> from sympy.simplify.fu import L
    >>> from sympy.abc import x
    >>> from sympy import cos, sin
    >>> L(cos(x)+sin(x))
    2
    )r   countr&   r0   s    r2   Lri    s     RXX+,,---r4   c                 H    t          |           |                                 fS rI   )ri  	count_opsr   s    r2   r   r   !  s    addAKKMM2 r4   c                    t          t                    }t          t                    }| }t          |           } t	          | t
                    s | j        fd| j        D              S t          |           } | 	                    t          t                    rT ||           } |           |           k     r|} | 	                    t          t                    rt          |           } | 	                    t          t                    r< ||           }t          t!          |                    }t#          || ||g          } t#          t%          |           |           S )aH  Attempt to simplify expression by using transformation rules given
    in the algorithm by Fu et al.

    :func:`fu` will try to minimize the objective function ``measure``.
    By default this first minimizes the number of trig terms and then minimizes
    the number of total operations.

    Examples
    ========

    >>> from sympy.simplify.fu import fu
    >>> from sympy import cos, sin, tan, pi, S, sqrt
    >>> from sympy.abc import x, y, a, b

    >>> fu(sin(50)**2 + cos(50)**2 + sin(pi/6))
    3/2
    >>> fu(sqrt(6)*cos(x) + sqrt(2)*sin(x))
    2*sqrt(2)*sin(x + pi/3)

    CTR1 example

    >>> eq = sin(x)**4 - cos(y)**2 + sin(y)**2 + 2*cos(x)**2
    >>> fu(eq)
    cos(x)**4 - 2*cos(y)**2 + 2

    CTR2 example

    >>> fu(S.Half - cos(2*x)/2)
    sin(x)**2

    CTR3 example

    >>> fu(sin(a)*(cos(b) - sin(b)) + cos(a)*(sin(b) + cos(b)))
    sqrt(2)*sin(a + b + pi/4)

    CTR4 example

    >>> fu(sqrt(3)*cos(x)/2 + sin(x)/2)
    sin(x + pi/3)

    Example 1

    >>> fu(1-sin(2*x)**2/4-sin(y)**2-cos(x)**4)
    -cos(x)**2 + cos(y)**2

    Example 2

    >>> fu(cos(4*pi/9))
    sin(pi/18)
    >>> fu(cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9))
    1/16

    Example 3

    >>> fu(tan(7*pi/18)+tan(5*pi/18)-sqrt(3)*tan(5*pi/18)*tan(7*pi/18))
    -sqrt(3)

    Objective function example

    >>> fu(sin(x)/cos(x))  # default objective function
    tan(x)
    >>> fu(sin(x)/cos(x), measure=lambda x: -x.count_ops()) # maximize op count
    sin(x)/cos(x)

    References
    ==========

    .. [1] https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.657.2478&rep=rep1&type=pdf
    c                 2    g | ]}t          |           S ))measure)fu)rL   r<   rn  s     r2   r`   zfu.<locals>.<listcomp>m  s&    AAAAAw///AAAr4   )r3  )r)   RL1RL2r   r8   r   rT   r9   r?   hasr!   r"   rB   r    r   r   r4  r   r   )r1   rn  fRL1fRL2r   rv1rv2s    `     r2   ro  ro  !  sJ   L #wD#wD
C	Bb$ CrwAAAAAAABB	RB	vvc3 d2hhGCLL772;;& 	B66#s 	RB	vvc3 3d2hh(3--  #r3$'222tBxx))))r4   c                    t          t                    }|rX| j        D ]O}|                                \  }}|dk     r| }| }|||r ||          ndf                             |           PnL|r;| j        D ]2}|t
          j         ||          f                             |           3nt          d          g }d}|D ]z}	||	         }
|	\  }}t          |
          dk    r:t          |
ddi} ||          }||k    r|}d}|                    ||z             \|                    ||
d         z             {|r	t          | } | S )a  Apply ``do`` to addends of ``rv`` that (if ``key1=True``) share at least
    a common absolute value of their coefficient and the value of ``key2`` when
    applied to the argument. If ``key1`` is False ``key2`` must be supplied and
    will be the only key applied.
    r   rf   zmust have at least one keyFrq   T)
r   r{   r9   r   rg   r   r:   
ValueErrorrV   r   )r1   r   key2key1abscr<   r   r9   r   rW   rn   r  rX   r   s                 r2   r   r   |  s    tD 7 	8 	8A>>##DAq1u BB!+TT!WWW!,-44Q7777	8 
 7 	- 	-A!%a!"))!,,,,	- 5666D
C    G1q66A: 	 Q'''A"Q%%Cax KK!KK!A$
 $ZIr4   z~
    TR0 TR1 TR2 TR3 TR4 TR5 TR6 TR7 TR8 TR9 TR10 TR10i TR11
    TR12 TR13 L TR2i TRmorrie TR12i
    TR14 TR15 TR16 TR111 TR22c                  X    t          d          t          d          caadt          z  ad S )NrF      rf   )r%   r   r   r   r\   r4   r2   r   r     s&    !WWd1ggNFF&IIIr4   c           	         t           t                       d | |fD             \  } }|                     |          \  }}|                     |                                          }dx}}t
          j        |j        v r#|                    t
          j                  }| }n5t
          j        |j        v r"|                    t
          j                  }| }d ||fD             \  } }d } || |          }	|	dS |	\  }
}} |||          }	|	dS |	\  }}}|s|s|r(t          |t                    r||||
||f\  }
}}}}}||}}|sP|p|}|p|}t          ||j                  sdS ||||j        d         |j        d         t          |t                    fS |
s|s|r|r|r|rt          ||j                  t          ||j                  urdS d ||fD             t          fd||fD                       sdS ||||j        d         |j        d         t          ||j                  fS |r|s|r|s
|r
||||dS |p|}|p|}|j        |j        k    rdS |
st
          j        }
|st
          j        }|
|u r%|t           z  }||||j        d         t           d	z  d
fS |
|z  t"          k    r#|d|z  z  }||||j        d         t           dz  d
fS |
|z  t$          k    r#|d|
z  z  }||||j        d         t           dz  d
fS dS )a)  Return the gcd, s1, s2, a1, a2, bool where

    If two is False (default) then::
        a + b = gcd*(s1*f(a1) + s2*f(a2)) where f = cos if bool else sin
    else:
        if bool, a + b was +/- cos(a1)*cos(a2) +/- sin(a1)*sin(a2) and equals
            n1*gcd*cos(a - b) if n1 == n2 else
            n1*gcd*cos(a + b)
        else a + b was +/- cos(a1)*sin(a2) +/- sin(a1)*cos(a2) and equals
            n1*gcd*sin(a + b) if n1 = n2 else
            n1*gcd*sin(b - a)

    Examples
    ========

    >>> from sympy.simplify.fu import trig_split
    >>> from sympy.abc import x, y, z
    >>> from sympy import cos, sin, sqrt

    >>> trig_split(cos(x), cos(y))
    (1, 1, 1, x, y, True)
    >>> trig_split(2*cos(x), -2*cos(y))
    (2, 1, -1, x, y, True)
    >>> trig_split(cos(x)*sin(y), cos(y)*sin(y))
    (sin(y), 1, 1, x, y, True)

    >>> trig_split(cos(x), -sqrt(3)*sin(x), two=True)
    (2, 1, -1, x, pi/6, False)
    >>> trig_split(cos(x), sin(x), two=True)
    (sqrt(2), 1, 1, x, pi/4, False)
    >>> trig_split(cos(x), -sin(x), two=True)
    (sqrt(2), 1, -1, x, pi/4, False)
    >>> trig_split(sqrt(2)*cos(x), -sqrt(6)*sin(x), two=True)
    (2*sqrt(2), 1, -1, x, pi/6, False)
    >>> trig_split(-sqrt(6)*cos(x), -sqrt(2)*sin(x), two=True)
    (-2*sqrt(2), 1, 1, x, pi/3, False)
    >>> trig_split(cos(x)/sqrt(6), sin(x)/sqrt(2), two=True)
    (sqrt(6)/3, 1, 1, x, pi/6, False)
    >>> trig_split(-sqrt(6)*cos(x)*sin(y), -sqrt(2)*sin(x)*sin(y), two=True)
    (-2*sqrt(2)*sin(y), 1, 1, x, pi/3, False)

    >>> trig_split(cos(x), sin(x))
    >>> trig_split(cos(x), sin(z))
    >>> trig_split(2*cos(x), -sin(x))
    >>> trig_split(cos(x), -sqrt(3)*sin(x))
    >>> trig_split(cos(x)*cos(y), sin(x)*sin(z))
    >>> trig_split(cos(x)*cos(y), sin(x)*sin(y))
    >>> trig_split(-sqrt(6)*cos(x), sqrt(2)*sin(x)*sin(y), two=True)
    Nc                 ,    g | ]}t          |          S r\   r   r   s     r2   r`   ztrig_split.<locals>.<listcomp>      '''1GAJJ'''r4   rf   c                 6    g | ]}|                                 S r\   as_exprr   s     r2   r`   ztrig_split.<locals>.<listcomp>       ***AAIIKK***r4   c                 2   dx}}t           j        }| j        r4|                                 \  }} t	          | j                  dk    s|sdS | j        rt          | j                  }n| g}|                    d          } t          | t                    r| }n:t          | t                    r| }n"| j        r| j        t           j        u r|| z  }ndS |rd|d         }t          |t                    r|r|}nB|}n?t          |t                    r|r|}n%|}n"|j        r|j        t           j        u r||z  }ndS |t           j        ur|nd||fS t          | t                    r| }nt          | t                    r| }||dS |t           j        ur|nd}|||fS )a  Return ``a`` as a tuple (r, c, s) such that
        ``a = (r or 1)*(c or 1)*(s or 1)``.

        Three arguments are returned (radical, c-factor, s-factor) as
        long as the conditions set by ``two`` are met; otherwise None is
        returned. If ``two`` is True there will be one or two non-None
        values in the tuple: c and s or c and r or s and r or s or c with c
        being a cosine function (if possible) else a sine, and s being a sine
        function (if possible) else oosine. If ``two`` is False then there
        will only be a c or s term in the tuple.

        ``two`` also require that either two cos and/or sin be present (with
        the condition that if the functions are the same the arguments are
        different or vice versa) or that a single cosine or a single sine
        be present with an optional radical.

        If the above conditions dictated by ``two`` are not met then None
        is returned.
        NrF   r   )r   r:   rx   r   rV   r9   r{   r^   r8   r   r    rJ   r   r   )r<   r   r   r   r   r9   rt   s          r2   pow_cos_sinztrig_split.<locals>.pow_cos_sin  s   ( AU8 %	NN$$EB16{{Q c tx AF||sA!S!! As##  aeqvo at  Ga%%   3''   X  !%16/  !GBB415222dAq883 	AA3 	A 	 	FQU?,RR1axr4   r   c                     h | ]	}|j         
S r\   r  )rL   r   s     r2   	<setcomp>ztrig_split.<locals>.<setcomp>P  s    1111111r4   c              3   *   K   | ]}|j         v V  d S rI   r  )rL   rm   r9   s     r2   rN   ztrig_split.<locals>.<genexpr>Q  s)      <<a16T><<<<<<r4   r   FrF   r}  r7  )r   r   r.   r   r  r   r  factorsquor8   r    rT   r9   r   r  r:   r   r   r   )r<   rt   r   uaubr   r   r   r  r   coacasacobcbsbr   r   r9   s                     @r2   r   r     s   f  ''A'''DAqXXa[[FB
%%((



CKB}
" VVAM""S	
"*	$ VVAM""S**"b***DAq? ? ?D 	AsA KCRAsA KCR  B " B!4!4 #&BR#; RS"bRB "7H"H"!QV$$ 	4BAF1Iqvay*Q2D2DDD 	T3 	T Tr Tb TR Tb"'***R2I2II F11R111<<<<B8<<<<< FBBGAJ
Jr27<S<SSS 	" 	 	r 			$&	24	AC	FH"H"6QV 	F 	%C 	%C#: 	76MCBq	2a466W 	71S5LCBq	2a466W	! 	71S5LCBq	2a466	7 	7r4   c                 .   | j         rt          | j                  dk    rdS | j        \  }}|t          j        t          j        fv rDt          j        }|j        r,|j        d         j        r|j        d         dk     r	| | }}| }|||fS d | j        D             \  }}|                    |          \  }}|	                    |          
                                }t          j        |j        v r$|                    t          j                  }d}d}n;t          j        |j        v r$|                    t          j                  }d}d}ndx}}d ||fD             \  }}|t          j        u r||}}||}}|dk    r| }| }|t          j        u r|||fS dS )a  If ``e`` is a sum that can be written as ``g*(a + s)`` where
    ``s`` is ``+/-1``, return ``g``, ``a``, and ``s`` where ``a`` does
    not have a leading negative coefficient.

    Examples
    ========

    >>> from sympy.simplify.fu import as_f_sign_1
    >>> from sympy.abc import x
    >>> as_f_sign_1(x + 1)
    (1, x, 1)
    >>> as_f_sign_1(x - 1)
    (1, x, -1)
    >>> as_f_sign_1(-x + 1)
    (-1, x, -1)
    >>> as_f_sign_1(-x - 1)
    (-1, x, 1)
    >>> as_f_sign_1(2*x + 2)
    (2, x, 1)
    rF   Nr   c                 ,    g | ]}t          |          S r\   r  r   s     r2   r`   zas_f_sign_1.<locals>.<listcomp>  r  r4   rf   c                 6    g | ]}|                                 S r\   r  r   s     r2   r`   zas_f_sign_1.<locals>.<listcomp>  r  r4   )rU   rV   r9   r   r  r:   rx   r   r.   r   r  r  r  )	rX   r<   rt   r   r  r  r   r   r   s	            r2   r  r  j  s   * 8 s16{{a' 6DAqQ]AE"" E8 	q	+ 	q	A 	2rqAA!Qw'''''DAqXXa[[FB
%%((



C}
" 	VVAM""	
"*	$ VVAM""R**"b***DAqAEz !1RB	Rx dSAEz Arz r4   c                 .    fd}t          | |          S )a2  Replace all hyperbolic functions with trig functions using
    the Osborne rule.

    Notes
    =====

    ``d`` is a dummy variable to prevent automatic evaluation
    of trigonometric/hyperbolic functions.


    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function
    c                    t          | t                    s| S | j        d         }|j        s|z  n$t	          j        fd|j        D                       }t          | t                    rt          t          |          z  S t          | t                    rt          |          S t          | t                    rt          t          |          z  S t          | t                    rt          |          t          z  S t          | t                    rt!          |          S t          | t"                    rt%          |          t          z  S t'          d| j        z            )Nr   c                     g | ]}|z  S r\   r\   )rL   rm   rc   s     r2   r`   z'_osborne.<locals>.f.<locals>.<listcomp>  s    4I4I4IQQqS4I4I4Ir4   unhandled %s)r8   r   r9   rU   r   r   r   r   r    r   r   r   r!   r   r"   r   r#   r   r$   NotImplementedErrorrT   )r1   r<   rc   s     r2   r=   z_osborne.<locals>.f  s4   "011 	IGAJxJAaCCS^4I4I4I4I!&4I4I4I%J%Jb$ 	@SVV8OD!! 	@q66MD!! 		@SVV8OD!! 	@q66!8OD!! 	@q66MD!! 	@q66!8O%nrw&>???r4   r   rX   rc   r=   s    ` r2   _osborner    s1    "@ @ @ @ @( Q??r4   c                 .    fd}t          | |          S )a1  Replace all trig functions with hyperbolic functions using
    the Osborne rule.

    Notes
    =====

    ``d`` is a dummy variable to prevent automatic evaluation
    of trigonometric/hyperbolic functions.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function
    c                    t          | t                    s| S | j        d                             d          \  }}|                    t
          j        i          |t          z  z   }t          | t                    rt          |          t          z  S t          | t                    rt          |          S t          | t                    rt          |          t          z  S t          | t                    rt          |          t          z  S t          | t                     rt#          |          S t          | t$                    rt'          |          t          z  S t)          d| j        z            )Nr   T)as_Addr  )r8   r&   r9   as_independentxreplacer   r:   r   r    r   r   r   r!   r   r"   r   r#   r   r$   r   r  rT   )r1   constr   r<   rc   s       r2   r=   z_osbornei.<locals>.f  s:   "344 	I71:,,Qt,<<qJJ15z""U1W,b# 	@7719C   	@77NC   		@7719C   	@7719C   	@77NC   	@7719%nrw&>???r4   r   r  s    ` r2   	_osborneir    s1     @ @ @ @ @( Q??r4   c                    ddl m ddlm |                     t
                    }d |D             |                     t                              }d D             t                      t          |          fdfS )a  Return an expression containing hyperbolic functions in terms
    of trigonometric functions. Any trigonometric functions initially
    present are replaced with Dummy symbols and the function to undo
    the masking and the conversion back to hyperbolics is also returned. It
    should always be true that::

        t, f = hyper_as_trig(expr)
        expr == f(t)

    Examples
    ========

    >>> from sympy.simplify.fu import hyper_as_trig, fu
    >>> from sympy.abc import x
    >>> from sympy import cosh, sinh
    >>> eq = sinh(x)**2 + cosh(x)**2
    >>> t, f = hyper_as_trig(eq)
    >>> f(fu(t))
    cosh(2*x)

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Hyperbolic_function
    r   r   )collectc                 .    g | ]}|t                      fS r\   r   )rL   r   s     r2   r`   z!hyper_as_trig.<locals>.<listcomp>  s     (((QQL(((r4   c                     g | ]	\  }}||f
S r\   r\   )rL   rW   rn   s      r2   r`   z!hyper_as_trig.<locals>.<listcomp>  s     $$$tq!QF$$$r4   c           	            t          |                               t                                        t          j                  S rI   )r  r  dictr   ImaginaryUnit)r   r  rc   repsr   s    r2   r   zhyper_as_trig.<locals>.<lambda>  sG    ''((!Q  d,,3. 3./0+@ +@ r4   )
r   r   sympy.simplify.radsimpr  atomsr&   r  r  r   r  )r1   trigsmaskedr  rc   r  r   s      @@@@r2   hyper_as_trigr    s    4 100000...... HH*++E((%(((D[[d$$F %$t$$$DAFA !@ !@ !@ !@ !@ !@ !@ @ @r4   c                     |                      t          t                    s| S t          t	          t          |                               S )a  Convert products and powers of sin and cos to sums.

    Explanation
    ===========

    Applied power reduction TRpower first, then expands products, and
    converts products to sums with TR8.

    Examples
    ========

    >>> from sympy.simplify.fu import sincos_to_sum
    >>> from sympy.abc import x
    >>> from sympy import cos, sin
    >>> sincos_to_sum(16*sin(x)**3*cos(2*x)**2)
    7*sin(x) - 5*sin(3*x) + 3*sin(5*x) - sin(7*x)
    )rr  r   r    r   r   rf  )exprs    r2   sincos_to_sumr    s;    & 88C .:gdmm,,---r4   )F)r   Fr   rI   )NT)ocollectionsr   sympy.core.addr   sympy.core.exprr   sympy.core.exprtoolsr   r   r	   sympy.core.functionr   sympy.core.mulr   sympy.core.numbersr   r   sympy.core.powerr   sympy.core.singletonr   sympy.core.sortingr   sympy.core.symbolr   sympy.core.sympifyr   sympy.core.traversalr   (sympy.functions.combinatorial.factorialsr   %sympy.functions.elementary.hyperbolicr   r   r   r   r   r   r   (sympy.functions.elementary.trigonometricr   r    r!   r"   r#   r$   r%   r&   sympy.ntheory.factor_r'   sympy.polys.polytoolsr(   sympy.strategies.treer)   sympy.strategies.corer*   r+   sympyr,   r3   r?   rB   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r"  r'  r4  r=  rG  rP  rT  rZ  rf  ri  r{   r  CTR1CTR2CTR3CTR4rp  rq  ro  r   r   fufuncsr  ziplocalsgetFUr   r   r   r  r  r  r  r  r\   r4   r2   <module>r     s   # # # # # #                   A A A A A A A A A A * * * * * *       $ $ $ $ $ $ $ $             " " " " " " & & & & & & # # # # # # & & & & & & * * * * * * = = = = = =< < < < < < < < < < < < < < < < < <? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? / / / / / / ( ( ( ( ( ( ( ( ( ( ( ( 1 1 1 1 1 1 1 1      ) ) )  0  <q q q qh' ' 'T  6= = =@B B B B*B B B B*  0H H H HV[ [ [|- - - -`A A AHM M M M`: : :z       Fx x xv, , ,^t t tnv v v vr   B   B  @   :+ + +\. . ."  < tCC#sCc3S#tT4(D$eUD:; ; < <S#sCc3S#tT4(D$eUD 
c
S#J)SzC:s+,	3xeS98D	eh
CdCsC0 	#tS#t$#tS	4dCc40	 32 X* X* X* X*v' ' ' 'T! "'  
T$$ss7DDVVXX\7!;!;<<==
>
>??   
s7 s7 s7 s7l6 6 6r% % %P$ $ $N(@ (@ (@V. . . . .r4   