a
    SG5d                     @   s  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d Z;dd Z<dd Z=deddZ>dd  Z?d!d" Z@d#d$ ZAdfd&d'ZBdgd(d)ZCd*d+ ZDdhd-d.ZEd/d0 ZFdid1d2ZGd3d4 ZHdjd6d7ZId8d9 ZJdkd:d;ZKd<d= ZLd>d? ZMd@dA ZNdldBdCZOdmdDdEZPdndFdGZQdHdI ZRdodJdKZSdLdM ZTdNdO ZUe:reVeWe8e;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]dPdQ fdRdSZ^dpdTdUZ_dV` ZaebeVeceaeVeWed jeeaZfdWdX Zgd5ahdqdYdZZid[d\ Zjd]d^ Zkd_d` Zldadb Zmdcdd Znd5S )r    )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                 C   s   |     S )zSimplification of rational polynomials, trying to simplify
    the expression, e.g. combine things like 3*x + 2*x, etc....
    )normalr'   expandrv r0   M/var/www/html/django/DPS/env/lib/python3.9/site-packages/sympy/simplify/fu.pyTR0   s    r2   c                 C   s   d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                 S   sH   t | tr"| jd }tjt| S t | trD| jd }tjt| S | S Nr   )
isinstancer"   argsr   Oner   r#   r   r/   ar0   r0   r1   f4   s    



zTR1.<locals>.fr   r/   r9   r0   r0   r1   TR1(   s    	r;   c                 C   s   d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                 S   sL   t | tr$| jd }t|t| S t | trH| jd }t|t| S | S r3   )r4   r    r5   r   r   r!   r7   r0   r0   r1   r9   R   s    



zTR2.<locals>.fr   r:   r0   r0   r1   TR2@   s    	r<   Fc                    s    fd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                    s  | j s
| S |  \ js" jr&| S fdd fddt D }s^| S     fddt  D } s| S fdd}|| | | g }D ]}t|tr~t|j	d d	d
}| v r" | | kr"|
t|j	d |   d  |<  |< nZrd| }| v r | | kr|
t|j	d d |   d  |<  |< qt|trt|j	d d	d
}| v r | | kr|
t|j	d |    d  |<  |< qr|jr|j	d tju rt|j	d trt|j	d j	d d	d
}| v r | | kr | jsT|jr|
t|j	d d |    d  |<  |< q|rt|dd  D   tdd   D   } | tdd |D  tdd |D   9 } | S )Nc                    sF   |j s| joD| jttfv pD oD| joDt| jdkoDtdd | jD S )N   c                 s   s&   | ]}t d d t|D V  qdS )c                 s   s(   | ] }t |tp|jo|jtu V  qd S N)r4   r   is_Powbase).0air0   r0   r1   	<genexpr>   s   z8TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>.<genexpr>N)anyr   	make_argsrA   r8   r0   r0   r1   rC      s   z.TR2i.<locals>.f.<locals>.ok.<locals>.<genexpr>)	
is_integeris_positivefuncr   r   is_Addlenr5   rD   )kehalfr0   r1   ok   s    zTR2i.<locals>.f.<locals>.okc                    s(   g | ] }| | s|  |fqS r0   poprA   rL   )nrP   r0   r1   
<listcomp>       z#TR2i.<locals>.f.<locals>.<listcomp>c                    s(   g | ] }| | s|  |fqS r0   rQ   rS   )drP   r0   r1   rU      rV   c                    s   g }| D ]B}|j rt|jdkr r,t|nt|}||kr|||f q|rt|D ]\}\}}| |= |||< qXt|  }|D ]6}| | ||  }||r|| |< q|||f q~d S N   )	rJ   rK   r5   r'   r   append	enumerater   as_powers_dict)rW   ddoneZnewkrL   Zknewiv)rO   rP   r0   r1   	factorize   s"    


