
    Xh                      B   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 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/m0Z0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9m:Z: d dl;m<Z< d Z=d Z>d Z?dCdZ@d ZAd ZBd ZCdDd ZDdDd!ZEd" ZFdEd$ZGd% ZHdEd&ZId' ZJdFd)ZKd* ZLdEd+ZMd, ZNd- ZOd. ZPdEd/ZQdDd0ZRdDd1ZSd2 ZTdDd3ZUd4 ZVd5 ZWe<rB eX eYe:e=e>e?eAeBeDeEeFeGeHeIeKeMeOe@ePeQeReSeNeTeUf                    \  Z=Z>Z?ZAZBZDZEZFZGZHZIZKZMZOZ@ZPZQZRZSZNZTZUeDe=feEe=fe9gZZeKeDe=feEe=fe=gfZ[ePeGe=fePeGeJe=fe9gZ\eBeJfe9gZ]eBeAeBeMeBeOeBe=fZ^eBeAeIeBeAeKfeDeFeKeBfe\eZeHe[eBeHeHe]fe9gZ_d6 fd7Z`dGd8Zad9b                                Zc ed eX eeec eX eY ef            jg        ec                                                  Zhed:             Zied;             Zjed<             ZkdCd=Zld> Zmd? Znd@ ZodA ZpdB Zqd(S )H    )defaultdictAdd)cacheit)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    c/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/sympy/simplify/fu.pyTR0r4       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!   r2   as     r3   fzTR1.<locals>.f5   se    b# 	 
A5Q<C   	 
A5Q<	r5   r   r2   r>   s     r3   TR1r@   )   s#       Rr5   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 r8   )r9   r"   r:   r!   r    r#   r<   s     r3   r>   zTR2.<locals>.fS   sm    b# 	!
Aq66#a&&= C   	!
Aq66#a&&= 	r5   r   r?   s     r3   TR2rC   A   s#    $   Rr5   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)r9   r    is_Powbase).0ais     r3   	<genexpr>z8TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>.<genexpr>   sR       , , #2s++Kry/KRW^ , , , , , ,r5   N)anyr   	make_argsrM   r=   s     r3   rO   z.TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>   sf       = =01  , ,-**, , , , , = = = = = =r5   )	
is_integeris_positivefuncr!   r    is_Addlenr:   rP   )kehalfs     r3   okzTR2i.<locals>.f.<locals>.ok   s     . ?3*$ > *=*=AFq *=  = =56V= = = = =@r5   c                 b    g | ]+} ||                   |                     |          f,S  pop)rM   rX   nr[   s     r3   
<listcomp>z#TR2i.<locals>.f.<locals>.<listcomp>   :    JJJ1bbAaDkkJ!QUU1XXJJJr5   c                 b    g | ]+} ||                   |                     |          f,S r]   r^   )rM   rX   dr[   s     r3   ra   z#TR2i.<locals>.f.<locals>.<listcomp>   rb   r5   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   )	rV   rW   r:   r)   r
   append	enumerater   as_powers_dict)	rd   ddonenewkrX   knewivrZ   r[   s	          r3   	factorizez"TR2i.<locals>.f.<locals>.factorize   s   D / /8 /AFa(,A6!999,q//DqyyQI... $-dOO # #LAy4!"DGGDz0022 - -A!tAwAr!Qxx - !aV,,,,DD r5   r   Fevaluaterg   rG   c                 "    g | ]\  }}|||z  S r]   r]   rM   brY   s      r3   ra   z#TR2i.<locals>.f.<locals>.<listcomp>   s%    <<<TQ!<AqD<<<r5   c                 "    g | ]\  }}|||z  S r]   r]   rt   s      r3   ra   z#TR2i.<locals>.f.<locals>.<listcomp>   s%    666tq!A6ad666r5   c                     g | ]
