o
    oh'                    @  s  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, d,e	ZeG d-d. d.ee	ZeG d/d0 d0eZeG d1d2 d2eZeG d3d4 d4eZeG d5d6 d6eZeG d7d8 d8eZeG d9d: d:eZeG d;d< d<eZeG d=d> d>eZeG d?d@ d@ee	ZeG dAdB dBeZeG dCdD dDeZeG dEdF dFeZeG dGdH dHeZeG dIdJ dJeZeG dKdL dLeZeG dMdN dNee	ZeG dOdP dPeZeG dQdR dReZeG dSdT dTeZeG dUdV dVeZeG dWdX dXeZeG dYdZ dZeZeG d[d\ d\eZeG d]d^ d^eZeG d_d` d`eZeG dadb dbeZeG dcdd ddeZeG dedf dfeZeG dgdh dheZeG didj djeZeG dkdl dleZeG dmdn dneZeG dodp dpeZeG dqdr dreZeG dsdt dteZeG dudv dvee	ZeG dwdx dxeZeG dydz dzeZeG d{d| d|eZeG d}d~ d~eZeG dd deZeG dd deZeG dd deZeG dd deZeG dd dee	ZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZeG dd deZG dd deZdd Zdd ZeKeJeIeLeMeNfZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zg Zded< g Ze,dZdd ZՐd+ddńZ֐d,ddȄZאd-d,dd˄Zdd̈́ Zِd,ddτZڐd.dd҄ZddԄ Zddք Zݐd,dd؄Zddڄ Zߐd,dd܄Zd-d,ddބZd/ddZedd Zedd Zedd Zedd Zedd Zdd Zdd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z d,ddZdd Zeɐdd dd Zeɐdd dd Zeɐdd dd Zeɐdd dd Zdd Zdd Zdd  Z	i Z
d!eҐd"< eeZd#eҐd$< e,d%Zd&d' Zd(d) Zd*S (0  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                   @  s:   e Zd ZU ded< ded< edddZedd	d
ZdS )Ruler   	integrandr   variablereturnc                 C     d S N selfri   ri   s/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/integrals/manualintegrate.pyevalM      z	Rule.evalboolc                 C  rg   rh   ri   rj   ri   ri   rl   contains_dont_knowQ   rn   zRule.contains_dont_knowNrf   r   rf   ro   )__name__
__module____qualname____annotations__r   rm   rp   ri   ri   ri   rl   rc   H   s   
 rc   c                   @     e Zd ZdZdddZdS )
AtomicRulez1A simple rule that does not depend on other rulesrf   ro   c                 C     dS )NFri   rj   ri   ri   rl   rp   Y      zAtomicRule.contains_dont_knowNrr   )rs   rt   ru   __doc__rp   ri   ri   ri   rl   rx   V       rx   c                   @  rw   )ConstantRulezintegrate(a, x)  ->  a*xrf   r   c                 C  s   | j | j S rh   )rd   re   rj   ri   ri   rl   rm   `      zConstantRule.evalNrq   rs   rt   ru   r{   rm   ri   ri   ri   rl   r}   ]   r|   r}   c                   @  s>   e Zd ZU dZded< ded< ded< ddd	ZdddZdS )ConstantTimesRulez.integrate(a*f(x), x)  ->  a*integrate(f(x), x)r   constantotherrc   substeprf   c                 C  s   | j | j  S rh   )r   r   rm   rj   ri   ri   rl   rm   k      zConstantTimesRule.evalro   c                 C  
   | j  S rh   r   rp   rj   ri   ri   rl   rp   n      
z$ConstantTimesRule.contains_dont_knowNrq   rr   rs   rt   ru   r{   rv   rm   rp   ri   ri   ri   rl   r   d   s   
 
r   c                   @  ,   e Zd ZU dZded< ded< d	ddZdS )
	PowerRulezintegrate(x**a, x)r   baser   rf   c                 C  s6   t | j| jd  | jd  t| jdft| jdfS NrQ   T)r*   r   r   r   r    rj   ri   ri   rl   rm   x   s   $zPowerRule.evalNrq   rs   rt   ru   r{   rv   rm   ri   ri   ri   rl   r   r   
   
 r   c                   @  r   )
NestedPowRulezintegrate((x**a)**b, x)r   r   r   rf   c                 C  s:   | j | j }t|| jd  t| jdf|t| j  dfS r   )r   rd   r*   r   r   r    )rk   mri   ri   rl   rm      s   zNestedPowRule.evalNrq   r   ri   ri   ri   rl   r      r   r   c                   @  .   e Zd ZU dZded< dddZdd	d
ZdS )AddRulezDintegrate(f(x) + g(x), x) -> integrate(f(x), x) + integrate(g(x), x)
list[Rule]substepsrf   r   c                 C     t dd | jD  S )Nc                 s      | ]}|  V  qd S rh   rm   .0r   ri   ri   rl   	<genexpr>       zAddRule.eval.<locals>.<genexpr>)r   r   rj   ri   ri   rl   rm         zAddRule.evalro   c                 C     t dd | jD S )Nc                 s  r   rh   rp   r   ri   ri   rl   r      r   z-AddRule.contains_dont_know.<locals>.<genexpr>)anyr   rj   ri   ri   rl   rp      r   zAddRule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r      
   
 
r   c                   @  s>   e Zd ZU dZded< ded< ded< dd	d
ZdddZdS )URulez;integrate(f(g(x))*g'(x), x) -> integrate(f(u), u), u = g(x)r   u_varr   u_funcrc   r   rf   c                 C  sP   | j  }| jjr | j \}}|dkr |t| jt| }|| j| jS )Nr   )r   rm   r   is_Powas_base_expsubsr    r   )rk   resultr   exp_ri   ri   rl   rm      s   
z
URule.evalro   c                 C  r   rh   r   rj   ri   ri   rl   rp      r   zURule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r      s   
 
	r   c                   @  sF   e Zd ZU dZded< ded< ded< ded	< dddZdddZdS )	PartsRulez@integrate(u(x)*v'(x), x) -> u(x)*v(x) - integrate(u'(x)*v(x), x)r   ur   dvrc   v_stepRule | Nonesecond_steprf   c                 C  s,   | j d usJ | j }| j| | j   S rh   )r   r   rm   r   )rk   vri   ri   rl   rm      s   
zPartsRule.evalro   c                 C  s   | j  p| jd uo| j S rh   )r   rp   r   rj   ri   ri   rl   rp      s   
zPartsRule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r      s   
 
r   c                   @  6   e Zd ZU dZded< ded< dddZdd
dZdS )CyclicPartsRulez9Apply PartsRule multiple times to integrate exp(x)*sin(x)zlist[PartsRule]parts_rulesr   coefficientrf   c                 C  sH   g }d}| j D ]}|||j |j   |d9 }qt| d| j  S )NrQ   r   )r   appendr   r   rm   r   r   )rk   r   signruleri   ri   rl   rm      s   

zCyclicPartsRule.evalro   c                 C  r   )Nc                 s  r   rh   r   r   ri   ri   rl   r      r   z5CyclicPartsRule.contains_dont_know.<locals>.<genexpr>)r   r   rj   ri   ri   rl   rp      r   z"CyclicPartsRule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r      s   
 
r   c                   @     e Zd ZdS )TrigRuleNrs   rt   ru   ri   ri   ri   rl   r          r   c                   @  rw   )SinRulezintegrate(sin(x), x) -> -cos(x)rf   r   c                 C     t | j S rh   )r,   re   rj   ri   ri   rl   rm      r~   zSinRule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  rw   )CosRulezintegrate(cos(x), x) -> sin(x)rf   r   c                 C  
   t | jS rh   )r-   re   rj   ri   ri   rl   rm      r   zCosRule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  rw   )
SecTanRulez%integrate(sec(x)*tan(x), x) -> sec(x)rf   r   c                 C  r   rh   )r1   re   rj   ri   ri   rl   rm      r   zSecTanRule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  rw   )
CscCotRulez&integrate(csc(x)*cot(x), x) -> -csc(x)rf   r   c                 C  r   rh   )r0   re   rj   ri   ri   rl   rm      r~   zCscCotRule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  rw   )Sec2Rulez!integrate(sec(x)**2, x) -> tan(x)rf   r   c                 C  r   rh   )r.   re   rj   ri   ri   rl   rm      r   zSec2Rule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  rw   )Csc2Rulez"integrate(csc(x)**2, x) -> -cot(x)rf   r   c                 C  r   rh   )r/   re   rj   ri   ri   rl   rm      r~   zCsc2Rule.evalNrq   r   ri   ri   ri   rl   r      r|   r   c                   @  r   )HyperbolicRuleNr   ri   ri   ri   rl   r      r   r   c                   @  rw   )SinhRulez integrate(sinh(x), x) -> cosh(x)rf   r   c                 C  r   rh   )r#   re   rj   ri   ri   rl   rm     r   zSinhRule.evalNrq   r   ri   ri   ri   rl   r     r|   r   c                   @  s   e Zd ZdZdd ZdS )CoshRulez integrate(cosh(x), x) -> sinh(x)c                 C  r   rh   )r&   re   rj   ri   ri   rl   rm     r   zCoshRule.evalNr   ri   ri   ri   rl   r   
  s    r   c                   @  r   )
ExpRulez integrate(a**x, x) -> a**x/ln(a)r   r   r   rf   c                 C  s   | j t| j S rh   )rd   r    r   rj   ri   ri   rl   rm     r   zExpRule.evalNrq   r   ri   ri   ri   rl   r     r   r   c                   @  s$   e Zd ZU dZded< dddZdS )	ReciprocalRulezintegrate(1/x, x) -> ln(x)r   r   rf   c                 C  r   rh   )r    r   rj   ri   ri   rl   rm      r   zReciprocalRule.evalNrq   r   ri   ri   ri   rl   r     s   
 r   c                   @  rw   )
ArcsinRulez'integrate(1/sqrt(1-x**2), x) -> asin(x)rf   r   c                 C  r   rh   )r3   re   rj   ri   ri   rl   rm   '  r   zArcsinRule.evalNrq   r   ri   ri   ri   rl   r   $  r|   r   c                   @  rw   )ArcsinhRulez'integrate(1/sqrt(1+x**2), x) -> asin(x)rf   r   c                 C  r   rh   )r(   re   rj   ri   ri   rl   rm   .  r   zArcsinhRule.evalNrq   r   ri   ri   ri   rl   r   +  r|   r   c                   @  4   e Zd ZU dZded< ded< ded< d
ddZd	S )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crf   c                 C  s`   | j | j| j| jf\}}}}tdt| t|||  ||d    | d| |  t| S )N   )r   r   r   re   r    r)   rk   r   r   r   xri   ri   rl   rm   9  s   Dz ReciprocalSqrtQuadraticRule.evalNrq   r   ri   ri   ri   rl   r   2     
 r   c                   @  s<   e Zd ZU dZded< ded< ded< ded< dd	d