z"TR2i.<locals>.f.<locals>.factorizer   FevaluaterY   r=   c                 S   s   g | ]\}}|r|| qS r0   r0   rA   brM   r0   r0   r1   rU      rV   c                 S   s   g | ]\}}|r|| qS r0   r0   rc   r0   r0   r1   rU      rV   c                 S   s   g | ]\}}|| qS r0   r0   rc   r0   r0   r1   rU      rV   )is_Mulas_numer_denomis_Atomr\   listkeysr4   r   r   r5   rZ   r    rJ   r   r6   rG   rH   r   items)r/   Zndoner]   r`   trL   r8   a1rN   )rW   rT   rP   r1   r9   z   sb    



 $"(zTR2i.<locals>.fr   )r/   rO   r9   r0   rN   r1   TR2i^   s    Urm   c                    s"   ddl m   f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                    s   t | ts| S |  | jd } t | ts0| S | jd tjd  jtjd | jd  j  u rhdu rn n<ttttt	t
t
t	tttti}|t|  tjd | jd  } | S )Nr      r=   T)r4   r%   rI   r5   r   PirH   r   r   r    r!   r"   r#   type)r/   Zfmaprn   r0   r1   r9      s    

< zTR3.<locals>.f)sympy.simplify.simplifyro   r   r:   r0   rn   r1   TR3   s    
rt   c                 C   s   | 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
    r0   r.   r0   r0   r1   TR4   s    ru   c                    s    fd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                    s$  | j r| jj ks| S | jjs"| S | jdk dkr4| S | jkdkrF| S | jdkrT| S | jdkrv| jjd d S | jd dkr| jd } | jjd | jjd d |  S | jdkrd}n:s| jd r| S | jd }nt| j}|s| S | jd }| jjd d | S d S )Nr   TrY   r=   rp   )r?   r@   rI   expis_realr5   r&   )r/   rM   pr9   ghmaxpowr0   r1   _f1  s4    


,



z_TR56.<locals>._fr   )r/   r9   rz   r{   r|   r}   r~   r0   ry   r1   _TR56  s    #r   rp   c                 C   s   t | ttdd ||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                 S   s   d|  S rX   r0   xr0   r0   r1   <lambda>i  rV   zTR5.<locals>.<lambda>r|   r}   )r   r   r   r/   r|   r}   r0   r0   r1   TR5W  s    r   c                 C   s   t | ttdd ||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                 S   s   d|  S rX   r0   r   r0   r0   r1   r   ~  rV   zTR6.<locals>.<lambda>r   )r   r   r   r   r0   r0   r1   TR6l  s    r   c                 C   s   d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                 S   s<   | j r| jjtkr| jdks | S dtd| jjd   d S )Nr=   rY   r   )r?   r@   rI   r   rv   r5   r.   r0   r0   r1   r9     s    zTR7.<locals>.fr   r:   r0   r0   r1   TR7  s    r   Tc                    s    fd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                    s  | j s0| jr,| jjttfv r,| jjs0| jjs0| S  rdd | 	 D \}}t
|dd}t
|dd}||ksr||krt|| } | j r| jd jrt| jdkr| jd jrt|   } | S tg tg d g i}tt| D ]}|jttfv r|t| |jd  q|jr\|jjr\|jdkr\|jjttfv r\|t|j |jjd g|j  q|d  | q|t }|t }|r|st|dkst|dks| S |d  }tt|t|}t|D ]8}	| }
| }|t|
| t|
|  d  qt|dkrH| }
| }|t|
| t|
|  d  q|r`|t|  t|dkr| }
| }|t|
|  t|
|  d  q`|r|t|  t
tt| S )Nc                 S   s   g | ]}t |qS r0   r	   rA   r^   r0   r0   r1   rU     rV   z"TR8.<locals>.f.<locals>.<listcomp>Ffirstr   r=   rY   )re   r?   r@   rI   r   r   rv   rG   rH   rf   TR8r   r5   is_RationalrK   rJ   r   as_coeff_Mulr   rE   rr   rZ   
is_IntegerextendminrangerR   r
   )r/   rT   rW   newnnewdr5   r8   csr^   rl   a2r   r0   r1   r9     sp    
