U
    9%e&                    @  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 Zd,dd,dddÄZddĜddƄZאd*ddĜddɄZdd˄ ZddĜdd̈́ZddϜddфZddӄ ZddՄ ZddĜddׄZddل ZddĜddۄZd+ddĜdd݄ZddĜ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 Zedd Zd d Zdd Zdd Zdd Zdd	 Zd
d Zdd Z ddĜ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 (,  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                   @  sB   e Zd ZU ded< ded< eddddZeddd	d
ZdS )Ruler   	integrandr   variablereturnc                 C  s   d S N selfri   ri   ^/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/sympy/integrals/manualintegrate.pyevalM   s    z	Rule.evalboolc                 C  s   d S rh   ri   rj   ri   ri   rl   contains_dont_knowQ   s    zRule.contains_dont_knowN)__name__
__module____qualname____annotations__r   rm   ro   ri   ri   ri   rl   rc   H   s   
rc   c                   @  s   e Zd ZdZddddZdS )
AtomicRulez1A simple rule that does not depend on other rulesrn   rf   c                 C  s   dS )NFri   rj   ri   ri   rl   ro   Y   s    zAtomicRule.contains_dont_knowN)rp   rq   rr   __doc__ro   ri   ri   ri   rl   rt   V   s   rt   c                   @  s   e Zd ZdZddddZdS )ConstantRulezintegrate(a, x)  ->  a*xr   rf   c                 C  s   | j | j S rh   )rd   re   rj   ri   ri   rl   rm   `   s    zConstantRule.evalNrp   rq   rr   ru   rm   ri   ri   ri   rl   rv   ]   s   rv   c                   @  sF   e Zd ZU dZded< ded< ded< dddd	Zd
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   )ry   r{   rm   rj   ri   ri   rl   rm   k   s    zConstantTimesRule.evalrn   c                 C  s
   | j  S rh   r{   ro   rj   ri   ri   rl   ro   n   s    z$ConstantTimesRule.contains_dont_knowNrp   rq   rr   ru   rs   rm   ro   ri   ri   ri   rl   rx   d   s   
rx   c                   @  s0   e Zd ZU dZded< ded< 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.evalNrp   rq   rr   ru   rs   rm   ri   ri   ri   rl   r~   r   s   
r~   c                   @  s0   e Zd ZU dZded< ded< ddddZdS )	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.evalNr   ri   ri   ri   rl   r      s   
r   c                   @  s6   e Zd ZU dZded< ddddZddd	d
ZdS )AddRulezDintegrate(f(x) + g(x), x) -> integrate(f(x), x) + integrate(g(x), x)
list[Rule]substepsr   rf   c                 C  s   t dd | jD  S )Nc                 s  s   | ]}|  V  qd S rh   rm   .0r{   ri   ri   rl   	<genexpr>   s     zAddRule.eval.<locals>.<genexpr>)r   r   rj   ri   ri   rl   rm      s    zAddRule.evalrn   c                 C  s   t dd | jD S )Nc                 s  s   | ]}|  V  qd S rh   ro   r   ri   ri   rl   r      s     z-AddRule.contains_dont_know.<locals>.<genexpr>)anyr   rj   ri   ri   rl   ro      s    zAddRule.contains_dont_knowNr}   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dd	d
Zd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.evalrn   c                 C  s
   | j  S rh   r|   rj   ri   ri   rl   ro      s    zURule.contains_dont_knowNr}   ri   ri   ri   rl   r      s   
	r   c                   @  sN   e Zd ZU dZded< ded< ded< ded	< dd
ddZd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 k	st| j }| j| | j   S rh   )r   AssertionErrorr   rm   r   )rk   vri   ri   rl   rm      s    
zPartsRule.evalrn   c                 C  s   | j  p| jd k	o| j S rh   )r   ro   r   rj   ri   ri   rl   ro      s    
zPartsRule.contains_dont_knowNr}   ri   ri   ri   rl   r      s   
r   c                   @  s>   e Zd ZU dZded< ded< ddddZd	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.evalrn   c                 C  s   t dd | jD S )Nc                 s  s   | ]}|  V  qd S rh   r   r   ri   ri   rl   r      s     z5CyclicPartsRule.contains_dont_know.<locals>.<genexpr>)r   r   rj   ri   ri   rl   ro      s    z"CyclicPartsRule.contains_dont_knowNr}   ri   ri   ri   rl   r      s
   
r   c                   @  s   e Zd ZdS )TrigRuleNrp   rq   rr   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )SinRulezintegrate(sin(x), x) -> -cos(x)r   rf   c                 C  s   t | j S rh   )r,   re   rj   ri   ri   rl   rm      s    zSinRule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )CosRulezintegrate(cos(x), x) -> sin(x)r   rf   c                 C  s
   t | jS rh   )r-   re   rj   ri   ri   rl   rm      s    zCosRule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )
SecTanRulez%integrate(sec(x)*tan(x), x) -> sec(x)r   rf   c                 C  s
   t | jS rh   )r1   re   rj   ri   ri   rl   rm      s    zSecTanRule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )
CscCotRulez&integrate(csc(x)*cot(x), x) -> -csc(x)r   rf   c                 C  s   t | j S rh   )r0   re   rj   ri   ri   rl   rm      s    zCscCotRule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )Sec2Rulez!integrate(sec(x)**2, x) -> tan(x)r   rf   c                 C  s
   t | jS rh   )r.   re   rj   ri   ri   rl   rm      s    zSec2Rule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )Csc2Rulez"integrate(csc(x)**2, x) -> -cot(x)r   rf   c                 C  s   t | j S rh   )r/   re   rj   ri   ri   rl   rm      s    zCsc2Rule.evalNrw   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdS )HyperbolicRuleNr   ri   ri   ri   rl   r      s   r   c                   @  s   e Zd ZdZddddZdS )SinhRulez integrate(sinh(x), x) -> cosh(x)r   rf   c                 C  s
   t | jS rh   )r#   re   rj   ri   ri   rl   rm     s    zSinhRule.evalNrw   ri   ri   ri   rl   r     s   r   c                   @  s   e Zd ZdZdd ZdS )CoshRulez integrate(cosh(x), x) -> sinh(x)c                 C  s
   t | jS rh   )r&   re   rj   ri   ri   rl   rm     s    zCoshRule.evalNrw   ri   ri   ri   rl   r   
  s   r   c                   @  s0   e Zd ZU dZded< ded< ddddZdS )	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     s    zExpRule.evalNr   ri   ri   ri   rl   r     s   
r   c                   @  s(   e Zd ZU dZded< ddddZdS )ReciprocalRulezintegrate(1/x, x) -> ln(x)r   r   rf   c                 C  s
   t | jS rh   )r    r   rj   ri   ri   rl   rm      s    zReciprocalRule.evalNr   ri   ri   ri   rl   r     s   
r   c                   @  s   e Zd ZdZddddZdS )
ArcsinRulez'integrate(1/sqrt(1-x**2), x) -> asin(x)r   rf   c                 C  s
   t | jS rh   )r3   re   rj   ri   ri   rl   rm   '  s    zArcsinRule.evalNrw   ri   ri   ri   rl   r   $  s   r   c                   @  s   e Zd ZdZddddZdS )ArcsinhRulez'integrate(1/sqrt(1+x**2), x) -> asin(x)r   rf   c                 C  s
   t | jS rh   )r(   re   rj   ri   ri   rl   rm   .  s    zArcsinhRule.evalNrw   ri   ri   ri   rl   r   +  s   r   c                   @  s8   e Zd ZU dZded< ded< ded< 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    z ReciprocalSqrtQuadraticRule.evalNr   ri   ri   ri   rl   r   2  s
   
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	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                   sb  | j | j| j| j | jf\}}} g    tt d D ]v}t d | } | ||  }|  |d   d| d | d | 8  <  |d   |d | | 8  < qB d  d  }}t	||  |d   }	||| d|   }
|
dkrd}n"t
td|	 dd}|
|  }t fdd	ttD || f |	 | S )
Nr   rQ   r   r   F
degeneratec                 3  s*   | ]"}| t  d  |   V  qdS )r   N)lenr   ir   Z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sry   ZI0stepri   r   rl   rm   F  s2    &
("

zSqrtQuadraticDenomRule.evalNr   ri   ri   ri   rl   r   >  s   
r   c                   @  s8   e Zd ZU dZded< ded< ded< ddddZd	S )
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.evalNr   ri   ri   ri   rl   r   b  s
   
r   c                   @  s6   e Zd ZU dZded< ddddZddd	d
ZdS )AlternativeRulez Multiple ways to do integration.r   alternativesr   rf   c                 C  s   | j d  S Nr   )r   rm   rj   ri   ri   rl   rm   s  s    zAlternativeRule.evalrn   c                 C  s   t dd | jD S )Nc                 s  s   | ]}|  V  qd S rh   r   r   ri   ri   rl   r   w  s     z5AlternativeRule.contains_dont_know.<locals>.<genexpr>)r   r   rj   ri   ri   rl   ro   v  s    z"AlternativeRule.contains_dont_knowNr}   ri   ri   ri   rl   r   n  s   
r   c                   @  s,   e Zd ZdZddddZddddZd	S )
DontKnowRulezLeave the integral as is.r   rf   c                 C  s   t | j| jS rh   )rR   rd   re   rj   ri   ri   rl   rm   }  s    zDontKnowRule.evalrn   c                 C  s   dS NTri   rj   ri   ri   rl   ro     s    zDontKnowRule.contains_dont_knowN)rp   rq   rr   ru   rm   ro   ri   ri   ri   rl   r   z  s   r   c                   @  s   e Zd ZdZddddZdS )DerivativeRulezintegrate(f'(x), x) -> f(x)r   rf   c                 C  sb   t | jtstt| jj}t|D ]*\}\}}|| jkr$||d f||<  qPq$t| jjf| S NrQ   )	
isinstancerd   r   r   listvariable_count	enumeratere   expr)rk   r   r   varcountri   ri   rl   rm     s    
zDerivativeRule.evalNrw   ri   ri   ri   rl   r     s   r   c                   @  s>   e Zd ZU dZded< ded< ddddZd	dd
dZdS )RewriteRulez;Rewrite integrand to another form that is easier to handle.r   	rewrittenrc   r{   rf   c                 C  s
   | j  S rh   )r{   rm   rj   ri   ri   rl   rm     s    zRewriteRule.evalrn   c                 C  s
   | j  S rh   r|   rj   ri   ri   rl   ro     s    zRewriteRule.contains_dont_knowNr}   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)rp   rq   rr   ru   ri   ri   ri   rl   r     s   r   c                   @  s2   e Zd ZU ded< ddddZdddd	Zd
S )PiecewiseRulez%Sequence[tuple[Rule, bool | Boolean]]subfunctionsr   rf   c                 C  s   t dd | jD  S )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.evalrn   c                 C  s   t dd | jD S )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   ro     s    z PiecewiseRule.contains_dont_knowNrp   rq   rr   rs   rm   ro   ri   ri   ri   rl   r     s   
r   c                   @  sB   e Zd ZU ded< ded< ded< ddddZd	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.evalrn   c                 C  s
   | j  S rh   r|   rj   ri   ri   rl   ro     s    z HeavisideRule.contains_dont_knowNr   ri   ri   ri   rl   r     s
   
r   c                   @  s4   e Zd ZU ded< ded< ded< ddddZdS )	DiracDeltaRuler   r   r   r   rf   c                 C  sR   | j | j| j| jf\}}}}|dkr8t|||  | S t|||  |d | S Nr   rQ   )r   r   r   re   r8   r9   rk   r   r   r   r   ri   ri   rl   rm     s    zDiracDeltaRule.evalNrp   rq   rr   rs   rm   ri   ri   ri   rl   r     s   
r   c                   @  sR   e Zd ZU ded< ded< ded< ded< ded< dd	d
dZd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|t|d }t|| |}t|dkstt|d \}}t|tr|}|}	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   r\   rY   r   r)   r3   r2   r4   r*   r{   rm   trigsimpr   )rk   r   r   r   Ztrig_functionZrelationZnumerdenomZoppositeZ
hypotenuseadjacentZinversesubstitutionri   ri   rl   rm     s@    
zTrigSubstitutionRule.evalrn   c                 C  s
   | j  S rh   r|   rj   ri   ri   rl   ro     s    z'TrigSubstitutionRule.contains_dont_knowNr   ri   ri   ri   rl   r     s   
'r   c                   @  s8   e Zd ZU dZded< ded< ded< ddddZd	S )

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.evalNr   ri   ri   ri   rl   r     s
   
r   c                   @  s   e Zd ZU ded< dS )OrthogonalPolyRuler   r   Nrp   rq   rr   rs   ri   ri   ri   rl   r     s   
r   c                   @  s,   e Zd ZU ded< ded< 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    :0zJacobiRule.evalNr   ri   ri   ri   rl   r     s   
r   c                   @  s$   e Zd ZU ded< 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.evalNr   ri   ri   ri   rl   r      s   
r   c                   @  s   e Zd ZddddZdS )ChebyshevTRuler   rf   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.evalNrp   rq   rr   rm   ri   ri   ri   rl   r   ,  s   r   c                   @  s   e Zd ZddddZdS )ChebyshevURuler   rf   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.evalNr   ri   ri   ri   rl   r  6  s   r  c                   @  s   e Zd ZddddZdS )LegendreRuler   rf   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.evalNr   ri   ri   ri   rl   r  ?  s   r  c                   @  s   e Zd ZddddZdS )HermiteRuler   rf   c                 C  s(   | j | j }}t|d |d|d   S r  )r   re   rJ   r   ri   ri   rl   rm   H  s    zHermiteRule.evalNr   ri   ri   ri   rl   r  F  s   r  c                   @  s   e Zd ZddddZdS )LaguerreRuler   rf   c                 C  s&   | j | j }}t||t|d | S r   )r   re   rK   r   ri   ri   rl   rm   O  s    zLaguerreRule.evalNr   ri   ri   ri   rl   r  M  s   r  c                   @  s$   e Zd ZU ded< ddddZdS )AssocLaguerreRuler   r   rf   c                 C  s   t | jd | jd | j S r   )rL   r   r   re   rj   ri   ri   rl   rm   X  s    zAssocLaguerreRule.evalNr   ri   ri   ri   rl   r  T  s   
r  c                   @  s   e Zd ZU ded< ded< dS )IRuler   r   r   Nr   ri   ri   ri   rl   r  \  s   
r  c                   @  s   e Zd ZddddZdS )CiRuler   rf   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  s    zCiRule.evalNr   ri   ri   ri   rl   r  b  s   r  c                   @  s   e Zd ZddddZdS )ChiRuler   rf   c                 C  s>   | j | j| j  }}}t|t||  t|t||   S rh   )r   r   re   r#   r?   r&   rA   r	  ri   ri   rl   rm   k  s    zChiRule.evalNr   ri   ri   ri   rl   r
  i  s   r
  c                   @  s   e Zd ZddddZdS )EiRuler   rf   c                 C  s*   | j | j| j  }}}t|t||  S rh   )r   r   re   r   rB   r	  ri   ri   rl   rm   r  s    zEiRule.evalNr   ri   ri   ri   rl   r  p  s   r  c                   @  s   e Zd ZddddZdS )SiRuler   rf   c                 C  s>   | j | j| j  }}}t|t||  t|t||   S rh   )r   r   re   r-   r>   r,   r@   r	  ri   ri   rl   rm   y  s    zSiRule.evalNr   ri   ri   ri   rl   r  w  s   r  c                   @  s   e Zd ZddddZdS )ShiRuler   rf   c                 C  s>   | j | j| j  }}}t|t||  t|t||   S rh   )r   r   re   r&   r?   r#   rA   r	  ri   ri   rl   rm     s    zShiRule.evalNr   ri   ri   ri   rl   r  ~  s   r  c                   @  s   e Zd ZddddZdS )LiRuler   rf   c                 C  s*   | j | j| j  }}}t|| | | S rh   )r   r   re   rC   r	  ri   ri   rl   rm     s    zLiRule.evalNr   ri   ri   ri   rl   r    s   r  c                   @  s4   e Zd ZU ded< ded< ded< ddddZdS )	ErfRuler   r   r   r   rf   c                 C  s  | j | j| j| jf\}}}}|jrtttj|  d t	||d d|    t
d| | | dt|    |dk fttj| d t	||d d|    td| | | dt|   dfS ttj| 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.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s4   e Zd ZU ded< ded< ded< ddddZdS )	FresnelCRuler   r   r   r   rf   c                 C  s   | j | j| j| jf\}}}}ttj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     s    <<zFresnelCRule.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s4   e Zd ZU ded< ded< ded< ddddZdS )	FresnelSRuler   r   r   r   rf   c                 C  s   | j | j| j| jf\}}}}ttjd|  t|d d|  | td| | | td| tj   t	|d d|  | t
d| | | td| tj     S r  )r   r   r   re   r)   r   r  r,   r=   r-   r<   r   ri   ri   rl   rm     s    <<zFresnelSRule.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s,   e Zd ZU ded< ded< ddddZdS )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.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s,   e Zd ZU ded< ded< ddddZdS )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    zUpperGammaRule.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s,   e Zd ZU ded< ded< ddddZdS )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     s    zEllipticFRule.evalNr   ri   ri   ri   rl   r    s   
r  c                   @  s,   e Zd ZU ded< ded< ddddZdS )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     s    zEllipticERule.evalNr   ri   ri   ri   rl   r    s   
r  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r2| t|d  S t| trT|  t|d  S t| trx| t| t| S t| tr|  t| t| S t| trt fdd| j D S t| t	rt
| j dkrt| 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                   s   g | ]}t | qS ri   )manual_diffr   argr  ri   rl   r     s     zmanual_diff.<locals>.<listcomp>rQ   )argsr   r.   diffr1   r/   r0   r   sumr   r   r   r  )fr  r  ri   r  rl   r    s     





"r  c                   s   t |dkr>|d }t|ttfr,| }qZt|sZtdnt |dkrR|g}ntdg }|D ]J\} t|trb|jd | 	fdd fdd} |
t f qb| 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   r"  )newri   rl   r$    r%  )r   r   r   r   itemsra   
ValueErrorr    r  replacer   r   r   r   )r   r  sequenceZnew_subsoldri   )r&  r#  rl   manual_subs  s$    




r,  c                   s   g } fdd}ddfdd}fddt t |  D ]`}|kr\qNt|}|||}|d	k	rN|\}	}| krqN||	|f}
|
|krN||
 qN|S )