ZdS )SqrtQuadraticDenomRulez(integrate(poly(x)/sqrt(a+b*x+c*x**2), x)r   r   r   r   z
list[Expr]coeffsrf   c                   sd  | j | j| j| j | jf\}}} g    tt d D ];}t d | } | ||  }|  |d   d| d | d | 8  <  |d   |d | | 8  < q! d  d }}t	||  |d   }	||| d|   }
|
dkrd}nt
td|	 dd}|
|  }tg  fdd	ttD || R  |	 | S )
Nr   rQ   r   r   F
degeneratec                 3  s,    | ]}| t  d  |   V  qdS )r   N)lenr   ir   result_coeffsr   ri   rl   r   ^  s    $z.SqrtQuadraticDenomRule.eval.<locals>.<genexpr>)r   r   r   r   copyre   ranger   r   r)   inverse_trig_ruleIntegralInform   r   )rk   r   r   r   r   ncoeffdesr   I0stepri   r   rl   rm   F  s2   &
("
zSqrtQuadraticDenomRule.evalNrq   r   ri   ri   ri   rl   r   >  s   
 r   c                   @  r   )SqrtQuadraticRulez integrate(sqrt(a+b*x+c*x**2), x)r   r   r   r   rf   c                 C  s   t t| j| jdd}| S )NFr   )sqrt_quadratic_ruler   rd   re   rm   )rk   r   ri   ri   rl   rm   i  s   zSqrtQuadraticRule.evalNrq   r   ri   ri   ri   rl   r   b  r   r   c                   @  r   )AlternativeRulez Multiple ways to do integration.r   alternativesrf   r   c                 C  s   | j d  S Nr   )r   rm   rj   ri   ri   rl   rm   s     zAlternativeRule.evalro   c                 C  r   )Nc                 s  r   rh   r   r   ri   ri   rl   r   w  r   z5AlternativeRule.contains_dont_know.<locals>.<genexpr>)r   r   rj   ri   ri   rl   rp   v  r   z"AlternativeRule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r   n  r   r   c                   @  s$   e Zd ZdZd
ddZdddZd	S )DontKnowRulezLeave the integral as is.rf   r   c                 C  s   t | j| jS rh   )rR   rd   re   rj   ri   ri   rl   rm   }  r   zDontKnowRule.evalro   c                 C  ry   NTri   rj   ri   ri   rl   rp     rz   zDontKnowRule.contains_dont_knowNrq   rr   )rs   rt   ru   r{   rm   rp   ri   ri   ri   rl   r   z  s    
r   c                   @  rw   )DerivativeRulezintegrate(f'(x), x) -> f(x)rf   r   c                 C  sd   t | jtsJ t| jj}t|D ]\}\}}|| jkr'||d f||<  nqt| jjg|R  S NrQ   )
isinstancerd   r   listvariable_count	enumeratere   expr)rk   r   r   varcountri   ri   rl   rm     s   
zDerivativeRule.evalNrq   r   ri   ri   ri   rl   r     r|   r   c                   @  r   )RewriteRulez;Rewrite integrand to another form that is easier to handle.r   	rewrittenrc   r   rf   c                 C  r   rh   )r   rm   rj   ri   ri   rl   rm     r   zRewriteRule.evalro   c                 C  r   rh   r   rj   ri   ri   rl   rp     r   zRewriteRule.contains_dont_knowNrq   rr   r   ri   ri   ri   rl   r     s   
 
r   c                   @  s   e Zd ZdZdS )CompleteSquareRulez7Rewrite a+b*x+c*x**2 to a-b**2/(4*c) + c*(x+b/(2*c))**2N)rs   rt   ru   r{   ri   ri   ri   rl   r     s    r   c                   @  s*   e Zd ZU ded< dddZddd	Zd
S )PiecewiseRulez%Sequence[tuple[Rule, bool | Boolean]]subfunctionsrf   r   c                 C  r   )Nc                 S  s   g | ]
\}}|  |fqS ri   r   r   r   condri   ri   rl   
<listcomp>  s    z&PiecewiseRule.eval.<locals>.<listcomp>)r*   r   rj   ri   ri   rl   rm     s   zPiecewiseRule.evalro   c                 C  r   )Nc                 s  s    | ]	\}}|  V  qd S rh   r   )r   r   _ri   ri   rl   r     s    z3PiecewiseRule.contains_dont_know.<locals>.<genexpr>)r   r   rj   ri   ri   rl   rp     r   z PiecewiseRule.contains_dont_knowNrq   rr   rs   rt   ru   rv   rm   rp   ri   ri   ri   rl   r     s   
 