\  }}||z  S r]   r]   rt   s      r3   ra   z#TR2i.<locals>.f.<locals>.<listcomp>   s     ///A1///r5   c                     g | ]
\  }}||z  S r]   r]   rt   s      r3   ra   z#TR2i.<locals>.f.<locals>.<listcomp>   s     6N6N6N1q!t6N6N6Nr5   )is_Mulas_numer_denomis_Atomrj   listkeysr9   r!   r    r:   rh   r"   rV   r   r;   rS   rT   r   items)r2   ndonerk   rp   trX   r=   a1rd   r`   r[   rZ   s           @@@r3   r>   zTR2i.<locals>.f{   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66adadllHHS^^QqT1222"&&AaD1Q44 ,QBQww1R5AaD==#afQik"2"2QqT!9:::'++!quAs## 'q	E22266adadllHHS^^adU2333"&&AaD1Q4 '!( 'qvayAE'9'9qvay#.. (:q	q)E:::66adadll!l +HHS1--!u4555"&&AaD1Q4 	Pq<<QWWYY<<<<>66qwwyy66678B#/////06N6N6N6N6N1OOOB	r5   r   )r2   rZ   r>   s    ` r3   TR2ir   _   s4    8S S S S Sj Rr5   c                 j    ddl m fd}|                     d 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      rG   T)r9   r'   rU   r:   r   PirT   r    r!   r"   r#   r$   r%   type)r2   fmapr   s     r3   r>   zTR3.<locals>.f   s    "344 	IWWXXbgaj))**"344 	IGAJa,a"'!*1D0QYYYYUYYYYYYc3S#sCc3ODd2hhQ 344B	r5   c                 ,    t          | t                    S rJ   )r9   r'   xs    r3   <lambda>zTR3.<locals>.<lambda>   s    *Q 566 r5   c                 2    |                      d d           S )Nc                     | j         o| j        S rJ   )	is_numberry   r`   s    r3   r   z'TR3.<locals>.<lambda>.<locals>.<lambda>   s    ak.ah r5   c                       | j         | j         S rJ   rU   r:   r   s    r3   r   z'TR3.<locals>.<lambda>.<locals>.<lambda>   s    fafafo r5   replacer   s    r3   r   zTR3.<locals>.<lambda>   s     !))..%%' ' r5   )sympy.simplify.simplifyr   r   r   )r2   r>   r   s     @r3   TR3r      sg    $ 100000	 	 	 	 	 
66	' 	'
( 
(B Rr5   c                 2    |                      d d           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
    c                 v    t          | t                    o$| j        d         t          z  x}j        o|j        dv S )Nr   )rg   rG      r      )r9   r'   r:   r   is_Rationalq)r   rs     r3   r   zTR4.<locals>.<lambda>  s?    q/00 Eq	"_Q)E./c_.D r5   c                 r    |                       | j        d         j         | j        d         j                   S r8   r   r   s    r3   r   zTR4.<locals>.<lambda>   s-    FF>16!9>16!9>233 r5   r   r1   s    r3   TR4r     s0    0 ::	E 	E	4 	4	5 5 5r5   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   Trg   rG   r   )rK   rL   rU   expis_realr:   r(   )r2   rY   pr>   ghmaxpows      r3   _fz_TR56.<locals>._f>  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..r5   r   )r2   r>   r   r   r   r   r   s    ````` r3   _TR56r   $  sG    4!/ !/ !/ !/ !/ !/ !/ !/ !/F Rr5   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 rf   r]   r   s    r3   r   zTR5.<locals>.<lambda>v  
    Q r5   r   r   )r   r!   r    r2   r   r   s      r3   TR5r   d  !    $ S#CSAAAAr5   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 rf   r]   r   s    r3   r   zTR6.<locals>.<lambda>  r   r5   r   )r   r    r!   r   s      r3   TR6r   y  r   r5   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 )NrG   rg   r   )rK   rL   rU   r    r   r:   r1   s    r3   r>   zTR7.<locals>.f  sN    	 	bglc11bfkkIC"',q/)***A--r5   r   r?   s     r3   TR7r     s#     . . .
 Rr5   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                 D	   | 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!          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          }t/          t          |          t          |                    }t1          |          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          t5          t!          |                     S )Nc                 ,    g | ]}t          |          S r]   r   rM   rn   s     r3   ra   z"TR8.<locals>.f.<locals>.<listcomp>  s    ???aJqMM???r5   Ffirstr   rG   rg   )ry   rK   rL   rU   r    r!   r   rS   rT   rz   TR8r	   r:   r   rW   rV   r   as_coeff_MulrQ   r   rh   