Nc                   s   |dkrdS  | }t d||  t||  }|rDdS  r|rtfdd  D }tfdd| D }||krdS |jddS )Nr   Fz!substituted: {}, u: {}, u_var: {}c                   s   g | ]}t | qS ri   rU   r   tr  ri   rl   r   -  s     z<find_substitutions.<locals>.test_subterm.<locals>.<listcomp>c                   s   g | ]}t | qS ri   r-  r.  )r   ri   rl   r   .  s     )Zas_Add)	rb   formatr,  cancelhas_freeis_rational_functionmaxas_numer_denomas_independent)r   u_diffsubstitutedZ
deg_beforeZ	deg_after)rd   r  r   ri   rl   test_subterm"  s    
z(find_substitutions.<locals>.test_subtermr   termc                   s   g }g }t ddd gd}| tD ]F}|jd } |jkr>q$||  }|r`|||  q$|| q$|r|tt|   |S )Nr   c                 S  s   | j S rh   )
is_Integerr   ri   ri   rl   r$  6  r%  z:find_substitutions.<locals>.exp_subterms.<locals>.<lambda>
propertiesr   )r   r   r   r  free_symbolsmatchr   rW   )r;  Zlinear_coeffstermsr   r   r  rA  r  ri   rl   exp_subterms3  s    

z(find_substitutions.<locals>.exp_subtermsc                   sZ  t  ttfttttfr& jd gS t  tt	t
ttfrF jd gS t  ttfr` jd gS t  trv jd gS t  trg } jD ]}|| || q|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rVg } jD ]}|| || q2|S g S )	Nr   rQ   r      c                   s   g | ]}|  r|qS ri   hasr  r  ri   rl   r   W  s     
 zAfind_substitutions.<locals>.possible_subterms.<locals>.<listcomp>c                   s8   g | ]0}d |  k r&t  jd  k rn q j| qS )rQ   )absr  r   )r   r   r:  ri   rl   r   Y  s
     
 c                 S  s   g | ]}|j r|qS ri   )r   r.  ri   ri   rl   r   \  s    )r   r+   r!   inverse_trig_functionsr   r    r8   r  rG   rH   rI   rJ   rK   rM   rL   rN   r   r   extendr   r<  rT   r   Zis_Addr   )r;  rr   r  )possible_subtermsr  r:  rl   rK  D  sJ    
    