r   c                   @  s:   e Zd ZU ded< ded< ded< dddZdd
dZdS )HeavisideRuler   hargibndrc   r   rf   c                 C  s(   | j  }t| j||| j| j  S rh   )r   rm   r8   r  r   re   r  )rk   r   ri   ri   rl   rm     s   
zHeavisideRule.evalro   c                 C  r   rh   r   rj   ri   ri   rl   rp     r   z HeavisideRule.contains_dont_knowNrq   rr   r  ri   ri   ri   rl   r    s   
 
r  c                   @  0   e Zd ZU ded< ded< ded< d	ddZdS )
DiracDeltaRuler   r   r   r   rf   c                 C  sR   | j | j| j| jf\}}}}|dkrt|||  | S t|||  |d | S Nr   rQ   )r   r   r   re   r8   r9   rk   r   r   r   r   ri   ri   rl   rm     s   zDiracDeltaRule.evalNrq   rs   rt   ru   rv   rm   ri   ri   ri   rl   r    
   
 r  c                   @  sJ   e Zd ZU ded< ded< ded< ded< ded< dd
dZdddZdS )TrigSubstitutionRuler   thetafuncr   rc   r   zbool | Booleanrestrictionrf   c                 C  s  | j | j| j}}}|t|dt| }|t|dt| }|t|dt	| }t
|t}t|dks>J |d }t|| |}t|dksQJ t|d \}}t|trs|}|}	t|d |d  }
t|d }n.t|tr|}
|}	t|d |d  }t|d }n|}|}
t|d |d  }	t|d }t|||	 ft||
|	 ft	|||
 f||fg}t| j | | jfS )NrQ   r   r   )r  r  re   r   r1   r,   r0   r-   r/   r.   r   findr+   r   r\   rY   r   r)   r3   r2   r4   r*   r   rm   trigsimpr  )rk   r  r  r   trig_functionrelationnumerdenomopposite
hypotenuseadjacentinversesubstitutionri   ri   rl   rm     s@   

zTrigSubstitutionRule.evalro   c                 C  r   rh   r   rj   ri   ri   rl   rp     r   z'TrigSubstitutionRule.contains_dont_knowNrq   rr   r  ri   ri   ri   rl   r    s   
 
'r  c                   @  r   )
ArctanRulezAintegrate(a/(b*x**2+c), x) -> a/b / sqrt(c/b) * atan(x/sqrt(c/b))r   r   r   r   rf   c                 C  sD   | j | j| j| jf\}}}}|| t||  t|t||   S rh   )r   r   r   re   r)   r4   r   ri   ri   rl   rm   	  s   (zArctanRule.evalNrq   r   ri   ri   ri   rl   r    r   r  c                   @  s   e Zd ZU ded< dS )OrthogonalPolyRuler   r   Nrs   rt   ru   rv   ri   ri   ri   rl   r    s   
 r  c                   @  (   e Zd ZU ded< ded< dddZdS )	
JacobiRuler   r   r   rf   c                 C  s   | j | j| j| jf\}}}}tdt|d |d |d | || |  t|| | df|t|df|| d |d  d || | d  t|dfS )Nr   rQ   r      )r   r   r   re   r*   rN   r   r   r
  ri   ri   rl   rm     s   :0zJacobiRule.evalNrq   r  ri   ri   ri   rl   r        
 r   c                   @      e Zd ZU ded< dddZdS )GegenbauerRuler   r   rf   c                 C  sj   | j | j| j}}}tt|d |d |d|d   t|dft|d ||d  t|dftjdfS )NrQ   r   r   T)	r   r   re   r*   rM   r   rG   r   Zero)rk   r   r   r   ri   ri   rl   rm   $  s   (zGegenbauerRule.evalNrq   r  ri   ri   ri   rl   r$        
 r$  c                   @     e Zd ZdddZdS )ChebyshevTRulerf   r   c                 C  s^   | j | j}}tt|d ||d  t|d ||d   d tt|df|d d dfS )NrQ   r   T)r   re   r*   rG   r   r   rk   r   r   ri   ri   rl   rm   .  s   zChebyshevTRule.evalNrq   rs   rt   ru   rm   ri   ri   ri   rl   r(  ,      r(  c                   @  r'  )ChebyshevURulerf   r   c                 C  s:   | j | j}}tt|d ||d  t|dftjdfS r   )r   re   r*   rG   r   r   r%  r)  ri   ri   rl   rm   8  s
   zChebyshevURule.evalNrq   r*  ri   ri   ri   rl   r,  6  r+  r,  c                   @  r'  )LegendreRulerf   r   c                 C  s6   | j | j}}t|d |t|d | d| d  S NrQ   r   )r   re   rI   r)  ri   ri   rl   rm   A  s   (zLegendreRule.evalNrq   r*  ri   ri   ri   rl   r-  ?  r+  r-  c                   @  r'  )HermiteRulerf   r   c                 C  s(   | j | j}}t|d |d|d   S r.  )r   re   rJ   r)  ri   ri   rl   rm   H  s   zHermiteRule.evalNrq   r*  ri   ri   ri   rl   r/  F  r+  r/  c                   @  r'  )LaguerreRulerf   r   c                 C  s&   | j | j}}t||t|d | S r   )r   re   rK   r)  ri   ri   rl   rm   O  s   zLaguerreRule.evalNrq   r*  ri   ri   ri   rl   r0  M  r+  r0  c                   @  r#  )AssocLaguerreRuler   r   rf   c                 C  s   t | jd | jd | j S r   )rL   r   r   re   rj   ri   ri   rl   rm   X     zAssocLaguerreRule.evalNrq   r  ri   ri   ri   rl   r1  T  r&  r1  c                   @  s   e Zd ZU ded< ded< dS )IRuler   r   r   Nr  ri   ri   ri   rl   r3  \  s   
 r3  c                   @  r'  )CiRulerf   r   c                 C  s>   | j | j| j}}}t|t||  t|t||   S rh   )r   r   re   r,   r>   r-   r@   rk   r   r   r   ri   ri   rl   rm   d     (zCiRule.evalNrq   r*  ri   ri   ri   rl   r4  b  r+  r4  c                   @  r'  )ChiRulerf   r   c                 C  >   | j | j| j}}}t|t||  t|t||   S rh   )r   r   re   r#   r?   r&   rA   r5  ri   ri   rl   rm   k  r6  zChiRule.evalNrq   r*  ri   ri   ri   rl   r7  i  r+  r7  c                   @  r'  )EiRulerf   r   c                 C  s*   | j | j| j}}}t|t||  S rh   )r   r   re   r   rB   r5  ri   ri   rl   rm   r     zEiRule.evalNrq   r*  ri   ri   ri   rl   r9  p  r+  r9  c                   @  r'  )SiRulerf   r   c                 C  r8  rh   )r   r   re   r-   r>   r,   r@   r5  ri   ri   rl   rm   y  r6  zSiRule.evalNrq   r*  ri   ri   ri   rl   r;  w  r+  r;  c                   @  r'  )ShiRulerf   r   c                 C  r8  rh   )r   r   re   r&   r?   r#   rA   r5  ri   ri   rl   rm     r6  zShiRule.evalNrq   r*  ri   ri   ri   rl   r<  ~  r+  r<  c                   @  r'  )LiRulerf   r   c                 C  s*   | j | j| j}}}t|| | | S rh   )r   r   re   rC   r5  ri   ri   rl   rm     r:  zLiRule.evalNrq   r*  ri   ri   ri   rl   r=    r+  r=  c                   @  r  )
