
    MZd                        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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l0m1Z1 d dl2m3Z3m4Z4m5Z5m6Z6 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 d dlEmFZF g dddfdZGe1e/fZHd ZId/dZJd ZKdddZLd  ZMd!aNd" ZOd# ZPd$ ZQd% ZRd& ZSd' ZTd/d(ZUed/d)       ZVdd*d+ZWd, ZXd- ZYd0d.ZZy!)1    )defaultdict)reduce)sympifyBasicSExprfactor_termsMulAdd	bottom_up)cacheit)	count_ops_mexpandFunctionClassexpand
expand_mul_coeff_isneg
Derivative)IIntegerigcd)_nodes)DummysymbolsWild)
SYMPY_INTS)	sincosexpcoshtanhsinhtancotcoth)atan2)HyperbolicFunction)TrigonometricFunction)Polyfactorcancelparallel_poly_from_expr)ZZ)PolificationFailed)groebner)cse)identity)greedy)iterable)debugFgrlexc                    d d fd}t        d      | j                  t        j                        } t        j                  fg}t	        |       j                         \  }}	 t        ||g      \  \  }	}
}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%                  |
j                         r t'        |||z         j(                  | }g }|j+                         D ]}  \  }}t#        t        ||g      d   j                        }d}|rd}|D ]  }t'        |      }|j-                  |j                        r* |j                   t#        |j                        j/                  |       r\d}|j1                  |j3                         j                          |r|D cg c]	  }||v s| }}|j4                  D cg c]:  } |j                   |j%                  |j                         r|j7                         < }}|j9                  t;        t=        ||      D cg c]
  \  }}||z   c}}  |||z  ||||t        |      j                  |      z          t?        | S  || t        |      |||z   |t        |      j                  |      S # t        $ r | cY S w xY wc c}w c c}w c c}}w )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|j	                  |       >t        |      rq|j	                  |d   |dd f       |j                  t        |dd D cg c]  } |d   |       c} |d   t        |dd        gz         d   j                         |j	                  |        ||||fS c c}w )z-Split hints into (n, funcs, iterables, gens).   r   N)

isinstancer   r   r   appendr3   extendr,   r   gens)hintsnfuncs	iterablesr<   exs          9/usr/lib/python3/dist-packages/sympy/simplify/trigsimp.pyparse_hintsz&trigsimp_groebner.<locals>.parse_hints   s    !#R$y 	A!j'23A}-Q!  !A$!"/ 3&'e,TQqT!W,!S!AB%[0A/BBDDEGGKtM A	 %D(( -s   C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(  r4 |j                   ||| z         ||| z        z   ||| z        z
         _|||fv sf |||z        j                  d      j                  ||       }
 |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   r8   Ttrig)r   r   r   r#   r    r"   r!   r:   r   subslistset)rB   termsr   rF   fncoeffcstrelcns              rC   build_idealz&trigsimp_groebner.<locals>.build_ideal   s@    #J 
	/IB#sCFAIA	$9A$=>4tAwzDGQJ'>'BC!E 	/1a A:"A,AHHSM1WAHHQuQwZ%'
2QuQwZ?@Aq6\E!G+++6;;AqABAHHRa[2-.	/
	/ CF|    c                 j	     #|      \  }}}}t        d|||f       t        |       } | j                  |       t        t        |            }t        t        |            }t        t        |             } t        t
        t        t        t        t        h}| D cg c]9  }|j                  |v r)|j                  d   j                         |j                  f; }}| D cg c]  }|j                  |vs| }	}g }