z-find_substitutions.<locals>.possible_subtermsF)r   dictfromkeysr  r   r   )rd   r  r   resultsr9  rC  r   r7  Znew_integrandry   r   ri   )rd   rK  r  r   rl   find_substitutions  s"    #


rO  c                   s    fdd}|S )z$Strategy that rewrites an integrand.c                   s\   | \}}t d||  |  rX|  }||krXt||}t|tsX|rXt||||S d S )Nz/Integral: {} is rewritten with {} on symbol: {})rb   r0  integral_stepsr   r   r   )integralrd   r  r   r{   r`   rewriteri   rl   	_rewriterx  s    
zrewriter.<locals>._rewriterri   )r`   rS  rT  ri   rR  rl   rewriterv  s    	rU  c                   s    fdd}|S )zAStrategy that rewrites an integrand based on some other criteria.c                   s`   | \} }|\}}t d|||  | t| } | r\| }||kr\t|||t||S d S )Nz@Integral: {} is rewritten with {} on symbol: {} and criteria: {})rb   r0  r   r   rP  )criteriarQ  rd   r  r  r   rR  ri   rl   _proxy_rewriter  s    z'proxy_rewriter.<locals>._proxy_rewriterri   )r`   rS  rW  ri   rR  rl   proxy_rewriter  s    	rX  c                   s    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   rZ  rl   multiplexer  s    r]  c                    s    fdd}|S )zHStrategy that makes an AlternativeRule out of multiple possible results.c                   s   g }d}t d  D ]L}|d }t d|| || }|rt|ts|| kr||kr|| qt|dkrv|d S |rdd |D }|rt| |f S t| |f 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   r0  r   r   r   r   r   )rQ  Zaltsr   r   r   Zdoablerulesri   rl   _alternatives  s(    z#alternatives.<locals>._alternativesri   )r`  ra  ri   r_  rl   r     s    r   c                 C  s   t |  S rh   )rv   rQ  ri   ri   rl   constant_rule  s    rc  c                 C  s   | \}}|  \}}||jkrRt|trRt|d dkrDt|||S t||||S ||jkrt|trt||||}tt	|j
r|S t	|j
rtd|S t|||tt	|dftd|dfgS d S )NrQ   r   T)r   r@  r   r   r[   r   r~   r   r   r    is_zerorv   r   r   )rQ  rd   r  r   Zexptr   ri   ri   rl   
power_rule  s     