ErfRuler   r   r   r   rf   c                 C  s  | j | j| j| jf\}}}}|jrhtttjt|  d t	||d d|    t
d| | | dt|    |dk fttjt| d t	||d d|    td| | | dt|   dfS ttjt| d t	||d d|    td| | | dt|   S )Nr   r!  r   r   T)r   r   r   re   is_extended_realr*   r)   r   Pir   r:   r;   r   ri   ri   rl   rm     s"   . ,,zErfRule.evalNrq   r  ri   ri   ri   rl   r>    r  r>  c                   @  r  )
FresnelCRuler   r   r   r   rf   c                 C  s   | j | j| j| jf\}}}}ttjtd|  t|d d|  | td| | | td| tj   t	|d d|  | t
d| | | td| tj     S Nr   r!  )r   r   r   re   r)   r   r@  r,   r<   r-   r=   r   ri   ri   rl   rm        <<zFresnelCRule.evalNrq   r  ri   ri   ri   rl   rA    r  rA  c                   @  r  )
FresnelSRuler   r   r   r   rf   c                 C  s   | j | j| j| jf\}}}}ttjtd|  t|d d|  | td| | | td| tj   t	|d d|  | t
d| | | td| tj     S rB  )r   r   r   re   r)   r   r@  r,   r=   r-   r<   r   ri   ri   rl   rm     rC  zFresnelSRule.evalNrq   r  ri   ri   ri   rl   rD    r  rD  c                   @  r  )	PolylogRuler   r   r   rf   c                 C  s   t | jd | j| j S r   )rP   r   r   re   rj   ri   ri   rl   rm     s   zPolylogRule.evalNrq   r  ri   ri   ri   rl   rE    r"  rE  c                   @  r  )	UpperGammaRuler   r   r   rf   c                 C  sF   | j | j| j}}}|| | | |   t|d | |  | S r   )r   r   re   rD   )rk   r   r   r   ri   ri   rl   rm     s   0zUpperGammaRule.evalNrq   r  ri   ri   ri   rl   rF    r"  rF  c                   @  r  )	EllipticFRuler   r   r   rf   c                 C  s   t | j| j| j t| j S rh   )rF   re   r   r   r)   rj   ri   ri   rl   rm        zEllipticFRule.evalNrq   r  ri   ri   ri   rl   rG    r"  rG  c                   @  r  )	EllipticERuler   r   r   rf   c                 C  s   t | j| j| j t| j S rh   )rE   re   r   r   r)   rj   ri   ri   rl   rm     rH  zEllipticERule.evalNrq   r  ri   ri   ri   rl   rI    r"  rI  c                   @  s   e Zd ZU ded< ded< dS )r   r   rd   r   symbolNr  ri   ri   ri   rl   r     s   
 r   c                   s
  | j r| j d }t| tr| t|d  S t| tr)|  t|d  S t| tr;| t| t| S t| trN|  t| t| S t| tr_t fdd| j D S t| t	rt
| j dkrt| j d tr| j d t| j d   S |  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      | ]}t | V  qd S rh   )manual_diffr   argrJ  ri   rl   r         zmanual_diff.<locals>.<genexpr>rQ   )argsr   r.   diffr1   r/   r0   r   sumr   r   r   rL  )frJ  rN  ri   rO  rl   rL    s    







rL  c                   s   t |dkr|d }t|ttfr| }nt|stdnt |dkr)|g}ntdg }|D ]%\} t|trV|jd | 	fdd fdd} |
t f q1| t|| S )	zn
    A wrapper for `expr.subs(*args)` with additional logic for substitution
    of invertible functions.
    rQ   r   z(Expected an iterable of (old, new) pairsr   z$subs accepts either 1 or 2 argumentsc                   s   | j o| j kS rh   )r   r   r   )x0ri   rl   <lambda>      zmanual_subs.<locals>.<lambda>c                   s   t | j   S rh   )r   rU  )newri   rl   rW        )r   r   r   r   itemsra   
ValueErrorr    rQ  replacer   r   r   r   )r   rQ  sequencenew_subsoldri   )rY  rV  rl   manual_subs  s(   



ra  c                   s   g } fdd}d
fdd}fddt t |  D ]0}|kr,q%t|}|||}|d	urU|\}	}| krGq%||	|f}
|
|vrU||
 q%|S )Nc                   s   |dkrdS  | }t d||  t||  }|r"dS  rL|rLtfdd  D }tfdd| D }||krLdS |jddS )Nr   Fz!substituted: {}, u: {}, u_var: {}c                 3  rK  rh   rU   r   trO  ri   rl   r   -  rP  z;find_substitutions.<locals>.test_subterm.<locals>.<genexpr>c                 3  rK  rh   rb  rc  )r   ri   rl   r   .  rP  )as_Add)	rb   formatra  cancelhas_freeis_rational_functionmaxas_numer_denomas_independent)r   u_diffsubstituted
deg_before	deg_after)rd   rJ  r   ri   rl   test_subterm"  s   
z(find_substitutions.<locals>.test_subtermtermr   c                   s   g }g }t ddd gd}| tD ]#}|jd } |jvrq||  }|r0|||  q|| q|rC|tt|   |S )Nr   c                 S  s   | j S rh   )
is_Integerr   ri   ri   rl   rW  6      z:find_substitutions.<locals>.exp_subterms.<locals>.<lambda>
propertiesr   )r   r  r   rQ  free_symbolsmatchr   rW   )rr  linear_coeffstermsr   r   rN  ry  rO  ri   rl   exp_subterms3  s   

z(find_substitutions.<locals>.exp_subtermsc                   sV  t  ttgttttR r jd gS t  tt	t
ttfr& jd gS t  ttfr3 jd gS t  tr> jd gS t  trYg } jD ]}|| || qH|S t  trfdd jD } jjr| fddt jD   jjr|dd  jD  |S t  trg } jD ]}|| || q|S g S )	Nr   rQ   r      c                      g | ]	}|  r|qS ri   hasrM  rO  ri   rl   r  W      zAfind_substitutions.<locals>.possible_subterms.<locals>.<listcomp>c                   s8   g | ]}d |  k rt  jd  k rn n j| qS )rQ   )absrQ  r   )r   r   rr  ri   rl   r  Y  s    "c                 S  s   g | ]}|j r|qS ri   )r   rc  ri   ri   rl   r  \  s    
)r   r+   r!   inverse_trig_functionsr   r    r8   rQ  rG   rH   rI   rJ   rK   rM   rL   rN   r   r   extendr   rs  rT   r   is_Addr   )rr  rr   rN  )possible_subtermsrJ  r  rl   r  D  sL   








z-find_substitutions.<locals>.possible_subtermsF)rr  r   )r   dictfromkeysrL  r   r   )rd   rJ  r   resultsrq  r|  r   rm  new_integrandr   r  ri   )rd   r  rJ  r   rl   find_substitutions  s$   #



r  c                       fdd}|S )z$Strategy that rewrites an integrand.c                   sh   | \}}t d||  |  r,|  }||kr.t||}t|ts0|r2t||||S d S d S d S d S )Nz/Integral: {} is rewritten with {} on symbol: {})rb   rf  integral_stepsr   r   r   )integralrd   rJ  r   r   r`   rewriteri   rl   	_rewriterx  s   
zrewriter.<locals>._rewriterri   )r`   r  r  ri   r  rl   rewriterv     	r  c                   r  )zAStrategy that rewrites an integrand based on some other criteria.c                   sd   | \} }|\}}t d|||  | t| } | r.| }||kr0t|||t||S d S d S )Nz@Integral: {} is rewritten with {} on symbol: {} and criteria: {})rb   rf  r   r   r  )criteriar  rd   rJ  rQ  r   r  ri   rl   _proxy_rewriter  s   z'proxy_rewriter.<locals>._proxy_rewriterri   )r`   r  r  ri   r  rl   proxy_rewriter  r  r  c                       fdd}|S )z4Apply the rule that matches the condition, else Nonec                   s*      D ]\}}|| r||   S qd S rh   )r[  )r   keyr   
conditionsri   rl   multiplexer_rl  s
   z#multiplexer.<locals>.multiplexer_rlri   )r  r  ri   r  rl   multiplexer  s   r  c                    r  )zHStrategy that makes an AlternativeRule out of multiple possible results.c                   s   g }d}t d  D ]&}|d }t d|| || }|r0t|ts0|| kr0||vr0|| q