i }|D ]+  \  \  }}}|j                  |g       j                  ||f       - g }|j                         D ]7  \  }}|D cg c]  }|d   	 }}|D cg c]  }|d   	 }}t!        t"        |      }t%        ||      D cg c]  \  }}|||z  f }}}t        ||z          t
        t        t        gt        t        t        gfD ]@  \  }}}t'         fd|||fD              s j)                  |        j)                  |       B  D ])  }t+        d|dz         D ]  }|j                  ||f        + g }|D ]  \  }}|t        k(  r.|j                  t        |f       |j                  t
        |f       |t        t
        fv rt         v r|j                  t        |f       |t        k(  r.|j                  t        |f       |j                  t        |f       |t        t        fv st         v s|j                  t        |f        |j                  |       |t-        | z  } !||      }|j                  |       |
j                  |D ch c]  \  }} |||z         c}}       : |D ]  \  }}|t        k(  r |j                  t        |ft
        |fg       /|t        k(  r |j                  t        |ft        |fg       Xt/        dt1        |      z  t2              } |t5        |       j7                  d      j9                  t        t%        ||                  }|j                   |t5        |       |z
          "| v r9|j                  "d	z  dz          |	j;                  "       |
j                  "       ||	|
fS c c}w c c}w c c}w c c}w c c}}w c c}}w )
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.
        z1n=%s   funcs: %s   iterables: %s    extragens: %sr   r8   c              3   &   K   | ]  }|v  
 y wN ).0rB   fss     rC   	<genexpr>z:trigsimp_groebner.<locals>.analyse_gens.<locals>.<genexpr>5  s     21qBw2s   zd:%iclsTrH   rG   )r4   rK   r;   rL   r   r   r#   r"   r    r!   funcargsas_coeff_mul
setdefaultr:   itemsr   r   zipanyaddranger
   r   lenr   r   r   rJ   remove)$r<   r=   r>   r?   r@   	extragensallfuncsg	trigtermsfreegensnewgenstrigdictrO   varrN   reskeyvalrB   fnsgcdvrM   rP   rQ   rR   kextrarra   dummysexprr\   rU   myIrD   s$                                   @rC   analyse_gensz'trigsimp_groebner.<locals>.analyse_gens   sZ    *5U);&5)YAi+	- DzI SZ Y(	CI c4t4AE ,A(* ffQi,,.7 ,	 ,  $>!qvvX'=A>> ) 	=LUC"R(//<	= ( -	9HC" "%%A1Q4%C%!$%A1Q4%C%s#C03C>Wb!b!C%[>E>US[!B #sOdD$-?@ 1a2Aq	22FF1IFF1I  *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JJqMNN7ABqsG78[-	9` " 
	2HBSy  3+T{!;<t  4,t!=> #d)!3?3<(//T/:??SQUEV@WX

2c4j>D01
	2 $;JJsAvz"OOC NN3Hg%%Y, ?0 &%>2 8s*   >RR3R
RR$	R)R/r   zinitial gens:zideal:z	new gens:z -- lenz
free gens:)orderr<   domainzgroebner basis:r   )ratsimpmodprime)r<   r8   TF)r   r<   quickr   
polynomial) r   rJ   r   ImaginaryUnitr+   as_numer_denomr,   r.   r4   r<   ri   r/   r-   rK   sympy.simplify.ratsimpr   has_only_gensrL   intersectionr)   ejectrM   
issuperset
differenceupdateexcludepolysas_exprr:   r
   re   r   ) r}   r=   r   r   r   r   rJ   numdenompnumpdenomoptidealro   r<   Gr   rs   monomrO   ourgenschangedprB   realgensrm   ourGabrU   r~   rD   s                                 @@@rC   trigsimp_groebnerr      s   f)(0e&N *C99Q__c*D!//"#D,,.JC5sElCv 
/388$(59E8T	(E	+tYD	2	,)SY7 e$r:A	
T!WiQ8
 7(F((#d)*@*@*MN1d3T(]+1148IIK 	JLE515%.A!DIIJG G 9AQA"--aff5*1??CK,B,B7,KL"&qyy{'7'789  $(8a1<8H8 *+ DA#AOOW%9%9!&&%AB IIK DD DJJsc(E.BCdaQTCD&uU{D,4E"2<>>Bd4jI J5	J< Cy $q'Xd]z;;?4:	F{  H 9DCs*   ,L> 		MM'?MM>MMc                 0    d fdt        |       S )Nc                 `    	 | j                   d   |j                   d   k(  S # t        $ r Y yw xY w)Nr   F)ra   
IndexError)rB   rF   s     rC   
check_argsz%_trigsimp_inverse.<locals>.check_args  s4    	66!9q	)) 		s   ! 	--c                    t        | dd       }|Ut        | j                  d    |             r7t          |       d      t              r| j                  d   j                  d   S t        | t              r| j                  \  }}t        |      r t	        | |             S t        |      r$t        j                   t	        ||             z
  S  ||      rrt        |t              rt        |t              r|j                  d   S t        |t              r3t        |t              r#t        j                  dz  |j                  d   z
  S | S )Ninverser   r8   rG   )
