
    wg'                      U d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< ddl=m>Z> ddl?m@Z@ ddlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZN ddlOmPZPmQZQ ddlRmSZSmTZTmUZUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\ ddl]m^Z^ ddl_m`Z`maZa ddlbmcZcmdZdmeZemfZfmgZgmhZhmiZimjZjmkZk ddllmmZm d d!lnmoZo dd"lpmqZq dd#lrmsZs dd$ltmuZumvZvmwZwmxZx dd%lymzZz dd&l{m|Z| dd'l}m~Z~ dd(lmZmZmZmZ dd)lmZ dd*lmZ e G d+ d,e	             Ze G d- d.ee	             Ze G d/ d0e             Ze G d1 d2e             Ze G d3 d4e             Ze G d5 d6e             Ze G d7 d8e             Ze G d9 d:e             Ze G d; d<e             Ze G d= d>e             Ze G d? d@ee	             Ze G dA dBe             Ze G dC dDe             Ze G dE dFe             Ze G dG dHe             Ze G dI dJe             Ze G dK dLe             Ze G dM dNee	             Ze G dO dPe             Ze G dQ dRe             Ze G dS dTe             Ze G dU dVe             Ze G dW dXe             Ze G dY dZe             Ze G d[ d\e             Ze G d] d^e             Ze G d_ d`e             Ze G da dbe             Ze G dc dde             Ze G de dfe             Ze G dg dhe             Ze G di dje             Ze G dk dle             Ze G dm dne             Ze G do dpe             Ze G dq dre             Ze G ds dte             Ze G du dvee	             Ze G dw dxe             Ze G dy dze             Ze G d{ d|e             Ze G d} d~e             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d dee	             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Ze G d de             Z G d de      Zd Zd ZeKeJeIeLeMeNfZd Zd Zd Zd Zd Zd Zd Zd Zd Zg Zded<   g Z e,d      Zd ZddZddZdddZd ZddZddZd Zd ZddZd ZddZdddZddZed        Zed        ZedÄ        ZedĄ        Zedń        ZdƄ ZdǄ Z edȄ       Z edɄ       Z edʄ       Z ed˄       Z ed̄       Z ed̈́       Z ed΄       Z edτ       Z edЄ       Z edф       Z ed҄       Z edӄ       Z edԄ       Z edՄ       Z edք       Z edׄ       Zd؄ Zdل Zdڄ Zdۄ Zd܄ Zd݄ Zdބ Z dd߄Zd Z ed d       Z ed d       Z ed d       Z ed d       Zd Zd Zd Z	i Z
ded<    ee      Zded<    e,d      Zd Zd Zy)a/  Integration method that emulates by-hand techniques.

This module also provides functionality to get the steps used to evaluate a
particular integral, in the ``integral_steps`` function. This will return
nested ``Rule`` s representing the integration rules used.

Each ``Rule`` class represents a (maybe parametrized) integration rule, e.g.
``SinRule`` for integrating ``sin(x)`` and ``ReciprocalSqrtQuadraticRule``
for integrating ``1/sqrt(a+b*x+c*x**2)``. The ``eval`` method returns the
integration result.

The ``manualintegrate`` function computes the integral by calling ``eval``
on the rule returned by ``integral_steps``.

The integrator can be extended with new heuristics and evaluation
techniques. To do so, extend the ``Rule`` class, implement ``eval`` method,
then write a function that accepts an ``IntegralInfo`` object and returns
either a ``Rule`` instance or ``None``. If the new technique requires a new
match, add the key and call to the antiderivative function to integral_steps.
To enable simple substitutions, add the match to find_substitutions.

    )annotations)
NamedTupleTypeCallableSequence)ABCabstractmethod)	dataclass)defaultdict)Mapping)Add)cacheit)Dict)Expr)
Derivative)	fuzzy_not)Mul)IntegerNumberE)Pow)EqNeBoolean)S)DummySymbolWild)Abs)explog)HyperbolicFunctioncschcoshcothsechsinhtanhasinh)sqrt)	Piecewise)TrigonometricFunctioncossintancotcscsecacosasinatanacotacscasec)	Heaviside
DiracDelta)
erferfifresnelcfresnelsCiChiSiShiEili)
uppergamma)
elliptic_e
elliptic_f)	
chebyshevt
chebyshevulegendrehermitelaguerreassoc_laguerre
gegenbauerjacobiOrthogonalPolynomial)polylog   )Integral)And)primefactors)degreelcm_listgcd_listPoly)fraction)simplify)solve)switchdo_one	null_safe	condition)iterable)debugc                  F    e Zd ZU ded<   ded<   edd       Zed	d       Zy)
Ruler   	integrandr   variablec                     y N selfs    d/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/integrals/manualintegrate.pyevalz	Rule.evalM           c                     y rh   ri   rj   s    rl   contains_dont_knowzRule.contains_dont_knowQ   rn   ro   Nreturnr   rs   bool)__name__
__module____qualname____annotations__r	   rm   rq   ri   ro   rl   rd   rd   H   s3    O   ro   rd   c                      e Zd ZdZddZy)
AtomicRulez1A simple rule that does not depend on other rulesc                     y)NFri   rj   s    rl   rq   zAtomicRule.contains_dont_knowY   s    ro   Nrt   )rv   rw   rx   __doc__rq   ri   ro   rl   r{   r{   V   s
    ;ro   r{   c                      e Zd ZdZddZy)ConstantRulezintegrate(a, x)  ->  a*xc                4    | j                   | j                  z  S rh   )re   rf   rj   s    rl   rm   zConstantRule.eval`   s    ~~--ro   Nrr   rv   rw   rx   r}   rm   ri   ro   rl   r   r   ]   s
    ".ro   r   c                  @    e Zd ZU dZded<   ded<   ded<   d
dZddZy	)ConstantTimesRulez.integrate(a*f(x), x)  ->  a*integrate(f(x), x)r   constantotherrd   substepc                P    | j                   | j                  j                         z  S rh   )r   r   rm   rj   s    rl   rm   zConstantTimesRule.evalk   s    }}t||00222ro   c                6    | j                   j                         S rh   r   rq   rj   s    rl   rq   z$ConstantTimesRule.contains_dont_known       ||..00ro   Nrr   rt   rv   rw   rx   r}   ry   rm   rq   ri   ro   rl   r   r   d   s    8NKM31ro   r   c                  .    e Zd ZU dZded<   ded<   ddZy)	PowerRulezintegrate(x**a, x)r   baser    c                    t        | j                  | j                  dz   z  | j                  dz   z  t        | j                  d      ft	        | j                        df      S NrR   T)r+   r   r    r   r!   rj   s    rl   rm   zPowerRule.evalx   sP    ii$((Q,'$((Q,7DHHb9IJ^T"
 	
ro   Nrr   rv   rw   rx   r}   ry   rm   ri   ro   rl   r   r   r   s    
J	I
ro   r   c                  .    e Zd ZU dZded<   ded<   ddZy)NestedPowRulezintegrate((x**a)**b, x)r   r   r    c                    | j                   | j                  z  }t        || j                  dz   z  t	        | j                  d      f|t        | j                         z  df      S r   )r   re   r+   r    r   r!   )rk   ms     rl   rm   zNestedPowRule.eval   sU    II&!txx!|,b2.>?c$))n,d35 	5ro   Nrr   r   ri   ro   rl   r   r      s    !
J	I5ro   r   c                  ,    e Zd ZU dZded<   ddZddZy)	AddRulezDintegrate(f(x) + g(x), x) -> integrate(f(x), x) + integrate(g(x), x)
list[Rule]substepsc                4    t        d | j                  D         S )Nc              3  <   K   | ]  }|j                           y wrh   )rm   .0r   s     rl   	<genexpr>zAddRule.eval.<locals>.<genexpr>   s     AW\\^A   )r   r   rj   s    rl   rm   zAddRule.eval   s    A4==ABBro   c                :    t        d | j                  D              S )Nc              3  <   K   | ]  }|j                           y wrh   rq   r   s     rl   r   z-AddRule.contains_dont_know.<locals>.<genexpr>   s     MG7--/Mr   )anyr   rj   s    rl   rq   zAddRule.contains_dont_know   s    Mt}}MMMro   Nrr   rt   r   ri   ro   rl   r   r      s    NCNro   r   c                  @    e Zd ZU dZded<   ded<   ded<   ddZdd	Zy
)URulez;integrate(f(g(x))*g'(x), x) -> integrate(f(u), u), u = g(x)r   u_varr   u_funcrd   r   c                P   | j                   j                         }| j                  j                  rQ| j                  j	                         \  }}|dk(  r/|j                  t        | j                        t        |             }|j                  | j                  | j                        S )Nr   )r   rm   r   is_Powas_base_expsubsr!   r   )rk   resultr   exp_s       rl   rm   z
URule.eval   su    ""$;;002JD$rzS_s4yjA{{4::t{{33ro   c                6    | j                   j                         S rh   r   rj   s    rl   rq   zURule.contains_dont_know   r   ro   Nrr   rt   r   ri   ro   rl   r   r      s    EMLM41ro   r   c                  J    e Zd ZU dZded<   ded<   ded<   ded	<   dd
ZddZy)	PartsRulez@integrate(u(x)*v'(x), x) -> u(x)*v(x) - integrate(u'(x)*v(x), x)r   ur   dvrd   v_stepRule | Nonesecond_stepc                    | j                   J | j                  j                         }| j                  |z  | j                   j                         z
  S rh   )r   r   rm   r   )rk   vs     rl   rm   zPartsRule.eval   sH    +++KKvvzD,,11333ro   c                    | j                   j                         xs* | j                  d uxr | j                  j                         S rh   )r   rq   r   rj   s    rl   rq   zPartsRule.contains_dont_know   s@    {{--/ TD(RT-=-=-P-P-R	Tro   Nrr   rt   r   ri   ro   rl   r   r      s%    JIHL4
Tro   r   c                  6    e Zd ZU dZded<   ded<   d	dZd
dZy)CyclicPartsRulez9Apply PartsRule multiple times to integrate exp(x)*sin(x)zlist[PartsRule]parts_rulesr   coefficientc                    g }d}| j                   D ]@  }|j                  ||j                  z  |j                  j	                         z         |dz  }B t        | d| j                  z
  z  S )NrR   r   )r   appendr   r   rm   r   r   )rk   r   signrules       rl   rm   zCyclicPartsRule.eval   sl    $$ 	DMM$-$++*:*:*<<=BJD	 F|q4#3#3344ro   c                :    t        d | j                  D              S )Nc              3  <   K   | ]  }|j                           y wrh   r   r   s     rl   r   z5CyclicPartsRule.contains_dont_know.<locals>.<genexpr>   s     PG7--/Pr   )r   r   rj   s    rl   rq   z"CyclicPartsRule.contains_dont_know   s    Pt?O?OPPPro   Nrr   rt   r   ri   ro   rl   r   r      s    C  5Qro   r   c                      e Zd Zy)TrigRuleNrv   rw   rx   ri   ro   rl   r   r          ro   r   c                      e Zd ZdZddZy)SinRulezintegrate(sin(x), x) -> -cos(x)c                .    t        | j                         S rh   )r-   rf   rj   s    rl   rm   zSinRule.eval       DMM"""ro   Nrr   r   ri   ro   rl   r   r      s
    )#ro   r   c                      e Zd ZdZddZy)CosRulezintegrate(cos(x), x) -> sin(x)c                ,    t        | j                        S rh   )r.   rf   rj   s    rl   rm   zCosRule.eval       4==!!ro   Nrr   r   ri   ro   rl   r   r      s
    ("ro   r   c                      e Zd ZdZddZy)
SecTanRulez%integrate(sec(x)*tan(x), x) -> sec(x)c                ,    t        | j                        S rh   )r2   rf   rj   s    rl   rm   zSecTanRule.eval   r   ro   Nrr   r   ri   ro   rl   r   r      s
    /"ro   r   c                      e Zd ZdZddZy)