t|dkr;|d S |rXdd |D }|rOtg | |R  S tg | |R  S d S )Nr   zList of Alternative RulesrQ   Rule {}: {}c                 S  s   g | ]}|  s|qS ri   r   )r   r   ri   ri   rl   r    s    z7alternatives.<locals>._alternatives.<locals>.<listcomp>)rb   rf  r   r   r   r   r   )r  altsr   r   r   doablerulesri   rl   _alternatives  s&   
z#alternatives.<locals>._alternativesri   )r  r  ri   r  rl   r     s   r   c                 C     t |  S rh   )r}   r  ri   ri   rl   constant_rule     r  c                 C  s   | \}}|  \}}||jvr)t|tr)t|d dkr"t|||S t||||S ||jvrat|trct||||}tt	|j
rC|S t	|j
rMtd|S t|||tt	|dftd|dfgS d S d S )NrQ   r   T)r   rx  r   r   r[   r   r   r   r   r    is_zeror}   r   r   )r  rd   rJ  r   exptr   ri   ri   rl   
power_rule  s"   

r  c                 C  s0   | \}}t |jd trt||t|jd S d S r   )r   rQ  r   r   r   r  rd   rJ  ri   ri   rl   exp_rule  s   r  c                   s   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 ]6}t||rV||d}|j|  u rVt fdd|jd | D sV|| | g|jd | R    S q d S )Nr}  r   rQ   c                 3  s    | ]}|  V  qd S rh   r  )r   r   rO  ri   rl   r     rP  z'orthogonal_poly_rule.<locals>.<genexpr>)rN   r   rM   r$  rG   r(  rH   r,  rI   r-  rJ   r/  rK   r0  rL   r1  r   getrQ  r   )r  orthogonal_poly_classesorthogonal_poly_var_indexrd   klass	var_indexri   rO  rl   orthogonal_poly_rule  s.   
$r  z/list[tuple[Type, Expr, Callable | None, tuple]]_special_function_patternsr   c                   sp  | \}}t stdtgdd gd}tdtgd}tdtgd}tdtgd	d gd}td
tgdd gd}t|||||f |t | }|td  |t  | }	t tt|ddt d tftt|ddt d t	ftt
|ddt d tftt|ddt d tftt|ddt d tftdt|dd d tftt|	ddd tftt|	ddd tftt|	ddd tftt| t|t dd d tftt||t ddt d tftdt||ttddd    dd tftt||ttddd   dd tff ||t}
t D ]5\}}}}t|
|r4|
|  r4t fddtD }|d u s)|| r4|||g|R    S q d S )Nr   c                 S     | j  S rh   r  rU  ri   ri   rl   rW        z'special_function_rule.<locals>.<lambda>excluderw  r   r  r   r   c                 S  r  rh   r  rU  ri   ri   rl   rW    r  r   c                 S  s   | j o| j S rh   )is_nonnegative
is_integerrU  ri   ri   rl   rW    rZ  r   FevaluaterQ   c                 S     | |kS rh   ri   r   r   ri   ri   rl   rW    r  c                 S  r  rh   ri   r  ri   ri   rl   rW  
  r  c                 3  s(    | ]}  |d ur  |V  qd S rh   )r  )r   wry  ri   rl   r     s    z(special_function_rule.<locals>.<genexpr>) r  r   _symbol_wildsr  r   r   r9  r,   r4  r#   r7  r-   r;  r&   r<  r   r    r=  r>  rD  rA  rF  rP   rE  r)   rG  rI  r   r   ry  tuple)r  rd   rJ  r   r   r   r   r   linear_patternquadratic_pattern
_integrandtype_pattern
constraintr   	wild_valsri   r  rl   special_function_rule  sR    
r  generic_stepdegenerate_stepr   rf   c                   sl   |d u r|S t |tr fdd|jD }n| fg}t |tr&||j7 }n||tjf t|j|j|S )Nc                   s    g | ]\}}|| @   fqS ri   rZ   r   generic_condri   rl   r    s    z(_add_degenerate_step.<locals>.<listcomp>)r   r   r   r   r   truerd   re   )r  r  r  r   ri   r  rl   _add_degenerate_step  s   



r  r  c                   s   | \}t dgdt ddgd  tjG dd dt d fddz|\}}W n
  yC   Y d S w tju rLd }n	t|d}t|||}t||S )Nr   r  r   r   c                   @  r   )z nested_pow_rule.<locals>.NoMatchNr   ri   ri   ri   rl   NoMatch/  s    r  r   r   rf   tuple[Expr, Expr]c                   s  |  stjtjfS | jrG|  \}}|stjtjfS fdd|D }dd |D }|tj t|dkrE| t	dd |D  fS  | j
rd| j| j}}| rX |\}}||| fS | }	|	r|	 |	 }
}|
|  }t|d|tjfS  )	Nc                   s   g | ]} |qS ri   ri   )r   rr  )_get_base_expri   rl   r  9      z:nested_pow_rule.<locals>._get_base_exp.<locals>.<listcomp>c                 S  s   h | ]\}}|qS ri   ri   )r   r   r  ri   ri   rl   	<setcomp>:  r  z9nested_pow_rule.<locals>._get_base_exp.<locals>.<setcomp>rQ   c                 s  s    | ]\}}|V  qd S rh   ri   )r   r  r   ri   ri   rl   r   =  r   z9nested_pow_rule.<locals>._get_base_exp.<locals>.<genexpr>r   )rh  r   Oner%  is_Mulas_coeff_muldiscardr   popr   r   r   r   ry  r   )r   r  r{  r  basesr   r   base_sub_expry  r   r  r  a_b_r  r  r   ri   rl   r  2  s2   




z&nested_pow_rule.<locals>._get_base_exp)r   r   rf   r  )r   r   r  	Exceptionr}   r   r   r  )r  rd   r   r   r  r  ri   r  rl   nested_pow_rule&  s"   
r  Tc                   s  | \    \}}tdgd}tdgd}tddgd}|||  |d   s3dS d fd
d}fdd|||fD \}}}t|d}|rV|tju rYd}	n|jrdt|| }	ntt	||  | }	t
d| d dkr-| d|  ||d d|   }
}t|d}d}|tjurtdt||
 d   |
 tj}|tju r|S t |||}t|||}|jr'|jr'g }t|d| d|
ft|dk|dk ft|d|d|
ft|dk|dkffD ]\}}|tju r||   S |tjur||| |f q|r%|js||tjf t |}n|}t|||	S |tjkrAt |||}t|||	S dS )zm
    Set degenerate=False on recursive call where coefficient of quadratic term
    is assumed non-zero.
    r   r  r   r   r   r   Nrf   rc   c                   s   t d}dt|| || | d    }t|| |  }dt| }	d }
|ur0|}
|}dt|||d    }| ||}|	dkrOt|	| |	||}|
d ur[t|||
|}|dkrft ||}|S )Nr   rQ   r   r   )r   r)   r   r   r   )	RuleClassr   sign_ar   sign_chr   r   quadratic_baser   r   standard_formr   rd   rJ  ri   rl   make_inverse_trigj  s"   $
z,inverse_trig_rule.<locals>.make_inverse_trigc                      g | ]	}  |tjqS ri   r  r   r%  r   r  ri   rl   r  }  r  z%inverse_trig_rule.<locals>.<listcomp>rQ   r!  r   )rf   rc   )r   r   ry  r   r   r  r  r}   sqrt_linear_ruler   r[   r   r)   NegativeOnefalser   r  is_realr   rS   r   r   is_positiver   Halfr   )r  r   r   r   r   r   r   r  r  r  r  knon_square_condsquare_stepr  r   r  rQ  r   ri   )rd   ry  rJ  rl   r   [  s\   
$

(
" 
r   c                   s6   | \}  fdd|  D }d |v rd S t| |S )Nc                   s   g | ]}t | qS ri   )r  )r   grO  ri   rl   r    s    zadd_rule.<locals>.<listcomp>)as_ordered_termsr   )r  rd   r  ri   rO  rl   add_rule  s
   