&(&&(zTR8.<locals>.fr   r/   r   r9   r0   r   r1   r     s    7r   c                 C   s   d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                    s"   | j s
| S d fdd	 t|  S )NTc                    s  | j s
| S tt| j}t|dkrd}tt|D ]p}|| }|d u rJq4t|d t|D ]F}|| }|d u rrq\|| } |}	|	|kr\|	||< d ||< d} q4q\q4|rtdd |D  } | j rʈ | } | S t| }
|
s| S |
\}}}}}}|rf||kr*|| d t|| d  t|| d  S |dk r>|| }}d| t	|| d  t	|| d  S ||kr|| d t	|| d  t|| d  S |dk r|| }}d| t|| d  t	|| d  S d S )	Nr=   FrY   Tc                 S   s   g | ]}|r|qS r0   r0   rA   r~   r0   r0   r1   rU   #  rV   z.TR9.<locals>.f.<locals>.do.<locals>.<listcomp>r   
rJ   rh   r   r5   rK   r   r   
trig_splitr   r   )r/   r   r5   hitr^   rB   jajwasnewsplitgcdn1n2r8   rd   Ziscosdor0   r1   r     sP    
,

(
,

zTR9.<locals>.f.<locals>.do)T)rJ   process_common_addendsr.   r0   r   r1   r9     s    >zTR9.<locals>.fr   r:   r0   r0   r1   TR9  s    Dr   c                    s    fd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                    s  | j ttfvr| S | j }| jd }|jr r>tt|j}n
t|j}| }t	|}|jr|tkrt|t
t|dd t|t
t|dd  S t|t
t|dd t|t
t|dd  S nH|tkrt|t| t|t|  S t|t| t|t|  S | S )Nr   Fr   )rI   r   r   r5   rJ   rh   r   rR   r   
_from_argsTR10)r/   r9   argr5   r8   rd   r   r0   r1   r9   T  s,    


  zTR10.<locals>.fr   r   r0   r   r1   r   B  s    r   c                 C   s    t du rt  d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                    s  | j s
| S d
 fdd	 t|  dd } | j rtt}| jD ]^}d}|jr|jD ]4}|jrR|jtj	u rR|j
jrR|| | d} qqR|s>|tj | q>g }|D ]}t| tfD ]}||v rtt|| D ]}|| | d u rqtt|| D ]j}|| | d u rqt|| | || |  }	 |	}
|
|	kr||
 d || |< d || |<  qqqqq|rt|dd	 | D   } q( | } qq(| S )NTc                    sb  | j s
| S tt| j}t|dkrd}tt|D ]p}|| }|d u rJq4t|d t|D ]F}|| }|d u rrq\|| } |}	|	|kr\|	||< d ||< d} q4q\q4|rtdd |D  } | j rʈ | } | S t|ddi}
|
s| S |
\}}}}}}|r,|| }||kr|t||  S |t||  S || }||krN|t	||  S |t	||  S d S )Nr=   FrY   Tc                 S   s   g | ]}|r|qS r0   r0   r   r0   r0   r1   rU     rV   z0TR10i.<locals>.f.<locals>.do.<locals>.<listcomp>twor   )r/   r   r5   r   r^   rB   r   r   r   r   r   r   r   r   r8   rd   samer   r0   r1   r     sL    


zTR10i.<locals>.f.<locals>.doc                 S   s   t t| jS r>   )tupler   free_symbolsr   r0   r0   r1   r     rV   z"TR10i.<locals>.f.<locals>.<lambda>r   rY   c                 S   s   g | ]}t d d |D  qS )c                 S   s   g | ]}|r|qS r0   r0   r   r0   r0   r1   rU     rV   z/TR10i.<locals>.f.<locals>.<listcomp>.<listcomp>r   )rA   r_   r0   r0   r1   rU     s   z$TR10i.<locals>.f.<locals>.<listcomp>)T)rJ   r   r   rh   r5   re   r?   rv   r   Halfr@   r   rZ   r6   _ROOT3	_invROOT3r   rK   r   values)r/   Zbyradr8   r   rB   r5   rd   r^   r   r   r   r0   r   r1   r9     sX    8




