
    Ed                        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mZ d dlmZ d dlmZmZmZmZmZmZ d dlmZmZ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#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+ d dl,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> d dl?m@Z@ d dlAmBZB d dlCmDZD g dddfdZEe/e-fZFd ZGd ZHdddZId ZJdaKd  ZLd! ZMd" ZNd# ZOd$ ZPd% ZQd-d&ZRed-d'            ZSdd(d)ZTd* ZUd+ ZVd.d,ZWdS )/    )defaultdict)reduce)sympifyBasicSExprfactor_termsMulAdd	bottom_up)cacheit)	count_ops_mexpandFunctionClassexpand
expand_mul
Derivative)IIntegerigcd)_nodes)DummysymbolsWild)
SYMPY_INTS)	sincosexpcoshtanhsinhtancotcoth)HyperbolicFunction)TrigonometricFunction)Polyfactorcancelparallel_poly_from_expr)ZZ)PolificationFailed)groebner)cse)identity)greedy)iterable)debugFgrlexc                    d d fd}t          d          |                     t          j                  } t          j        fg}t	          |                                           \  }	 t          |g          \  \  }}	}
n# t          $ r | cY S w xY wt          d|
j	                    ||
j	        |          \  }t          d|           t          ddt                               t          d	dt                               s| S t          |t          
          t          dt                    dt                               ddlm r |	j        t#                                        |	j	                   r t'          |z             j         }g }|                                D ]E\  }}t#          t          |g          d         j	                  d}|rd}|D ]}t'          |          }                    |j	                  s] |j        t#          |j	                                                 s.d}                    |                                j	                   |fdD             }fdj        D             }|                    t9          d t;          |          D               |z  ||t          |                              |          z             Gt=          | S  | t                    z   t          |                              |          S )a   
    Simplify trigonometric expressions using a groebner basis algorithm.

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

    This routine takes a fraction involving trigonometric or hyperbolic
    expressions, and tries to simplify it. The primary metric is the
    total degree. Some attempts are made to choose the simplest possible
    expression of the minimal degree, but this is non-rigorous, and also
    very slow (see the ``quick=True`` option).

    If ``polynomial`` is set to True, instead of simplifying numerator and
    denominator together, this function just brings numerator and denominator
    into a canonical form. This is much faster, but has potentially worse
    results. However, if the input is a polynomial, then the result is
    guaranteed to be an equivalent polynomial of minimal degree.

    The most important option is hints. Its entries can be any of the
    following:

    - a natural number
    - a function
    - an iterable of the form (func, var1, var2, ...)
    - anything else, interpreted as a generator

    A number is used to indicate that the search space should be increased.
    A function is used to indicate that said function is likely to occur in a
    simplified expression.
    An iterable is used indicate that func(var1 + var2 + ...) is likely to
    occur in a simplified .
    An additional generator also indicates that it is likely to occur.
    (See examples below).

    This routine carries out various computationally intensive algorithms.
    The option ``quick=True`` can be used to suppress one particularly slow
    step (at the expense of potentially more complicated results, but never at
    the expense of increased total degree).

    Examples
    ========

    >>> from sympy.abc import x, y
    >>> from sympy import sin, tan, cos, sinh, cosh, tanh
    >>> from sympy.simplify.trigsimp import trigsimp_groebner

    Suppose you want to simplify ``sin(x)*cos(x)``. Naively, nothing happens:

    >>> ex = sin(x)*cos(x)
    >>> trigsimp_groebner(ex)
    sin(x)*cos(x)

    This is because ``trigsimp_groebner`` only looks for a simplification
    involving just ``sin(x)`` and ``cos(x)``. You can tell it to also try
    ``2*x`` by passing ``hints=[2]``:

    >>> trigsimp_groebner(ex, hints=[2])
    sin(2*x)/2
    >>> trigsimp_groebner(sin(x)**2 - cos(x)**2, hints=[2])
    -cos(2*x)

    Increasing the search space this way can quickly become expensive. A much
    faster way is to give a specific expression that is likely to occur:

    >>> trigsimp_groebner(ex, hints=[sin(2*x)])
    sin(2*x)/2

    Hyperbolic expressions are similarly supported:

    >>> trigsimp_groebner(sinh(2*x)/sinh(x))
    2*cosh(x)

    Note how no hints had to be passed, since the expression already involved
    ``2*x``.

    The tangent function is also supported. You can either pass ``tan`` in the
    hints, to indicate that tan should be tried whenever cosine or sine are,
    or you can pass a specific generator:

    >>> trigsimp_groebner(sin(x)/cos(x), hints=[tan])
    tan(x)
    >>> trigsimp_groebner(sinh(x)/cosh(x), hints=[tanh(x)])
    tanh(x)

    Finally, you can use the iterable form to suggest that angle sum formulae
    should be tried:

    >>> ex = (tan(x) + tan(y))/(1 - tan(x)*tan(y))
    >>> trigsimp_groebner(ex, hints=[(tan, x, y)])
    tan(x + y)
    c                    d}g g g }}}| D ]t          t          t          f          r}!t          t                    r|                               Lt                    r|                    d         dd         f           |                    t          fddd         D              d         t          dd                    gz             d         j	                   |                               ||||fS )z-Split hints into (n, funcs, iterables, gens).   r   Nc                 2    g | ]} d          |          S r    ).0xes     7lib/python3.11/site-packages/sympy/simplify/trigsimp.py
<listcomp>z:trigsimp_groebner.<locals>.parse_hints.<locals>.<listcomp>   s%    ,,,TQqT!WW,,,    )

isinstancer   r   r   appendr1   extendr*   r   gens)hintsnfuncs	iterablesrC   r<   s        @r=   parse_hintsz&trigsimp_groebner.<locals>.parse_hints   s6   !#R$y 	 	A!j'233 A}-- 
Q!   !A$!""/// 3,,,,ae,,,!S!ABB%[0A0A/BBD DDEGGKM M M M A%D((r?   c           	         g }t          d          }|D ]=\  }}t          t          t          t          |           dz  t          |           dz  z   dz
  gt          t
          t          t	          |           dz  t          |           dz  z
  dz
  gfD ]\  }}}}	|dk    r|||fv r |j        |	           $||k    r; |j         ||| z             ||| z            z   ||| z            z
             e|||fv rU |||z                                d          	                    ||           }
 |j         ||| z            |