r  c                 C  sH   | \}}| |\}}|dkr t||}|d ur"t|||||S d S d S r   )rl  r  r   )r  rd   rJ  r   rT  	next_stepri   ri   rl   mul_rule  s   
r  *tuple[Expr, Expr, Expr, Expr, Rule] | Nonec                   s   fdd}ddd}|t |t ||tt|tg}td}t| t gtR r,||  } t|D ]\}}|| }|r	|\}	}
 |	jvrL|		|sL d S |	
|d}	|

|d}
||krd|	 sd d S t|	t r|d|
 }| r|t| dkr| d S ||kr|
js|
	tst|
trt|
 }| r d S |	 }| }|	|
|||f  S d	}|d
k rd}n4||kr|
jrtdd |
jD rd}n ||d d  D ]}|| }|r|d 
|d|
rd} nq|r	|	 }tt|
 }| s	| }|	|
|||f  S q0d S )Nc                   sX   |    } t| ts| jsg n	 fdd| jD }|r*t| }| |   }||fS d S )Nc                   r~  ri   )is_algebraic_exprrM  rO  ri   rl   r    r  z;_parts_rule.<locals>.pull_out_algebraic.<locals>.<listcomp>)rg  togetherr   r*   r  rQ  r   )rd   	algebraicr   r   rO  ri   rl   pull_out_algebraic  s   z'_parts_rule.<locals>.pull_out_algebraicrf   *Callable[[Expr], tuple[Expr, Expr] | None]c                    s   d fdd}|S )Nrd   r   rf   tuple[Expr, Expr] | Nonec                   sJ   t  fddD r#fdd jD }|r#t| } | }||fS d S )Nc                 3      | ]}  |V  qd S rh   r  r   rT  rd   ri   rl   r     rP  zI_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<genexpr>c                   s&   g | ] t  fd dD r qS )c                 3  s    | ]}t  |V  qd S rh   )r   )r   clsrN  ri   rl   r     rP  zT_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<listcomp>.<genexpr>)r   )r   	functionsr	  rl   r    s    
zJ_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<listcomp>)r   rQ  r   )rd   rQ  r   r   r
  r  rl   pull_out_u_rl  s   z6_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl)rd   r   rf   r  ri   )r  r  ri   r
  rl   
pull_out_u  s   
z_parts_rule.<locals>.pull_out_u	temporaryrQ   Fr   Tc                 s  s     | ]}t |tttfV  qd S rh   )r   r-   r,   r   r   r   ri   ri   rl   r     s    z_parts_rule.<locals>.<genexpr>r   )rf   r  )r    r  r-   r,   r   r   r   r   rx  r  r   is_polynomialrU   is_Derivativer+   rO   r  rp   rR  rm   rQ  allequalsr[   )rd   rJ  r  r  liate_rulesdummyindexr   r   r   r   rec_dvr   dur   acceptlruler  ri   rO  rl   _parts_rule  sr   








r  c              
     s  | \}|  \}}t|}g }|r|\}}}}}	td|||||	 || t|tr1d S t|ttt	t
tfrR|ti}
t|
 dkrJd S t|
  d7  < tdD ]n}td|||| || |  }|dkrp nU|jvrt|dd |D dt| | }|dkr|rt|| |||}|  S ||   \}}t|}|r|\}}}}}	||9 }||9 }||||||	f qV  fd	d
 |r|d \}}}}}	t||||	 |dd  || }|dkr|rt|| |||}|S d S )Nz,u : {}, dv : {}, v : {}, du : {}, v_step: {}r   rQ   r!  z7Cyclic integration {} with v: {}, du: {}, integrand: {}c              	   S  s(   g | ]\}}}}}t d d |||d qS rh   )r   )r   r   r   r   r  r   ri   ri   rl   r  6  s    zparts_rule.<locals>.<listcomp>r   c              
     sD   | r| d \}}}}}t |||| | dd  || S t|S r	  )r   r  )stepsrd   r   r   r   r  r   make_second_steprJ  ri   rl   r  I  s   $
z$parts_rule.<locals>.make_second_stepr   )as_coeff_Mulr  rb   rf  r   r   rR   r-   r,   r   r&   r#   xreplace_cache_dummy_parts_u_cacher   rg  rx  r   r   r   r   )r  rd   r   r   r  r   r   r   r  r   cachekeyr  r   r   next_constantnext_integrandri   r  rl   
parts_rule  s\   




$r&  c                 C  sD  | \}}|t |krt||S |t|krt||S |t|d kr't||S |t|d kr4t||S t|t	rDt |j
 t|j
  }nTt|trTt|j
 t |j
  }nDt|tru|j
d }t|d t	|t|  t|t	|  }n#t|tr|j
d }t|d t|t|  t|t|  }nd S t|||t||S Nr   r   )r-   r   r,   r   r1   r   r0   r   r   r.   rQ  r/   r   r  )r  rd   rJ  r   rN  ri   ri   rl   	trig_ruleW  s2   









r(  c                 C  sH   | \}}|t |t| krt||S |t|t| kr"t||S d S rh   )r1   r.   r   r0   r/   r   r  ri   ri   rl   trig_product_rules  s   

r)  c           "      C  s  | \}}t d|gd}t d|gd}t d|gd}||||d  |  }|r|| || || }}}t|||||}|jr|jr|| dk}|tju rQ|S |dt|  t|  }	t| | }
d||
  }d||
  }t||||
 t| |d|t||||
 g}||  }}t	|||}|	dkrt
|	|d	d
}t|||	||}t||||}|tju r|S t||||f|tjfgS t|||d}|dkrt||d| |d |}t|||t|df|dfgS t d|gd}||||d  ||  |  }|r<|| || }}|jrd S td}||d|   }||||d|   }t||}|r:t|||||S d S t d|gd}||| | ||d  ||  |  }|r|| || || || || f\}}}}}|jrwd S ||d  ||  | }|d|  }d| | | }| | | }td}t||||t|d |}|dkrt|| | |||| |}|jr|S t|| |}t	||||g} || | ||  }!t|||!| S d S )Nr   r  r   r   r   r   rQ   r   Fr  r   Tr   r   r   )r   ry  r  r?  r   r  r)   r   r   r   r   r   r  r   r   r   r  r   r   r  r   )"r  rd   rJ  r   r   r   ry  general_rulepositive_condr   r   r1r2	log_stepsr   subnegative_steppowerr   match2r   r   
integrand2r  r   match3denominatorconstnumer1numer2step1step2r   rewritenri   ri   rl   quadratic_denom_rule{  s   

"
*,

r<  c                   s  | \}}t d|gd}t d|dgd}d } g g g }}}|tD ]_}	|	j|	j}
}|js5||
jvr6q$|js< dS |
|||  }|sHq$|| || }}|| |  ks^ | j	sa dS  dksn | dkt
ju rs||} ||
 || ||j q$ dkrdS t|| |  d  }td| fdd	t|||D | |   }t|d      }| st||||}t d}|t
jur|t|d}t||}t||||f|t
jfg}|S dS )
z*
    Substitute common (a+b*x)**(1/n)
    r   r  r   r   NrQ   r   c                   s<   i | ]\}}}|t j|  |  t j|  |   qS ri   )r   r  )r   r   r   qb0q0r   ri   rl   
<dictcomp>  s    .z$sqrt_linear_rule.<locals>.<dictcomp>)r   r  r   r   r   rs  rx  is_Rationalry  r  r   r  r   r=  rV   r   r   zipr  rp   r   r   r  r  r   )r  rd   r   r   r   a0r  qsbspow_r   r   ry  a1b1u_xrn  r   r   r  
simplifiedr  ri   r>  rl   r    sT   