zTR10i.<locals>.f)_ROOT2_rootsr   r:   r0   r0   r1   TR10ir  s    kr   Nc                    s    fd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                    s4  | j ttfvr| S  r| j }| d }tj}|jr@| \}}|j ttfvrR| S | jd |jd krt }t }|tu r|d |d  | S d| | | S | S | jd js0| jd jdd\}}|j	d dkr0|j	d | |j
 }tt|}tt|}| j tkr d| | } n|d |d  } | S )Nr=   r   T)rational)rI   r   r   r   r6   re   r   r5   	is_Numberrx   qTR11)r/   r9   rk   cor   r   mr   r@   r0   r1   r9      s6    zTR11.<locals>.fr   )r/   r@   r9   r0   r   r1   r     s    *#r   c                 C   s   d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                 S   sL   t | ts| S dd }t||  \}}dd }|| ||} || ||} | S )Nc                 S   s\   t t}t| D ]D}| \}}|jr|dkr|jttfv r|t	| 
|jd  q|S r3   )r   setr   rE   as_base_expr   rI   r   r   rr   addr5   )flatr5   fird   rM   r0   r0   r1   sincos_args_  s    z%_TR11.<locals>.f.<locals>.sincos_argsc                 S   sX   |t  D ]J}|d }||t v r&t}n||t  v rt }nqt| |} || | q| S Nr=   )r   r   r   remove)r/   num_argsden_argsnargrO   rI   r0   r0   r1   handle_matchk  s    
z&_TR11.<locals>.f.<locals>.handle_match)r4   r   maprf   )r/   r   r   r   r   r0   r0   r1   r9   [  s    
z_TR11.<locals>.fr   r:   r0   r0   r1   _TR11F  s    %r   c                    s    fd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                    s   | j tks| S | jd }|jr r2tt|j}n
t|j}| }t|}|jrft	t|dd}nt|}t|| dt||   S | S )Nr   Fr   rY   )
rI   r    r5   rJ   rh   r   rR   r   r   TR12)r/   r   r5   r8   rd   tbr   r0   r1   r9     s    