z
             ?t          t          |                    S )av  
        Build generators for our ideal. ``Terms`` is an iterable with elements of
        the form (fn, coeff), indicating that we have a generator fn(coeff*x).

        If any of the terms is trigonometric, sin(x) and cos(x) are guaranteed
        to appear in terms. Similarly for hyperbolic functions. For tan(n*x),
        sin(n*x) and cos(n*x) are guaranteed.
        y   r6   Ttrig)r   r   r   r"   r   r!   r    rA   r   subslistset)r;   termsr   rJ   fncoeffcstrelcns              r=   build_idealz&trigsimp_groebner.<locals>.build_ideal   s    #JJ 
	/ 
	/IB#sCFFAIA	$9A$=>4tAwwzDGGQJ'>'BC!E 	/ 	/1a A: /"A, /AHSMMMM1W /AHQQuQwZZ%'

2QQuQwZZ?@@@@Aq6\ /E!G+++66;;AqAABAHRRa[[2-...	/ CFF||r?   c           	      	    "|          \  }}}}t          d|z  d|d|d|           t          |           } |                     |           t          t          |                    }t          t          |                    }t          t          |                     } t          t
          t          t          t          t          hfd| D             }fd| D             }g }i }	|D ]3\  \  }
}}|	
                    |g                               |
|f           4g }|	                                D ]g\  }}d |D             }d |D             }t          t          |          fd	t          ||          D             }t          ||z             t
          t          t          gt          t          t          gfD ]N\  }}}t!          fd
|||fD                       r*                    |                               |           OD ]/}t%          d|dz             D ]}|                    ||f           0g }|D ]\  }}|t          k    r8|                    t          |f           |                    t
          |f           |t          t
          fv r%t          v r|                    t          |f           |t          k    r8|                    t          |f           |                    t          |f           |t          t          fv r%t          v r|                    t          |f           |                    |           t'          | z    |          }|                    |           |                    fd|D                        i|D ]
\  }}|t          k    r&|                    t          |ft
          |fg           7|t          k    r&|                    t          |ft          |fg           ht)          dt+          |          z  t,                    } |t/          |                               d                              t          t          ||                              }|                     |t/          |           |z
             !| v rE|                    !dz  dz              |                    !           |                    !           |||fS )z
        Analyse the generators ``gens``, using the hints ``hints``.

        The meaning of ``hints`` is described in the main docstring.
        Return a new list of generators, and also the ideal we should
        work with.
        zn=%szfuncs:z
iterables:z
extragens:c                 n    g | ]1}|j         v |j        d                                          |j         f2S r8   )funcargsas_coeff_mulr:   gallfuncss     r=   r>   z;trigsimp_groebner.<locals>.analyse_gens.<locals>.<listcomp>  sM     , , ,A(*,afQi,,..7 , , ,r?   c                 &    g | ]}|j         v|S r9   )r\   r_   s     r=   r>   z;trigsimp_groebner.<locals>.analyse_gens.<locals>.<listcomp>  s%    >>>!qvX'=>A>>>r?   c                     g | ]
}|d          S )r6   r9   r:   r;   s     r=   r>   z;trigsimp_groebner.<locals>.analyse_gens.<locals>.<listcomp>/      %%%A1Q4%%%r?   c                     g | ]
}|d          S r8   r9   rd   s     r=   r>   z;trigsimp_groebner.<locals>.analyse_gens.<locals>.<listcomp>0  re   r?   c                 $    g | ]\  }}||z  fS r9   r9   )r:   rR   vgcds      r=   r>   z;trigsimp_groebner.<locals>.analyse_gens.<locals>.<listcomp>2  s%    >>>Wb!b!C%[>>>r?   c              3       K   | ]}|v V  	d S Nr9   )r:   r;   fss     r=   	<genexpr>z:trigsimp_groebner.<locals>.analyse_gens.<locals>.<genexpr>5  s'      221qBw222222r?   r6   c                 2    h | ]\  }} ||z            S r9   r9   )r:   rR   rh   r;   s      r=   	<setcomp>z:trigsimp_groebner.<locals>.analyse_gens.<locals>.<setcomp>K  s)    777ABBqsGG777r?   zd:%iclsTrL   rK   )r2   rO   rB   rP   r   r   r"   r!   r   r    
setdefaultrA   itemsr   r   zipanyaddranger
   r   lenr   r   r   rN   remove)#rC   rD   rE   rF   rG   	extragens	trigtermsfreegensnewgenstrigdictrS   varrR   reskeyvalfnsrQ   rT   rU   rV   kextrarh   rr]   dummysexprra   rl   ri   r;   rY   myIrH   s#                               @@@@r=   analyse_gensz'trigsimp_groebner.<locals>.analyse_gens   s/    *5U););&5)Yfqj(E<y	2 	2 	2 DzzI SZZ  Y((	CII c4t4, , , , , , ,	 ?>>>t>>> ) 	= 	=LUC"R((//<<<< (( -	9 -	9HC" &%%%%C%%%%%Cs##C>>>>C>>>EUS[!!B #sOdD$-?@  1a2222Aq	22222 FF1IIIFF1III * *q!a% * *ALL"a))))*E 