is_Integerextendminranger_   r   )r2   r`   rd   newnnewdr:   r=   csrn   r   a2r   s               r3   r>   zTR8.<locals>.f  s   I	I	 GLS#J&&V '"$'"5 'I 		??2+<+<+>+>???DAqq&&&Dq&&&DqyyDAIItDy))9 1!7 1BG))bgaj.?)boo//0BIRb$+r"" 		% 		%Av#s##T!WW$$QVAY////( %qu/ %AEAII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!ffqjjBBKKR"WBG4a7888 !ffqjj  	&KKAEEGG%%%!ffqjjBBKK#b2g,,R"W5q8999 !ffqjj  	&KKAEEGG%%%:c4j))***r5   r   r2   r   r>   s    ` r3   r   r     s/    "5+ 5+ 5+ 5+ 5+n Rr5   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 )NrG   Frg   Tc                     g | ]}||S r]   r]   rM   r   s     r3   ra   z.TR9.<locals>.f.<locals>.do.<locals>.<listcomp>0      777bB7r777r5   r   
rV   r|   r   r:   rW   r   r   
trig_splitr    r!   )r2   r   r:   hitrn   rN   jajwasnewsplitgcdn1n2r=   ru   iscosdos                    r3   r   zTR9.<locals>.f.<locals>.do  sT    9 	(())D4yyA~~s4yy)) " "AaBz "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  ;88r6!8CQ	NN23Aqy>>AA66aqA#vc1q5!)nn,S!a%^^;;88r6!8CQ	NN23Aqy>>AA66aqAuS!a%^^+CQ	NN::r5   T)rV   process_common_addends)r2   r   s    @r3   r>   zTR9.<locals>.f  sC    y 	I<	; <	; <	; <	; <	; <	;| &b"---r5   r   r?   s     r3   TR9r     s'    .B. B. B.H Rr5   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   )rU   r    r!   r:   rV   r|   r   r_   r   
_from_argsTR10)r2   r>   argr:   r=   ru   r   s         r3   r>   zTR10.<locals>.fa  s   73*$$IGgaj: 	9 &GCH--..CH~~