zTR12.<locals>.fr   r   r0   r   r1   r     s    r   c                 C   s   d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                 S   s  | j s| js| js| S |  \}}|jr.|js2| S i }dd }tt|}t|D ]\}}||}|r|\}	}
t	dd |
jD  }t
j||< |	||< qT|j rt|}|jr||j t
j||< qT|jrT|jjs|jjrT||j}|r |\}	}
t	dd |
jD  }|j||< |	|j ||< qTt|}|jrT||j t
j||< qT|sP| S dd }ttt|}d}t|D ]@\}}||}|sH|| }|rt
j||< n|j rt|}|jrv||j t
j||< qvnh|jrv|jjs|jjrv||j}|rt
j||< n*t|}|jrv||j t
j||< qvnqvn
t
j||< d}t	d	d |D  }|| }|t
j}|d ur|r|||< n
|| ||  t| 9  < qv|rt| t|  td
d | D   } | S )Nc                 S   sT   t | }|rP|\}}}|tju rP|jrPt|jdkrPtdd |jD rP||fS d S )Nr=   c                 s   s   | ]}t |tV  qd S r>   )r4   r    )rA   r   r0   r0   r1   rC     rV   z/TR12i.<locals>.f.<locals>.ok.<locals>.<genexpr>)as_f_sign_1r   NegativeOnere   rK   r5   all)dir   rz   r9   r   r0   r0   r1   rP     s    
zTR12i.<locals>.f.<locals>.okc                 S   s   g | ]}|j d  qS r   r5   rA   _r0   r0   r1   rU     rV   z$TR12i.<locals>.f.<locals>.<listcomp>c                 S   s   g | ]}|j d  qS r   r   r   r0   r0   r1   rU     rV   c                 S   s>   | j r:t| jdkr:| j\}}t|tr:t|tr:||fS d S r   )rJ   rK   r5   r4   r    )nir8   rd   r0   r0   r1   rP     s    
FTc                 S   s   g | ]}|j d  qS r   r   r   r0   r0   r1   rU     rV   c                 S   s,   g | ]$\}}t d d |jD  d | qS )c                 S   s   g | ]}t |qS r0   )r    rF   r0   r0   r1   rU     s   z/TR12i.<locals>.f.<locals>.<listcomp>.<listcomp>rY   )r   r5   )rA   r^   rM   r0   r0   r1   rU     s   )rJ   re   r?   rf   r5   rh   r   rE   r[   r   r   r6   r'   r   rv   rG   r@   rH   r   r   extract_additivelyrR   r    rj   )r/   rT   rW   dokrP   Zd_argsr^   r   r   rz   rk   r   Zn_argsr   r   edZnewedr0   r0   r1   r9     s    











zTR12i.<locals>.fr   r:   r0   r0   r1   TR12i  s    cr   c                 C   s   d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                 S   sp  | j s
| S tg tg d g i}tt| D ]:}|jttfv rT|t| |j	d  q(|d  | q(|t }|t }t
|dk rt
|dk r| S |d  }t
|dkr| }| }|dt|t||  t|t||     q|r|t|  t
|dkrP| }| }|dt|t||   t|t||    q|rh|t|  t| S )Nr   r=   rY   )re   r    r!   r   r   rE   rI   rr   rZ   r5   rK   rR   )r/   r5   r8   rk   r   t1t2r0   r0   r1   r9   /  s2    44zTR13.<locals>.fr   r:   r0   r0   r1   TR13!  s    r   c                    s   d fd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                    s  | j s
| S |r.|  \}}|d|d S tt i }g }| jD ]T}| \}}|jrt|tr|jd 	 \}	}
 |
 
|	 |||< qD|
| qDg } D ]H}
 |
 }|  |rd}|d  }}||v r|d7 }|d9 }q|dkrtd| | |
 d|  t||
  }d }g }t|D ]@}|d }t|
| dd}|
| t|| |pb|| }q(t|D ]B}| }t|
| dd}||  |8  < || sr|| qr|
||  qt|d|
 }|
|||   qq|rt||  fdd D   } | S )Nr   rY   r=   Fra   c                    s*   g | ]"} | D ]}t || d dqqS )Fra   )r   )rA   r8   rL   r   r0   r1   rU     s   z'TRmorrie.<locals>.f.<locals>.<listcomp>)re   rf   r   rh   r5   r   r   r4   r   r   rZ   sortr   r   r   rR   r   r   )r/   r   rT   rW   Zcossotherr   rd   rM   r   r8   r   rL   cccinewargtakeZccsr^   keyr9   r   r1   r9     s`    




(


zTRmorrie.<locals>.f)Tr   r.   r0   r   r1   TRmorrieP  s    ;9r   c                    s    fd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              	      sF  | j s
| S rX|  \}}|tjurXt|dd}t|dd}||ksL||krT|| } | S g }g }| jD ]}|jr| \}}	|	js|j	s|
| qf|}ntj}	t|}
|
r|
d jttfvr|	tju r|
| qf|
||	  qf|
\}}}|
||	j|	|||f qftt|}t|}ttd }\}}}	}}}|r,|d |r|d  |	 jrz|	 jrz | | kr | | kr|dt |	 |	 }|	 |krfdd|D }||	  |8  < |d| n< |	 |kr( fdd|D }||	  |8  < |d| t | tr>t}nt}|
 |  |  | | jd d	  |  q8n |	 |	 kr | | kr | | kr|d |	 }t | trt}nt}|
 |  |  | | jd d	  |  q8|
 |  |	   q8t||krBt| } | S )