CscCotRulez&integrate(csc(x)*cot(x), x) -> -csc(x)c                .    t        | j                         S rh   )r1   rf   rj   s    rl   rm   zCscCotRule.eval   r   ro   Nrr   r   ri   ro   rl   r   r      s
    0#ro   r   c                      e Zd ZdZddZy)Sec2Rulez!integrate(sec(x)**2, x) -> tan(x)c                ,    t        | j                        S rh   )r/   rf   rj   s    rl   rm   zSec2Rule.eval   r   ro   Nrr   r   ri   ro   rl   r   r      s
    +"ro   r   c                      e Zd ZdZddZy)Csc2Rulez"integrate(csc(x)**2, x) -> -cot(x)c                .    t        | j                         S rh   )r0   rf   rj   s    rl   rm   zCsc2Rule.eval   r   ro   Nrr   r   ri   ro   rl   r   r      s
    ,#ro   r   c                      e Zd Zy)HyperbolicRuleNr   ri   ro   rl   r   r      r   ro   r   c                      e Zd ZdZddZy)SinhRulez integrate(sinh(x), x) -> cosh(x)c                ,    t        | j                        S rh   )r$   rf   rj   s    rl   rm   zSinhRule.eval      DMM""ro   Nrr   r   ri   ro   rl   r   r     s
    *#ro   r   c                      e Zd ZdZd Zy)CoshRulez integrate(cosh(x), x) -> sinh(x)c                ,    t        | j                        S rh   )r'   rf   rj   s    rl   rm   zCoshRule.eval  r   ro   Nr   ri   ro   rl   r   r   
  s
    *#ro   r   c                  .    e Zd ZU dZded<   ded<   ddZy)ExpRulez integrate(a**x, x) -> a**x/ln(a)r   r   r    c                F    | j                   t        | j                        z  S rh   )re   r!   r   rj   s    rl   rm   zExpRule.eval  s    ~~DII..ro   Nrr   r   ri   ro   rl   r   r     s    *
J	I/ro   r   c                  $    e Zd ZU dZded<   ddZy)ReciprocalRulezintegrate(1/x, x) -> ln(x)r   r   c                ,    t        | j                        S rh   )r!   r   rj   s    rl   rm   zReciprocalRule.eval   s    499~ro   Nrr   r   ri   ro   rl   r   r     s    $
Jro   r   c                      e Zd ZdZddZy)
ArcsinRulez'integrate(1/sqrt(1-x**2), x) -> asin(x)c                ,    t        | j                        S rh   )r4   rf   rj   s    rl   rm   zArcsinRule.eval'  r   ro   Nrr   r   ri   ro   rl   r   r   $  s
    1#ro   r   c                      e Zd ZdZddZy)ArcsinhRulez'integrate(1/sqrt(1+x**2), x) -> asin(x)c                ,    t        | j                        S rh   )r)   rf   rj   s    rl   rm   zArcsinhRule.eval.  s    T]]##ro   Nrr   r   ri   ro   rl   r   r   +  s
    1$ro   r   c                  8    e Zd ZU dZded<   ded<   ded<   ddZy)	ReciprocalSqrtQuadraticRulezWintegrate(1/sqrt(a+b*x+c*x**2), x) -> log(2*sqrt(c)*sqrt(a+b*x+c*x**2)+b+2*c*x)/sqrt(c)r   abcc           	         | j                   | j                  | j                  | j                  f\  }}}}t	        dt        |      z  t        |||z  z   ||dz  z  z         z  |z   d|z  |z  z         t        |      z  S )N   )r   r   r   rf   r!   r*   rk   r   r   r   xs        rl   rm   z ReciprocalSqrtQuadraticRule.eval9  st    VVTVVTVVT]]:
1a1T!W9T!AaC%!Q$,//1!A#a%78a@@ro   Nrr   r   ri   ro   rl   r   r   2  s    aGGGAro   r   c                  B    e Zd ZU dZded<   ded<   ded<   ded<   d
dZy	)SqrtQuadraticDenomRulez(integrate(poly(x)/sqrt(a+b*x+c*x**2), x)r   r   r   r   z
list[Expr]coeffsc           	        | j                   | j                  | j                  | j                  j	                         | j
                  f\  }}}g j	                         t        t              dz
        D ]g  }t              dz
  |z
  }|   ||z  z  }j                  |       |dz   xx   d|z  dz
  |z  dz  |z  z  cc<   |dz   xx   |dz
  |z  |z  z  cc<   i d   d   }}t        ||z  z   |dz  z  z         }	|||z  d|z  z  z
  }
|
dk(  rd}n-t        t        d|	z        d      }|
|j                         z  }t        g fdt        t                    D        ||z   |	z  |z   S )	Nr   rR   r   r   F
degeneratec              3  R   K   | ]  }|   t              d z
  |z
  z  z     yw)r   N)len)r   ir   result_coeffsr   s     rl   r   z.SqrtQuadraticDenomRule.eval.<locals>.<genexpr>^  s4      9 #1%a#f+a-/&:: 9s   $')r   r   r   r   copyrf   ranger  r   r*   inverse_trig_ruleIntegralInform   r   )rk   r   r   r   r  ncoeffdesr   I0stepr   r  r   s                @@@rl   rm   zSqrtQuadraticDenomRule.evalF  s   !VVTVVTVVT[[5E5E5GV1a s6{1}% 	)AFAaA1IqsOE  '1Q3KAaCE19Q;u,,K1Q3KAaC75=(K	) bz6":11Q3qAvQqS!A#Y;q=B$\!A#q%9eLD$))+%B ? 9#C$679 ?:;A#??@ACEF 	Fro   Nrr   r   ri   ro   rl   r   r   >  s     2GGGFro   r   c                  8    e Zd ZU dZded<   ded<   ded<   ddZy)	SqrtQuadraticRulez integrate(sqrt(a+b*x+c*x**2), x)r   r   r   r   c                x    t        t        | j                  | j                        d      }|j	                         S )NFr   )sqrt_quadratic_ruler  re   rf   rm   )rk   r  s     rl   rm   zSqrtQuadraticRule.evali  s*    "<#N[`ayy{ro   Nrr   r   ri   ro   rl   r  r  b  s    *GGGro   r  c                  ,    e Zd ZU dZded<   ddZddZy)	AlternativeRulez Multiple ways to do integration.r   alternativesc                <    | j                   d   j                         S Nr   )r  rm   rj   s    rl   rm   zAlternativeRule.evals  s      #((**ro   c                :    t        d | j                  D              S )Nc              3  <   K   | ]  }|j                           y wrh   r   r   s     rl   r   z5AlternativeRule.contains_dont_know.<locals>.<genexpr>w  s     QG7--/Qr   )r   r  rj   s    rl   rq   z"AlternativeRule.contains_dont_knowv  s    Qt?P?PQQQro   Nrr   rt   r   ri   ro   rl   r  r  n  s    *+Rro   r  c                       e Zd ZdZddZddZy)DontKnowRulezLeave the integral as is.c                B    t        | j                  | j                        S rh   )rS   re   rf   rj   s    rl   rm   zDontKnowRule.eval}  s    66ro   c                     yNTri   rj   s    rl   rq   zDontKnowRule.contains_dont_know  s    ro   Nrr   rt   )rv   rw   rx   r}   rm   rq   ri   ro   rl   r  r  z  s    #7ro   r  c                      e Zd ZdZddZy)DerivativeRulezintegrate(f'(x), x) -> f(x)c                   t        | j                  t              sJ t        | j                  j                        }t        |      D ]#  \  }\  }}|| j                  k(  s||dz
  f||<    n t        | j                  j                  g| S NrR   )
isinstancere   r   listvariable_count	enumeraterf   expr)rk   r&  r  varcounts        rl   rm   zDerivativeRule.eval  s    $..*555dnn;;<(8 	OA|Udmm#%(%!)$4q!	 $..--???ro   Nrr   r   ri   ro   rl   r!  r!    s    %@ro   r!  c                  6    e Zd ZU dZded<   ded<   d	dZd
dZy)RewriteRulez;Rewrite integrand to another form that is easier to handle.r   	rewrittenrd   r   c                6    | j                   j                         S rh   )r   rm   rj   s    rl   rm   zRewriteRule.eval  s    ||  ""ro   c                6    | j                   j                         S rh   r   rj   s    rl   rq   zRewriteRule.contains_dont_know  r   ro   Nrr   rt   r   ri   ro   rl   r,  r,    s    EOM#1ro   r,  c                      e Zd ZdZy)CompleteSquareRulez7Rewrite a+b*x+c*x**2 to a-b**2/(4*c) + c*(x+b/(2*c))**2N)rv   rw   rx   r}   ri   ro   rl   r1  r1    s    Aro   r1  c                  (    e Zd ZU ded<   ddZddZy)PiecewiseRulez%Sequence[tuple[Rule, bool | Boolean]]subfunctionsc                x    t        | j                  D cg c]  \  }}|j                         |f c}} S c c}}w rh   )r+   r4  rm   )rk   r   conds      rl   rm   zPiecewiseRule.eval  sD    040A0AC,w $LLND1 C D 	D Cs   6
c                :    t        d | j                  D              S )Nc              3  B   K   | ]  \  }}|j                           y wrh   r   )r   r   _s      rl   r   z3PiecewiseRule.contains_dont_know.<locals>.<genexpr>  s     TJGQ7--/Ts   )r   r4  rj   s    rl   rq   z PiecewiseRule.contains_dont_know  s    T$BSBSTTTro   Nrr   rt   rv   rw   rx   ry   rm   rq   ri   ro   rl   r3  r3    s    77DUro   r3  c                  <    e Zd ZU ded<   ded<   ded<   d	dZd
dZy)HeavisideRuler   hargibndrd   r   c                    | j                   j                         }t        | j                        ||j	                  | j
                  | j                        z
  z  S rh   )r   rm   r9   r=  r   rf   r>  )rk   r   s     rl   rm   zHeavisideRule.eval  sB    
 ""$#vDMM4990U'UVVro   c                6    | j                   j                         S rh   r   rj   s    rl   rq   z HeavisideRule.contains_dont_know  r   ro   Nrr   rt   r:  ri   ro   rl   r<  r<    s    
J
JMW1ro   r<  c                  4    e Zd ZU ded<   ded<   ded<   ddZy)DiracDeltaRuler   r	  r   r   c                    | j                   | j                  | j                  | j                  f\  }}}}|dk(  rt	        |||z  z         |z  S t        |||z  z   |dz
        |z  S Nr   rR   )r	  r   r   rf   r9   r:   rk   r	  r   r   r   s        rl   rm   zDiracDeltaRule.eval  se    VVTVVTVVT]]:
1a6QqsU#A%%!AaC%1%a''ro   Nrr   rv   rw   rx   ry   rm   ri   ro   rl   rB  rB    s    GGG(ro   rB  c                  P    e Zd ZU ded<   ded<   ded<   ded<   ded<   dd	Zdd
Zy)TrigSubstitutionRuler   thetafuncr-  rd   r   zbool | Booleanrestrictionc                   | j                   | j                  | j                  }}}|j                  t	        |      dt        |      z        }|j                  t        |      dt        |      z        }|j                  t        |      dt        |      z        }t        |j                  t                    }t        |      dk(  sJ |d   }t        ||z
  |      }t        |      dk(  sJ t        |d         \  }}t!        |t              r'|}|}	t#        |dz  |dz  z
        }
t%        |d         }n]t!        |t
              r'|}
|}	t#        |dz  |dz  z
        }t'        |d         }n&|}|}