At$$Ax 988q66$s1vvU";";";;AtCFF%88889 9 q66$s1vvU";";";;AtCFF%88889 9 88q66#a&&=3q66#a&&=88q66#a&&=3q66#a&&=88	r5   r   r   s    ` r3   r   r   O  s.    $    6 Rr5   c                 (    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)

    c           	         | 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 )NrG   Frg   Tc                     g | ]}||S r]   r]   r   s     r3   ra   z0TR10i.<locals>.f.<locals>.do.<locals>.<listcomp>  r   r5   twor   )r2   r   r:   r   rn   rN   r   r   r   r   r   r   r   r   r=   ru   samer   s                    r3   r   zTR10i.<locals>.f.<locals>.do  s    9 	(())D4yyA~~s4yy)) " "AaBz "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88s1q5zz>)3q1u::~%f88s1q5zz>)3q1u::~%r5   c                 D    t          t          | j                            S rJ   )tupler   free_symbolsr   s    r3   r   z"TR10i.<locals>.f.<locals>.<lambda>  s    eGAN$;$;<< r5   r   rg   c                 4    g | ]}t          d  |D              S )c                     g | ]}||S r]   r]   r   s     r3   ra   z/TR10i.<locals>.f.<locals>.<listcomp>.<listcomp>  s    (>(>(>2(>(>(>(>r5   r   )rM   ro   s     r3   ra   z$TR10i.<locals>.f.<locals>.<listcomp>  s<     #- #- #- $'(>(>a(>(>(>#? #- #- #-r5   r   )rV   r   r   r|   r:   ry   rK   r   r   HalfrL   r   rh   r;   _ROOT3	_invROOT3r   rW   r   values)r2   byradr=   r   rN   r:   ru   rn   r   r   r   r   s              @r3   r>   zTR10i.<locals>.f  s   y 	I6	& 6	& 6	& 6	& 6	& 6	&p $<<> >
 i &	%%EW 
+ 
+8 "f " "9 "16)9)9 " 2 *:!"I,,Q///"#C!E +!%L''*** D * * ((1*ikk2 * *AEzz!&s58}}!5!5 * *A$Qx{2 (%*3uQx==%9%9 	* 	*#(8A;#6$,&)%(1+a*C&D&D&(bgg#&#::$(KK$4$4$426E!HQK26E!HQK$)E	 $.*  4 #- #-"\\^^#- #- #- - / RVVM i &	P 	r5   r   r?   s     r3   TR10ir     s'    $i i iV Rr5   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 )NrG   r   T)rational)rU   r    r!   r   r;   ry   r   r:   	is_Numberr   r   TR11)	r2   r>   r   cor   r   mr   rL   s	           r3   r>   zTR11.<locals>.f)  s   73*$$I 	%A$q&		ABx )((Avc3Z''	wqzQVAY&&IIII88qD1a4K++Q3q58OI% 	% 71:**D*99DAqsQw!||c1fQhqslSNNSNN7c>>1QBBA1B	r5   r   )r2   rL   r>   s    ` r3   r   r     s0    T! ! ! ! !F Rr5   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 r8   )r   setr   rQ   as_base_expr   rU   r    r!   r   addr:   )flatr:   firu   rY   s        r3   sincos_argsz%_TR11.<locals>.f.<locals>.sincos_argsh  s     s##DmD)) 5 5~~''1< 5AEEv#s++T!WW))!&)444Kr5   c                     |t                    D ]a}|dz  }||t                   v rt          }n||t                    v rt           }n6t          | |          } ||                             |           b| S NrG   )r!   r    r   remove)r2   num_argsden_argsnargrZ   rU   s         r3   handle_matchz&_TR11.<locals>.f.<locals>.handle_matcht  s|     ! 	, 	,Av8C=((DDXc]**DD"d^^%%d++++Ir5   )r9   r   maprz   )r2   r  r  r  r  s        r3   r>   z_TR11.<locals>.fd  s    "d## 	I
	 
	 
	 !b.?.?.A.ABB(	 	 	  \"h11\"h11	r5   r   r?   s     r3   _TR11r  O  s$    *# # #J Rr5   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   rg   )
rU   r"   r:   rV   r|   r   r_   r   r   TR12)r2   r   r:   r=   ru   tbr   s         r3   r>   zTR12.<locals>.f  s    w#~~Igaj: 	1 &GCH--..CH~~

At$$Ax #a&&...VVFFRK!c!ffRi-00	r5   r   r   s    ` r3   r  r    s.        & Rr5   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 )NrG   c              3   @   K   | ]}t          |t                    V  d S rJ   )r9   r"   )rM   r  s     r3   rO   z/TR12i.<locals>.f.<locals>.ok.<locals>.<genexpr>  s,      AABJr3//AAAAAAr5   )as_f_sign_1r   NegativeOnery   rW   r:   all)dir   r   r>   r   s        r3   r[   zTR12i.<locals>.f.<locals>.ok  s    BA  1a%%!(%s16{{a7G7GAA!&AAAAA 8Ha4K	   %%%%7G7G7G7Gr5   c                 (    g | ]}|j         d          S r   r:   rM   _s     r3   ra   z$TR12i.<locals>.f.<locals>.<listcomp>  s    444!&)444r5   c                 (    g | ]}|j         d          S r  r   r!  s     r3   ra   z$TR12i.<locals>.f.<locals>.<listcomp>  s    888AafQi888r5   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
  )rV   rW   r:   r9   r"   )nir=   ru   s      r3   r[   zTR12i.<locals>.f.<locals>.ok  sy    y  S\\Q..w1a%%  *Q*<*<  a4K   ..       r5   FTc                 (    g | ]}|j         d          S r  r   r!  s     r3   ra   z$TR12i.<locals>.f.<locals>.<listcomp>  s    +++AafQi+++r5   c                 P    g | ]#\  }}t          d  |j        D              dz
  |z  $S )c                 ,    g | ]}t          |          S r]   )r"   rR   s     r3   ra   z/TR12i.<locals>.f.<locals>.<listcomp>.<listcomp>"  s+     8( 8( 8(A8( 8( 8(r5   rg   )r   r:   )rM   rn   rY   s      r3   ra   z$TR12i.<locals>.f.<locals>.<listcomp>"  se     1J 1J 1J59Q 36 8( 8( !8( 8( 8( 3)+,3-/021 1J 1J 1Jr5   )rV   ry   rK   rz   r:   r|   r   rQ   ri   r   r   r;   r)   r   r   rS   rL   rT   r
   r  extract_additivelyr_   r"   r~   )r2   r`   rd   dokr[   d_argsrn   r  r   r   r   r   n_argsr   r%  edneweds                    r3   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 	r5   r   r?   s     r3   TR12ir/    s'    *a a aF Rr5   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           	      P   | j         s| S t          g t          g d g i}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   rG   rg   )ry   r"   r#   r   rQ   rU   r   rh   r:   rW   r_   )r2   r:   r=   r   r   t1t2s          r3   r>   zTR13.<locals>.f8  s,   y 	I Rb$+r"" 	% 	%Av#s##T!WW$$QVAY////T
!!!$$$$IIq66A::#a&&1**IDz!ffqjjBBKKSWWSb\\1CGGCRLL4HHIJJJ !ffqjj  	&KKAEEGG%%%!ffqjjBBKKCGGCRLL003r773rBw<<3GGHHH !ffqjj  	&KKAEEGG%%%Dzr5   r   r?   s     r3   TR13r4  *  s#      < Rr5   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   rg   rG   Frq   c                 N    g | ]!}|         D ]}t          ||z  d           "S )Frq   )r    )rM   r=   rX   r:   s      r3   ra   z'TRmorrie.<locals>.f.<locals>.<listcomp>  sX     &I &I &I-.Q&I &I;<AaC%(((&I &I &I &Ir5   )ry   rz   r   r|   r:   r  r   r9   r    r   rh   sortr!   r   r   r_   r  r   )r2   r   r`   rd   cossotherr   ru   rY   r   r=   r   rX   cccinewargtakeccsrn   keyr:   r>   s                       @r3   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AggFA!GB Agg q55 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 	r5   r   r   r?   s    @r3   TRmorrierA  Y  s5    v7 7 7 7 7 7r Rr5   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   rg   r   r   c                      g | ]
}|         S r]   r]   )rM   rn   Bs     r3   ra   z#TR14.<locals>.f.<locals>.<listcomp>"      &:&:&:qt&:&:&:r5   c                      g | ]
}|         S r]   r]   )rM   rn   As     r3   ra   z#TR14.<locals>.f.<locals>.<listcomp>&  rF  r5   rG   )ry   rz   r   r;   TR14r:   rK   r  rS   rT   rh   r  rU   r    r!   r   r|   r   rW   r   r_   r   insertr9   r   )r2   r`   rd   r   r   r:  processr=   ru   rY   r   r   r>   sinotherr}   r   r>  remrH  rE  r   s                      @@r3   r>   zTR14.<locals>.f  sO   y 	I 		 $$&&DAq~~AU+++AU+++199		dB	 	: 	:Ax }}1  LLOOOEAA !	#s3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||&:&:&:&:T&:&:&: #A$ 'q# 6 6 6 6!"1&:&:&:&: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	r5   r   r   s    ` r3   rI  rI    s4    ,^ ^ ^ ^ ^@ Rr5   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 )NrG   rg   c                     d| z   S rf   r]   r   s    r3   r   z!TR15.<locals>.f.<locals>.<lambda>b  
    !a% r5   r   )r9   r   rL   r!   r   TR15r   r#   r2   rY   iar=   r   r   s       r3   r>   zTR15.<locals>.fY      2s## 	