re  c                 C  s0   | \}}t |jd tr,t||t|jd S d S r   )r   r  r   r   r   rQ  rd   r  ri   ri   rl   exp_rule  s    rg  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 ]j}t||r@||d}|j|  kr@t fdd|jd | D s@|| | f|jd |    S q@d S )NrD  r   rQ   c                 3  s   | ]}|  V  qd S rh   rE  )r   r   r  ri   rl   r     s     z'orthogonal_poly_rule.<locals>.<genexpr>)rN   r   rM   r   rG   r   rH   r  rI   r  rJ   r  rK   r  rL   r  r   getr  r   )rQ  Zorthogonal_poly_classesZorthogonal_poly_var_indexrd   klassZ	var_indexri   r  rl   orthogonal_poly_rule  s@               
rj  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 ]h\}}}}t|
|r|
|  rt fddtD }|d ksT|| r|||f|   S qd S )Nr   c                 S  s   | j  S rh   rd  r"  ri   ri   rl   r$    r%  z'special_function_rule.<locals>.<lambda>excluder?  r   rn  r   r   c                 S  s   | j  S rh   rl  r"  ri   ri   rl   r$    r%  r   c                 S  s   | j o
| j S rh   )is_nonnegative
is_integerr"  ri   ri   rl   r$    r%  r   FevaluaterQ   c                 S  s   | |kS rh   ri   r   r   ri   ri   rl   r$    r%  c                 S  s   | |kS rh   ri   rt  ri   ri   rl   r$  
  r%  c                 3  s&   | ]}  |d k	r  |V  qd S rh   )rh  )r   wrA  ri   rl   r     s    z(special_function_rule.<locals>.<genexpr>) rk  r   _symbol_wildsrI  r   r   r  r,   r  r#   r
  r-   r  r&   r  r   r    r  r  r  r  r  rP   r  r)   r  r  r   r   rA  tuple)rQ  rd   r  r   r   r   r   r   Zlinear_patternZquadratic_patternZ