t#        |dz  |dz  z         }	t)        |d         }t        |      ||	z  ft        |      |
|	z  ft        |      ||
z  f||fg}t+        | j,                  j/                         j                  |      j1                         | j2                  f      S )NrR   r   r   )rI  rJ  rf   r   r2   r-   r1   r.   r0   r/   r%  findr,   r  r\   rZ   r$  r*   r4   r3   r5   r+   r   rm   trigsimprK  )rk   rI  rJ  r   trig_functionrelationnumerdenomopposite
hypotenuseadjacentinversesubstitutions                rl   rm   zTrigSubstitutionRule.eval  s   TYYQtyyUQs5z\2yyUQs5z\2yyUQs5z\2TYY'<=>=!Q&&&%a(T=18}!!!,umS)HJE1Huax/0H8A;'Gs+HJE1Huax/0H8A;'GHHeQh12J8A;'G Z*,-Z*,-Z(*+G	
 \\ %%l3<<>@P@PQ
 	
ro   c                6    | j                   j                         S rh   r   rj   s    rl   rq   z'TrigSubstitutionRule.contains_dont_know  r   ro   Nrr   rt   r:  ri   ro   rl   rH  rH    s'    K
JOM%
N1ro   rH  c                  8    e Zd ZU dZded<   ded<   ded<   ddZy)	
ArctanRulezAintegrate(a/(b*x**2+c), x) -> a/b / sqrt(c/b) * atan(x/sqrt(c/b))r   r   r   r   c                    | j                   | j                  | j                  | j                  f\  }}}}||z  t	        ||z        z  t        |t	        ||z        z        z  S rh   )r   r   r   rf   r*   r5   r   s        rl   rm   zArctanRule.eval	  sT    VVTVVTVVT]]:
1asT!A#YaQqS	k!222ro   Nrr   r   ri   ro   rl   rZ  rZ    s    KGGG3ro   rZ  c                      e Zd ZU ded<   y)OrthogonalPolyRuler   r	  Nrv   rw   rx   ry   ri   ro   rl   r]  r]    s    Gro   r]  c                  *    e Zd ZU ded<   ded<   ddZy)
JacobiRuler   r   r   c           	     N   | j                   | j                  | j                  | j                  f\  }}}}t	        dt        |dz   |dz
  |dz
  |      z  ||z   |z   z  t        ||z   |z   d      f|t        |d      f||z   dz   |dz  z  dz  ||z
  |z  dz  z   t        |d      f      S )Nr   rR   r      )r	  r   r   rf   r+   rO   r   r   rE  s        rl   rm   zJacobiRule.eval  s    VVTVVTVVT]]:
1ava!eQUAE1--q1uqy92a!eai;KL1aM!eaiAa1q5!)A+-r!Qx8: 	:ro   Nrr   rF  ri   ro   rl   r`  r`    s    GG:ro   r`  c                       e Zd ZU ded<   ddZy)GegenbauerRuler   r   c                   | j                   | j                  | j                  }}}t        t	        |dz   |dz
  |      d|dz
  z  z  t        |d      ft        |dz   |      |dz   z  t        |d      ft        j                  df      S )NrR   r   r   T)	r	  r   rf   r+   rN   r   rH   r   Zero)rk   r	  r   r   s       rl   rm   zGegenbauerRule.eval$  s    &&$&&$--a1Aq1ua(!QU)4bAh?Aq!1q5)2a95VVTN 	ro   Nrr   rF  ri   ro   rl   rd  rd     s    Gro   rd  c                      e Zd ZddZy)ChebyshevTRulec                    | j                   | j                  }}t        t        |dz   |      |dz   z  t        |dz
  |      |dz
  z  z
  dz  t	        t        |      d      f|dz  dz  df      S )NrR   r   T)r	  rf   r+   rH   r   r   rk   r	  r   s      rl   rm   zChebyshevTRule.eval.  s|    vvt}}1Q"AE*Q"AE*+,-./1#a&!}>T!VTN 	ro   Nrr   rv   rw   rx   rm   ri   ro   rl   rh  rh  ,  s    ro   rh  c                      e Zd ZddZy)ChebyshevURulec                    | j                   | j                  }}t        t        |dz   |      |dz   z  t	        |d      ft
        j                  df      S r   )r	  rf   r+   rH   r   r   rf  rj  s      rl   rm   zChebyshevURule.eval8  sM    vvt}}1Aq!1q5)2a95VVTN 	ro   Nrr   rk  ri   ro   rl   rm  rm  6  s    ro   rm  c                      e Zd ZddZy)LegendreRulec                    | j                   | j                  }}t        |dz   |      t        |dz
  |      z
  d|z  dz   z  S NrR   r   )r	  rf   rJ   rj  s      rl   rm   zLegendreRule.evalA  sA    vvt}}1Aq!HQUA$661qAAro   Nrr   rk  ri   ro   rl   rp  rp  ?  s    Bro   rp  c                      e Zd ZddZy)HermiteRulec                b    | j                   | j                  }}t        |dz   |      d|dz   z  z  S rr  )r	  rf   rK   rj  s      rl   rm   zHermiteRule.evalH  s1    vvt}}1q1ua !QU),,ro   Nrr   rk  ri   ro   rl   rt  rt  F  s    -ro   rt  c                      e Zd ZddZy)LaguerreRulec                j    | j                   | j                  }}t        ||      t        |dz   |      z
  S r#  )r	  rf   rL   rj  s      rl   rm   zLaguerreRule.evalO  s/    vvt}}11~Q 222ro   Nrr   rk  ri   ro   rl   rw  rw  M      3ro   rw  c                       e Zd ZU ded<   ddZy)AssocLaguerreRuler   r   c                f    t        | j                  dz   | j                  dz
  | j                         S r#  )rM   r	  r   rf   rj   s    rl   rm   zAssocLaguerreRule.evalX  s(    tvvz466A:t}}EEEro   Nrr   rF  ri   ro   rl   r{  r{  T  s    GFro   r{  c                  "    e Zd ZU ded<   ded<   y)IRuler   r   r   Nr^  ri   ro   rl   r~  r~  \  s    GGro   r~  c                      e Zd ZddZy)CiRulec                    | j                   | j                  | j                  }}}t        |      t	        ||z        z  t        |      t        ||z        z  z
  S rh   )r   r   rf   r-   r?   r.   rA   rk   r   r   r   s       rl   rm   zCiRule.evald  F    &&$&&$--a11vb1g~Ar!A#w..ro   Nrr   rk  ri   ro   rl   r  r  b      /ro   r  c                      e Zd ZddZy)ChiRulec                    | j                   | j                  | j                  }}}t        |      t	        ||z        z  t        |      t        ||z        z  z   S rh   )r   r   rf   r$   r@   r'   rB   r  s       rl   rm   zChiRule.evalk  H    &&$&&$--a1Aws1Q3x$q'#ac("222ro   Nrr   rk  ri   ro   rl   r  r  i  ry  ro   r  c                      e Zd ZddZy)EiRulec                ~    | j                   | j                  | j                  }}}t        |      t	        ||z        z  S rh   )r   r   rf   r    rC   r  s       rl   rm   zEiRule.evalr  s1    &&$&&$--a11vb1g~ro   Nrr   rk  ri   ro   rl   r  r  p  s    ro   r  c                      e Zd ZddZy)SiRulec                    | j                   | j                  | j                  }}}t        |      t	        ||z        z  t        |      t        ||z        z  z   S rh   )r   r   rf   r.   r?   r-   rA   r  s       rl   rm   zSiRule.evaly  r  ro   Nrr   rk  ri   ro   rl   r  r  w  r  ro   r  c                      e Zd ZddZy)ShiRulec                    | j                   | j                  | j                  }}}t        |      t	        ||z        z  t        |      t        ||z        z  z   S rh   )r   r   rf   r'   r@   r$   rB   r  s       rl   rm   zShiRule.eval  r  ro   Nrr   rk  ri   ro   rl   r  r  ~  ry  ro   r  c                      e Zd ZddZy)LiRulec                r    | j                   | j                  | j                  }}}t        ||z  |z         |z  S rh   )r   r   rf   rD   r  s       rl   rm   zLiRule.eval  s1    &&$&&$--a1!A#'{1}ro   Nrr   rk  ri   ro   rl   r  r    s    ro   r  c                  4    e Zd ZU ded<   ded<   ded<   ddZy)ErfRuler   r   r   r   c                   | j                   | j                  | j                  | j                  f\  }}}}|j                  rt        t        t        j                        t        |       z  dz  t        ||dz  d|z  z  z
        z  t        d|z  |z  |z
  dt        |       z  z        z  |dk  ft        t        j                        t        |      z  dz  t        ||dz  d|z  z  z
        z  t        d|z  |z  |z   dt        |      z  z        z  df      S t        t        j                        t        |      z  dz  t        ||dz  d|z  z  z
        z  t        d|z  |z  |z   dt        |      z  z        z  S )Nr   rb  r   r   T)r   r   r   rf   is_extended_realr+   r*   r   Pir    r;   r<   r   s        rl   rm   zErfRule.eval  sh   VVTVVTVVT]]:
1aaddD!H$Q&QAqs^)<<Aa!aaRj12345E;addDG#A%A1ac
N(;;!A#a%!)aQi012379: :
 ADDz$q'!!#c!adAaCj.&99ac!eai!DG),-. 	.ro   Nrr   rF  ri   ro   rl   r  r    s    GGG	.ro   r  c                  4    e Zd ZU ded<   ded<   ded<   ddZy)FresnelCRuler   r   r   r   c           
        | j                   | j                  | j                  | j                  f\  }}}}t	        t
        j                        t	        d|z        z  t        |dz  d|z  z  |z
        t        d|z  |z  |z   t	        d|z  t
        j                  z        z        z  t        |dz  d|z  z  |z
        t        d|z  |z  |z   t	        d|z  t
        j                  z        z        z  z   z  S Nr   rb  )r   r   r   rf   r*   r   r  r-   r=   r.   r>   r   s        rl   rm   zFresnelCRule.eval      VVTVVTVVT]]:
1aADDz$qs)#1ac
Q!A#a%!)T!A#add(^)C DD1ac
Q!A#a%!)T!A#add(^)C DDEF 	Fro   Nrr   rF  ri   ro   rl   r  r        GGGFro   r  c                  4    e Zd ZU ded<   ded<   ded<   ddZy)FresnelSRuler   r   r   r   c           
        | j                   | j                  | j                  | j                  f\  }}}}t	        t
        j                        t	        d|z        z  t        |dz  d|z  z  |z
        t        d|z  |z  |z   t	        d|z  t
        j                  z        z        z  t        |dz  d|z  z  |z
        t        d|z  |z  |z   t	        d|z  t
        j                  z        z        z  z
  z  S r  )r   r   r   rf   r*   r   r  r-   r>   r.   r=   r   s        rl   rm   zFresnelSRule.eval  r  ro   Nrr   rF  ri   ro   rl   r  r    r  ro   r  c                  *    e Zd ZU ded<   ded<   ddZy)PolylogRuler   r   r   c                b    t        | j                  dz   | j                  | j                  z        S r#  )rQ   r   r   rf   rj   s    rl   rm   zPolylogRule.eval  s$    tvvz466DMM#9::ro   Nrr   rF  ri   ro   rl   r  r    s    GG;ro   r  c                  *    e Zd ZU ded<   ded<   ddZy)UpperGammaRuler   r   r  c                    | j                   | j                  | j                  }}}||z  | |z  | z  z  t        |dz   | |z        z  |z  S r#  )r   r  rf   rE   )rk   r   r  r   s       rl   rm   zUpperGammaRule.eval  sQ    &&$&&$--a1!tr!trl"ZAr!t%<<Q>>ro   Nrr   rF  ri   ro   rl   r  r    s    GG?ro   r  c                  *    e Zd ZU ded<   ded<   ddZy)EllipticFRuler   r   r  c                    t        | j                  | j                  | j                  z        t	        | j                        z  S rh   )rG   rf   r  r   r*   rj   s    rl   rm   zEllipticFRule.eval  ,    $--7TVVDDro   Nrr   rF  ri   ro   rl   r  r        GGEro   r  c                  *    e Zd ZU ded<   ded<   ddZy)EllipticERuler   r   r  c                    t        | j                  | j                  | j                  z        t	        | j                        z  S rh   )rF   rf   r  r   r*   rj   s    rl   rm   zEllipticERule.eval  r  ro   Nrr   rF  ri   ro   rl   r  r    r  ro   r  c                  "    e Zd ZU ded<   ded<   y)r  r   re   r   symbolNr^  ri   ro   rl   r  r    s    ONro   r  c                >   | j                   r| j                   d   }t        | t              r |j                        t	        |      dz  z  S t        | t
              r!|j                         t        |      dz  z  S t        | t              r)|j                        t	        |      z  t        |      z  S t        | t              r*|j                         t        |      z  t        |      z  S t        | t              rt        fd| j                   D              S t        | t              r^t        | j                         dk(  rFt        | j                   d   t              r)| j                   d   t        | j                   d         z  S | j                        S )a  Derivative of f in form expected by find_substitutions

    SymPy's derivatives for some trig functions (like cot) are not in a form
    that works well with finding substitutions; this replaces the
    derivatives for those particular forms with something that works better.

    r   r   c              3  6   K   | ]  }t        |        y wrh   )manual_diffr   argr  s     rl   r   zmanual_diff.<locals>.<genexpr>  s     BC{3/B   rR   )argsr$  r/   diffr2   r0   r1   r   sumr   r  r   r  )fr  r  s    ` rl   r  r    s;    	vvffQia88F#c#hk113HHV$$s3x{22388F#c#h.S993HHV$$s3x/#c(::3B166BBB3166{aJqvvay&$Avvay;qvvay&#AAA66&>ro   c                   t        |      dk(  rB|d   }t        |t        t        f      r|j	                         }n3t        |      s(t        d      t        |      dk(  r|g}nt        d      g }|D ]Y  \  }t        |t              s|j                  d   | j                  fdfd      } |j                  t              f       [ | j                  t        |      |z         S )zn
    A wrapper for `expr.subs(*args)` with additional logic for substitution
    of invertible functions.
    rR   r   z(Expected an iterable of (old, new) pairsr   z$subs accepts either 1 or 2 argumentsc                >    | j                   xr | j                  k(  S rh   )r   r   )r   x0s    rl   <lambda>zmanual_subs.<locals>.<lambda>  s    !((*Cqvv| ro   c                4    t        | j                   z        S rh   )r    )r   news    rl   r  zmanual_subs.<locals>.<lambda>  s    #aeeCi. ro   )r  r$  r   r   itemsra   