getattrr9   ra   r(   r&   r   r   Pir   r   )rvrm   rF   rB   r   fs       rC   r   z_trigsimp_inverse.<locals>.f  s   B	4(MjQS9313q6#89771:??1%% b% 77DAqA%A,''attaa!o--!Qa%*Q*<66!9$a%*Q*<44!8affQi//	rV   )r   )r   r   r   s    @@rC   _trigsimp_inverser     s    . RrV   c                 Z  	 ddl m t        |       } t        | dd      }| |di S j	                  dd      }|s7j	                  dd       j	                  dd       j	                  d	d
      }nd}d 	fdd 	fd	fdfdd|   } ||       }|rt        |      }|S )a6  Returns a reduced expression by using known trig identities.

    Parameters
    ==========

    inverse : bool, optional
        If ``inverse=True``, it will be assumed that a composition of inverse
        functions, such as sin and asin, can be cancelled in any order.
        For example, ``asin(sin(x))`` will yield ``x`` without checking whether
        x belongs to the set where this relation is true. The default is False.
        Default : True

    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 simplification 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                 ^    fd |       }t        |t              s|S t        |fi S )Nc                     | j                   r| S | j                  D cg c]
  } |       }}| j                  s| j                  r|D cg c]  }t	        |fi  }} | j
                  | S c c}w c c}w rY   is_Atomra   is_Functionis_Powr   r`   rA   rB   ra   optstraverses      rC   r   z0trigsimp.<locals>.groebnersimp.<locals>.traverse  m    yy)*0AHQK0D0}}>BC)!4t4CC1664=  1C   A/A4)r9   r   r   )exr   newr   s    ` @rC   groebnersimpztrigsimp.<locals>.groebnersimp  s2    	! rl#t$J ---rV   c                      | fi S rY   rZ   )rB   r   r   s    rC   <lambda>ztrigsimp.<locals>.<lambda>,  s    A rV   c                     t        |       S rY   )futrigrB   s    rC   r   ztrigsimp.<locals>.<lambda>-  s
    vay rV   c                      | fi S rY   rZ   )rB   r   r   s    rC   r   ztrigsimp.<locals>.<lambda>.  s    |A66 rV   c                 8    t         | ddt        g            S NTrG   )r   r=   )r   r#   )rB   r   s    rC   r   ztrigsimp.<locals>.<lambda>/  s     vl1*.q#h'@  A rV   c                     t        | fi S rY   )trigsimp_old)rB   r   s    rC   r   ztrigsimp.<locals>.<lambda>1  s    a040 rV   )r   r   r/   combinedr   rZ   )sympy.simplify.fur   r   r   popr   )