_integrandtype_pattern
constraintr   Z	wild_valsri   rv  rl   special_function_rule  sR       
r}  r   )generic_stepdegenerate_steprg   c                   sl   |d kr|S t |tr, fdd|jD }n
| fg}t |trL||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  rb  c                   s   | \}t dgdt ddgd  tjG dd dt ddd	 fd
dz|\}}W n  k
r   Y d S X tjkrd }nt|d}t|||}t||S )Nr   ro  r   r   c                   @  s   e Zd ZdS )z nested_pow_rule.<locals>.NoMatchNr   ri   ri   ri   rl   NoMatch/  s   r  r   ztuple[Expr, Expr])r   rg   c                   s  |  stjtjfS | jr|  \}}|s8tjtjfS fdd|D }dd |D }|tj t|dkr| t	dd |D  fS  | j
r| j| j }}| r |\}}||| fS | }	|	r
|	 |	  }
}|
|  }t|d|tjfS  d S )	Nc                   s   g | ]} |qS ri   ri   )r   r;  )_get_base_expri   rl   r   9  s     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>:  s     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   =  s     z9nested_pow_rule.<locals>._get_base_exp.<locals>.<genexpr>r   )r2  r   Oner   is_MulZas_coeff_muldiscardr   popr   r   r   r   rA  r   )r   r   rB  rN  basesr   r   Zbase_Zsub_exprA  r   r  r  Za_Zb_r  r{  r   ri   rl   r  2  s2    




z&nested_pow_rule.<locals>._get_base_exp)r   r   r  	Exceptionrv   r   r   r  )rQ  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   sfdS dd	 fd
d}fdd|||fD \}}}t|d}|r|tjkrd}	n0|jrt|| }	ntt	||  | }	t
d| d dkrf| d|  ||d d|    }
}t|d}d}|tjk	rbtdt||
 d   |
 tj}|tjkrr|S t |||}t|||}|jrZ|jrZg }t|d| d|
ft|dk|dk ft|d|d|
ft|dk|dkffD ]@\}}|tjkr||   S |tjk	r||| |f q|rV|jsH||tjf t |}n|}t|||	S |tjkrt |||}t|||	S dS )zm
    Set degenerate=False on recursive call where coefficient of quadratic term
    is assumed non-zero.
    r   ro  r   r   r   r   Nrc   rf   c                   s   t d}dt|| || | d    }t|| |  }dt| }	d }
|k	r`|}
|}dt|||d    }| ||}|	dkrt|	| |	||}|
d k	rt|||
|}|dkrt ||}|S )Nr   rQ   r   r   )r   r)   rx   r   r   )Z	RuleClassr   Zsign_ar   Zsign_chr   r   Zquadratic_basery   r   Zstandard_formr{   rd   r  ri   rl   make_inverse_trigj  s"    $
z,inverse_trig_rule.<locals>.make_inverse_trigc                   s   g | ]}  |tjqS ri   rh  r   r   r   rv  ri   rl   r   }  s     z%inverse_trig_rule.<locals>.<listcomp>rQ   r   r   )r   r   rA  r   r   r  rd  rv   sqrt_linear_ruler   r[   r   r)   ZNegativeOnefalser   r  Zis_realr   rS   r   r   is_positiver   Halfr   )rQ  r   r   r   r   r   r   r  r  r  r  kZnon_square_condZsquare_stepr~  r   r`  r  r   ri   )rd   rA  r  rl   r   [  sX    
$
(" r   c                   s6   | \}  fdd|  D }d |kr*d S t| |S )Nc                   s   g | ]}t | qS ri   )rP  )r   gr  ri   rl   r     s   zadd_rule.<locals>.<listcomp>)Zas_ordered_termsr   )rQ  rd   rN  ri   r  rl   add_rule  s
    
r  c                 C  sD   | \}}| |\}}|dkr@t||}|d k	r@t|||||S d S r   )r6  rP  rx   )rQ  rd   r  r   r!  	next_stepri   ri   rl   mul_rule  s    
r  z*tuple[Expr, Expr, Expr, Expr, Rule] | Nonerf   c                   s0   fdd}dddd}|t |t ||tt|tg}td}t| t ftrZ||  } t|D ]\}}|| }|rb|\}	}
 |	jkr|		|s d S |	
|d}	|

|d}
||kr|	 s d S t|	t rd|
 }| rt| dkr d S ||krb|
js$|
	ts$t|
trbt|
 }| r> d S |	 }| }|	|
|||f  S d	}|d
k rvd}nv||kr|
jrtdd |
jD rd}nH||d d  D ]6}|| }|r|d 
|d|
rd} qq|rb|	 }tt|
 }| sb| }|	|
|||f  S qbd S )Nc                   sX   |    } t| ts| js g n fdd| jD }|rTt| }| |   }||fS d S )Nc                   s   g | ]}|  r|qS ri   )Zis_algebraic_exprr  r  ri   rl   r     s     
 z;_parts_rule.<locals>.pull_out_algebraic.<locals>.<listcomp>)r1  Ztogetherr   r*   r  r  r   )rd   Z	algebraicr   r   r  ri   rl   pull_out_algebraic  s    z'_parts_rule.<locals>.pull_out_algebraicz*Callable[[Expr], tuple[Expr, Expr] | None]rf   c                    s   ddd fdd}|S )Nr   ztuple[Expr, Expr] | None)rd   rg   c                   sJ   t  fddD rFfdd jD }|rFt| } | }||fS d S )Nc                 3  s   | ]}  |V  qd S rh   rE  r   r!  rd   ri   rl   r     s     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r  ri   rl   r     s     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   r  r   )rd   r  r   r   r  r  rl   pull_out_u_rl  s    z6_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rlri   )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   )r    rH  r-   r,   r   r   r   r   r@  rF  r   is_polynomialrU   Zis_Derivativer+   rO   rP  ro   r  rm   r  allequalsr[   )rd   r  r  r  Zliate_rulesdummyindexr   r   r   r   Zrec_dvr   dur   acceptZlrulerJ  ri   r  rl   _parts_rule  sr    
 








r  c              
     s  | \}|  \}}t|}g }|r|\}}}}}	td|||||	 || t|trdd S t|ttt	t
tfr|ti}
t|
 dkrd S t|
  d7  < tdD ]}td|||| || |  }|dkr q|jkr@t|dd |D dt| | }|dkr8|r8t|| |||}|  S ||   \}}t|}|r|\}}}}}	||9 }||9 }||||||	f q qq fd	d
 |r|d \}}}}}	t||||	 |dd  || }|dkr
|r
t|| |||}|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   rP  )stepsrd   r   r   r   r  r   make_second_stepr  ri   rl   r  I  s    $z$parts_rule.<locals>.make_second_stepr   )Zas_coeff_Mulr  rb   r0  r   r   rR   r-   r,   r   r&   r#   xreplace_cache_dummy_parts_u_cacher   r1  r@  r   r   rx   r   )rQ  rd   ry   r   r  r   r   r   r  r   cachekeyr   r   r   Znext_constantZnext_integrandri   r  rl   
parts_rule  sZ    