, 
,A9 +LL#q***LL#q***#s# +r	 +LL#q***: ,LL$+++LL$+++$% ,$"* ,LL$+++LLCIAAu%%AJJqMMMNN77777778888 " 
	2 
	2HBSy 	2  3+T{!;<<<<t 2  4,t!=>>>> #d))!3???r3<((//T/::??SQUEVEV@W@WXX

22c4j>>D01111$; 	 JJsAvz"""OOC   NN3Hg%%r?   r   zinitial gens:zideal:z	new gens:z -- lenz
free gens:)orderrC   domainzgroebner basis:r   )ratsimpmodprime)rC   r6   TFc                     g | ]}|v |	S r9   r9   )r:   r;   ourgenss     r=   r>   z%trigsimp_groebner.<locals>.<listcomp>  s"    888a1<8888r?   c                 |    g | ]8} |j                             |j                   $|                                9S r9   )has_only_gensintersectionrC   as_expr)r:   r`   r   s     r=   r>   z%trigsimp_groebner.<locals>.<listcomp>  sU     D D DA#AOW%9%9!&%A%ABDAIIKK D D Dr?   c                     g | ]
\  }}||z  S r9   r9   )r:   abs      r=   r>   z%trigsimp_groebner.<locals>.<listcomp>  s     CCCdaQTCCCr?   )r   rC   quickr   
polynomial)r   rN   r   ImaginaryUnitr)   as_numer_denomr*   r,   r2   rC   rx   r-   r+   rO   sympy.simplify.ratsimpr   r   rP   r   r'   ejectrQ   
issuperset
differenceupdateexcludepolysrA   r
   rt   r   )r   rD   r   r   r   r   rN   numpnumpdenomoptidealr   monomrS   changedprealgensourGGrY   denomr|   rC   r   r   rH   r   s     ``               @@@@@@@@@r=   trigsimp_groebnerr      s   f) ) )(  0e& e& e& e& e& e& e&N **C99Q_c**D!/"#D,,..JC5sElCCv   	/38$$$(L599E8T	(E	+tYD		222	,)SYY777  e$r:::A	
T!WWiQ888
 766666 ,F(F(#d))*@*@*M*MN ,F1d3T(]+++148IIKK 	J 	JLE515%.AA!DIJJG G 9 9 9AQA"--af55 9*1?CKK,B,B7,K,KL9"&qyy{{'7888  9 98884888HD D D D D D DDJJsCCc(E.B.BCCCD&uU{D,4E"2<> > >>Bd4jjI J J J J Cy $q''Xd]z; ; ;;?4::	Fs   <B B#"B#c                 x   ddl m t          |           } t          | dd          }| |di S                     dd          }|sC                    dd                               dd                               d	d
          }nd}d fdd fdfdfdd|         } ||           S )a  Returns a reduced expression by using known trig identities.

    Parameters
    ==========

    method : string, optional
        Specifies the method to use. Valid choices are:

        - ``'matching'``, default
        - ``'groebner'``
        - ``'combined'``
        - ``'fu'``
        - ``'old'``

        If ``'matching'``, simplify the expression recursively by targeting
        common patterns. If ``'groebner'``, apply an experimental groebner
        basis algorithm. In this case further options are forwarded to
        ``trigsimp_groebner``, please refer to
        its docstring. If ``'combined'``, it first runs the groebner basis
        algorithm with small default parameters, then runs the ``'matching'``
        algorithm. If ``'fu'``, run the collection of trigonometric
        transformations described by Fu, et al. (see the
        :py:func:`~sympy.simplify.fu.fu` docstring). If ``'old'``, the original
        SymPy trig simplication function is run.
    opts :
        Optional keyword arguments passed to the method. See each method's
        function docstring for details.

    Examples
    ========

    >>> from sympy import trigsimp, sin, cos, log
    >>> from sympy.abc import x
    >>> e = 2*sin(x)**2 + 2*cos(x)**2
    >>> trigsimp(e)
    2

    Simplification occurs wherever trigonometric functions are located.

    >>> trigsimp(log(e))
    log(2)

    Using ``method='groebner'`` (or ``method='combined'``) might lead to
    greater simplification.

    The old trigsimp routine can be accessed as with method ``method='old'``.

    >>> from sympy import coth, tanh
    >>> t = 3*tanh(x)**7 - 2/coth(x)**7
    >>> trigsimp(t, method='old') == t
    True
    >>> trigsimp(t)
    tanh(x)**7

    r   )fu_eval_trigsimpNoldFdeep	recursivemethodmatchingc                 p    fd |           }t          |t                    s|S t          |fi S )Nc                     | j         r| S fd| j        D             }| j        s| j        rfd|D             } | j        | S )Nc                 &    g | ]} |          S r9   r9   r:   r;   traverses     r=   r>   zDtrigsimp.<locals>.groebnersimp.<locals>.traverse.<locals>.<listcomp>  !    000AHHQKK000r?   c                 *    g | ]}t          |fi S r9   r   r:   r;   optss     r=   r>   zDtrigsimp.<locals>.groebnersimp.<locals>.traverse.<locals>.<listcomp>  *    CCC)!44t44CCCr?   is_Atomr]   is_Functionis_Powr\   r<   r]   r   r   s     r=   r   z0trigsimp.<locals>.groebnersimp.<locals>.traverse  i    y 0000000D} D DCCCCdCCC164= r?   )r@   r   r   )exr   newr   s    ` @r=   groebnersimpztrigsimp.<locals>.groebnersimp  s^    	! 	! 	! 	! 	! 	! hrll#t$$ 	J -----r?   c                      | fi S rk   r9   )r;   r   r   s    r=   <lambda>ztrigsimp.<locals>.<lambda>  s    A r?   c                      t          |           S rk   )futrigr;   s    r=   r   ztrigsimp.<locals>.<lambda>  s    vayy r?   c                      | fi S rk   r9   )r;   r   r   s    r=   r   ztrigsimp.<locals>.<lambda>  s    ||A6666 r?   c                 H    t           | ddt          g                    S NTrK   )r   rD   )r   r"   )r;   r   s    r=   r   ztrigsimp.<locals>.<lambda>  s4    vll1*.q#h'@ '@ '@  A  A r?   c                     t          | fi S rk   )trigsimp_old)r;   r   s    r=   r   ztrigsimp.<locals>.<lambda>  s    a00400 r?   )r   r   r-   combinedr   r9   )sympy.simplify.fur   r   getattrpop)r   r   r   r   r   trigsimpfuncr   r   s    `    @@r=   trigsimpr     s3   p %$$$$$4==DT#3T::N &~%%%%%
((5%
 
 C d###(J//. . . '&&&&((66666A A A A0000  L <r?   c                    ddl m}m} d }t          | |          }fdt          |          }|                    t
                    r" ||          \  }  ||                    }|                    t                    r ||          }|                    t                    r|                     t                    r|} | S )a#  
    Simplifies exponential / trigonometric / hyperbolic functions.

    Examples
    ========

    >>> from sympy import exptrigsimp, exp, cosh, sinh
    >>> from sympy.abc import z

    >>> exptrigsimp(exp(z) + exp(-z))
    2*cosh(z)
    >>> exptrigsimp(cosh(z) - sinh(z))
    exp(-z)
    r   )hyper_as_trigTR2ic                     | g} | j         t           r-|                    |                     t                               |                    |                     t
                               t          |dt          iS )Nr   )has_trigsrA   rewriter   r   minr   )r<   choicess     r=   exp_trigzexptrigsimp.<locals>.exp_trig  sf     #15&> 	+NN199S>>***qyy~~&&&G++++r?   c                   
 | j         s| S |                                 \  }}t          |          dk    r t          |           t          | z  S |                                 }|                                
t          j        ffd	|t          j                 }|D ]}|j	        rt          |j
                  dk    r||j
        d         } |j
        d         |z            \  }}|sP||         }	
|xx         |	z  cc<   || |	z  dz  k    rp
t          j        xx         |z  cc<   d}|dk    r'
d|z  t          |dz            z  xx         |	z  cc<   
d|z  t          |dz            z  xx         |	z  cc<   
d|t          j        |z  z  z
           |	 k    rh
d|t          j        |z  z  z
  = |dk    r&
| t          |dz            z  xx         |	z  cc<   G
| t          |dz            z  xx         |	z  cc<   m
d|t          j        |z  z  z   xx         |	z  cc<   
|xx         |	z  cc<   t          
fd
D              S )Nr6   c                    | t           j        u r|t           j        fS t          | t                    s| j        r| j        t           j        k    r	|| j        fS |t           j        u r |  t           j                   S dS )N)sign)NN)r   Exp1Oner@   r   r   base)r   r   signlogs     r=   r   z'exptrigsimp.<locals>.f.<locals>.signlog5  s    qv~ "QU{"D#&& "4; "49;N "TX~% "wuAE62222!zr?   rK   r   c                 &    g | ]}||         z  S r9   r9   )r:   r   newds     r=   r>   z*exptrigsimp.<locals>.f.<locals>.<listcomp>\  s!    ...AQQZ...r?   )is_Mulargs_cncrx   r
   as_powers_dictcopyr   r   r   is_Addr]   r   r!   r    )rvcommutative_partnoncommutative_partrvdeer   rT   r   r;   mr   r   fs             @@r=   r   zexptrigsimp.<locals>.f)  s   y 	I02-- #$$q( 	G1S*+,,S2E-FFF!!xxzz u 	" 	" 	" 	" 	" 	" [ 	! 	!Ax !CKK1, !F1I!'!&)A+..a FQ1!Aa< !LLLB&LLLBqy 2QqSac]+++q0++++RT$qs))^,,,1,,,,!d1619n,-!3 	!Qafai/0qy 0aRQqS		\***a/****aRQqS		\***a/****T!&!)^+,,,1,,,GGGqLGGG.......//r?   )r   r   r   r   r   r%   r&   r   )r   r   r   r   newexprr<   r   s         @r=   exptrigsimpr     s     65555555, , , h''G30 30 30 30 30h ##G {{%&& }W%%1!DDGG**{{())  $w-- KKNN 488A;; Kr?   T)firstc                j   | }|r" | j         t           s| S  t                      j        d  | j        t           D              }t          |          dk    rddlm}  ||           }|j        r ||d          p|}t          |t                    rOd} |                                D ]5\  }}|}	t          |          }dd<   t          |fi }
|
|k    r|	}
| |
z  } 6| }nF|j        r?|D ]:}|                     |          \  }}|rdd<   |t          |fi z   } | j        s n;| }                    d	d          }                    d
d          }                    dd          }d d fdfdd|         }|rgt#          |           \  }} ||d         |          }t%          |          D ]0}|                    |d         |d                   } |||          }1|}n || |          }                    dd          r%t+          |          }||k    rt-          d|           |S )aC  
    Reduces expression by using known trig identities.

    Notes
    =====

    deep:
    - Apply trigsimp inside all objects with arguments

    recursive:
    - Use common subexpression elimination (cse()) and apply
    trigsimp recursively (this is quite expensive if the
    expression is large)

    method:
    - Determine the method to use. Valid choices are 'matching' (default),
    'groebner', 'combined', 'fu' and 'futrig'. If 'matching', simplify the
    expression recursively by pattern matching. If 'groebner', apply an
    experimental groebner basis algorithm. In this case further options
    are forwarded to ``trigsimp_groebner``, please refer to its docstring.
    If 'combined', first run the groebner basis algorithm with small
    default parameters, then run the 'matching' algorithm. 'fu' runs the
    collection of trigonometric transformations described by Fu, et al.
    (see the `fu` docstring) while `futrig` runs a subset of Fu-transforms
    that mimic the behavior of `trigsimp`.

    compare:
    - show input and output from `trigsimp` and `futrig` when different,
    but returns the `trigsimp` value.

    Examples
    ========

    >>> from sympy import trigsimp, sin, cos, log, cot
    >>> from sympy.abc import x
    >>> e = 2*sin(x)**2 + 2*cos(x)**2
    >>> trigsimp(e, old=True)
    2
    >>> trigsimp(log(e), old=True)
    log(2*sin(x)**2 + 2*cos(x)**2)
    >>> trigsimp(log(e), deep=True, old=True)
    log(2)

    Using `method="groebner"` (or `"combined"`) can sometimes lead to a lot
    more simplification:

    >>> e = (-sin(x) + 1)/cos(x) + cos(x)/(-sin(x) + 1)
    >>> trigsimp(e, old=True)
    (1 - sin(x))/cos(x) + cos(x)/(1 - sin(x))
    >>> trigsimp(e, method="groebner", old=True)
    2/cos(x)

    >>> trigsimp(1/cot(x)**2, compare=True, old=True)
          futrig: tan(x)**2
    cot(x)**(-2)

    c                     g | ]	}|j         
S r9   )free_symbols)r:   rV   s     r=   r>   z trigsimp_old.<locals>.<listcomp>  s     M M MA M M Mr?   r6   r   )separatevarsT)dictFr   r   r   r   r   c                 F    fd|r |           } t          | fi S )Nc                     | j         r| S fd| j        D             }| j        s| j        rfd|D             } | j        | S )Nc                 &    g | ]} |          S r9   r9   r   s     r=   r>   zHtrigsimp_old.<locals>.groebnersimp.<locals>.traverse.<locals>.<listcomp>  r   r?   c                 *    g | ]}t          |fi S r9   r   r   s     r=   r>   zHtrigsimp_old.<locals>.groebnersimp.<locals>.traverse.<locals>.<listcomp>  r   r?   r   r   s     r=   r   z4trigsimp_old.<locals>.groebnersimp.<locals>.traverse  r   r?   r   )r   r   r   r   s     `@r=   r   z"trigsimp_old.<locals>.groebnersimp  sO    	! 	! 	! 	! 	! 	!  	"B ,,t,,,r?   c                 "    t          | |          S rk   	_trigsimp)r;   ds     r=   r   ztrigsimp_old.<locals>.<lambda>  s    )Aq// r?   c                      | |fi S rk   r9   )r;   r
  r   r   s     r=   r   ztrigsimp_old.<locals>.<lambda>  s    ,,q!"<"<t"<"< r?   c           	      L    t           | |ddt          g          |          S r   )r	  r"   )r;   r
  r   s     r=   r   ztrigsimp_old.<locals>.<lambda>  s7    )LL'(T!S-K -K -K#$#& #& r?   )r   r-   r   comparez	futrig:)r   r   rP   unionatomsrx   sympy.simplify.simplifyr  r   r@   r  rs   r   r   r   as_independentr   r.   reversedrN   getr   print)r   r   r   r   trigsymsr  r
  r   rh   wasvnewrU   r   r<   r   r   r   r   wr`   subresultr   r   s     `                    @r=   r   r   m  s   t C  tx  	K355; M MV9L M M MNx==1 	<<<<<<T""Ax 4 L...3!!T"" GGII ! !DAqC"1A$)DM#A....Dqy #"DLDD8 % & &#221551 &,1DM#$x':':T':':#:D#'; & %Ce,,I88FE""DXXh
++F
- 
- 
- 21<<<<<& & & &  L  	*4yy1L1t$$A;; 	& 	&Cs1vs1v&&AQ%%AAdD))xx	5!! "3KK; 	"+q!!!Mr?   c                     | j         |j         k    og|                     t                    r|                    t                    p3|                     t                    o|                    t                    S )zHelper to tell whether ``a`` and ``b`` have the same sorts
    of symbols in them -- no need to test hyperbolic patterns against
    expressions that have no hyperbolics in them.)r\   r   r&   r%   )r   r   s     r=   _dotrigr    sf     6QV A	#$$E/D)E)E 	@	 !!?aee,>&?&?Ar?   Nc                  L   t          dt                    \  } }}t          dd          }| t          |          |z  z  t          |          |z  z  | t	          |          |z  z  t          |          t          |          f| t	          |          |z  z  t          |          |z  z  | t          |          |z  z  t          |          t          |          f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          |          t          |          f| t	          |          |z  z  t          |          |z  z  | t          |          |z  z  t          |          t          |          f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          |          t          |          f| t          |          |z  z  t	          |          |z  z  | t          |          t          |          f| t          |          dz   |z  z  t          |          dz
  |z  z  | t          |          dz   |z  z  t          |          dz   t          |          dz
  f| t          |          dz   |z  z  t          |          dz
  |z  z  | t          |          dz   |z  z  t          |          dz   t          |          dz
  f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          j
        t          j
        f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          j
        t          j
        f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          j
        t          j
        f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          j
        t          j
        f| t          |          |z  z  t          |          |z  z  | t          |          |z  z  t          j
        t          j
        f| t          |          |z  z  t          |          |z  z  | t          j
        t          j
        f|t          |           t          |          z   z  dt          |           t          |          z  z   z  t          | |z             |z  t          j
        t          j
        ff}|t          |           z  t          |          z  |t          |           z  t          |          z  z   |z   t          | |z             |z  |z   f|t          |           z  t          |          z  |t          |           z  t          |          z  z
  |z   t          | |z             |z  |z   f|t          |           z  t          |          z  |t          |           z  t          |          z  z
  |z   t          | |z
            |z  |z   f|t          |           z  t          |          z  |t          |           z  t          |          z  z   |z   t          | |z
            |z  |z   f|t          |           z  t          |          z  |t          |          z  t          |           z  z   |z   t          | |z             |z  |z   f|t          |           z  t          |          z  |t          |           z  t          |          z  z   |z   t          | |z             |z  |z   ff}| t          |          dz  z  | | t          |          dz  z  z
  f| t	          |          dz  z  | dt          |          z  dz  z  | z
  f| t          |          dz  z  | dt          |          z  dz  z  | z
  f| t          ||z             z  | t          |          t          |          z  t          |          t          |          z  z   z  f| t          ||z             z  | t          |          t          |          z  t          |          t          |          z  z
  z  f| t	          ||z             z  | t	          |          t	          |          z   dt	          |          t	          |          z  z
  z  z  f| t          |          dz  z  | t          |          dz  z  | z
  f| t          |          dz  z  | | dt          |          z  dz  z  z
  f| t          |          dz  z  | | dt          |          z  dz  z  z   f| t          ||z             z  | t          |          t          |          z  t          |          t          |          z  z   z  f| t          ||z             z  | t          |          t          |          z  t          |          t          |          z  z   z  f| t          ||z             z  | t          |          t          |          z   dt          |          t          |          z  z   z  z  ff}| | t          |          dz  z  z
  |z   | t          |          dz  z  |z   t          f| | dt          |          z  dz  z  z
  |z   |  t	          |          dz  z  |z   t          f| | dt          |          z  dz  z  z
  |z   |  t          |          dz  z  |z   t          f| | t          |          dz  z  z
  |z   |  t          |          dz  z  |z   t          f| | dt          |          z  dz  z  z
  |z   | t          |          dz  z  |z   t          f| | dt          |          z  dz  z  z   |z   | t          |          dz  z  |z   t          f| |z  | |z  t          |          dz  z  z
  |z   | |z  t          |          dz  z  |z   t          f| |z  | |z  dt          |          z  dz  z  z
  |z   |  |z  t	          |          dz  z  |z   t          f| |z  | |z  dt          |          z  dz  z  z
  |z   |  |z  t          |          dz  z  |z   t          f| |z  | |z  t          |          dz  z  z
  |z   |  |z  t          |          dz  z  |z   t          f| |z  | |z  dt          |          z  dz  z  z
  |z   | |z  t          |          dz  z  |z   t          f| |z  | |z  dt          |          z  dz  z  z   |z   | |z  t          |          dz  z  |z   t          ff}| |||||||fat          S )Nza b crp   r
  F)commutativer6   rK   )r   r   r   r   r"   r#   r!   r   r    r   r   r$   _trigpat)r   r   rT   r
  matchers_divisionmatchers_addmatchers_identity	artifactss           r=   	_trigpatsr$    s   g4(((GAq!Se$$$A 
3q6619SVVQY	#a&&!)SVVSVV<	
3q6619SVVQY	#a&&!)SVVSVV<	
3q6619SVVQY	#a&&!)SVVSVV<	
3q6619SVVQY	#a&&!)SVVSVV<	
3q6619SVVQY	#a&&!)SVVSVV<	
3q6619SVVQY	3q663q662	
CFFQJ?	CFFQJ?	*A	zAos1vvz3q66A:	7	
CFFQJ?	CFFQJ?	*A	zAos1vvz3q66A:	7 
477A:d1ggqj	 !DGGQJ,qu=	
477A:d1ggqj	 !DGGQJ,qu=	
477A:d1ggqj	 !DGGQJ,qu=	
477A:d1ggqj	 !DGGQJ,qu=	
477A:d1ggqj	 !DGGQJ,qu=	
477A:d1ggqj	 !QUAE2	
DGGd1gg	DGGDGGO 3	4QKKM15!%	)'0 
3q66#a&&1SVV8CFF?	*Q	.AE

1q0@A	
3q66#a&&1SVV8CFF?	*Q	.AE

1q0@A	
3q66#a&&1SVV8CFF?	*Q	.AE

1q0@A	
3q66#a&&1SVV8CFF?	*Q	.AE

1q0@A	
477477	QtAwwYtAww.	.	2DQKKMA4EF	
477477	QtAwwYtAww.	.	2DQKKMA4EFL 
3q6619a!CFFAI+o&	
3q6619a3q66Ao)*	
3q6619a3q66Ao)*	
3q1u::q#a&&Q-#a&&Q-789	
3q1u::q#a&&Q-#a&&Q-789	
3q1u::q3q66CFF?QQA->?@A	
477A:qa!|a'(	
477A:q1aQi!^++,	
477A:q1aQi!^++,	
4A;;477477?T!WWT!WW_<=>	
4A;;477477?T!WWT!WW_<=>	
4A;;DGGd1gg-DGGDGGO0CDEF( 
Qs1vvqy[1	aA	kAos3	
Q#a&&1}_	q	 1"SVVQY,"2C8	
Q#a&&1}_	q	 1"SVVQY,"2C8	
QtAwwz\	A	r$q''1*}q0$7	
Q$q''	A~		!1T!WWaZ<!#3T:	
Q$q''	A~		!1T!WWaZ<!#3T: 
1qs3q6619}	q	 !A#c!ffai-!"3S9	
1qsAc!ffHq= 	 1	$qbd3q6619nq&8#>	
1qsAc!ffHq= 	 1	$qbd3q6619nq&8#>	
1qs477A:~		!A2a4Q
?Q#6=	
1qsAd1ggI>!	!A	%qs477A:~'94@	
1qsAd1ggI>!	!A	%qs477A:~'94@!I& 1a-|9&HOr?   c                    t          t                    }t          t                    }g }| j        D ]}	|	j        s|	j        ||fv rs|	                                \  }
}|
j        s|j        rN|
j        |k    r||
j        d         xx         |z  cc<   `|
j        |k    r||
j        d         xx         |z  cc<   |                    |	           t          |          t          |          z  }d}|r|
                                }|
                    |          }|
                    |          }| ||          k    r-|                     ||           ||          z             d}n
|||<   |||<   ||s| S |r:|                                \  }}|                     ||          |z             |:|r:|                                \  }}|                     ||          |z             |:t          | S )zHelper for _match_div_rewrite.

    Replace f(b_)**c_*g(b_)**(rexp(c_)) with h(b)**rexph(c) if f(b_)
    and g(b_) are both positive or if c_ is an integer.
    r   FT)r   intr]   r   r\   as_base_expis_positive
is_integerrA   rP   r   popitemr
   )r   r   r`   rexphrexphfargsgargsr]   r;   r   r<   commonhitr   feges                    r=   _replace_mul_fpowxgpowr4  S  s6    EEDY 
 
8 	qv!Q' 	==??DAq}  6Q; !&)$$$)$$$Vq[ !&)$$$)$$$AZZ#e**$F
C
 	jjllYYs^^YYs^^b> 	KK#b		)***CCE#JE#J  	  
 QAAcFFAI    QAAcFFAI   :r?   c                     | S rk   r9   r   s    r=   r   r     s     r?   c                     |  S rk   r9   r   s    r=   r   r     s    1" r?   c                     t           j        S rk   )r   r   r   s    r=   r   r     s     r?   c                    |dk    r/t          | t          t          t          t          t
                    } nD|dk    r/t          | t          t          t
          t          t
                    } n|dk    r/t          | t          t          t
          t          t
                    } n|dk    r/t          | t          t          t          t          t                    } n|dk    r/t          | t          t          t          t          t                    } np|dk    r/t          | t          t          t
          t          t
                    } n;|dk    r/t          | t          t          t          t          t
                    } n|dk    r.t          | t          t          t
          t          t
                    } n|d	k    r.t          | t          t          t
          t          t
                    } n|d
k    r.t          | t          t          t          t          t                    } nj|dk    r.t          | t          t          t          t          t                    } n6|dk    r.t          | t          t          t
          t          t
                    } ndS | S )zhelper for __trigsimpr   r6   rK               	   
            N)r4  r   r   _midnr"   _idnr#   _oner!   r   r    r$   )r   is     r=   _match_div_rewriterF    s   Av &%dC3 	
a #%dC#t 	
a  %dC#t 	
a %dC3 	
a %dC3 	
a %dC$  
a %dD$4 	
a %dD$$ 	
b %dD$$ 	
b 
%dD$4   	
b %dD$4   	
b %dD$$  tKr?   c                 D     | j         t           rt          | |          S | S rk   )r   r   
__trigsimp)r   r   s     r=   r	  r	    s*     tx &$%%%Kr?   c                    ddl m} t          t                       t          \  }}}}}}| j        rm| j        sP|                                 \  }	}
t          t          j	        |	                    t          j	        |
          z  } nt          |          D ]\  }\  }}}}t          | |          st          | |          }||| k    r|}  nό9|                     |          r                    |d          r|         j        s:|                              }|j        s|                              }|j        st%          fd                             t(          t*                    D                       r|                              }  n| j        rg }| j        D ]}|j        s@|                                \  }	}
t          j	        |
          }
t          j	        |	          }nt0          j        }
t          |          }|D ]3\  }}|                    |          |                              } n4|                    ||
z             || j        k    r-t7          | } t9          | t;          |           t<                    } | j        r|D ]\  }}t          | |          s ||           } |                     t*                    rx|                     |          Iv rEv rAt%          fd|                             t(          t*                    D                       r|                              }  n|D ]$\  }}}t          | |          stA          d|g          }|                    |          }|                    |          }|                     |          }d}|r|| k    r| }||         dk    s+||          ||         j        v s||         ||         z   dk    rnu||v r||         ||         z  ||         z   dk    rnR|                    |          } |                     |          }|!                    |t0          j"                   |r|| k    &n2| j        s| j#        s	r"| j        r | j$        fd	| j        D              } 	  | j        tJ           stL          |                     tN                    }| (                    tN          
          }||k    rtL          tS          |          }||k    r+tU          |tS          |          gt<                    d         }|                    tN                    |z
  s|} n# tL          $ r Y nw xY w| S )zrecursive helper for trigsimpr   )TR10iNc              3   F   K   | ]}|j         d                   k    V  dS r   Nr]   )r:   r  r   r   s     r=   rm   z__trigsimp.<locals>.<genexpr>  sJ       H H116!9A. H H H H H Hr?   )r   c              3   R   K   | ]!}|j         d                            fv V  "dS rL  rM  )r:   r  r   r   r   s     r=   rm   z__trigsimp.<locals>.<genexpr>  s\       IH IH:;q	c!fc!f%55IH IH IH IH IH IHr?   r   )r   c                 0    g | ]}t          |          S r9   r  )r:   r   r   s     r=   r>   z__trigsimp.<locals>.<listcomp>$  s#    AAA!9Q--AAAr?   )r   )+r   rJ  r  r$  r   is_commutativer   r	  r
   
_from_args	enumerater  rF  matchr  r)  rN   r(  ru   r  r&   r%   r   r]   r   r   rA   r   r   r   r   r   r   rr   Zeror   r\   r   	TypeErrorr   r   r(   sorted)r   r   rJ  rT   r
  r   r!  r"  r#  comncrE  patternsimpok1ok2r   okr]   termr  r   a_tr   r  r<   r   fnewr   r   r   s    `                          @@@r=   rH  rH    s    (''''' #+!Aq!Q!<y{ #" !	mmooGCS^C00$77r8J8JJDD09:K0L0L  ,,GT3tW-- ,T155 !$ !&  jj)) 3771a== q6, % XXc]]!~ %$ XXc]]!~ %$  H H H H HA13E9G 9G H H H H H ! 99S>>DE{ >CI 	! 	!D& --//R^B''~c**UT4((D#4  jj)) !;;s++DE KKR    49 	::DtVD\\y999D; 	#/  tW-- uT{{88.// 
**W--C  !18 !S !S IH IH IH IH IH IH?B1v||13E@G @GIH IH IH FH FH ! !!;;s++DE
 $- 	( 	(GVR4))  sRD)))Cll1c**G[[C((F

7##AC 	(t 	(S6Q; 31Q49,01#10B6 afQqTkAaD0A5 {{1~~JJw''Q'''  	(t 	(	(. 
 C Ct C	 CtyAAAAtyAAABtx  	OJJsOOll3Tl**!8 	Oc{{3; 	?#vc{{+;;;A>C		#" 	D    Ks   ;B8U4 4
V V)hyperc                   ddl m} t          |           } t          | t                    s| S | j        s| S | }t          | t                    } |rF|                     t                    r, ||           \  } } |t          | t                              } | |k    r4| j
        r-| j        d         j        rt          |                                  } | S )a  Return simplified ``e`` using Fu-like transformations.
    This is not the "Fu" algorithm. This is called by default
    from ``trigsimp``. By default, hyperbolics subexpressions
    will be simplified, but this can be disabled by setting
    ``hyper=False``.

    Examples
    ========

    >>> from sympy import trigsimp, tan, sinh, tanh
    >>> from sympy.simplify.trigsimp import futrig
    >>> from sympy.abc import x
    >>> trigsimp(1/tan(x)**2)
    tan(x)**(-2)

    >>> futrig(sinh(x)/tanh(x))
    cosh(x)

    r   )r   )r   r   r   r@   r   r]   r   _futrigr   r%   r   is_Rationalr
   as_coeff_Mul)r<   ra  kwargsr   r   r   s         r=   r   r   :  s    ( 0/////

Aa 6 
C!WA %)** %}Q1Ai7##$$Cx #AH #!6 #!!"Hr?   c           !      T   ddl m}mm}mm}mm}m}m	m
mm}mm}m}m}	m}
mm |                     t*                    s| S | j        r|                     t*                    \  }} nd}fd}d t0          ||fdt0          fdgfd|
|||	fd	|
t0          fd
g||t0          |gt0          fdgfdfdgfdfdg|t0          gt0          fdg|t0          fdgfg} t3          ||          |           } ||| z  } | S )zHelper for futrig.r   )TR1TR2TR3r   TR10LrJ  TR8TR6TR15TR16TR111TR5TRmorrieTR11_TR11TR14TR22TR12Nc                      |           |                                  t          |           t          | j                  | j        fS rk   )r   r   rx   r]   r   )r;   rl  s    r=   r   z_futrig.<locals>.<lambda>t  s2    aaddAKKMM6!99c!&kk18L r?   c                 6    |                      t                    S rk   )r   r&   r   s    r=   r   z_futrig.<locals>.<lambda>u  s    aee122 r?   c                 0    t          t          |           S rk   _eapplyr(   r;   trigss    r=   r   z_futrig.<locals>.<lambda>|      '&!U++ r?   c                 0    t          t          |           S rk   r}  r   r~  s    r=   r   z_futrig.<locals>.<lambda>~      WXq%88 r?   c                 (    t          d |           S )Nc                 D    t          |                                           S rk   )r(   normal)rE  s    r=   r   z+_futrig.<locals>.<lambda>.<locals>.<lambda>  s    F188::$6$6 r?   r}  r~  s    r=   r   z_futrig.<locals>.<lambda>  s    '665AA r?   c                 0    t          t          |           S rk   r|  r~  s    r=   r   z_futrig.<locals>.<lambda>  r  r?   c                 0    t          t          |           S rk   r  r~  s    r=   r   z_futrig.<locals>.<lambda>  r  r?   c                 ,      |                     S rk   r9   )r;   ri  r   s    r=   r   z_futrig.<locals>.<lambda>  s    TT##a&&\\ r?   c                 B    t          t           |                     S rk   r}  r   )r;   rr  r  s    r=   r   z_futrig.<locals>.<lambda>  s    gj##a&&%88 r?   c                 B    t          t           |                     S rk   r  )r;   ro  r  s    r=   r   z_futrig.<locals>.<lambda>  s     gDDGGU, , r?   c                 B    t          t           |                     S rk   r  )r;   rn  r  s    r=   r   z_futrig.<locals>.<lambda>  s    wz33q66599 r?   c                 B    t          t           |                     S rk   r  )r;   rp  r  s    r=   r   z_futrig.<locals>.<lambda>  s     wDDGGU, , r?   c                 B    t          t           |                     S rk   r  )r;   rw  r  s    r=   r   z_futrig.<locals>.<lambda>  s     WQ( ( r?   c                 B    t          t           |                     S rk   )r}  r	   )r;   rx  r  s    r=   r   z_futrig.<locals>.<lambda>  s     W$$q''5* * r?   )	objective)r   rh  ri  rj  r   rk  rl  rJ  rm  rn  ro  rp  rq  rr  rs  rt  ru  rv  rw  rx  r   r&   r   r  r/   r0   )r<   rh  rj  rk  rJ  rm  rq  rs  rt  ru  rv  rS   Lopstreerl  rx  ro  rp  ri  rw  r   rr  rn  r  s                 @@@@@@@@@@r=   rc  rc  e  s                                            
 55&'' x ##$9::qqLLLLD22E++++	88889AAAAeS++++	88889	3	)))))*88888, , , , ,	-
 :9999, , , , ,	- 		4	 ( ( ( ( ( 	)S$	 * * * * * 	+C#	
$DJ 	%tt$$$Q''A AIHr?   c                     t          | t                    rt          | j                  S t          | t                    sdS t          d | j        D                       S )zD_eapply helper to tell whether ``e`` and all its args
    are Exprs.Fc              3   4   K   | ]}t          |          V  d S rk   )_is_Expr)r:   rE  s     r=   rm   z_is_Expr.<locals>.<genexpr>  s(      ++qx{{++++++r?   )r@   r   r  r   r   allr]   )r<   s    r=   r  r    s\     !Z    a u++AF++++++r?   c                      t          |t                    s|S t          |          s|j        s  |          S  |j         fd|j        D              S )zdApply ``func`` to ``e`` if all args are Exprs else only
    apply it to those args that *are* Exprs.c                 N    g | ]!} |          rt          |          n|"S rk   r  )r:   eicondr\   s     r=   r>   z_eapply.<locals>.<listcomp>  sJ        #?dd2hh?bR  r?   )r@   r   r  r]   r\   )r\   r<   r  s   ` `r=   r}  r}    s     a {{ !& tAww16     &    r?   )Frk   )Xcollectionsr   	functoolsr   
sympy.corer   r   r   r   r	   r
   r   r   sympy.core.cacher   sympy.core.functionr   r   r   r   r   r   sympy.core.numbersr   r   r   sympy.core.sortingr   sympy.core.symbolr   r   r   sympy.external.gmpyr   sympy.functionsr   r   r   r   r    r!   r"   r#   r$   %sympy.functions.elementary.hyperbolicr%   (sympy.functions.elementary.trigonometricr&   sympy.polysr'   r(   r)   r*   sympy.polys.domainsr+   sympy.polys.polyerrorsr,   sympy.polys.polytoolsr-   sympy.simplify.cse_mainr.   sympy.strategies.corer/   sympy.strategies.treer0   sympy.utilities.iterablesr1   sympy.utilities.miscr2   r   r   r   r   r   r  r  r$  r4  rC  rB  rD  rF  r	  rH  r   rc  r  r}  r9   r?   r=   <module>r     s   # # # # # #      - - - - - - - - - - - - - - - - - - - - $ $ $ $ $ $9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 / / / / / / / / / / % % % % % % 2 2 2 2 2 2 2 2 2 2 * * * * * * K K K K K K K K K K K K K K K K K K K K K K D D D D D D J J J J J J E E E E E E E E E E E E " " " " " " 5 5 5 5 5 5 * * * * * * ' ' ' ' ' ' * * * * * * ( ( ( ( ( ( . . . . . . & & & & & & #%E!&LF LF LF LF^  !3	4^ ^ ^B[ [ [~ !% E E E E EPA A A Q Q Qh) ) )X {) ) )X    	~ ~ ~ 	~D  ( ( ( ( (V< < <~, , ,	 	 	 	 	 	r?   