r}   r   r   r   r   r   trigsimpfuncexpr_simplifiedr   r   s
     `     @@rC   trigsimpr     s    ~ %4=DT#3T:N!%%%
((5%
 Cd#(J/. '(6A0 L #4(O+O<rV   c                 F   ddl m}m} d }t        | |      }fdt        |      }|j	                  t
              r ||      \  }  ||            }|j	                  t              r ||      }|j	                  t              r| j	                  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$|j                  | j                  t                     |j                  | j                  t
                     t        |dt        iS )Nrt   )has_trigsr:   rewriter   r   minr   )rA   choicess     rC   exp_trigzexptrigsimp.<locals>.exp_trigL  sP     #155&>NN199S>*qyy~&G+++rV   c                 x   | j                   s| S | j                         \  }}t        |      dkD  r t        |       t        | z  S | j	                         }|j                         }t        j                  ffd	|t        j                     }|D ]  }|j                  st        |j                        dk(  s*|j                  d   } |j                  d   |z        \  }}	|	sW||   }
||xx   |
z  cc<   ||	 |
z  dz  k(  rb|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(  r]|d|t        j                  |	z  z  z
  = |dk(  r|| t        |	dz        z  xx   |
z  cc<   5|| t        |	dz        z  xx   |
z  cc<   T|d|t        j                  |	z  z  z   xx   |
z  cc<   ||xx   |
z  cc<    t        |D cg c]
  }|||   z   c} S c c}w )Nr8   c                 4   | 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 y)N)sign)NN)r   Exp1Oner9   r   r   base)r}   r   signlogs     rC   r   z'exptrigsimp.<locals>.f.<locals>.signlogb  sl    qvv~QUU{"D#&4;;499;NTXX~%uAEE622!rV   rG   r   )is_Mulargs_cncri   r
   as_powers_dictcopyr   r   r   is_Addra   r    r"   r!   )r   commutative_partnoncommutative_partrvdnewdeery   rP   r   rB   mr   r   s              @rC   r   zexptrigsimp.<locals>.fV  s%   yyI02-- #$q(S*+,S2E-FFF!xxz uu 	" [ 	!AxxCK1,FF1I!!&&)A+.aFQ1!Aa<LB&LBqyQqSac]+q0+RT$qs)^,1,!d16619n,-!3Qaffai/0qyaRQqS	\*a/*aRQqS	\*a/*T!&&!)^+,1,GqLG5	!8 .AQQZ.//.s   $H7)r   r   r   r   r   r'   r(   r   )r}   r   r   r   newexprrA   r   s         @rC   exptrigsimpr   ;  s     6, h'G30h #G {{%&W%1DG*{{()w- KKN488A;KrV   T)firstc                    | }|r0 | j                   t         s| S  t               j                   | j                  t         D cg c]  }|j
                   c} }t        |      dkD  rddlm}  ||       }|j                  r ||d      xs |}t        |t              rGd} |j                         D ]/  \  }}	|	}
t        |	      }	dd<   t        |	fi }||	k(  r|
}| |z  } 1 | }nN|j                  rB|D ];  }| j!                  |      \  }}|sdd<   |t        |fi z   } | j                  r; n | }j#                  dd      }j#                  d	d      }j#                  d
d      }d d fdfdd|   }|rNt%        |       \  }} ||d   |      }t'        |      D ]#  }|j)                  |d   |d         } |||      }% |}n	 || |      }j+                  dd      rt-        |      }||k7  rt/        d|       |S c c}w )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)

    r8   r   )separatevarsT)dictFr   r   r   r   r   c                 >    fd|r |       } t        | fi S )Nc                     | j                   r| S | j                  D cg c]
  } |       }}| j                  s| j                  r|D cg c]  }t	        |fi  }} | j
                  | S c c}w c c}w rY   r   r   s      rC   r   z4trigsimp_old.<locals>.groebnersimp.<locals>.traverse  r   r   )r   )r   r   r   r   s     `@rC   r   z"trigsimp_old.<locals>.groebnersimp  s&    	! "B ,t,,rV   c                     t        | |      S rY   )	_trigsimp)rB   ds     rC   r   ztrigsimp_old.<locals>.<lambda>  s    )Aq/ rV   c                      | |fi S rY   rZ   )rB   r   r   r   s     rC   r   ztrigsimp_old.<locals>.<lambda>	  s    ,q!"<t"< rV   c           	      <    t         | |ddt        g      |      S r   )r   r#   )rB   r   r   s     rC   r   ztrigsimp_old.<locals>.<lambda>
  s$    )L'(T!S-K#$#& rV   )r   r/   r   comparez	futrig:)r   r   rL   unionatomsfree_symbolsri   sympy.simplify.simplifyr   r   r9   r   rd   r   r   r   as_independentr   r0   reversedrJ   getr   print)r}   r   r   r   rR   trigsymsr   r   ry   rx   wasvnewrQ   r{   rA   r   r   r   r   wrm   subresultr   r   s     `                     @rC   r   r     s6   t Ctxx K35;;V9L MA MNx=1<T"Axx .3!!T"GGI !DAqC"1A$)DM#A..Dqy"DLD! 88% &#22151,1DM#$x':T':#:D#';; %& Ce,I88FE"DXXh