27C(@(@ 	IFq5A::!a%())"'11rT"c3ScBBB77B	r5   r   r2   r   r   r>   s    `` r3   rS  rS  I  4           Rr5   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 )NrG   rg   c                     d| z   S rf   r]   r   s    r3   r   z!TR16.<locals>.f.<locals>.<lambda>  rR  r5   r   )r9   r   rL   r    r   rS  r   r"   rT  s       r3   r>   zTR16.<locals>.fz  rV  r5   r   rW  s    `` r3   TR16r\  j  rX  r5   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 r8   )r9   r   rL   rT   r   rS   is_negativer"   r#   r:   r!   r%   r    r$   r1   s    r3   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	r5   r   r?   s     r3   TR111r`    s#       Rr5   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 rf   r]   r   s    r3   r   z!TR22.<locals>.f.<locals>.<lambda>  
    1q5 r5   r   c                     | dz
  S rf   r]   r   s    r3   r   z!TR22.<locals>.f.<locals>.<lambda>  rd  r5   )	r9   r   rL   rU   r#   r"   r   r$   r%   r   s    r3   r>   zTR22.<locals>.f  si    2s## 	c
(B(BI2sCcsCCC2sCcsCCC	r5   r   rW  s    `` r3   TR22rf    s4    $      Rr5   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   rG   rg   c                 b    g | ]+}t          |          t          d |z  z
  z            z  ,S rG   r   r    rM   rX   r`   r   s     r3   ra   z&TRpower.<locals>.f.<locals>.<listcomp>  sJ     $/ $/ $/ %-QNN3AaC{3C3C$C $/ $/ $/r5   c                     g | ];}t          |          t          j        |z  z  t          d |z  z
  z            z  <S rj  )r   r   r  r!   rl  s     r3   ra   z&TRpower.<locals>.f.<locals>.<listcomp>  sc     =Q =Q =Q:; >Fa^^M1$>%%(!ac'1%5%5>6 =Q =Q =Qr5   c                 b    g | ]+}t          |          t          d |z  z
  z            z  ,S rj  rk  rl  s     r3   ra   z&TRpower.<locals>.f.<locals>.<listcomp>  sJ     $) $) $) %-QNN3AaC{3C3C$C $) $) $)r5   c                     g | ];}t          |          t          j        |z  z  t          d |z  z
  z            z  <S rj  )r   r   r  r    rl  s     r3   ra   z&TRpower.<locals>.f.<locals>.<listcomp>  sc     9K 9K 9K:; :B!QM1$:%%(!ac'1%5%5:6 9K 9K 9Kr5   )r9   r   rL   r!   r    r  r:   r   rT   is_oddr   r   r   r  is_evenr   )r2   ru   r`   r   s     @@r3   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#....	r5   r   r?   s     r3   TRpowerrr    s#    *  , Rr5   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'   r1   s    r3   Lru    s     RXX+,,---r5   c                 H    t          |           |                                 fS rJ   )ru  	count_opsr   s    r3   r   r   *  s    addAKKMM2 r5   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 )a7  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://www.sciencedirect.com/science/article/pii/S0895717706001609
    c                 2    g | ]}t          |           S ))measure)fu)rM   r=   rz  s     r3   ra   zfu.<locals>.<listcomp>v  s&    AAAAAw///AAAr5   )r@  )r*   RL1RL2r   r9   r   rU   r:   r@   hasr"   r#   rC   r!   r    r   rA  r   r   )r2   rz  fRL1fRL2r   rv1rv2s    `     r3   r{  r{  *  sH   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))))r5   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   rg   zmust have at least one keyFrr   T)