r  c                   s  | \}t dgd t dgdt ddgdt d}t ddd	 gd
}||t   d   |  }|s@d S |  | | || || f\ }}|}|d u rad S td}|rm|tju rpd }n jrt|t |  }nt	t
|t   |  }d fdd}	|dkr|   d   |d d   }
|
 t   d    }|	|
|}t|||}n|dkr|	||}nd S t|||S )Nr   r  r   r   r   rT  r   c                 S  s   | j o| jS rh   )rs  is_oddrt  ri   ri   rl   rW        z%sqrt_quadratic_rule.<locals>.<lambda>rv  r   
numer_polyrX   rd   r   c              	     s  t    d   }|  }|dkr|dkr|  ntj|  f\}}|d  }||  }d   | }d }	d }
|dkrvtd}tdt | ||tj }t	||   d   |}
|dkrvt
|| |||
}
|dkrttd| dd}	|dkrt
|| |d| |	}	|
r|	rt|| || dd}t||t||
|	g}|S |
p|	}|S |  }t| |}|S )Nr   rQ   r   r   Fr   r  )r)   rU   
all_coeffsr   r%  as_exprr   r   r  r   r   r   r   r   r   r   r   )rN  rd   r  degr   r   ABpre_substituteconstant_steplinear_stepr   pow_ruleaddr   r   r   r   r   r   ri   rl   sqrt_quadratic_denom_rule  s8   "$z6sqrt_quadratic_rule.<locals>.sqrt_quadratic_denom_rulerQ   r   )rN  rX   rd   r   )r   ry  r)   as_polyr   r   r  r  r  r  r   rP  r   r  )r  r   rd   rT  r   ry  f_polyr  r  rZ  rN  r   r   r  ri   rY  rl   r     s:   *,

" ($
r   tuple[Expr, Symbol]c                 C  sr  | \}}t |tr|jd |kr|jtkrt||S |jtkr$t||S td}|jt	krJt|t| }t
|||t|||t|td| ||S |jtkrlt|t| }t
|||t|||t|td| ||S |t	}|jtkrt
|||t|||t	|d td|d d  |tdtjtjS |jtkrt
|||t|||t	|d td| ||S d S d S d S )Nr   r   rQ   r   )r   r!   rQ  r  r&   r   r#   r   r   r'   r   r   r   r$   r  r%   r  r   r  r"   )r  rd   rJ  r   r   ri   ri   rl   hyperbolic_rule7  s@   