+F
- 2<& L 4y11t$A; 	&Cs1vs1v&AQ%A	& dD)xx	5!3K;+q!MM !Ns   Hc                     | j                   |j                   k(  xrZ | j                  t              xr |j                  t              xs, | j                  t              xr |j                  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     rC   _dotrigr  "  s^     66QVV A	#$E/D)E 	@	 !?aee,>&?ArV   Nc                     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 cr^   r   F)commutativer8   rG   )r   r   r   r   r#   r$   r"   r    r!   r   r   r%   _trigpat)r   r   rP   r   matchers_divisionmatchers_addmatchers_identity	artifactss           rC   	_trigpatsr  ,  s
   g4(GAq!Se$A 
3q619SVQY	#a&!)SVSV<	
3q619SVQY	#a&!)SVSV<	
3q619SVQY	#a&!)SVSV<	
3q619SVQY	#a&!)SVSV<	
3q619SVQY	#a&!)SVSV<	
3q619SVQY	3q63q62	
CFQJ?	CFQJ?	*A	zAos1vz3q6A:	7	
CFQJ?	CFQJ?	*A	zAos1vz3q6A:	7 
47A:d1gqj	 !DGQJ,quu=	
47A:d1gqj	 !DGQJ,quu=	
47A:d1gqj	 !DGQJ,quu=	
47A:d1gqj	 !DGQJ,quu=	
47A:d1gqj	 !DGQJ,quu=	
47A:d1gqj	 !QUUAEE2	
DGd1g	DGDGO 3	4QKM155!%%	)'0 
3q6#a&1SV8CF?	*Q	.AE
1q0@A	
3q6#a&1SV8CF?	*Q	.AE
1q0@A	
3q6#a&1SV8CF?	*Q	.AE
1q0@A	
3q6#a&1SV8CF?	*Q	.AE
1q0@A	
4747	QtAwYtAw.	.	2DQKMA4EF	
4747	QtAwYtAw.	.	2DQKMA4EFL 
3q619a!CFAI+o&	
3q619a3q6Ao)*	
3q619a3q6Ao)*	
3q1u:q#a&Q-#a&Q-789	
3q1u:q#a&Q-#a&Q-789	
3q1u:q3q6CF?QQA->?@A	
47A:qa!|a'(	
47A:q1aQi!^++,	
47A:q1aQi!^++,	
4A;4747?T!WT!W_<=>	
4A;4747?T!WT!W_<=>	
4A;DGd1g-DGDGO0CDEF( 
Qs1vqy[1	aA	kAos3	
Q#a&1}_	q	 1"SVQY,"2C8	
Q#a&1}_	q	 1"SVQY,"2C8	
QtAwz\	A	r$q'1*}q0$7	
Q$q'	A~		!1T!WaZ<!#3T:	
Q$q'	A~		!1T!WaZ<!#3T: 
1qs3q619}	q	 !A#c!fai-!"3S9	
1qsAc!fHq= 	 1	$qbd3q619nq&8#>	
1qsAc!fHq= 	 1	$qbd3q619nq&8#>	
1qs47A:~		!A2a4Q
?Q#6=	
1qsAd1gI>!	!A	%qs47A:~'94@	
1qsAd1gI>!	!A	%qs47A:~'94@!I& 1a-|9&HOrV   c                    t        t              }t        t              }g }| j                  D ]  }	|	j                  s|	j                  ||fv r|	j                         \  }
}|
j                  s|j                  rT|
j                  |k(  r||
j                  d   xx   |z  cc<   t|
j                  |k(  r||
j                  d   xx   |z  cc<   |j                  |	        t        |      t        |      z  }d}|rm|j                         }|j                  |      }|j                  |      }| ||      k(  r#|j                   ||       ||      z         d}n
|||<   |||<   |rm|s| S |r0|j                         \  }}|j                   ||      |z         |r0|r0|j                         \  }}|j                   ||      |z         |r0t        | 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   intra   r   r`   as_base_expis_positive
is_integerr:   rL   r   popitemr
   )r}   r   rm   rexphrexphfargsgargsra   rB   r   rA   commonhitrt   feges                    rC   _replace_mul_fpowxgpowr$    s    EEDYY 
88qvv!Q'==?DAq}}66Q;!&&)$)$VVq[!&&)$)$A
 Z#e*$F
C
jjlYYs^YYs^b>KK#b	)*CE#JE#J  
QAcFAI  QAcFAI  :rV   c                     | S rY   rZ   r   s    rC   r   r     s     rV   c                     |  S rY   rZ   r   s    rC   r   r     s    1" rV   c                 "    t         j                  S rY   )r   r   r   s    rC   r   r     s
     rV   c                    |dk(  r&t        | t        t        t        t        t
              } | S |dk(  r&t        | t        t        t
        t        t
              } | S |dk(  r&t        | t        t        t
        t        t
              } | S |dk(  r&t        | t        t        t        t        t              } | S |dk(  r&t        | t        t        t        t        t              } | S |dk(  r&t        | t        t        t
        t        t
              } | S |dk(  r&t        | t        t        t        t        t
              } | S |dk(  r&t        | t        t        t
        t        t
              } | S |d	k(  r&t        | t        t        t
        t        t
              } | S |d
k(  r&t        | t        t        t        t        t              } | S |dk(  r&t        | t        t        t        t        t              } | S |dk(  r&t        | t        t        t
        t        t
              } | S y)zhelper for __trigsimpr   r8   rG               	   
            N)r$  r   r   _midnr#   _idnr$   _oner"   r    r!   r%   )r}   is     rC   _match_div_rewriter6    s   Av%dC3L KI 
a%dC#tF KC 
a%dC#t@ K= 
a%dC3: K7 
a%dC34 K1 
a%dC$. K) 
a%dD$4& K# 
a%dD$$  K 
b%dD$$ K 
b%dD$4  K 
b%dD$4  K 
b%dD$$ K rV   c                 D     | j                   t         rt        | |      S | S rY   )r   r   
__trigsimp)r}   r   s     rC   r   r     s$     txx$%%KrV   c           	      4
   ddl m} t        
t                t        \  }}}}}}| j                  rG| j
                  sI| j                         \  }	}
t        t        j                  |	      |      t        j                  |
      z  } nt        |      D ]  \  }\  }}}}t        | |      st        | |      }|
|| k7  r|}  n0| j                  |      sDj                  |d      sW|   j                  s<|j!                        }|j"                  s|j!                        }|j"                  st%        fd   j'                  t(        t*              D              r|j!                        }  n | j,                  rg }| j.                  D ]  }|j
                  s>|j                         \  }	}
t        j                  |
      }
t        j                  |	      }nt0        j2                  }
t        ||      }|D ]+  \  }}|j                  |      |j!                        } n |j5                  ||
z          || j.                  k7  r#t7        | } t9        | t;        |       t<              } | j,                  r|D ]  \  }}t        | |      s ||       } | j?                  t*              s1| j                  |      9v r5v r1t%        fd|   j'                  t(        t*              D              r~|j!                        }  n |D ]  \  }}}t        | |      stA        d|g      }|j!                  |      }|j!                  |      }| j                  |      }d}|s\|| k7  sb| }||   dk(  s#||    ||   j.                  v s||   ||   z   dk(  r||v r||   ||   z  ||   z   dk(  r|j!                  |      } | j                  |      }|jC                  |t0        jD                         |s|| k7  r nW| j                  s| jF                  s|r=| j.                  r1 | jH                  | j.                  D cg c]  }t        ||       c} } 	  | j>                  tJ         stL        | j'                  tN              }| jQ                  tN        |	      }||k(  rtL        tS        |      }||k7  rtU        |tS        |      gt<              d   }|j'                  tN              |z
  s|} | S c c}w # tL        $ r Y | S w xY w)
zrecursive helper for trigsimpr   )TR10iNc              3   H   K   | ]  }|j                   d       k(    ywr   Nra   )r[   r  r   rs   s     rC   r]   z__trigsimp.<locals>.<genexpr>  s(      H1166!9A. Hs   ")rt   c              3   P   K   | ]  }|j                   d          fv   ywr<  r=  )r[   r  r   r   rs   s     rC   r]   z__trigsimp.<locals>.<genexpr>0  s4      IH:;q	c!fc!f%55IHs   #&r   )r   )r   )+r   r:  r  r  r   is_commutativer   r   r
   
_from_args	enumerater  r6  matchr  r  rJ   r  rf   r   r(   r'   r   ra   r   r   r:   r   r   r   r   r   r   rc   Zeror   r`   r   	TypeErrorr   r   r*   sorted)r}   r   r:  rP   r   r  r  r  r  comncr5  patternsimpok1ok2r   okra   termr
  r   a_tr   r  r   rA   r   fnewr   rs   s                            `   @@rC   r8  r8    s    (#+!Aq!Q!<y{{""mmoGCS^^C0$7r8JJD09:K0L ,,GT3tW-,T15&$&  jj)3771a=q6,, XXc]!~~$ XXc]!~~$  HA13E9G H H 99S>D;> {{II 	!D&&--/R^^B'~~c*UUT4(D#4 jj)?!;;s+D	
 KKR 	! 499:DtVD\y9D;;#/ tW-T{88./**W-C {18SS IH?B1v||13E@GIH FH !!;;s+D$ $- 	(GVR4) sRD)Cll1c*G[[C(F

7#ACtS6Q;31Q499,#10B6afQqTkAaD0A5{{1~JJw'Q' t	(. 
t		tyytyyA!9Q-ABtxx OJJsOll3Tl*!8Oc{3;#vc{+;A>C		#"D K% B  Ks   T*BT
 
	TT)hyperc                |   ddl m} t        |       } t        | t              s| S | j
                  s| S | }t        | t              } |r6| j                  t              r! ||       \  } } |t        | t                    } | |k7  r;| j                  r/| j
                  d   j                  rt        | j                          } | 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   r9   r   ra   r   _futrigr   r'   r   is_Rationalr
   as_coeff_Mul)rA   rP  kwargsr   r   r   s         rC   r   r   g  s    ( 0
Aa66
C!WA)*Q1i7#$CxAHH!6!6!"HrV   c           !      ,   ddl m}mm}mm}mm}m}m	m
mm}mm}m}m}	m}
mm | j)                  t*              s| S | j,                  r| j/                  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Lr:  TR8TR6TR15TR16TR111TR5TRmorrieTR11_TR11TR14TR22TR12Nc                      |       | j                         t        |       t        | j                        | j                  fS rY   )r   r   ri   ra   r   )rB   r[  s    rC   r   z_futrig.<locals>.<lambda>  s,    adAKKM6!9c!&&k188L rV   c                 ,    | j                  t              S rY   )r   r(   r   s    rC   r   z_futrig.<locals>.<lambda>  s    aee12 rV   c                 &    t        t        |       S rY   _eapplyr*   rB   trigss    rC   r   z_futrig.<locals>.<lambda>      '&!U+ rV   c                 &    t        t        |       S rY   rl  r   rm  s    rC   r   z_futrig.<locals>.<lambda>      WXq%8 rV   c                      t        d |       S )Nc                 4    t        | j                               S rY   )r*   normal)r5  s    rC   r   z+_futrig.<locals>.<lambda>.<locals>.<lambda>  s    F188:$6 rV   )rl  rm  s    rC   r   z_futrig.<locals>.<lambda>  s    '65A rV   c                 &    t        t        |       S rY   rk  rm  s    rC   r   z_futrig.<locals>.<lambda>  ro  rV   c                 &    t        t        |       S rY   rq  rm  s    rC   r   z_futrig.<locals>.<lambda>  rr  rV   c                        |             S rY   rZ   )rB   rX  r   s    rC   r   z_futrig.<locals>.<lambda>  s    T#a&\ rV   c                 2    t        t         |             S rY   rl  r   )rB   ra  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    gj#a&%8 rV   c                 2    t        t         |             S rY   rz  )rB   r^  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    gDGU, rV   c                 2    t        t         |             S rY   rz  )rB   r]  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    wz3q659 rV   c                 2    t        t         |             S rY   rz  )rB   r_  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    wDGU, rV   c                 2    t        t         |             S rY   rz  )rB   rf  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    WQ( rV   c                 2    t        t         |             S rY   )rl  r	   )rB   rg  rn  s    rC   r   z_futrig.<locals>.<lambda>  s    W$q'5* rV   )	objective)r   rW  rX  rY  r   rZ  r[  r:  r\  r]  r^  r_  r`  ra  rb  rc  rd  re  rf  rg  r   r(   r   r  r1   r2   )rA   rW  rY  rZ  r:  r\  r`  rb  rc  rd  re  rO   Lopstreer[  rg  r^  r_  rX  rf  r   ra  r]  rn  s                 @@@@@@@@@@rC   rR  rR    s8        
 55&'xx##$9:qLD2E+	89AeS+	89	3	)*8,	-
 :,	- 		4	 ( 	)S$	 * 	+C#	
$DJ 	%tt$Q'AAIHrV   c                     t        | t              rt        | j                        S t        | t              syt        d | j                  D              S )zD_eapply helper to tell whether ``e`` and all its args
    are Exprs.Fc              3   2   K   | ]  }t        |        y wrY   )_is_Expr)r[   r5  s     rC   r]   z_is_Expr.<locals>.<genexpr>  s     +qx{+s   )r9   r   r  r}   r   allra   )rA   s    rC   r  r    s?     !Z a+AFF+++rV   c           	          t        |t              s|S t        |      s|j                  s | |      S  |j                  |j                  D cg c]  }| ||      rt        | |      n| c} S c c}w )zdApply ``func`` to ``e`` if all args are Exprs else only
    apply it to those args that *are* Exprs.)r9   r   r  ra   r`   rl  )r`   rA   condeis       rC   rl  rl    sq     a{!&&Aw166&& #ld2hbR?   s   A/)FrY   )[collectionsr   	functoolsr   
sympy.corer   r   r   r   r	   r
   r   r   sympy.core.cacher   sympy.core.functionr   r   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%   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_mainr0   sympy.strategies.corer1   sympy.strategies.treer2   sympy.utilities.iterablesr3   sympy.utilities.miscr4   r   r   r   r   r   r   r  r  r  r$  r3  r2  r4  r6  r   r8  r   rR  r  rl  rZ   rV   rC   <module>r     s   # - - - $G G G / / % 2 2 * K K K ! D J E E " 5 * ' * ( . &"$E!&LF^  !3	4DiX[~ !% EPA Qh)X )X 	~ 	~D  (V<~,	rV   