ValueErrorr!   r  replacer   r    r   r%  )r(  r  sequencenew_subsoldr  r  s        @@rl   manual_subsr    s    
 4yA~7hw0~~'H(#GHH	Ta6?@@H 	,Sc3
 !B<< C(*DOORSN+	, 99T(^h.//ro   c                H    g } fd}dfd}fdt        t        j                           |       z               D ]X  }|k(  r	t        |      } |||      }|dus#|\  }	}| j	                        k(  r>||	|f}
|
|vsH|j                  |
       Z |S )Nc                   |dk(  ry|z  }t        dj                  ||              t        ||       j                         }|j	                        ryj                        r[|j                        rJt        fdj                         D              }t        fd|j                         D              }||kD  ry|j                  d      S )Nr   Fz!substituted: {}, u: {}, u_var: {}c              3  6   K   | ]  }t        |        y wrh   rV   )r   tr  s     rl   r   z;find_substitutions.<locals>.test_subterm.<locals>.<genexpr>-  s     S1VAv.Sr  c              3  6   K   | ]  }t        |        y wrh   r  )r   r  r   s     rl   r   z;find_substitutions.<locals>.test_subterm.<locals>.<genexpr>.  s     SF1e,Sr  )as_Add)	rb   formatr  cancelhas_freeis_rational_functionmaxas_numer_denomas_independent)r   u_diffsubstituted
deg_before	deg_afterre   r  r   s        rl   test_subtermz(find_substitutions.<locals>.test_subterm"  s    Q;&(188aOP!+q%8??A'))&1k6V6VW\6]S	8P8P8RSSJSk6P6P6RSSI:%))%)>>ro   c                f   g }g }t        dd g      }| j                  t              D ]\  }|j                  d   }|j                  vr!|j                  |z        }|r|j                  ||          L|j                  |       ^ |r&|j                  t        t        |      z               |S )Nr	  c                    | j                   S rh   )
is_Integerr	  s    rl   r  z:find_substitutions.<locals>.exp_subterms.<locals>.<lambda>6  s
    ALL ro   
propertiesr   )r   rM  r    r  free_symbolsmatchr   rX   )termlinear_coeffstermsr	  r   r  r  r  s          rl   exp_subtermsz(find_substitutions.<locals>.exp_subterms3  s    "8!9:IIcN 	#D))A,CS---IIah'E$$U1X.T"	# LLXm4V;<=ro   c           
        t        | t        t        gt        t        t
        t              r| j                  d   gS t        | t        t        t        t        t        f      r| j                  d   gS t        | t        t        f      r| j                  d   gS t        | t              r| j                  d   gS t        | t               r=g }| j                  D ]*  }|j#                  |       |j%                   |             , |S t        | t&              r| j                  D cg c]  }|j)                        s| }}| j                  j*                  r|j%                  t-        | j                        D cg c]5  }d|cxk  rt/        | j                  d         k  rn n| j0                  |z  7 c}       | j0                  j2                  r;|j%                   | j0                        D cg c]  }|j4                  r| c}       |S t        | t6              r=g }| j                  D ]*  }|j#                  |       |j%                   |             , |S g S c c}w c c}w c c}w )Nr   rR   r      )r$  r,   r"   inverse_trig_functionsr    r!   r9   r  rH   rI   rJ   rK   rL   rN   rM   rO   r   r   extendr   hasr  rU   absr   is_Addr   r   )r  rr   r  r  r  possible_subtermsr  s         rl   r  z-find_substitutions.<locals>.possible_subtermsD  s   d24F 242 2"%2'02 3 IIaL>!z: '85 6IIaL>!z>:;IIaL>!f%IIaL>!c"AYY /*1-./ Hc" $		=SWWV_=A=xx""TXX0F 2110s499Q<00 ))Q, 2 399##HH):499)E %A88   % &Hc"Ayy 1*3/01 H	 >2%s   'I>I<:I$2I)F)r  r   )r%  dictfromkeysr  r   r   )re   r  r   resultsr  r  r   r  new_integrandr   rW  r  s   ```        @rl   find_substitutionsr    s    G?""!F $-- 1) <|I?V VWX -;Q'$Q/%&3#Hm	vu ==x7L7*|,- Nro   c                      fd}|S )z$Strategy that rewrites an integrand.c                    | \  }}t        dj                  ||              |  r9 |  }||k7  r.t        ||      }t        |t              s|rt        ||||      S y y y y )Nz/Integral: {} is rewritten with {} on symbol: {})rb   r  integral_stepsr$  r  r,  )integralre   r  r-  r   r`   rewrites        rl   	_rewriterzrewriter.<locals>._rewriterx  s}    $	6?FFyRY[abch*II%(F;!'<8W&y&)WMM >E8 &  ro   ri   )r`   r  r  s   `` rl   rewriterr  v  s    N ro   c                      fd}|S )zAStrategy that rewrites an integrand based on some other criteria.c           	         | \  } }|\  }}t        dj                  |||              | t        |      z   } | r# | }||k7  rt        |||t	        ||            S y y )Nz@Integral: {} is rewritten with {} on symbol: {} and criteria: {})rb   r  r%  r,  r  )criteriar  re   r  r  r-  r`   r  s         rl   _proxy_rewriterz'proxy_rewriter.<locals>._proxy_rewriter  s    %($	6PWWXacjlrt|}~$x.(dII%"9fiPY[aAbcc & ro   ri   )r`   r  r  s   `` rl   proxy_rewriterr    s    d ro   c                      fd}|S )z4Apply the rule that matches the condition, else Nonec                Z    j                         D ]  \  }} ||       s ||       c S  y rh   )r  )r(  keyr   
conditionss      rl   multiplexer_rlz#multiplexer.<locals>.multiplexer_rl  s0    #))+ 	"IC4yDz!	"ro   ri   )r  r  s   ` rl   multiplexerr    s    " ro   c                       fd}|S )zHStrategy that makes an AlternativeRule out of multiple possible results.c                   g }d}t        d       D ]Z  }|dz   }t        dj                  ||              ||       }|s.t        |t              r?|| k7  sE||vsJ|j	                  |       \ t        |      dk(  r|d   S |r<|D cg c]  }|j                         r| }}|rt        g | | S t        g | | S y c c}w )Nr   zList of Alternative RulesrR   Rule {}: {})rb   r  r$  r  r   r  rq   r  )r  altsr*  r   r   doableruless         rl   _alternativesz#alternatives.<locals>._alternatives  s    )* 	$DAIE-&&ud34(^Fz&,?("vT'9F#	$ t9>7N'+Mt43J3J3LdMFM&99&99&77$77 Ms   	CCri   )r  r  s   ` rl   r  r    s    8( ro   c                    t        |  S rh   )r   r  s    rl   constant_ruler        ""ro   c           
        | \  }}|j                         \  }}||j                  vr<t        |t              r,t	        |dz         dk(  rt        |||      S t        ||||      S ||j                  vrt        |t              rt        ||||      }t        t        |      j                        r|S t        |      j                  rt        d|      S t        |||t        t        |      d      ft        d|      dfg      S y y )NrR   r   T)r   r  r$  r   r[   r   r   r   r   r!   is_zeror   r3  r   )r  re   r  r   exptr   s         rl   
power_ruler    s     Iv&&(JD$T&&&:dF+CD1H"!)VT::FD$77	t((	(Zf-Ey&$5SY&&'KY6**Y2c$i#$!V$d+1
  	 .F	(ro   c                    | \  }}t        |j                  d   t              rt        ||t        |j                  d         S y r  )r$  r  r   r   r   r  re   r  s      rl   exp_ruler    s>     Iv)..#V,y&!Y^^A->?? -ro   c                   t         t        t        t        t        t
        t        t        t        t        t        t        t        t        t        t        i}t         dt        dt        di}| \  }|D ]p  }t!        ||      s|j#                  |d      }|j$                  |   u s4t'        fd|j$                  d | D              rV ||   |g|j$                  d |  c S  y )Nr  r   rR   c              3  @   K   | ]  }|j                          y wrh   r  )r   r   r  s     rl   r   z'orthogonal_poly_rule.<locals>.<genexpr>  s     FaAEE&MF   )rO   r`  rN   rd  rH   rh  rI   rm  rJ   rp  rK   rt  rL   rw  rM   r{  r$  getr  r   )r  orthogonal_poly_classesorthogonal_poly_var_indexre   klass	var_indexr  s         @rl   orthogonal_poly_ruler$    s    
NNN,,)	
 	A!

 !Iv( ji'155eQ?Iy)V3F9>>*9+EFF9259)Viinn]g^gNhiijro   z/list[tuple[Type, Expr, Callable | None, tuple]]_special_function_patternsr   c                   | \  }}t         sft        dt        gd g      }t        dt        g      }t        dt        g      }t        dt        gd g      }t        d	t        gd
 g      }t        j	                  |||||f       |t        z  |z   }|t        dz  z  |t        z  z   |z   }	t         j	                  t
        t        |d      t        z  d t        ft
        t        |d      t        z  d t        ft
        t        |d      t        z  d t        ft
        t        |d      t        z  d t        ft
        t        |d      t        z  d t        ft         dt#        |d      z  d t$        ft        t        |	d      d t&        ft        t        |	d      d t(        ft        t        |	d      d t*        ft
        t        |z  t        |t        z  d      z  d t,        ft
        t/        ||t        z  d      t        z  d t0        ft         dt3        ||t        t        d      dz  z  z
        z  d t4        ft         t3        ||t        t        d      dz  z  z
        d t6        ff       |j9                  |t              }