r   r|   r:   r   rh   r   r;   
ValueErrorrW   r   )r2   r   key2key1abscr=   r   r:   r   rX   ro   r"  rY   r   s                 r3   r   r     s    tD 7 	8 	8A>>##DAq1uu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axxKK!KK!A$
 $ZIr5   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                       t          d          S r
  r&   r]   r5   r3   _ROOT2r        77Nr5   c                       t          d          S )Nr   r  r]   r5   r3   r   r     r  r5   c                  &    dt          d          z  S )Nrg   r   r  r]   r5   r3   r   r     s    T!WW9r5   c           	         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)
    c                 ,    g | ]}t          |          S r]   r   r   s     r3   ra   ztrig_split.<locals>.<listcomp>      '''1GAJJ'''r5   rg   c                 6    g | ]}|                                 S r]   as_exprr   s     r3   ra   ztrig_split.<locals>.<listcomp>       ***AAIIKK***r5   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.
        NrG   r   )r   r;   ry   r   rW   r:   r|   r_   r9   r    r!   rK   r   r   )r=   r   r   r   r   r:   ru   s          r3   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##  aeqvooat  Ga%%   3''   X  !%16//!GBB41522dAq883 	AA3 	A9FQU??RR1axr5   Nr   c                     h | ]	}|j         