NFr   rY      r   c                    s   g | ]} | qS r0   r0   r   )Br0   r1   rU     rV   z#TR14.<locals>.f.<locals>.<listcomp>c                    s   g | ]} | qS r0   r0   r   )Ar0   r1   rU     rV   r=   )re   rf   r   r6   TR14r5   r?   r   rG   rH   rZ   r   rI   r   r   r   rh   r   rK   r   rR   r   insertr4   r   )r/   rT   rW   r   r   r   processr8   rd   rM   r   rz   r9   siZnotherri   rk   r   remr   )r   r   r1   r9     s    






2
2zTR14.<locals>.fr   r   r0   r   r1   r     s    `r   c                    s    fd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                    st   t | trt | jts| S | j}|d dkrDt| j|d  | j S d|  }t|ttdd  d}||krp|} | S )Nr=   rY   c                 S   s   d|  S rX   r0   r   r0   r0   r1   r   Y  rV   z!TR15.<locals>.f.<locals>.<lambda>r   )r4   r   r@   r   rv   TR15r   r!   r/   rM   iar8   r   r0   r1   r9   P  s    zTR15.<locals>.fr   r/   r|   r}   r9   r0   r   r1   r   @  s    r   c                    s    fd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                    st   t | trt | jts| S | j}|d dkrDt| j|d  | j S d|  }t|ttdd  d}||krp|} | S )Nr=   rY   c                 S   s   d|  S rX   r0   r   r0   r0   r1   r   z  rV   z!TR16.<locals>.f.<locals>.<lambda>r   )r4   r   r@   r   rv   r   r   r    r   r   r0   r1   r9   q  s    zTR16.<locals>.fr   r   r0   r   r1   TR16a  s    r   c                 C   s   d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                 S   s   t | tr"| jjs&| jjr"| jjs&| S t | jtrJt| jj	d | j  S t | jt
rnt| jj	d | j  S t | jtrt| jj	d | j  S | S r3   )r4   r   r@   rH   rv   rG   is_negativer    r!   r5   r   r#   r   r"   r.   r0   r0   r1   r9     s     zTR111.<locals>.fr   r:   r0   r0   r1   TR111  s    r   c                    s    fd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                    sR   t | tr| jjttfv s| S t| ttdd  d} t| ttdd  d} | S )Nc                 S   s   | d S rX   r0   r   r0   r0   r1   r     rV   z!TR22.<locals>.f.<locals>.<lambda>r   c                 S   s   | d S rX   r0   r   r0   r0   r1   r     rV   )	r4   r   r@   rI   r!   r    r   r"   r#   r.   r   r0   r1   r9     s
    zTR22.<locals>.fr   r   r0   r   r1   TR22  s    r   c                 C   s   d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                    s  t | trt | jttfs| S |  \} |jd  jr jr j	rt |trdd   t
 fddt d d D   } n j	rt |trdd   tj d d   t
 fddt d d D   } n jrt |trdd   t
 fddt d D   } nN jrjt |trjdd   tj d   t
 fddt d D   }  jr| d   t  d  7 } | S )	Nr   r=   rY   c                    s*   g | ]"}t  |t d |    qS r=   r   r   rS   rT   r   r0   r1   rU     s   z&TRpower.<locals>.f.<locals>.<listcomp>c                    s4   g | ],}t  |tj|  t d |    qS r   )r   r   r   r   rS   r   r0   r1   rU     s   c                    s*   g | ]"}t  |t d |    qS r   r   rS   r   r0   r1   rU     s   c                    s4   g | ],}t  |tj|  t d |    qS r   )r   r   r   r   rS   r   r0   r1   rU     s   )r4   r   r@   r   r   r   r5   r   rH   is_oddr   r   r   r   is_evenr   )r/   rd   r0   r   r1   r9     s0    
*
&

zTRpower.<locals>.fr   r:   r0   r0   r1   TRpower  s    r   c                 C   s   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%   r.   r0   r0   r1   L  s    r   c                 C   s   t | |  fS r>   )r   	count_opsr   r0   r0   r1   r   !  rV   r   c                    s   t t }t t }| }t| } t| tsD| j fdd| jD  S t| } | 	t
tr|| } | | k rt|} | 	t
trt| } | 	ttr|| }tt|}t|| ||g d} tt| |  d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                    s   g | ]}t | d qS )measure)furF   r   r0   r1   rU   m  rV   zfu.<locals>.<listcomp>)r   )r(   RL1RL2r   r4   r   rI   r5   r;   hasr    r!   r<   r   r   r   r   r   rm   )r/   r  ZfRL1ZfRL2r   Zrv1Zrv2r0   r   r1   r  !  s$    F


r  c                 C   s  t t}|rX| jD ]B}| \}}|dk r6| }| }|||rF||ndf | qn2|r| jD ]}|tj||f | qbntdg }d}|D ]h}	||	 }
|	\}}t|
dkrt	|
ddi}||}||kr|}d}|||  q|||
d   q|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   rY   zmust have at least one keyFrb   T)
r   rh   r5   r   rZ   r   r6   
ValueErrorrK   r   )r/   r   key2key1Zabscr8   r   r5   r   rL   r_   r   rM   r   r0   r0   r1   r   |  s8    
"
r   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                   C   s   t dt d aadt ad S )Nr=      rY   )r$   r   r   r   r0   r0   r0   r1   r     s    r   c                    s*  t du rt  dd | |fD \} }| |\}}| | }d }}tj|jv rh|tj}| }ntj|jv r|tj}| }dd ||fD \} }dd }|| |}	|	du rdS |	\}
}}|||}	|	du rdS |	\}}}|s|s|r$t	|t
r$||||
||f\}
}}}}}|| }}|sr|p2|}|p<|}t	||jsPdS ||||jd |jd t	|tfS |
s
|s
|r
|r
|r
|r
t	||jt	||jurdS d	d
 ||fD  t fdd||fD sdS ||||jd |jd t	||jfS |r|sP|r"|sP|rT|du r<|du sP|du rT|du rTdS |p\|}|pf|}|j|jkrzdS |
stj}
|stj}|
|u r|t 9 }||||jd td dfS |
| tkr|d| 9 }||||jd td dfS |
| tkr&|d|
 9 }||||jd td 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                 S   s   g | ]}t |qS r0   r   r   r0   r0   r1   rU     rV   ztrig_split.<locals>.<listcomp>rY   c                 S   s   g | ]}|  qS r0   as_exprr   r0   r0   r1   rU     rV   c                 S   sl  d }}t j}| jr|  \}} t| jdks4|s8dS | jrJt| j}n| g}|d} t| t	rj| }n0t| t
rz| }n | jr| jt ju r|| 9 }ndS |r|d }t|t	r|r|}q|}n:t|t
r|r|}q|}n |jr|jt ju r||9 }ndS |t jur
|nd||fS t| t	r&| }nt| t
r6| }|du rN|du rNd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.
        Nr=   r   )r   r6   re   r   rK   r5   rh   rR   r4   r   r   r?   rv   r   )r8   r   r   r   r   r5   rd   r0   r0   r1   pow_cos_sin  sN    






ztrig_split.<locals>.pow_cos_sinr   c                 S   s   h | ]
}|j qS r0   r   )rA   r   r0   r0   r1   	<setcomp>P  rV   ztrig_split.<locals>.<setcomp>c                 3   s   | ]}|j  v V  qd S r>   r   r   r   r0   r1   rC   Q  rV   ztrig_split.<locals>.<genexpr>rp   Fr=   r	  r   )r   r   r,   r   r  r   r   factorsquor4   r   rI   r5   r   r   r6   r   r   r   )r8   rd   r   uaubr   r   r   r  r   ZcoacasaZcobcbsbr   r   r0   r   r1   r     s    3B






"$


r   c           	      C   sX  | j rt| jdkrdS | j\}}|tjtjfv rvtj}|jrl|jd jrl|jd dk rl| |  }}| }|||fS dd | jD \}}||\}}|	|
 }tj|jv r|tj}d}d}n*tj|jv r|tj}d}d}nd }}dd ||fD \}}|tju r(|| }}|| }}|dkr>| }| }|tju rT|||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)
    r=   Nr   c                 S   s   g | ]}t |qS r0   r
  r   r0   r0   r1   rU     rV   zas_f_sign_1.<locals>.<listcomp>rY   c                 S   s   g | ]}|  qS r0   r  r   r0   r0   r1   rU     rV   )rJ   rK   r5   r   r   r6   re   r   r,   r   r  r  r  )	rM   r8   rd   rz   r  r  r   r   r   r0   r0   r1   r   j  s<    
 



r   c                    s    fd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                    s   t | ts| S | jd }|js&|  nt fdd|jD }t | trVtt| S t | t	rht
|S t | tr~tt| S t | trt|t S t | trt|S t | trt|t S td| j d S )Nr   c                    s   g | ]}|  qS r0   r0   r   rW   r0   r1   rU     rV   z'_osborne.<locals>.f.<locals>.<listcomp>unhandled %s)r4   r   r5   rJ   r   r   r   r   r   r   r   r   r    r   r!   r   r"   r   r#   NotImplementedErrorrI   r7   r  r0   r1   r9     s"    

(





z_osborne.<locals>.fr   rM   rW   r9   r0   r  r1   _osborne  s    r  c                    s    fd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                    s   t | ts| S | jd j dd\}}| tji|t  }t | trTt	|t S t | t
rft|S t | tr|t|t S t | trt|t S t | trt|S t | trt|t S td| j d S )Nr   T)as_Addr  )r4   r%   r5   as_independentxreplacer   r6   r   r   r   r   r   r    r   r!   r   r"   r   r#   r   r  rI   )r/   constr   r8   r  r0   r1   r9     s"    






z_osbornei.<locals>.fr   r  r0   r  r1   	_osbornei  s    r!  c                    sn   ddl m ddlm  | t}dd |D | t}dd D t t	| fd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   rn   )collectc                 S   s   g | ]}|t  fqS r0   r   )rA   rk   r0   r0   r1   rU     rV   z!hyper_as_trig.<locals>.<listcomp>c                 S   s   g | ]\}}||fqS r0   r0   )rA   rL   r_   r0   r0   r1   rU     rV   c                    s     t | ttjS r>   )r!  r  dictr   ImaginaryUnitr   r"  rW   repsro   r0   r1   r     s   zhyper_as_trig.<locals>.<lambda>)
rs   ro   sympy.simplify.radsimpr"  atomsr%   r  r#  r   r  )r/   trigsmaskedr0   r%  r1   hyper_as_trig  s    
r+  c                 C   s$   |  tts| S ttt| S d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)
    N)r  r   r   r   r
   r   )exprr0   r0   r1   sincos_to_sum  s    r-  )F)rp   F)rp   F)T)T)N)T)T)rp   F)rp   F)rp   F)NT)F)ocollectionsr   Zsympy.core.addr   sympy.core.exprr   Zsympy.core.exprtoolsr   r   r   sympy.core.functionr
   Zsympy.core.mulr   sympy.core.numbersr   r   sympy.core.powerr   Z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+   r2   r;   r<   rm   rt   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh   r   ZCTR1ZCTR2ZCTR3ZCTR4r  r  r  r   r   Zfufuncsr#  ziplocalsgetFUr   r   r   r   r  r!  r+  r-  r0   r0   r0   r1   <module>   s   $(	
t*@


K^
0 
P=
#{/w
y
!
! 
.		
[
* 
 79('+