t         D ]S  \  }}}}t;        |
|      s|
j=                  |      s)t?        fdt        D              }| || sI |||g| c S  y )Nr   c                    | j                    S rh   r  r   s    rl   r  z'special_function_rule.<locals>.<lambda>      199} ro   excluder  r   r,  r   r  c                    | j                    S rh   r(  r)  s    rl   r  z'special_function_rule.<locals>.<lambda>  r*  ro   r  c                8    | j                   xr | j                   S rh   )is_nonnegative
is_integerr)  s    rl   r  z'special_function_rule.<locals>.<lambda>  s    1++<= ro   r   FevaluaterR   c                    | |k7  S rh   ri   r   r  s     rl   r  z'special_function_rule.<locals>.<lambda>  
    Q!V ro   c                    | |k7  S rh   ri   r5  s     rl   r  z'special_function_rule.<locals>.<lambda>
  r6  ro   c              3  b   K   | ]&  }j                  |      j                  |       ( y wrh   )r  )r   wr  s     rl   r   z(special_function_rule.<locals>.<genexpr>  s.      "?1%*YYq\%= #())A, "?s   ,/) r%  r   _symbol_wildsr  r   r    r  r-   r  r$   r  r.   r  r'   r  r   r!   r  r  r  r  r  rQ   r  r*   r  r  r   r$  r  tuple)r  re   r  r   r   r   r  r  linear_patternquadratic_pattern
_integrandtype_pattern
constraintr   	wild_valsr  s                   @rl   special_function_rulerD    s    Iv%wi5L4MNwi(wi(wi5L4MNwi=5? @q!Q1o& 7QgqjL1W94q8"))#nu5g=tVL#nu5g=tVL$~6w>gN#nu5g=tVL$~6w>gN!C77vF#'%8$H#'%8$M#'%8$M'1*S7U;;T>R'!QwY7?{S!DQs7U;Q>>>??#]4$q1S591<<<=#]4+
 	" 0J,F ?(w
Dj%($$W-E! "? "? ?	%Y)?	6>I>>?ro   c                h   ||S t        |t              r2|j                  D cg c]  \  }}||| z  j                         f }}}n|| fg}t        |t              r||j                  z  }n!|j	                  |t
        j                  f       t        |j                  |j                  |      S c c}}w rh   )	r$  r3  r4  r[   r   r   truere   rf   )generic_condgeneric_stepdegenerate_stepr   r6  r4  s         rl   _add_degenerate_steprJ    s    ,.-9-F-FH)GT !4,#6"@"@"BC H H &|45/=1444_aff56//1F1FUUHs   B.c                  	
 | \  }t        dg      t        ddg      		z  z   t        j                  
 G d dt              d	
fd	  |      \  }}
t        j                  u rd }nt	        |j                  d            }t        |||      }t        
||      S # $ r Y y w xY w)	Nr   r-  r   r   c                      e Zd Zy) nested_pow_rule.<locals>.NoMatchNr   ri   ro   rl   NoMatchrM  /  s    ro   rN  c                   | j                        s t        j                  t        j                  fS | j                  r| j                         \  }}|s t        j                  t        j                  fS |D cg c]
  } |       }}|D ch c]  \  }}|	 }}}|j                  t        j                         t        |      dk(  r|j                         t        d |D         fS | j                  r=| j                  | j                  }}|j                        r |      \  }}	||	|z  fS | j                        }
|
r0|
   |
   }}||z  z   }t        |d      |t        j                  fS c c}w c c}}w )NrR   c              3  &   K   | ]	  \  }}|  y wrh   ri   )r   r9  r  s      rl   r   z9nested_pow_rule.<locals>._get_base_exp.<locals>.<genexpr>=  s     )@1!)@s   r   )r  r   Onerf  is_Mulas_coeff_muldiscardr  popr   r   r   r    r  r   )r(  r9  r  r  r  r   basesr  base_sub_expr  r   rN  _get_base_expa_b_rG  rA  r   s               rl   rY  z&nested_pow_rule.<locals>._get_base_exp2  sT   }}Q55!&&= ;;((*HAuuuaff}$7<=t}T*=G=#*+41aQ+E+MM!%% 5zQyy{C)@)@$AAAM;;99dhhqAzz!}*1-NE7'A+%%

7#9eBiqA!GEa8L!%%<' >+s   8E9E>)r(  r   rs   ztuple[Expr, Expr])r   r   rF  	Exceptionr   r   r   rJ  )r  re   r   r   rI  rH  rN  rY  rZ  r[  rG  rA  r   s         @@@@@@@rl   nested_pow_ruler]  &  s    LIq	cA3	B	cAq6	"BAgG66L)  8"9-
d qvv 'y~~a';Q? AtT:LlOLL  s   B5 5B=<B=c                   | \  j                         \  }}t        dg      }t        dg      }t        ddg      }|j                  ||z  z   |dz  z  z         }|sydfd}|||fD 	cg c]"  }	|j                  |	t        j
                        $ c}	\  }}}t        |d      }
|r|
t        j                  u rd}n:|j                  rt        ||z        }nt        t        ||z  z   |z              }t        d|z  d	z         dk(  r| d|z  z  ||dz  d
|z  z  z
  }}t        |d      }d}|t        j                  ur4t        d	t        ||z
  dz  z        z  |z
  t        j                        }|t        j                   u r|S t#        |||      }t%        |||      }|j&                  r|j&                  rg }t(        |d	| d|ft+        |dkD  |dk        ft,        |d	|d	|ft+        |dkD  |dkD        ffD ]G  \  }}|t        j                  u r || c S |t        j                   us2|j/                   || |f       I |r;|j0                  s!|j/                  |t        j                  f       t3        |      }n|}t%        |
||      S |t        j4                  k(  rt7        |||      }t%        |
||      S yc c}	w )zm
    Set degenerate=False on recursive call where coefficient of quadratic term
    is assumed non-zero.
    r   r-  r   r   r   r   Nc                l   t        d      }dt        ||z  ||z  |z
  dz  z  z         z  }t        ||z        |z
  z  }dt        |      z  }	d }
|ur|}
|}dt        |||dz  z  z         z  } | ||      }|	dk7  rt        |	|z  |	||      }|
t        |||
|      }|dk7  rt	        ||      }|S )Nr   rR   r   r   )r   r*   r   r   r1  )	RuleClassr   sign_ar   sign_chr   r-  quadratic_baser   r   standard_formr   re   r  s                rl   make_inverse_trigz,inverse_trig_rule.<locals>.make_inverse_trigj  s    c
d6!8fQhq1}&<<==	acF1H-T!W9'#F"N$v~q/@(@@AAM>:q='(>R_ahiGIvufgFG6(FIwOGro   rR   rb  r   )rs   rd   )r   r   r  r  r   rf  r   rF  r  r   sqrt_linear_ruler  r[   r   r*   NegativeOnefalser   rJ  is_realr   rT   r   r   is_positiver3  Halfr  )r  r   r   r    r   r   r   r  rf  r  rG  rI  rc  knon_square_condsquare_steprH  r  r  r  r6  re   r  s                        @@rl   r  r  [  s   
 !Iv%%'ID#S6(#AS6(#AS61+&AJJq1V8|a	k12E& /0AY7uyyAFF#7GAq!a8L/	
&qCx8*<QZC8OQW+XY#	ar1Q3xQT1Q3Z1Q(!&&('$q&(Q*?(?PQSTS`S`aKagg%29faAN#O\;O99EaQBA.AE1q50ABq!Q1-s1q5!a%/@A C
d 166>,d33qww&LL"3T":D!ABC }}LL,!78$Y>##L$HH
aff} FAq!<#L$HH K 8s   7'Kc                    | \  }}|j                         D cg c]  }t        ||       }}d |v rd S t        |||      S c c}w rh   )as_ordered_termsr  r   )r  re   r  gr  s        rl   add_rulers    sX     Iv 1135 a( 5G 57?4K	67(KK5s   A c                |    | \  }}|j                  |      \  }}|dk7  rt        ||      }|t        |||||      S y y r#  )r  r  r   )r  re   r  r
  r  	next_steps         rl   mul_rulerv    sX     Iv ''/HE1z"1f-	 $Yq)LL ! ro   c                   fd}d
d} |t                |t         | |t        t               |t              g}t        d      }t        | t         gt              r|| z  } t        |      D ]  \  }} ||       }|s|\  }	}
|	j                  vr|	j                  |      s y |	j                  |d      }	|
j                  |d      }
||k(  r|	j                        s y t        |	t               r'd|
z  }|j                        rt        |      dk(  r y ||k(  ry|
j                  s%|
j                  t              st        |
t              rHt!        |
      }|j#                         r y |	j%                        }|j'                         }|	|
|||fc S d}|dk  rd}np||k(  r+|
j(                  rt+        d |
j(                  D              rd}n@||dz   d  D ]5  } ||       }|s|d	   j                  |d      j-                  |
      s3d} n |s|	j%                        }t!        t/        |
            }|j#                         r|j'                         }|	|
|||fc S  y )Nc                "   | j                         j                         } t        | t              s| j                  sg n+| j
                  D cg c]  }|j                        s| c}}|rt        | }| |z  j                         }||fS y c c}w rh   )r  togetherr$  r+   rR  r  is_algebraic_exprr   )re   r  	algebraicr   r   r  s        rl   pull_out_algebraicz'_parts_rule.<locals>.pull_out_algebraic  s    $$&//1	%i;9CSCSR!*Q#33H3H3P#Q 	YAa-'')Bb5L  Rs   B#Bc                      d fd}|S )Nc                     t         fdD              rA j                  D cg c]  t        fdD              r }}|rt        | } |z  }||fS y c c}w )Nc              3  @   K   | ]  }j                  |        y wrh   r  r   r  re   s     rl   r   zI_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<genexpr>  s     79==#7r  c              3  6   K   | ]  }t        |        y wrh   )r$  )r   clsr  s     rl   r   zI_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<genexpr>  s     Iz#s3Ir  )r   r  r   )re   r  r  r   r   	functionss   ``   rl   pull_out_u_rlz6_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl  si    7Y77'0~~ KIyII  K KT
A"QBb5LKs   A)re   r   rs   ztuple[Expr, Expr] | Noneri   )r  r  s   ` rl   
pull_out_uz_parts_rule.<locals>.pull_out_u  s    	 ro   	temporaryrR   Fr   Tc              3  R   K   | ]  }t        |t        t        t        f       ! y wrh   )r$  r.   r-   r    )r   r   s     rl   r   z_parts_rule.<locals>.<genexpr>  s%      " q3S/2 "s   %'r   )rs   z*Callable[[Expr], tuple[Expr, Expr] | None])r!   r  r.   r-   r    r   r$  r'  r  r  r   is_polynomialrV   is_Derivativer,   rP   r  rq   r  rm   r  allequalsr[   )re   r  r|  r  liate_rulesdummyindexr   r   r   r   rec_dvr   dur   acceptlruler  s    `                rl   _parts_ruler    s`    c?J0F$G%z#s';c?$K
 +E)c;$:;<I%	 - 70tiEAr Q^^+AEE%Lua A"B ))!//&2I!S!2((066*a/ ))##rvv.C'D"2';<+B7F002#VVF^"KKM "aV33 Fqy,, "" "!F(4 Ei(AQqTYYua077;!%	 VVF^'f=002Ab!R//o70p ro   c                Z   | \  }|j                         \  }}t        |      }g }|r|\  }}}}}	t        dj                  |||||	             |j	                  |       t        |t              ry t        |t        t        t        t        t        f      r5|j                  t        i      }
t        |
   dkD  ry t        |