S r]   r   )rM   r   s     r3   	<setcomp>ztrig_split.<locals>.<setcomp>]  s    1111111r5   c              3   *   K   | ]}|j         v V  d S rJ   r   )rM   rn   r:   s     r3   rO   ztrig_split.<locals>.<genexpr>^  s)      <<a16T><<<<<<r5   r   FrG   r   r   )r/   r   r  r   r  factorsquor9   r!   rU   r:   r    r  r;   r  r   r   r   )r=   ru   r   uaubr   r   r   r  r   coacasacobcbsbr   r   r:   s                     @r3   r   r     s   d ('A'''DAqXXa[[FB
%%((



CKB}
""VVAM""S	
"*	$	$VVAM""S**"b***DAq? ? ?D 	AsAyKCRAsAy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IF11R111<<<<B8<<<<< FBBGAJ
Jr27<S<SSS 	" 	 	r 		ZBJ"*FH"H"6QVF 	%C 	%C#::688OCBq	2a466W  1S5LCBq	2a466W	##1S5LCBq	2a466 $#r5   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)
    rG   Nr   c                 ,    g | ]}t          |          S r]   r  r   s     r3   ra   zas_f_sign_1.<locals>.<listcomp>  r  r5   rg   c                 6    g | ]}|                                 S r]   r  r   s     r3   ra   zas_f_sign_1.<locals>.<listcomp>  r  r5   )rV   rW   r:   r   r  r;   ry   r   r/   r   r  r  r  )	rY   r=   ru   r   r  r  r   r   r   s	            r3   r  r  w  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AEzz!1RB	RxxdSAEzzArz zr5   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]   )rM   rn   rd   s     r3   ra   z'_osborne.<locals>.f.<locals>.<listcomp>  s    4I4I4IQQqS4I4I4Ir5   unhandled %s)r9   r   r:   rV   r   r   r   r   r!   r   r    r   r"   r   r#   r   r$   r   r%   NotImplementedErrorrU   )r2   r=   rd   s     r3   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&>???r5   r   rY   rd   r>   s    ` r3   _osborner    s1    "@ @ @ @ @( Q??r5   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  )r9   r'   r:   as_independentxreplacer   r;   r   r!   r   r    r   r"   r   r#   r   r$   r   r%   r   r  rU   )r2   constr   r=   rd   s       r3   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&>???r5   r   r  s    ` r3   	_osborneir    s1     @ @ @ @ @( Q??r5   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   )rM   r   s     r3   ra   z!hyper_as_trig.<locals>.<listcomp>  s     (((QQL(((r5   c                     g | ]	\  }}||f
S r]   r]   )rM   rX   ro   s      r3   ra   z!hyper_as_trig.<locals>.<listcomp>"  s     $$$tq!QF$$$r5   c           	            t          |                               t                                        t          j                  S rJ   )r  r  dictr   ImaginaryUnit)r   r  rd   repsr   s    r3   r   zhyper_as_trig.<locals>.<lambda>&  sG    ''((!Q  d,,3. 3./0+@ +@ r5   )
r   r   sympy.simplify.radsimpr  atomsr'   r  r  r   r  )r2   trigsmaskedr  rd   r  r   s      @@@@r3   hyper_as_trigr    s    4 100000...... HH*++E((%(((D[[d$$F %$t$$$DAFA !@ !@ !@ !@ !@ !@ !@ @ @r5   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)
    )r~  r    r!   r   r   rr  )exprs    r3   sincos_to_sumr  *  s;    & 88C .:gdmm,,---r5   )F)r   Fr   rJ   )NT)rcollectionsr   sympy.core.addr   sympy.core.cacher   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-   r4   r@   rC   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r/  r4  rA  rI  rS  r\  r`  rf  rr  ru  r|   r  CTR1CTR2CTR3CTR4r|  r}  r{  r   r   fufuncsr  ziplocalsgetFUr  r   r   r   r  r  r  r  r  r]   r5   r3   <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. . .b5 5 5@= = =@B B B B*B B B B*  0H H H HV[ [ [|- - - -`} } }@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!;!;<<==
>
>?? 	  	 	  	 	  	o7 o7 o7 o7d6 6 6r% % %P$ $ $N(@ (@ (@V. . . . .r5   