$r  c                 C  sF  | \}}|t |krt||S |t|kr4t||S |t|d krNt||S |t|d krht||S t|t	rt |j
 t|j
  }nt|trt|j
 t |j
  }nt|tr|j
d }t|d t	|t|  t|t	|  }nHt|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.   r  r/   r   rP  )rQ  rd   r  r   r  ri   ri   rl   	trig_ruleW  s2    








r  c                 C  sH   | \}}|t |t| kr&t||S |t|t| krDt||S d S rh   )r1   r.   r   r0   r/   r   rf  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kr|S |dt|  t|  }	t| | }
d||
  }d||
  }t||||
 t| |d|t||||
 g}||  }}t	|||}|	dkrVt
|	|d	d
}t|||	||}t||||}|tjkrt|S t||||f|tjfgS |S t d|gd}||||d  ||  |  }|r:|| ||  }}|jrd S td}||d|   }||||d|   }t||}|r6t|||||S d S t d|gd}||| | ||d  ||  |  }|r|| || || || || f\}}}}}|jrd S ||d  ||  | }|d|  }d| | | }| | | }td}t||||t|d |}|dkr:t|| | |||| |}|jrF|S t|| |}t	||||g}|| | ||  } t||| |S d S )Nr   ro  r   r   r   r   rQ   r   Frr  r   r   r   )r   rA  r   r  r   r  r)   r   rx   r   r   r   r  r   rd  r   r   rP  r   )!rQ  rd   r  r   r   r   rA  Zgeneral_ruleZpositive_condr   ry   r1r2Z	log_stepsr   subZnegative_stepr   Zmatch2r   r   Z
integrand2r  r   Zmatch3denominatorconstZnumer1Znumer2Zstep1Zstep2r   Z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sH||
jkrlqH|jsx dS |
|||  }|sqH|| ||  }}|| |  ks | j	s dS  dks܈ | dkt
jkr|| } ||
 || ||j qH dkrdS t|| |  d  }td| fdd	t|||D | |   }t|d      }| st||||}t d}|t
jk	r|d
d	 |D }t||}t||||f|t
jfg}|S dS )z*
    Substitute common (a+b*x)**(1/n)
    r   ro  r   r   NrQ   r   c                   s<   i | ]4\}}}|t j|  |  t j|  |   qS ri   )r   r  )r   r   r   qZb0q0r   ri   rl   
<dictcomp>  s    z$sqrt_linear_rule.<locals>.<dictcomp>c                 S  s   i | ]
}|d qS r   ri   )r   r   ri   ri   rl   r    s      )r   r   r   r   r   r<  r@  Zis_RationalrA  rp  r   r  r   r  rV   r   r   ziprP  ro   r   r   r   )rQ  rd   r   r   r   Za0r  qsbsZpow_r   r   rA  Za1b1Zu_xr8  r{   r   r  Z
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   |  }|sd S |  | | || || f\ }}|}|d krd S td}|r|tjkrd }n@jrt|t |  }n"t	t
|t   |  }ddd fdd}	|dkr|   d   |d d   }
|
 t   d    }|	|
|}t|||}n|dkr|	||}nd S t|||S )Nr   ro  r   r   r   r!  r   c                 S  s   | j o
| jS rh   )r<  is_oddr=  ri   ri   rl   r$    r%  z%sqrt_quadratic_rule.<locals>.<lambda>r>  r   rX   r   )
numer_polyrd   c              	     s  t    d   }|  }|dkrz|dkr>|  ntj|  f\}}|d  }||  }d   | }d }	d }
|dkrtd}tdt | ||tj }t	||   d   |}
|dkrt
|| |||
}
|dkr0ttd| dd}	|dkr0t
|| |d| |	}	|
rn|	rnt|| || dd}t||t||
|	g}n
|
pv|	}n|  }t| |}|S )Nr   rQ   r   r   Fr   rr  )r)   rU   Z
all_coeffsr   r   as_exprr   r~   r  r   rx   r   r   r   r   r   r   )r  rd   r   degr   r   ABZpre_substituteZconstant_stepZlinear_stepr   Zpow_ruleaddr   r   r   r   r   r   ri   rl   sqrt_quadratic_denom_rule  s4    
"$

z6sqrt_quadratic_rule.<locals>.sqrt_quadratic_denom_rulerQ   r   )r   rA  r)   Zas_polyr   r   r  rd  rP  r  r   r  r   r  )rQ  r   rd   r!  r   rA  Zf_polyr  r  r  r  r   r{   r~  ri   r  rl   r     s:    *,

" 
($

r   ztuple[Expr, Symbol]c                 C  sr  | \}}t |trn|jd |krn|jtkr8t||S |jtkrLt||S td}|jt	krt|t| }t
|||t|||t|td| ||S |jtkrt|t| }t
|||t|||t|td| ||S |t	}|jtkr4t
|||t|||t	|d td|d d  |tdtjtjS |jtkrnt
|||t|||t	|d td| ||S d S )Nr   r   rQ   r   )r   r!   r  r   r&   r   r#   r   r   r'   r   r   r   r$   rS  r%   r   r   r  r"   )rQ  rd   r  r   r   ri   ri   rl   hyperbolic_rule2  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   ro  r   r   c                 S  s
   t | tS rh   r   r   r=  ri   ri   rl   r$  Q  r%  zmake_wilds.<locals>.<lambda>rm  r   c                 S  s
   t | tS rh   r  r=  ri   ri   rl   r$  R  r%  )r   )r  r   r   r   r   ri   ri   rl   
make_wildsM  s
    r  c                 C  s>   t | \}}}}t||  | t||  |  }|||||fS rh   )r  r-   r,   r  r   r   r   r   r{  ri   ri   rl   sincos_patternV  s     r  c                 C  s>   t | \}}}}t||  | t||  |  }|||||fS rh   )r  r.   r1   r  ri   ri   rl   tansec_pattern]  s     r  c                 C  s>   t | \}}}}t||  | t||  |  }|||||fS rh   )r  r/   r0   r  ri   ri   rl   cotcsc_patternd  s     r  c                 C  sD   t d| gd}t d| gd}t d}t||  | | }||||fS )Nr   ro  r   r  )r   r8   )r  r   r   r  r{  ri   ri   rl   heaviside_patternk  s
    r  c                   s    fdd}|S )Nc                   s    |  S rh   ri   r  r   ri   rl   