xx   dz  cc<   t        d      D ]  }t        dj                  ||||             ||z  |z  j!                         }|dk(  r n|j"                  vrdt%        ||D 	cg c]  \  }}}}}	t'        d d |||	d        c}	}}}}dt)        |      z  |z        }|dk7  r|rt+        ||z  |||      }|c S ||z  j                         \  }}t        |      }|r)|\  }}}}}	||z  }||z  }|j	                  |||||	f        n fd|rC|d   \  }}}}}	t'        ||||	 |dd  ||z              }|dk7  r|rt+        ||z  |||      }|S y c c}	}}}}w )	Nz,u : {}, dv : {}, v : {}, du : {}, v_step: {}r   rR   rb  z7Cyclic integration {} with v: {}, du: {}, integrand: {}r   c                p    | r(| d   \  }}}}}t        |||| | dd  ||z              S t        |      S rD  )r   r  )	stepsre   r   r   r   r  r   make_second_stepr  s	          rl   r  z$parts_rule.<locals>.make_second_stepI  sU    #(8 Ar1b&Y2v?OPUVWVXPY[\_a[a?bcci00ro   r   )as_coeff_Mulr  rb   r  r   r$  rS   r.   r-   r    r'   r$   xreplace_cache_dummy_parts_u_cacher  r  r  r   r   r  r   )r  re   r   r   r  r   r   r   r  r   cachekeyr9  r   r   next_constantnext_integrandr  r  s                   @@rl   
parts_ruler    s    Iv#002HiF+FE%2q"f<CCAr1bRXYZVa" a#sCt45zz6<"89Hh'!+8$)$ q 	AKRRSTVWY[]fghFi/779Ka[555&y&38: :/!RB tT1b&$? :CJ&46 Mt,X	-A68U^`deD ./V,A,A,C)M> 8F'-$2q"f]"m#aQF341	41 $Qx2q"fFAr6;KERSRTIWX[]W];^_Mt$X	%968YX\]D 3:s   3H$c           	     j   | \  }}|t        |      k(  rt        ||      S |t        |      k(  rt        ||      S |t	        |      dz  k(  rt        ||      S |t        |      dz  k(  rt        ||      S t        |t              r&t        |j                   t        |j                   z  }nt        |t              r&t        |j                   t        |j                   z  }nt        |t              rN|j                  d   }t	        |      dz  t        |      t	        |      z  z   t	        |      t        |      z   z  }n_t        |t              rN|j                  d   }t        |      dz  t        |      t        |      z  z   t        |      t        |      z   z  }ny t        |||t        ||            S Nr   r   )r.   r   r-   r   r2   r   r1   r   r$  r/   r  r0   r,  r  )r  re   r  r-  r  s        rl   	trig_ruler  W  sy    IvCKy&))CKy&))CKN"	6**CKN"	6**)S!(3	+??		Is	#(3	+??		Is	#nnQ#hkCHs3x$77#hS)+		Is	#nnQ#hkCHs3x$77#hS)+	 	y&)^Iv5VWWro   c                    | \  }}|t        |      t        |      z  k(  rt        ||      S |t        |      t	        |      z  k(  rt        ||      S y rh   )r2   r/   r   r1   r0   r   r  s      rl   trig_product_ruler  s  sU     IvCK#f+--)V,,CK#f+--)V,, .ro   c                   | \  }}t        d|g      }t        d|g      }t        d|g      }|j                  |||dz  z  |z   z        }|r}||   ||   ||   }}}t        |||||      }|j                  r|j                  r ||z  dkD  }|t        j
                  u r|S |dt        |       z  t        |      z  z  }	t        | |z        }
d||
z
  z  }d||
z   z  }t        ||||
z
        t        | |d|t        ||||
z               g}||z
  x}}t        |||      }|	dk7  rt        |	|d	
      }t        |||	||      }t        ||||      }|t        j                  u r|S t        ||||f|t        j
                  fg      S t        |||d      }|dk7  rt        ||d|z  |dz  |      }t        |||t        |d      f|dfg      S t        d|g      }|j                  |||dz  z  ||z  z   |z   z        }|rf||   ||   }}|j                   ry t#        d      }||d|z  z  z   }|j%                  |||d|z  z  z
        }t'        ||      }|rt)        |||||      S y t        d|g      }|j                  ||z  |z   ||dz  z  ||z  z   |z   z        }|r||   ||   ||   ||   ||   f\  }}}}}|j                   ry ||dz  z  ||z  z   |z   }|d|z  z  }d|z  |z  |z   }| |z  |z   }t#        d      }t)        ||||t'        |dz  |            }|dk7  rt        ||z  |z  ||||z  |      }|j                   r|S t'        ||z  |      }t        ||||g      } ||z  |z  ||z  z   }!t        |||!|       S y )Nr   r-  r   r   r   r   rR   r   Fr2  r   Tr  r   r  )r   r  rZ  r  r   rF  r*   r   r   r   r   r,  ri  r3  r   r   r  r   r   r  r   )"r  re   r  r   r   r   r  general_rulepositive_condr
  r   r1r2	log_stepsr-  subnegative_steppowerr  match2r   r   
integrand2ru  r  match3denominatorconstnumer1numer2step1step2r   rewritens"                                     rl   quadratic_denom_ruler  {  s;    IvS6(#AS6(#AS6(#AOOAVq[1!456E(E!HeAha1!)VQ1=!"4"4aC!GM&##qqbz$q')*EQBqDzHF8O$BF8O$B'FF8OD*B3BrSY[abj[j@klnI 2g%I#C;MzsU;	 1)VUCQ^ _'	69mTM'$$ FlM5RUbdedjdjTk4lmm)VVR86%i1fbj%PEY,1a1IESW=0YZZS6(#A__Q!fk/AJ">"BCDFq	6!9199#J!QqS'!^^FAQqS	M:
":q1	VQ	BBS6(#A__ai!mFaK!f*0Lq0PQRFq	6!9fQiF1IM1aA99&!)ma&j014AaCQ3v:a<Q#JinQWa&@BA:%eFl;&>&+VK-?HE>>Lvk16:9fuen=<+F;,>>9fhAA
ro   c                   | \  }}t        d|g      }t        d|dg      }dx}}g g g }	}}|j                  t              D ]  }
|
j                  |
j                  }}|j
                  s||j                  vr6|j                  s y|j                  |||z  z         }|s^||   ||   }}||z  ||z  k7  s||z  j                  s y|dk(  s||z  dkD  t        j                  u r||}}|j                  |       |	j                  |       |j                  |j                          |dk(  ryt        |      }|||z  z   d|z  z  }t        d      }|j!                  t#        ||	|      D ci c]=  \  }}}|t        j$                  |z  z  ||z  t        j$                  |z  z  |||z  z  z  ? c}}}      j!                  |||z  |z
  |z        }t'        |||dz
  z  z  |z  |z  |      }|j)                         st+        |||||      }t-        |d      }|t        j                  urR|j!                  t.        j1                  |	d            }t'        ||      }t3        ||||f|t        j                  fg      }|S yc c}}}w )z*
    Substitute common (a+b*x)**(1/n)
    r   r-  r   r   NrR   r   )r   rM  r   r   r    r  r  is_Rationalr  r0  r   rF  r   qrW   r   r   ziprQ  r  rq   r   r   r  r  r3  )r  re   r   r   r   a0b0rV  qsbspow_r   r   r  a1b1q0u_xr   r  r  r   r  rG  
simplifiedrI  s                             rl   rg  rg    s    LIqS1#AS1a&!AKBBr2Es# YYd??at'8'88

1QqS5!q58Bb5BrE>"R%!7!77r"uqyQVV+BT
		"
		$&&!" 
Qw2,B19"
Cc
A..47r24F"H "H&0dAq #'q/AbDAEE!G3DQAY3N"N "H IIMaRSUWRWXZRZ\^Q^I_ [RT225b8!<G%%'9aC9"ayqvv%"b!(<=J,Z;O A|0DXYX^X^F_/`aD ("Hs   ,AI=c           
     b   | \  }t        dg      t        dg      t        ddg      t        d      }t        dd g	      }|j                  |t        z  z   d
z  z  z         |z  z        }|sy |   |   |   ||   ||   f\  }}|j                        }|y t	        d      }|r|t
        j                  u rd }nRj                  rt        |t              |z  z        }n*t        t        |t        z  z         |z  z              }dfd}	|dkD  r`|z  z   d
z  z  z   |dz   d
z  z  z  }
|
j                         t        z  z   d
z  z  z         z  } |	|
|      }t        |||      }n|dk(  r
 |	||      }ny t        |||      S )Nr   r-  r   r   r   r  r	  c                6    | j                   xr | j                  S rh   )r  is_oddr  s    rl   r  z%sqrt_quadratic_rule.<locals>.<lambda>  s    (A ro   r  r   c                $   t        z  z   dz  z  z         }| j                         }|dk  r>|dk(  r| j                         nt        j                  | j                         f\  }}|dz  z  }||z  z
  }dz  z  z   |z  }d }	d }
|dk7  rit        d      }t        dt        |      z  ||t        j                         }t        ||z  z   dz  z  z   |      }
|dk7  rt        ||z  |||
      }
|dk7  r4t        t        d|z        d      }	|dk7  rt        ||z  |d|z  |	      }	|
r3|	r1t        ||z  ||z  d      }t        ||t        ||
|	g            }|S |
xs |	}|S | j                         }t!        ||      }|S )Nr   rR   r   r   Fr   r2  )r*   rV   
all_coeffsr   rf  as_exprr   r   rl  r   r   r  r  r   r,  r   r   )
numer_polyre   rR  degr  r  ABpre_substituteconstant_steplinear_stepr   pow_ruleaddr  r   r   r   r   r   s                   rl   sqrt_quadratic_denom_rulez6sqrt_quadratic_rule.<locals>.sqrt_quadratic_denom_rule  s   QqsU1QT6\"!!8.1Qh:((*QVVZEWEWEY<ZDAq1Q3A!A#Ac!eAgu_N)-M'+KAv#J$QtAwY1qvvg>#NAq!AaC%!Q$,Q6"3An4DaN\g"hKAv 1,qw2JW\ ]6$5agq!QuWm$\M}!N*AeGeD$/	1c73PQT_anSoCp$q 	 #3m   **,F))Q1aHDro   rR   r   )r  rY   re   r   )r   r  r*   as_polyr   r   rF  r  r  rg  r  r  r,  rJ  )r  r   re   r  r	  r  f_polyrG  rI  r  r  r-  r   rH  r   r   r   r   s                 @@@@rl   r  r    s   LIqS1#AS1#AS1a&!AS	ASABCAOOAd1QqS51a4<0!334E!HeAha%(E!HDMAq!QYYq\F~a8L/	
(47A:q9*<$q1u+q.8H!+LM @ 	1uq1uQq!tV|!Qw77
&&(a!eAadFl);;	+J	B"9aGD	
b0ClOLLro   c                    | \  }}t        |t              r|j                  d   |k(  r|j                  t        k(  rt        ||      S |j                  t        k(  rt        ||      S t        d      }|j                  t        k(  rIt	        |      t        |      z  }t        |||t        |||t        |      t        d|z  ||                  S |j                  t        k(  rIt        |      t	        |      z  }t        |||t        |||t	        |      t        d|z  ||                  S |j                  t              }|j                  t        k(  rbt        |||t        |||t        |dz        t!        d|dz  dz   z  |t#        d      t"        j$                  t"        j$                                    S |j                  t&        k(  r5t        |||t        |||t        |dz        t        d|z  ||                  S y y y )Nr   r   rR   r   )r$  r"   r  rJ  r'   r   r$   r   r   r(   r,  r   r   r%   r  r&   rZ  r   rQ  r#   )r  re   r  r   r-  s        rl   hyperbolic_ruler  7  s    Iv)/0Y^^A5F&5P>>T!Iv..>>T!Iv..#J>>T!VT&\1Iy&)FAtF|^AaCQRTU=VWY Y>>T!VT&\1Iy&)FAtF|^AaCQRTU=VWY Y "))$/I~~%"9fiY4q>!!QTAX,1Q4FHI I ~~%"9fiY4q>%ac1a023 3 &) 6Q0ro   c                    t        d| g      }t        d| g      }t        d| gd g      }t        d| gd g      }||||fS )	Nr   r-  r   r   c                "    t        | t              S rh   r$  r   r  s    rl   r  zmake_wilds.<locals>.<lambda>V      *Q:P ro   r+  r	  c                "    t        | t              S rh   r  r  s    rl   r  zmake_wilds.<locals>.<lambda>W  r  ro   )r   )r  r   r   r   r	  s        rl   
make_wildsr  R  sT    S6(#AS6(#AS6(0P/QRAS6(0P/QRAaA:ro   c                v    t        |       \  }}}}t        || z        |z  t        || z        |z  z  }|||||fS rh   )r  r.   r-   r  r   r   r   r	  rA  s         rl   sincos_patternr  [  I    F#JAq!Q!F(mQQvX!11GAq!Qro   c                v    t        |       \  }}}}t        || z        |z  t        || z        |z  z  }|||||fS rh   )r  r/   r2   r  s         rl   tansec_patternr  b  r  ro   c                v    t        |       \  }}}}t        || z        |z  t        || z        |z  z  }|||||fS rh   )r  r0   r1   r  s         rl   cotcsc_patternr  i  r  ro   c                    t        d| g      }t        d| g      }t        d      }t        || z  |z         |z  }||||fS )Nr   r-  r   rr  )r   r9   )r  r   r   rr  rA  s        rl   heaviside_patternr  p  sM    S6(#AS6(#AS	A&1%)GAq!ro   c                      fd}|S )Nc                     |  S rh   ri   )r  rJ  s    rl   
uncurry_rlzuncurry.<locals>.uncurry_rlz  s    T{ro   ri   )rJ  r  s   ` rl   uncurryr  y  s    ro   c                      fd}|S )Nc           	     l    | \  }}}}}} ||||||      }||k7  rt        |||t        ||            S y rh   )r,  r  )	r  r   r   r   r	  re   r  r-  r  s	           rl   trig_rewriter_rlz'trig_rewriter.<locals>.trig_rewriter_rl  sO    (,%1aIvAq!Q	6:		!y&)^IW]=^__ "ro   ri   )r  r  s   ` rl   trig_rewriterr  ~  s    `
 ro   c                n    |j                   xr( |j                   xr |j                  xr |j                  S rh   )is_evenr0  r   r   r   r	  r  r  s         rl   r  r    s4    QYY *199 **)) ro   c                x    dt        d| z  |z        z
  dz  |dz  z  dt        d|z  |z        z   dz  |dz  z  z  S rr  )r-   r   r   r   r	  r  r  s         rl   r  r    sM    q3qs6z?':a&?QU%K'(3qs6z?':a&?QU%K%M ro   c                (    |j                   xr |dk\  S Nr  r  r  s         rl   r  r        188;NQ ro   c                ~    dt        | |z        dz  z
  |dz
  dz  z  t        | |z        z  t        ||z        |z  z  S rr  )r-   r.   r  s         rl   r  r    J    QQvX)9%9a!eq[$I$'&M%2$'&MQ$6%7 ro   c                (    |j                   xr |dk\  S r  r  r  s         rl   r  r    r  ro   c                ~    dt        ||z        dz  z
  |dz
  dz  z  t        ||z        z  t        | |z        |z  z  S rr  )r.   r-   r  s         rl   r  r    r  ro   c                (    |j                   xr |dk\  S Nrb  r  r  s         rl   r  r        AII<P!q& ro   c                    dt        ||z        dz  z   |dz  dz
  z  t        ||z        dz  z  t        | |z        |z  z  S rr  )r/   r2   r  s         rl   r  r    O    QQvX)9%9qsQw$G$'&M1$4%5$'&MQ$6%7 ro   c                    |j                   S rh   r  r  s         rl   r  r    
    188 ro   c                ~    t        | |z        dz  dz
  |dz
  dz  z  t        | |z        z  t        ||z        |z  z  S Nr   rR   )r2   r/   r  s         rl   r  r    sJ    S6]A%5%9A{$K%(6]%3%(6]a%7%8 ro   c                    |dk(  xr |dk(  S r  ri   r  s         rl   r  r    s    AF<MqAv ro   c                *    t        | |z        dz  dz
  S r	  )r2   r  s         rl   r  r    s    C&M1$4q$8 ro   c                (    |j                   xr |dk\  S r  r  r  s         rl   r  r    r  ro   c                    dt        ||z        dz  z   |dz  dz
  z  t        ||z        dz  z  t        | |z        |z  z  S rr  )r0   r1   r  s         rl   r  r    r  ro   c                    |j                   S rh   r  r  s         rl   r  r    r  ro   c                ~    t        | |z        dz  dz
  |dz
  dz  z  t        | |z        z  t        ||z        |z  z  S r	  )r1   r0   r  s         rl   r  r    sJ    S6]A%5%9A{$K$'&M%2$'&MQ$6%7 ro   c                z  	 | \  	}t        	fdt        t        fD              rt        |      \  }}}}}	j	                  |      }|sy  t        t        t        t        t        t        t        i      t        ||||fD cg c]"  }|j                  |t        j                        $ c}	|gz               S y c c}w )Nc              3  @   K   | ]  }j                  |        y wrh   r  r  s     rl   r   z#trig_sincos_rule.<locals>.<genexpr>       