"
r^  c                 C  sT   t d| gd}t d| gd}t d| gdd gd}t d| gd	d gd}||||fS )
Nr   r  r   r   c                 S  
   t | tS rh   r   r   rt  ri   ri   rl   rW  V     
 zmake_wilds.<locals>.<lambda>r  r   c                 S  r_  rh   r`  rt  ri   ri   rl   rW  W  ra  )r   )rJ  r   r   r   r   ri   ri   rl   
make_wildsR  s
   rb  c                 C  >   t | \}}}}t||  | t||  |  }|||||fS rh   )rb  r-   r,   rJ  r   r   r   r   r  ri   ri   rl   sincos_pattern[      re  c                 C  rc  rh   )rb  r.   r1   rd  ri   ri   rl   tansec_patternb  rf  rg  c                 C  rc  rh   )rb  r/   r0   rd  ri   ri   rl   cotcsc_patterni  rf  rh  c                 C  sD   t d| gd}t d| gd}t d}t||  | | }||||fS )Nr   r  r   r  )r   r8   )rJ  r   r   r  r  ri   ri   rl   heaviside_patternp  s
   ri  c                   r  )Nc                   s    |  S rh   ri   rQ  r  ri   rl   
uncurry_rlz  r  zuncurry.<locals>.uncurry_rlri   )r  rl  ri   rk  rl   uncurryy  s   rm  c                   r  )Nc                   sB   | \}}}}}} ||||||}||krt |||t||S d S rh   )r   r  )rQ  r   r   r   r   rd   rJ  r   r  ri   rl   trig_rewriter_rl  s
   z'trig_rewriter.<locals>.trig_rewriter_rlri   )r  ro  ri   rn  rl   trig_rewriter~  s   rp  c                 C  s   |j o|j o|jo|jS rh   )is_evenr  r   r   r   r   r   r   ri   ri   rl   rW    s    rW  c                 C  s@   dt d|  |  d |d  dt d| |  d |d   S r.  )r,   r   r   r   r   r   rJ  ri   ri   rl   rW    s    c                 C  s   |j o|dkS Nr}  rL  rr  ri   ri   rl   rW    rZ  c                 C  s<   dt | | d  |d d  t| |  t || |  S r.  )r,   r-   rs  ri   ri   rl   rW    
    
c                 C     |j o|dkS rt  ru  rr  ri   ri   rl   rW    rZ  c                 C  s<   dt || d  |d d  t||  t | | |  S r.  )r-   r,   rs  ri   ri   rl   rW    rv  c                 C  rw  Nr!  rq  rr  ri   ri   rl   rW    rZ  c                 C  @   dt || d  |d d  t|| d  t | | |  S r.  )r.   r1   rs  ri   ri   rl   rW    
    c                 C     |j S rh   ru  rr  ri   ri   rl   rW    ru  c                 C  <   t | | d d |d d  t| |  t || |  S Nr   rQ   )r1   r.   rs  ri   ri   rl   rW    rv  c                 C  s   |dko|dkS r'  ri   rr  ri   ri   rl   rW    rX  c                 C  s   t | | d d S r~  )r1   rs  ri   ri   rl   rW    r  c                 C  rw  rx  ry  rr  ri   ri   rl   rW    rZ  c                 C  rz  r.  )r/   r0   rs  ri   ri   rl   rW    r{  c                 C  r|  rh   ru  rr  ri   ri   rl   rW    ru  c                 C  r}  r~  )r0   r/   rs  ri   ri   rl   rW    rv  c                   s   | \ }t  fddttfD r@t|\}}}}} |s#d S ttttt	t
titfdd||||fD  |g S d S )Nc                 3  r  rh   r  r  r  ri   rl   r     rP  z#trig_sincos_rule.<locals>.<genexpr>c                   r  ri   r  r   r  ri   rl   r    r  z$trig_sincos_rule.<locals>.<listcomp>)r   r-   r,   re  ry  r  sincos_botheven_conditionsincos_bothevensincos_sinodd_conditionsincos_sinoddsincos_cosodd_conditionsincos_cosoddr  r  rJ  r  r   r   r   r   ri   rd   ry  rl   trig_sincos_rule  s$   
r  c                   s   | \ }  dt| t|i t fddttfD rMt|\}}}}} |s0d S ttt	t
tttitfdd||||fD  |g S d S )NrQ   c                 3  r  rh   r  r  r  ri   rl   r     rP  z#trig_tansec_rule.<locals>.<genexpr>c                   r  ri   r  r   r  ri   rl   r    r  z$trig_tansec_rule.<locals>.<listcomp>)r   r,   r1   r   r.   rg  ry  r  tansec_tanodd_conditiontansec_tanoddtansec_seceven_conditiontansec_seceventan_tansquared_conditiontan_tansquaredr  r  ri   r  rl   trig_tansec_rule  s*   
r  c              	     s   | \ }  dt| t|dt| t|t|t| t|i t fddttfD r]t|\}}}}} |sBd S t	t
tttitfdd||||fD  |g S d S )NrQ   c                 3  r  rh   r  r  r  ri   rl   r     rP  z#trig_cotcsc_rule.<locals>.<genexpr>c                   r  ri   r  r   r  ri   rl   r    r  z$trig_cotcsc_rule.<locals>.<listcomp>)r   r-   r0   r.   r/   r,   r   rh  ry  r  cotcsc_cotodd_conditioncotcsc_cotoddcotcsc_csceven_conditioncotcsc_cscevenr  r  ri   r  rl   trig_cotcsc_rule  s,   
r  c                 C  sj   | \}}t dtd| gd}|td| | }|r3dt| t| td|  }t|| |S d S )Nr   r   r  )r   r-   ry  r,   r  )r  rd   rJ  r   ry  
sin_doubleri   ri   rl   trig_sindouble_rule  s    r  c                 C  s"   t tttttttt| S rh   )r^   r_   r  r  r  r  r  ri   ri   rl   trig_powers_products_rule  s   r  c              	   C  s^  | \}}t dd|gd}t dd|gd}td}|||d   }||}|D ]}||}	|	|tj}
|	|tj}|
jrD|
dkpF|
j}|jrN|dkpP|j}|
jrX|
dk pZ|
j	}|jrb|dk pd|j	}d }|rz|rzt
|
t
| t| }d}n;|r|rt
|
t
|  }|t| }t|| k||k }n|r|rt
|
 t
| }|t| }t|| k||k }|r,i }ttttttfD ]}|||t
||d < d|| |t
||d	 < q||| }t||}||s,|t||9 }| }|dt| }|r|dt| t|i}t||}| s,t|||||||  S q'd S )
Nr   r   r  r   r  r   TrQ   r   )r   r   r  ry  r  r   r%  	is_numberr  is_negativer)   r.   r-   rS   r1   r,   r0   r/   r   r  ra  r  rL  r   r  rp   r  )r  rd   rJ  rR  rS  r  target_patternmatchesr   ry  r   r   
a_positive
b_positive
a_negative
b_negativex_funcr  r   substitutionsrT  replacedsecantsr   ri   ri   rl   trig_substitution_rule  sd   






r  c           	      C  sx   | \}}t |\}}}}||}|r8d|| kr:t|| |}|| || }}t|||| | | | |S d S d S r   )ri  ry  r  r  )	r  rd   rJ  r  r   r   r  ry  r   ri   ri   rl   heaviside_rule=  s   
r  c           
      C  s   | \}}t |jdkrtj}n|jd }|jr|dk rd S td|gdtd|dgd}}|jd |||  }|s=d S || || }}t|d}|tju rSd }nt	t
|||}t|||||}	t||	|S )NrQ   r   r   r  r   )r   rQ  r   r%  rs  r   ry  r   r  r}   r9   r  r  )
r  rd   r   r   r   r   ry  r  r  r  ri   ri   rl   dirac_delta_ruleH  s"   
 

r  c              	   C  sd  | \}}t d}t|||}d}|rtd g }|D ]\}}}	t|	|}
|d }td||
 |
 r5qt|d dkr| \}}|
rOt||	 |||	|
}
|j	rg }g }t
|tr_|j}n|| |D ]}t|jstt||d|}|r||t|df qf||
df t|	||}
|t|||||
 qt|dkrt|||S |r|d S d S d S )Nr   r   zList of Substitution RulesrQ   r  T)r   r  rb   r  rf  rp   r[   rk  r   rx  r   r   rQ  r   r   r  ra  r   r   r   r   r   )r  rd   rJ  r   r  r   waysr   r   rn  subruler  r  	piecewisecould_be_zeror   r   ri   ri   rl   substitution_rule^  sR   



)r  c                 C     |   S rh   )ri  r  ri   ri   rl   rW    r  c                 C  s
   |  |S rh   )apartr  ri   ri   rl   rW    ra  c                 C  ry   r   ri   r  ri   ri   rl   rW    s    c                 C  r  rh   )rg  r  ri   ri   rl   rW    r  c                   s&   t | ttfpt fdd| jD S )Nc                 3  s     | ]}|j p| V  qd S rh   )r   r  rM  rO  ri   rl   r     s    z<lambda>.<locals>.<genexpr>)r   r   r   r  rQ  r  ri   rO  rl   rW    s   &c                 C  r  rh   expandr  ri   ri   rl   rW    r  c                 C  s   t dd | tD dkS )Nc                 S  s   h | ]}|j d  qS )r   rj  r  ri   ri   rl   r    s    z<lambda>.<locals>.<setcomp>rQ   )r   atomsr+   r  ri   ri   rl   rW    r2  c                 C  s   | j ddS )NT)trigr  r  ri   ri   rl   rW    rM  c                 C  sJ   | d }|j }|j}|j}| j|v r!| j|v rt|  S t|| jS t|  S r   )	variablesr   rx  rJ  r   r   r}   )r  rd   diff_variablesundifferentiated_functionintegrand_variablesri   ri   rl   derivative_rule  s   

r  c                 C  sJ   | \}}| dt| r#|dt| t|}t|||t||S d S r   )ry  r,   r   r1   r   r  )r  rd   rJ  r   ri   ri   rl   rewrites_rule  s
   r  c                 C  r  rh   )r   r  ri   ri   rl   fallback_rule  r  r  zdict[Expr, Expr | None]_integral_cachezdict[Expr, int]r"  zc                   sv  |  ti}|tv r t| du rt| S t|  tfS dt|< t| }fdd  fd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tttttttttt t!t"ittttt#tt$t%t&t'|tt	t(t'|tt	t)t'|tt*gt+R  t,t'|tt	t-t.t/tt'|tt	t0tt1t2|}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                   s>   | j } |jvr
tS tttfD ]}t||r|  S qt|S rh   )rd   rx  r   r   r+   rO   r   type)r  rd   r  rO  ri   rl   r    s   

zintegral_steps.<locals>.keyc                    s    fdd}|S )Nc                   s    | }|o
t |S rh   )
issubclass)r  r  )r  klassesri   rl   _integral_is_subclass  s   zKintegral_steps.<locals>.integral_is_subclass.<locals>._integral_is_subclassri   )r  r  )r  )r  rl   integral_is_subclass  s   z,integral_steps.<locals>.integral_is_subclass)3r   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(  r8   r9   r  rO   r  r   r  r^  r   r  r  r`   partial_fractions_rulecancel_ruler    r  r&  distribute_expand_ruler  trig_expand_ruler  r  r  )rd   rJ  optionsr#  r  r  r   ri   )r  rJ  rl   r    s   -


-.r  c                 C  s   t | | }t  t|trCt|jdkrC|jd d }t|trC|jd d dkrC|	|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   rQ   T)r  rm   r"  clearr   r*   r   rQ  r   r  r   )rT  r   r   r   ri   ri   rl   manualintegrateC  s   1r  N)r  rc   r  r   rf   rc   )r  r   )T)rf   r  )r  r]  (  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.   r/   r0   r1   r2   r3   r4   r5   r6   r7   'sympy.functions.special.delta_functionsr8   r9   'sympy.functions.special.error_functionsr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   'sympy.functions.special.gamma_functionsrD   *sympy.functions.special.elliptic_integralsrE   rF   #sympy.functions.special.polynomialsrG   rH   rI   rJ   rK   rL   rM   rN   rO   &sympy.functions.special.zeta_functionsrP   	integralsrR   sympy.logic.boolalgrS   sympy.ntheory.factor_rT   sympy.polys.polytoolsrU   rV   rW   rX   sympy.simplify.radsimprY   sympy.simplify.simplifyr[   sympy.solvers.solversr\   sympy.strategies.corer]   r^   r_   r`   sympy.utilities.iterablesra   sympy.utilities.miscrb   rc   rx   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  r  r  r  r  r   r$  r(  r,  r-  r/  r0  r1  r3  r4  r7  r9  r;  r<  r=  r>  rA  rD  rE  rF  rG  rI  r   rL  ra  r  r  r  r  r  r   r  r  r  r  r  rv   r  r  r  r  r  r   r  r  r  r&  r(  r)  r<  r  r   r^  rb  re  rg  rh  ri  rm  rp  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  r  intr"  r!  r  r  ri   ri   ri   rl   <module>   s   (<0,	#	2		!W+5L_?M,C




<3~