uncurry_rlu  s    zuncurry.<locals>.uncurry_rlri   )r   r  ri   r  rl   uncurryt  s    r  c                   s    fdd}|S )Nc                   sB   | \}}}}}} ||||||}||kr>t |||t||S d S rh   )r   rP  )r  r   r   r   r   rd   r  r   rS  ri   rl   trig_rewriter_rlz  s    z'trig_rewriter.<locals>.trig_rewriter_rlri   )rS  r  ri   r  rl   trig_rewritery  s    r  c                 C  s   |j o|j o|jo|jS rh   )is_evenrp  r   r   r   r   r   r   ri   ri   rl   r$    s   r$  c                 C  s@   dt d|  |  d |d  dt d| |  d |d   S r  )r,   r   r   r   r   r   r  ri   ri   rl   r$    s   c                 C  s   |j o|dkS NrD  r  r  ri   ri   rl   r$    r%  c                 C  s<   dt | | d  |d d  t| |  t || |  S r  )r,   r-   r  ri   ri   rl   r$    s   
c                 C  s   |j o|dkS r  r  r  ri   ri   rl   r$    r%  c                 C  s<   dt || d  |d d  t||  t | | |  S r  )r-   r,   r  ri   ri   rl   r$    s   
c                 C  s   |j o|dkS Nr   r  r  ri   ri   rl   r$    r%  c                 C  s@   dt || d  |d d  t|| d  t | | |  S r  )r.   r1   r  ri   ri   rl   r$    s   c                 C  s   |j S rh   r  r  ri   ri   rl   r$    r%  c                 C  s<   t | | d d |d d  t| |  t || |  S Nr   rQ   )r1   r.   r  ri   ri   rl   r$    s   
c                 C  s   |dko|dkS r  ri   r  ri   ri   rl   r$    r%  c                 C  s   t | | d d S r  )r1   r  ri   ri   rl   r$    r%  c                 C  s   |j o|dkS r  r  r  ri   ri   rl   r$    r%  c                 C  s@   dt || d  |d d  t|| d  t | | |  S r  )r/   r0   r  ri   ri   rl   r$    s   c                 C  s   |j S rh   r  r  ri   ri   rl   r$    r%  c                 C  s<   t | | d d |d d  t| |  t || |  S r  )r0   r/   r  ri   ri   rl   r$    s   
c                   s   | \ }t  fddttfD rt|\}}}}} |sFd S ttttt	t
titfdd||||fD  |g S d S )Nc                 3  s   | ]}  |V  qd S rh   rE  r  r  ri   rl   r     s     z#trig_sincos_rule.<locals>.<genexpr>c                   s   g | ]}  |tjqS ri   r  r   rv  ri   rl   r     s     z$trig_sincos_rule.<locals>.<listcomp>)r   r-   r,   r  rA  r]  sincos_botheven_conditionsincos_bothevensincos_sinodd_conditionsincos_sinoddsincos_cosodd_conditionsincos_cosoddry  rQ  r  r{  r   r   r   r   ri   rd   rA  rl   trig_sincos_rule  s(    
   r  c                   s   | \ }  dt| t|i t fddttfD rt|\}}}}} |s`d S ttt	t
tttitfdd||||fD  |g S d S )NrQ   c                 3  s   | ]}  |V  qd S rh   rE  r  r  ri   rl   r     s     z#trig_tansec_rule.<locals>.<genexpr>c                   s   g | ]}  |tjqS ri   r  r   rv  ri   rl   r     s     z$trig_tansec_rule.<locals>.<listcomp>)r   r,   r1   r   r.   r  rA  r]  tansec_tanodd_conditiontansec_tanoddtansec_seceven_conditiontansec_seceventan_tansquared_conditiontan_tansquaredry  r  ri   r  rl   trig_tansec_rule  s0    
 
   r  c              	     s   | \ }  dt| t|dt| t|t|t| t|i t fddttfD rt|\}}}}} |sd S t	t
tttitfdd||||fD  |g S d S )NrQ   c                 3  s   | ]}  |V  qd S rh   rE  r  r  ri   rl   r     s     z#trig_cotcsc_rule.<locals>.<genexpr>c                   s   g | ]}  |tjqS ri   r  r   rv  ri   rl   r     s     z$trig_cotcsc_rule.<locals>.<listcomp>)r   r-   r0   r.   r/   r,   r   r  rA  r]  cotcsc_cotodd_conditioncotcsc_cotoddcotcsc_csceven_conditioncotcsc_cscevenry  r  ri   r  rl   trig_cotcsc_rule  s4    
 
  
  r  c                 C  sj   | \}}t dtd| gd}|td| | }|rfdt| t| td|  }t|| |S d S )Nr   r   ro  )r   r-   rA  r,   rP  )rQ  rd   r  r   rA  Z
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  rb  ri   ri   rl   trig_powers_products_rule  s    r  c              	   C  sb  | \}}t dd|gd}t dd|gd}td}|||d   }||}|D ]}||}	|	|tj}
|	|tj}|
jr|
dkp|
j}|jr|dkp|j}|
jr|
dk p|
j	}|jr|dk p|j	}d }|r|rt
|
t
| t| }d}n~|r4|r4t
|
t
|  }|t| }t|| k||k }n>|rr|rrt
|
 t
| }|t| }t|| k||k }|rNi }ttttttfD ]:}|||t
||d < d|| |t
||d	 < q||| }t||}||sN|t||9 }| }|dt| }|r2|dt| t|i}t||}| sNt|||||||  S qNd S )
Nr   r   ro  r   r   r   TrQ   r   )r   r   r   rA  rh  r   r   Z	is_numberr  Zis_negativer)   r.   r-   rS   r1   r,   r0   r/   r   r   r,  rF  r  r  rP  ro   r   )rQ  rd   r  r  r  r   Ztarget_patternmatchesr   rA  r   r   Z
a_positiveZ
b_positiveZ
a_negativeZ
b_negativeZx_funcr   ry   substitutionsr!  ZreplacedZsecantsr{   ri   ri   rl   trig_substitution_rule  sn    


   


 
    r  c           	      C  st   | \}}t |\}}}}||}|rpd|| krpt|| |}|| ||  }}t|||| | | | |S d S r   )r  rA  rP  r   )	rQ  rd   r  r{  r   r   r  rA  r{   ri   ri   rl   heaviside_rule8  s    
r  c           
      C  s   | \}}t |jdkrtj}n
|jd }|jr6|dk r:d S td|gdtd|dgd }}|jd |||  }|szd S || ||  }}t|d}|tjkrd }nt	t
|||}t|||||}	t||	|S )NrQ   r   r   ro  r   )r   r  r   r   r<  r   rA  r   r  rv   r9   r   r  )
rQ  rd   r   r   r   r   rA  r  r  r~  ri   ri   rl   dirac_delta_ruleC  s"    
 

r  c              	   C  sl  | \}}t d}t|||}d}|rhtd g }|D ]\}}}	t|	|}
|d }td||
 |
 rnq6t|d dkr(| \}}|
rt||	 |||	|
}
|j	r(g }g }t
|tr|j}n
|| |D ]8}t|jstt||d|}|r||t|df q||
df t|	||}
|t|||||
 q6t|dkrZt|||S |rh|d S d S )Nr   r   zList of Substitution RulesrQ   r^  T)r   rO  rb   rP  r0  ro   r[   r5  rx   r@  r   r   r  r   r   rd  r,  r   r   r   r   r   )rQ  rd   r  r   r  r   Zwaysr   r   r8  Zsubruler   r   Z	piecewiseZcould_be_zeror   r{   ri   ri   rl   substitution_ruleY  sL    



r  c                 C  s   |   S rh   )r3  r  ri   ri   rl   r$    r%  c                 C  s
   |  |S rh   )Zapartr  ri   ri   rl   r$    r%  c                 C  s   dS r   ri   r  ri   ri   rl   r$    r%  c                 C  s   |   S rh   )r1  r  ri   ri   rl   r$    r%  c                   s,   t  fdd| jD p*t| tp*t| tS )Nc                 3  s   | ]}|j p| V  qd S rh   )r   r  r  r  ri   rl   r     s     z<lambda>.<locals>.<genexpr>)r  r  r   r   r   r  ri   r  rl   r$    s    c                 C  s   |   S rh   expandr  ri   ri   rl   r$    r%  c                 C  s   t dd | tD dkS )Nc                 S  s   h | ]}|j d  qS r  r  r  ri   ri   rl   r    s     z<lambda>.<locals>.<setcomp>rQ   )r   Zatomsr+   r  ri   ri   rl   r$    s    c                 C  s   | j ddS )NT)Ztrigr  r  ri   ri   rl   r$    r%  c                 C  sP   | d }|j }|j}|j}| j|krD| j|kr6t|  S t|| jS nt|  S d S r   )	variablesr   r@  r  r   r   rv   )rQ  rd   Zdiff_variablesZundifferentiated_functionZintegrand_variablesri   ri   rl   derivative_rule  s    

r	  c                 C  sJ   | \}}| dt| rF|dt| t|}t|||t||S d S r   )rA  r,   r   r1   r   rP  )rQ  rd   r  r   ri   ri   rl   rewrites_rule  s    r
  c                 C  s   t |  S rh   )r   rb  ri   ri   rl   fallback_rule  s    r  zdict[Expr, Expr | None]_integral_cachezdict[Expr, int]r  zc                   sv  |  ti}|tkrBt| dkr,t| S t|  tfS n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*ft+ 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krtS tttfD ]}t||r|  S qt|S rh   )rd   r@  r   r   r+   rO   r   type)rQ  rd   r  r  ri   rl   rY    s    


zintegral_steps.<locals>.keyc                    s    fdd}|S )Nc                   s    | }|ot |S rh   )
issubclass)rQ  r  )rY  klassesri   rl   _integral_is_subclass	  s    zKintegral_steps.<locals>.integral_is_subclass.<locals>._integral_is_subclassri   )r  r  )rY  )r  rl   integral_is_subclass  s    z,integral_steps.<locals>.integral_is_subclass)3r  r  r  r   r   r^   r_   r}  r]   r   re  r   r  r  r   r   rg  r   r  r   r  r  r  r   r   r	  r+   r  r8   r9   r  rO   rj  r   rc  r  r   r
  r  r`   partial_fractions_rulecancel_ruler    rH  r  distribute_expand_ruler  trig_expand_ruler  r  r  )rd   r  optionsr  rQ  r  r   ri   )rY  r  rl   rP    s    -


            -.rP  c                 C  s   t | | }t  t|trt|jdkr|jd d }t|tr|jd d dkr|	|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)rP  rm   r  clearr   r*   r   r  r   r   r   )r!  r   r   r   ri   ri   rl   manualintegrate@  s    1r  N)T)T(  ru   
__future__r   typingr   r   r   r   abcr   r   dataclassesr	   collectionsr
   collections.abcr   Zsympy.core.addr   Zsympy.core.cacher   Zsympy.core.containersr   Zsympy.core.exprr   Zsympy.core.functionr   Zsympy.core.logicr   Zsympy.core.mulr   Zsympy.core.numbersr   r   r   Zsympy.core.powerr   Zsympy.core.relationalr   r   r   Zsympy.core.singletonr   Zsympy.core.symbolr   r   r   Z$sympy.functions.elementary.complexesr   Z&sympy.functions.elementary.exponentialr   r    Z%sympy.functions.elementary.hyperbolicr!   r"   r#   r$   r%   r&   r'   r(   Z(sympy.functions.elementary.miscellaneousr)   Z$sympy.functions.elementary.piecewiser*   Z(sympy.functions.elementary.trigonometricr+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   Z'sympy.functions.special.delta_functionsr8   r9   Z'sympy.functions.special.error_functionsr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   Z'sympy.functions.special.gamma_functionsrD   Z*sympy.functions.special.elliptic_integralsrE   rF   Z#sympy.functions.special.polynomialsrG   rH   rI   rJ   rK   rL   rM   rN   rO   Z&sympy.functions.special.zeta_functionsrP   Z	integralsrR   Zsympy.logic.boolalgrS   Zsympy.ntheory.factor_rT   Zsympy.polys.polytoolsrU   rV   rW   rX   Zsympy.simplify.radsimprY   Zsympy.simplify.simplifyr[   Zsympy.solvers.solversr\   Zsympy.strategies.corer]   r^   r_   r`   Zsympy.utilities.iterablesra   Zsympy.utilities.miscrb   rc   rt   rv   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  r  r  r
  r  r  r  r  r  r  r  r  r  r  r  r   r  r,  rH  rO  rU  rX  r]  r   rc  re  rg  rj  rk  rs   rx  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  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  rP  r  ri   ri   ri   rl   <module>   s  (<0,	#	2		!W+5L_?H,C




<3~