09==
0r  )r   r.   r-   r  r  r  sincos_botheven_conditionsincos_bothevensincos_sinodd_conditionsincos_sinoddsincos_cosodd_conditionsincos_cosoddr<  r  r   rf  
r  r  rA  r   r   r   r	  r  r  re   s
            @rl   trig_sincos_ruler    s     Iv

0c3Z
00,V4Aq!({%#]#]
  ,-q!Q<8aUYYq!&&!8 !	" 	" 1 9s    'B8c                  	 | \  	}	j                  dt        |      z  t        |      i      	t        	fdt        t        fD              rt        |      \  }}}}}	j                  |      }|sy  t        t        t        t        t        t        t        i      t        ||||fD cg c]"  }|j                  |t         j"                        $ c}	|gz               S y c c}w )NrR   c              3  @   K   | ]  }j                  |        y wrh   r  r  s     rl   r   z#trig_tansec_rule.<locals>.<genexpr>  r  r  )r   r-   r2   r   r/   r  r  r  tansec_tanodd_conditiontansec_tanoddtansec_seceven_conditiontansec_seceventan_tansquared_conditiontan_tansquaredr<  r  r   rf  r  s
            @rl   trig_tansec_ruler#    s     Iv	CKV  I 
0c3Z
00,V4Aq!({#]$n$n
  ,-q!Q<8aUYYq!&&!8 !	" 	" 1 9s   ('C c                $  	 | \  	}	j                  dt        |      z  t        |      dt        |      z  t	        |      t        |      t        |      z  t	        |      i      	t        	fdt        t        fD              rt        |      \  }}}}}	j                  |      }|sy  t        t        t        t        t        i      t        ||||fD cg c]"  }|j                  |t         j"                        $ c}	|gz               S y c c}w )NrR   c              3  @   K   | ]  }j                  |        y wrh   r  r  s     rl   r   z#trig_cotcsc_rule.<locals>.<genexpr>  r  r  )r   r.   r1   r/   r0   r-   r   r  r  r  cotcsc_cotodd_conditioncotcsc_cotoddcotcsc_csceven_conditioncotcsc_cscevenr<  r  r   rf  r  s
            @rl   trig_cotcsc_ruler*    s     Iv	CKV	CKVFc&k!3v;  I 
0c3Z
00,V4Aq!({#]$n
  ,-q!Q<8aUYYq!&&!8 !" 	" 1 9s   'Dc                    | \  }}t        dt        d|z        g      }|j                  t        d|z        |z        }|r8dt        |      z  t        |      z  t        d|z        z  }t	        ||z  |      S y )Nr   r   r-  )r   r.   r  r-   r  )r  re   r  r   r  
sin_doubles         rl   trig_sindouble_ruler-    sy     IvS3qx=/*AOOC&M!O,Es6{]3v;.s1V8}<
i*4f== ro   c           	          t        t        t              t        t              t        t              t        t
                    |       S rh   )r^   r_   r  r#  r*  r-  r  s    rl   trig_powers_products_ruler/    s@    26),-,-,-/02 3;< <ro   c           
        | \  }}t        dd|g      }t        dd|g      }t        d      }|||dz  z  z   }|j                  |      }|D ]  }|j                  |      }	|	j	                  |t
        j                        }
|	j	                  |t
        j                        }|
j                  xr |
dkD  xs |
j                  }|j                  xr |dkD  xs |j                  }|
j                  xr |
dk  xs |
j                  }|j                  xr |dk  xs |j                  }d }|r(|r&t        |
      t        |      z  t        |      z  }d}n{|r<|r:t        |
      t        |       z  }|t        |      z  }t        || kD  ||k        }n=|r;|r9t        |
       t        |      z  }|t        |      z  }t        || kD  ||k        }|si }t        t        t        t        t         t"        fD ]?  } ||      |t         ||      dz        <   d ||      z  |t         ||      d	z        <   A |j%                  ||      j'                         }t)        ||      }|j+                  |      r'|t-        ||      z  }|j'                         }|j                  dt        |      z        }|r(|j/                  dt        |      z  t        |      i      }t1        ||      }|j3                         rt5        ||||||      c S  y )
Nr   r   r-  r   rI  r   TrR   r   )r   r   rM  r  r  r   rf  	is_numberrk  is_negativer*   r/   r.   rT   r2   r-   r1   r0   r   rN  r  r  r  r  r  rq   rH  )r  re   r  r  r  rI  target_patternmatchesr(  r  r   r   
a_positive
b_positive
a_negative
b_negativex_funcrK  r   substitutionsr  replacedsecantsr   s                           rl   trig_substitution_ruler=    s    IvS1f+&AS1f+&A'NE619_Nnn^,G 2G

>*IIa IIa {{,q1u>
{{,q1u>
{{,q1u>
{{,q1u>
*1gd1goU3F KJAwtQBx'HE
*Ffy0&82CDKJQBxQ'HE
*Ffy0&82CDK M33% A34U8d1U8Q;/067%jd1U8b>23A
 !~~ff5>>@H"8];H<<'K66#,,."--#e*5'00#e*c%j2  H )59113/	6vx+G Gc2Gro   c                    | \  }}t        |      \  }}}}|j                  |      }|r;d||   k7  r2t        ||   |      }||   ||   }}t        ||||z  |z   | |z  |      S y y r  )r  r  r  r<  )	r  re   r  rA  r   r   rr  r  r   s	            rl   heaviside_ruler?  =  s     Iv(0GQ1OOG$EeAh q62Qxq1Y&1qbdGLL	 uro   c                   | \  }}t        |j                        dk(  rt        j                  }n|j                  d   }|j                  r|dk  ry t        d|g      t        d|dg      }}|j                  d   j                  |||z  z         }|sy ||   ||   }}t        |d      }|t        j                  u rd }nt        t        ||      |      }t        |||||      }	t        ||	|      S )NrR   r   r   r-  r   )r  r  r   rf  r  r   r  r   rF  r   r:   rB  rJ  )
r  re   r   r	  r   r   r  rG  rI  rH  s
             rl   dirac_delta_rulerA  H  s    LIq
9>>aFFNN1<<1q5aS!4aV#<qANN1##AacE*E8U1XqAa8Lqvv&z!Q'7;!)Q1a8LlOLLro   c           
     L   | \  }}t        d      }t        |||      }d}|rt        d       g }|D ]L  \  }}}	t        |	|      }
|dz   }t        dj	                  ||
             |
j                         rEt        |dz
        dk7  r|j                         \  }}|
rt        ||	z  |||	|
      }
|j                  rg }g }t        |t              r|j                  }n|j                  |       |D ]O  }t        |j                        rt        t!        ||d      |      }|s3|j                  |t#        |d      f       Q |j                  |
df       t%        |	||      }
|j                  t'        |||||
             O t)        |      dkD  rt+        |||      S |r|d   S y y )Nr   r   zList of Substitution RulesrR   r  T)r   r  rb   r  r  rq   r[   r  r   r  r$  r   r  r   r   r  r  r   r3  r   r  r  )r  re   r  r   r:  r*  waysr   r   r  subruler9  rR  	piecewisecould_be_zeror(  r   s                    rl   substitution_rulerG  ^  s    Iv#JE&y&%@ME*+&3 "	J"FA{$[%8GAIE-&&ug67))+A!#++-5/K;X_`G%% "I$&M!%-(-

%,,U3 - #(6&4[DRS5TV\&]G& ) 0 0$+$&tQK2" !## $$gt_5+KKGKKiHIE"	JH t9q="9fd;;7N S ro   c                "    | j                         S rh   )r  re   r  s     rl   r  r    s    i<<> ro   c                $    | j                  |      S rh   )apartrI  s     rl   r  r    s    ioof5 ro   c                     yr  ri   rI  s     rl   r  r    s    ro   c                "    | j                         S rh   )r  rI  s     rl   r  r        i..0 ro   c                p    t        | t        t        f      xs t        fd| j                  D              S )Nc              3  \   K   | ]#  }|j                   xs |j                         % y wrh   )r   r  r  s     rl   r   z<lambda>.<locals>.<genexpr>  s*     0s]`1Xs?P?PQW?X1X0ss   ),)r$  r   r   r  r  rI  s    `rl   r  r    s-    9sCj)sS0sdmdrdr0s-s ro   c                "    | j                         S rh   expandrI  s     rl   r  r    rN  ro   c                    t        | j                  t              D ch c]  }|j                  d    c}      dkD  S c c}w rD  )r  atomsr,   r  )re   r  r   s      rl   r  r    s4    	0E FG1QVVAYGH1L Gs   =c                &    | j                  d      S )NT)trigrR  rI  s     rl   r  r    s    i..D.9 ro   c                    | d   }|j                   }|j                  }|j                  }| j                  |v r,| j                  |v rt	        |  S t        || j                        S t        |  S r  )	variablesr(  r  r  r!  r  r   )r  re   diff_variablesundifferentiated_functionintegrand_variabless        rl   derivative_ruler]    sl    I((N )3@@--??n,!8,,	8??;;X&&ro   c           	         | \  }}|j                  dt        |      z        r?|j                  dt        |      z  t        |            }t	        |||t        ||            S y r#  )r  r-   r   r2   r,  r  )r  re   r  r-  s       rl   rewrites_ruler_    sZ     IvqV}%NN1S[=#f+>	9fi	SY9Z[[ &ro   c                    t        |  S rh   )r  r  s    rl   fallback_rulera    r  ro   zdict[Expr, Expr | None]_integral_cachezdict[Expr, int]r  zc                h   | j                  t        i      }|t        v r4t        |   t        |       S t        |   j                  t              fS dt        |<   t	        |       }fdfd} t        t        t              t        t        t        t        t        t              t        t              t        t              t        t                    t        t        t        t         t"        t$        t&        t        t        t(              t        t*              t        t,              t        t              t        t              t        t.                    t0        t2        t4        t6        t8        t,        t:        t<        t>        t@        tB        tD        i            t        t        t6              t        tF              t        tI        tJ        tL        tO         |t&        t              tP              tO         |t&        t              tR              tO         |t&        tT        gtV         tX              tO         |t&        t              tZ              t\        t^                    t        tO         |t&        t              t`                    t        tb                    td              |      }t        |= |S )a  Returns the steps needed to compute an integral.

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

    This function attempts to mirror what a student would do by hand as
    closely as possible.

    SymPy Gamma uses this to provide a step-by-step explanation of an
    integral. The code it uses to format the results of this function can be
    found at
    https://github.com/sympy/sympy_gamma/blob/master/app/logic/intsteps.py.

    Examples
    ========

    >>> from sympy import exp, sin
    >>> from sympy.integrals.manualintegrate import integral_steps
    >>> from sympy.abc import x
    >>> print(repr(integral_steps(exp(x) / (1 + exp(2 * x)), x)))     # doctest: +NORMALIZE_WHITESPACE
    URule(integrand=exp(x)/(exp(2*x) + 1), variable=x, u_var=_u, u_func=exp(x),
    substep=ArctanRule(integrand=1/(_u**2 + 1), variable=_u, a=1, b=1, c=1))
    >>> print(repr(integral_steps(sin(x), x)))     # doctest: +NORMALIZE_WHITESPACE
    SinRule(integrand=sin(x), variable=x)
    >>> print(repr(integral_steps((x**2 + 3)**2, x)))     # doctest: +NORMALIZE_WHITESPACE
    RewriteRule(integrand=(x**2 + 3)**2, variable=x, rewritten=x**4 + 6*x**2 + 9,
    substep=AddRule(integrand=x**4 + 6*x**2 + 9, variable=x,
    substeps=[PowerRule(integrand=x**4, variable=x, base=x, exp=4),
    ConstantTimesRule(integrand=6*x**2, variable=x, constant=6, other=x**2,
    substep=PowerRule(integrand=x**2, variable=x, base=x, exp=2)),
    ConstantRule(integrand=9, variable=x)]))

    Returns
    =======

    rule : Rule
        The first step; most rules have substeps that must also be
        considered. These substeps can be evaluated using ``manualintegrate``
        to obtain a result.

    Nc                    | j                   }|j                  vrt        S t        t        t
        fD ]  }t        ||      s|c S  t        |      S rh   )re   r  r   r   r,   rP   r$  type)r  re   r  r  s      rl   r  zintegral_steps.<locals>.key  sT    &&	///M13GH 	C)S)
	 Iro   c                       fd}|S )Nc                4     |       }|xr t        |      S rh   )
issubclass)r  rm  r  klassess     rl   _integral_is_subclasszKintegral_steps.<locals>.integral_is_subclass.<locals>._integral_is_subclass  s    HA/Aw//ro   ri   )rj  rk  r  s   ` rl   integral_is_subclassz,integral_steps.<locals>.integral_is_subclass  s    	0 %$ro   )3r  r  rb  r  r  r^   r_   rD  r]   r   r  r  rg  r  r   r    r  r   rs  r   rv  r  r?  r  r   r]  r,   r  r9   r:   rA  rP   r$  r   r  r  r  r_  rG  r`   partial_fractions_rulecancel_ruler!   r  r  distribute_expand_ruler/  trig_expand_ruler]  r=  ra  )re   r  optionsr  r  rl  r   r  s    `     @rl   r  r    s&   Z !!6<"89H?"8$,	622 $H-66|VL  %)!Iv.H%-V'(&	*-y9J/K!"23!"679 J	(+Y7H-I!.19=Q3R!"23!"568 !9~( "6M!
  	$ 	i o&l!(c2*, (c2! (c -+-  (c2*, * # & i 4S# >PQ,-/	
2 	[-Z  [-!F\ 	!Mro   c                   t        | |      j                         }t        j                          t	        |t
              rt        |j                        dk(  r}|j                  d   d   }t	        |t              r[|j                  d   d   dk(  rF|j                  |j                  d   d   t        |j                   f|j                  d   d   df      }|S )a$  manualintegrate(f, var)

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

    Compute indefinite integral of a single variable using an algorithm that
    resembles what a student would do by hand.

    Unlike :func:`~.integrate`, var can only be a single symbol.

    Examples
    ========

    >>> from sympy import sin, cos, tan, exp, log, integrate
    >>> from sympy.integrals.manualintegrate import manualintegrate
    >>> from sympy.abc import x
    >>> manualintegrate(1 / x, x)
    log(x)
    >>> integrate(1/x)
    log(x)
    >>> manualintegrate(log(x), x)
    x*log(x) - x
    >>> integrate(log(x))
    x*log(x) - x
    >>> manualintegrate(exp(x) / (1 + exp(2 * x)), x)
    atan(exp(x))
    >>> integrate(exp(x) / (1 + exp(2 * x)))
    RootSum(4*_z**2 + 1, Lambda(_i, _i*log(2*_i + exp(x))))
    >>> manualintegrate(cos(x)**4 * sin(x), x)
    -cos(x)**5/5
    >>> integrate(cos(x)**4 * sin(x), x)
    -cos(x)**5/5
    >>> manualintegrate(cos(x)**4 * sin(x)**3, x)
    cos(x)**7/7 - cos(x)**5/5
    >>> integrate(cos(x)**4 * sin(x)**3, x)
    cos(x)**7/7 - cos(x)**5/5
    >>> manualintegrate(tan(x), x)
    -log(cos(x))
    >>> integrate(tan(x), x)
    -log(cos(x))

    See Also
    ========

    sympy.integrals.integrals.integrate
    sympy.integrals.integrals.Integral.doit
    sympy.integrals.integrals.Integral
    r   r   rR   T)r  rm   r  clearr$  r+   r  r  r   rJ  r   )r  r)  r   r6  s       rl   manualintegratert  C  s    b As#((*F&)$V[[)9Q)>{{1~a dBFKKN1$5$=[[Q"B		N3Q"D)+F Mro   N)rH  rd   rI  r   rs   rd   )r  r  )T)rs   z*tuple[Expr, Expr, Expr, Expr, Rule] | None)r  ztuple[Expr, Symbol](  r}   
__future__r   typingr   r   r   r   abcr   r	   dataclassesr
   collectionsr   collections.abcr   sympy.core.addr   sympy.core.cacher   sympy.core.containersr   sympy.core.exprr   sympy.core.functionr   sympy.core.logicr   sympy.core.mulr   sympy.core.numbersr   r   r   sympy.core.powerr   sympy.core.relationalr   r   r   sympy.core.singletonr   sympy.core.symbolr   r   r   $sympy.functions.elementary.complexesr   &sympy.functions.elementary.exponentialr    r!   %sympy.functions.elementary.hyperbolicr"   r#   r$   r%   r&   r'   r(   r)   (sympy.functions.elementary.miscellaneousr*   $sympy.functions.elementary.piecewiser+   (sympy.functions.elementary.trigonometricr,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   'sympy.functions.special.delta_functionsr9   r:   'sympy.functions.special.error_functionsr;   r<   r=   r>   r?   r@   rA   rB   rC   rD   'sympy.functions.special.gamma_functionsrE   *sympy.functions.special.elliptic_integralsrF   rG   #sympy.functions.special.polynomialsrH   rI   rJ   rK   rL   rM   rN   rO   rP   &sympy.functions.special.zeta_functionsrQ   	integralsrS   sympy.logic.boolalgrT   sympy.ntheory.factor_rU   sympy.polys.polytoolsrV   rW   rX   rY   sympy.simplify.radsimprZ   sympy.simplify.simplifyr[   sympy.solvers.solversr\   sympy.strategies.corer]   r^   r_   r`   sympy.utilities.iterablesra   sympy.utilities.miscrb   rd   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r!  r,  r1  r3  r<  rB  rH  rZ  r]  r`  rd  rh  rm  rp  rt  rw  r{  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r$  r%  ry   r;  r:  rD  rJ  r]  r  rs  rv  r  r  r  r  r  rg  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r!  r"  r(  r)  r&  r'  r  r#  r*  r-  r/  r=  r?  rA  rG  rm  rn  ro  rp  r]  r_  ra  rb  intr  r  r  rt  ri   ro   rl   <module>r     sY
  . # 7 7 # ! # #  $ &   * &  1 1   1 1 " 1 1 4 ;) ) ) 9 :F F F F I( ( ( > M   ;  # . B B + , ' F F . & 
3 
 
 s   .: . . 
1 
1 
1 	

 	
 	
 5J 5 5 Nd N N 1D 1 1& T T T" Qd Q Q" 	z3 	 	 #h # # "h " " " " " # # # "x " " #x # # 	Z 	 	 #~ # # #~ # # /j / / Z   # # # $* $ $ A* A A  FZ  F  FF 
   Rd R R 4   	@Z 	@ 	@ 	1$ 	1 	1 	 	 	
 UD U U 1D 1 1" 	(Z 	( 	( /14 /1 /1d 3 3 3 S   	:# 	: 	: '   '   '   B% B B -$ - - 3% 3 3 F* F F J  
 /U / / 3e 3 3 U   /U / / 3e 3 3 U   .j . ." 	F: 	F 	F 	F: 	F 	F ;* ; ; ?Z ? ? EJ E E EJ E E: 
20B dD$= Un0#*@j2 OQ K P	
*(?VV2MjIIXLM\~<~X8-JZ)X@MF36 	 	 	 	 	 	 	 	 	 	
 $*+   OP ""NO 89
 ""NO 89
 ##PQ 9:
 ""CD :;
 ##MN 9; ##PQ 9:
 ""CD 9:
"""*"*><:GxMM,0f ">57   #0	2 "u02 
 N9	; '\# ,.( -"-c"2 2Sz{|;ro   