o
    *ήc                    @  s  U d Z ddlmZ ddl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mZmZ ddl m!Z! ddl"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/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZK ddlLmMZMmNZN ddlOmPZPmQZQmRZRmSZSmTZTmUZUmVZVmWZWmXZXmYZY ddlZm[Z[ ddl\m]Z]m^Z^ ddl_m`Z`maZambZbmcZcmdZdmeZemfZfmgZgmhZh ddlimjZj dd lkmlZl dd!lmmnZn dd"lompZp dd#lqmrZrmsZsmtZtmuZu dd$lvmwZw dd%lxmyZy dd&lzm{Z{ dd'l|m}Z}m~Z~mZmZ dd(lmZ dd)lmZ d\d+d,Zed-d.Zed/d0Zed1d2Zed3d4Zed5d6Zed7d8Zed9d:Zed;d<Zed=d<Zed>d2Zed?d@ZedAZedBZedCdDZedEdFZedGdDZedHdIZedJZedKZedLdMZedNdMZedOdPZedQdRZedSdTZedUdVZedWdDZedXdDZedYdTZedZd[Zed\d]Zed^d]Zed_d]Zed`d]Zedad]Zedbd[ZedcddZededdZedfddZedgddZedhddZedidDZedjdDZedkdDZedlddZedmddZedndoZedpdqZedrdqZG dsdt dteZi Zdudv Zdwdx Zdydz Zd{d| ZeHeGeFeIeJeKfZd}d~ Zdd Zdd Zdd ZddI Zdd Zdd Zdd Zdd Zg Zded< g Ze(dZdd Zdd Zːd]d^ddZdd Z͐d^ddZdd Zdd Zdd Zdd 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 Zedd Zdd΄ ZddЄ Zdd҄ ZddԄ Zddք Zdd؄ Zddڄ Zd^dd܄Zddބ Zedd dd Zedd dd Zedd dd Zedd dd Zdd Zdd Zdd Zi Zded< e	eZ ded< e(dZdd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeed d Z	eedd Z
eedd Zeed`d	d
Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd ZeedaddZeedd Zeedd Zeeeedd  Zeed!d" Zeed#d$ Zeed%d& Zeed'd( Zeed)d* Zeed+d, Zeed-d. Zeed/d0 Zeed1d2 Zeed3d4 Z eed5d6 Z!eed7d8 Z"eed9d: Z#eed;d< Z$eed=d> Z%eed?d@ Z&eedAdB Z'eedCdD Z(eedEdF Z)eedGdH Z*eedIdJ Z+eedKdL Z,eedMdN Z-eedOdP Z.eedQdR Z/eedSdT Z0eedUdV Z1dWdX Z2dYdZ Z3d[S (b  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 namedtuples representing the integration rules used. The
``manualintegrate`` function computes the integral using those steps given
an integrand; given the steps, ``_manualintegrate`` will evaluate them.

The integrator can be extended with new heuristics and evaluation
techniques. To do so, write a function that accepts an ``IntegralInfo``
object and returns either a namedtuple representing a rule or
``None``. Then, write another function that accepts the namedtuple's fields
and returns the antiderivative, and decorate it with
``@evaluates(namedtuple_type)``.  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)
namedtupledefaultdict)Mapping)reduce)Add)cacheit)Dict)Expr)
Derivative)	fuzzy_not)Mul)IntegerNumberE)Pow)EqNe)S)DummySymbolWild)Abs)explog)	HyperbolicFunctioncschcoshcothsechsinhtanhasinhatanh)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)debug c                   s2   dd   fdd}t | |d } |_||_|S )Nc                 S  s   | j |j kot| |S N)	__class__tuple__eq__selfother rn   F/tmp/pip-target-vg8gfxp4/lib/python/sympy/integrals/manualintegrate.pyrj   E   s   zRule.<locals>.__eq__c                   s    | | S rg   rn   rk   rj   rn   ro   <lambda>G       zRule.<locals>.<lambda>z context symbol)r   rj   __ne__)nameprops__neq__clsrn   rp   ro   RuleC   s   rx   ConstantRuleconstantConstantTimesRulezconstant other substep	PowerRulezbase expAddRulesubstepsURulezu_var u_func constant substep	PartsRulezu dv v_step second_stepCyclicPartsRulezparts_rules coefficientTrigRulezfunc argHyperbolicRuleExpRuleReciprocalRulefunc
ArcsinRuleArcsinhRuleReciprocalSqrtQuadraticRuleza b cSqrtQuadraticDenomRuleza b c coeffsSqrtQuadraticRuleAlternativeRulealternativesDontKnowRuleDerivativeRuleRewriteRulezrewritten substepCompleteSquareRulePiecewiseRulesubfunctionsHeavisideRulezharg ibnd substepDiracDeltaRulezn a bTrigSubstitutionRulez(theta func rewritten substep restriction
ArctanRuleArctanhRule
JacobiRuleGegenbauerRulezn aChebyshevTRulenChebyshevURuleLegendreRuleHermiteRuleLaguerreRuleAssocLaguerreRuleCiRuleza bChiRuleEiRuleSiRuleShiRuleErfRuleFresnelCRuleFresnelSRuleLiRulePolylogRuleUpperGammaRuleza eEllipticFRuleza dEllipticERulec                   @  s   e Zd ZU ded< ded< dS )IntegralInfor   	integrandr   symbolN)__name__
__module____qualname____annotations__rn   rn   rn   ro   r      s   
 r   c                       fdd}|S )Nc                   s    | _ | t < | S rg   )rule
evaluatorsr   r   rn   ro   
_evaluates   s   zevaluates.<locals>._evaluatesrn   )r   r   rn   r   ro   	evaluates   s   r   c                 C  sf   t | trdS t | tsdS | D ] }t |trt|r dS qt |tr0tdd |D r0 dS qdS )NTFc                 s  s    | ]}t |V  qd S rg   contains_dont_know.0irn   rn   ro   	<genexpr>   s    z%contains_dont_know.<locals>.<genexpr>)
isinstancer   ri   r   listany)r   valrn   rn   ro   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      c                      g | ]}t | qS rn   )manual_diffr   argr   rn   ro   
<listcomp>       zmanual_diff.<locals>.<listcomp>rS   )argsr   r-   diffr0   r.   r/   r
   sumr   lenr   r   )fr   r   rn   r   ro   r      s    







r   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.
    rS   r   z(Expected an iterable of (old, new) pairsr   z$subs accepts either 1 or 2 argumentsc                   s   | j o| j kS rg   )is_Powbasex)x0rn   ro   rq          zmanual_subs.<locals>.<lambda>c                   s   t | j   S rg   )r   r   )newrn   ro   rq          )r   r   r   r   itemsrd   
ValueErrorr   r   replaceappendr   subsr   )exprr   sequencenew_subsoldrn   )r   r   ro   manual_subs   s(   



r   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||  }|jv 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                   r   rn   rX   r   tr   rn   ro   r      r   z<find_substitutions.<locals>.test_subterm.<locals>.<listcomp>c                   r   rn   r   r   )u_varrn   ro   r      r   )as_Add)	re   formatr   cancelfree_symbolsis_rational_functionmaxas_numer_denomas_independent)uu_diffsubstituted
deg_before	deg_after)r   r   r   rn   ro   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 rg   )
is_Integerr   rn   rn   ro   rq          z:find_substitutions.<locals>.exp_subterms.<locals>.<lambda>
propertiesr   )r   findr   r   r   matchr   rZ   )r   linear_coeffstermsr   exp_r   r   r   rn   ro   exp_subterms   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   rS   r      c                      g | ]	}|  r|qS rn   hasr   r   rn   ro   r         zAfind_substitutions.<locals>.possible_subterms.<locals>.<listcomp>c                   s8   g | ]}d |  k rt  jd  k rn n j| qS )rS   )absr   r   )r   dr   rn   ro   r     s
    c                 S  s   g | ]}|j r|qS rn   )r   r   rn   rn   ro   r     s    
)r   r*   r   inverse_trig_functionsr   r   r8   r   rH   rI   rJ   rK   rL   rN   rM   rO   r   r   extendr   r   rW   r   is_Addr
   )r   rr   r   )possible_subtermsr   r  ro   r     sL   








z-find_substitutions.<locals>.possible_subtermsF)r   r   )r   dictfromkeysr   r   r   )r   r   r   resultsr   r  r   r   new_integrandrz   substitutionrn   )r   r  r   r   ro   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: {})re   r   integral_stepsr   r   r   )integralr   r   	rewrittensubsteprc   rewritern   ro   	_rewriter3  s   
zrewriter.<locals>._rewriterrn   )rc   r  r  rn   r  ro   rewriter1     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: {})re   r   r   r   r  )criteriar  r   r   r   r  r  rn   ro   _proxy_rewriterC  s   z'proxy_rewriter.<locals>._proxy_rewriterrn   )rc   r  r"  rn   r  ro   proxy_rewriterA  r   r#  c                   r   )z4Apply the rule that matches the condition, else Nonec                   s*      D ]\}}|| r||   S qd S rg   )r   )r   keyr   
conditionsrn   ro   multiplexer_rlS  s
   z#multiplexer.<locals>.multiplexer_rlrn   )r&  r'  rn   r%  ro   multiplexerQ  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 |rVdd |D }|rNt|g| R  S t|g| R  S d S )Nr   zList of Alternative RulesrS   Rule {}: {}c                 S  s   g | ]}t |s|qS rn   r   )r   r   rn   rn   ro   r   j  s    z7alternatives.<locals>._alternatives.<locals>.<listcomp>)re   r   r   r   r   r   r   )r  altscountr   resultdoablerulesrn   ro   _alternatives[  s,   
z#alternatives.<locals>._alternativesrn   )r/  r0  rn   r.  ro   r   Y  s   c                 C  s   t | jg| R  S rg   )ry   r   r  rn   rn   ro   constant_ruleq  s   r2  c                 C  s   | \}}|  \}}||jvr)t|tr)t|d dkr"t|||S t||||S ||jvrct|tret||||}tt	|j
rC|S t	|j
rNtdd|S t|tt	|dftdd|dfg||S d S d S )NrS   r   T)as_base_expr   r   r   r^   r   r|   r   r   r   is_zerory   r   r   )r  r   r   r   exptr   rn   rn   ro   
power_rulet  s&   
r6  c                 C  s0   | \}}t |jd trtt|jd ||S d S Nr   )r   r   r   r   r   )r  r   r   rn   rn   ro   exp_rule  s   r8  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 ]7}t||rW||d}|j|  u rWt fdd|jd | D sW|jd | | f }|| |   S q d S )Nr  r   rS   c                 3  s    | ]}|  V  qd S rg   r  )r   vr   rn   ro   r         z'orthogonal_poly_rule.<locals>.<genexpr>)rO   r   rN   r   rH   r   rI   r   rJ   r   rK   r   rL   r   rM   r   r   getr   r   )r  orthogonal_poly_classesorthogonal_poly_var_indexr   klass	var_indexr   rn   r   ro   orthogonal_poly_rule  s0   
r@  z/list[tuple[Type, Expr, Callable | None, tuple]]_special_function_patternsr   c                   sr  | \}}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 ]6\}}}}t|
|r5|
|  r5t fddtD }|d u s)|| r5|||f }||   S q d S )Nac                 S     | j  S rg   r4  r   rn   rn   ro   rq         z'special_function_rule.<locals>.<lambda>excluder   brG  cr
  c                 S  rC  rg   rD  r   rn   rn   ro   rq     rE  ec                 S  s   | j o| j S rg   )is_nonnegative
is_integerr   rn   rn   ro   rq     r   r   FevaluaterS   c                 S     | |kS rg   rn   rB  r
  rn   rn   ro   rq     rE  c                 S  rP  rg   rn   rQ  rn   rn   ro   rq     rE  c                 3  s(    | ]}  |d ur  |V  qd S rg   )r;  )r   wr   rn   ro   r     s    z(special_function_rule.<locals>.<genexpr>) rA  r   _symbol_wildsr  r   r   r   r+   r   r    r   r,   r   r#   r   r   r   r   r   r   r   r   rR   r   r'   r   r   r   r   r   ri   )r  r   r   rB  rH  rJ  r
  rK  linear_patternquadratic_pattern
_integrandtype_pattern
constraintr   	wild_valsr   rn   rS  ro   special_function_rule  sT    
r]  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 rn   r]   r   r  condgeneric_condrn   ro   r     s    z(_add_degenerate_step.<locals>.<listcomp>)r   r   r   r   r   truecontextr   )ra  generic_stepdegenerate_stepr   rn   r`  ro   _add_degenerate_step  s   



rf  Tr  c                   s>  | \    \}}tdgd}tdgd}tddgd}|||  |d   s3dS  fdd	fd
d|||fD \}}}t|d}|rU|tju rXd}n|jrft|| || }ntt	||  | }t
d| d dkr	| d|  ||d d|   }	}
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fdd|D }|
js||tjf t| }n|}t|||S |tjkrt||| }t|||S dS )zm
    Set degenerate=False on recursive call where coefficient of quadratic term
    is assumed non-zero.
    rB  rI  rH  rJ  r   r   Nc                   s   t d}dt|| || | d    }t|| |  }dt| }	d }
|ur0|}
|}dt|||d    }| ||}|	dkrOt|	|||	| }|
d ur\t||
d ||}|dkrgt|| }|S )Nr   rS   r   r   )r   r'   r{   r   r   )	RuleClassrB  sign_arJ  sign_chr   r  quadratic_baserz   u_funcstandard_formr  r   r   rn   ro   make_inverse_trig  s"   $
z,inverse_trig_rule.<locals>.make_inverse_trigc                      g | ]	}  |tjqS rn   r;  r   Zeror   rS  rn   ro   r     r  z%inverse_trig_rule.<locals>.<listcomp>rS      c                   s   g | ]
\}} | |fqS rn   rn   )r   r   r_  )ro  rn   ro   r   #  s    )r3  r   r   r   r   rb  r4  ry   sqrt_linear_ruler   r^   r   is_realr   rV   r   falser   is_positiver   rf  Halfr   )r  
degenerater   r   rB  rH  rJ  ra  re  rj  kstepgeneral_rulepossibilitiesr   r_  r/  rn   )r   ro  r   r   ro   inverse_trig_rule  sP   
$" 

r  c                   s6   | \}  fdd|  D }d |v rd S t|| S )Nc                   r   rn   )r  )r   gr   rn   ro   r   1  s    zadd_rule.<locals>.<listcomp>)as_ordered_termsr}   )r  r   r  rn   r   ro   add_rule/  s
   
r  c                 C  sH   | \}}| |\}}|dkr t||}|d ur"t|||||S d S d S NrS   )r   r  r{   )r  r   r   coeffr   	next_steprn   rn   ro   mul_rule6  s   
r  c                   s   fdd}dd }|t |t ||tt|tg}td}t| t gtR r+||  } t|D ]\}}|| }|r|\}	}
 |	jvrK|		|sK d S |	
|d}	|

|d}
||krc|	 sc d S t|	t r{d|
 }| r{t| dkr{ d S ||kr|
js|
	tst|
trt|
 }t|r d S |	 }t|}|	|
|||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|
 }t|st|}|	|
|||f  S q/d S )Nc                   sX   |    } t| ts| jsg n	 fdd| jD }|r*t| }| |   }||fS d S )Nc                   r  rn   )is_algebraic_exprr   r   rn   ro   r   H  r  z;_parts_rule.<locals>.pull_out_algebraic.<locals>.<listcomp>)r   togetherr   r)   is_Mulr   r   )r   	algebraicr   dvr   rn   ro   pull_out_algebraicD  s   z'_parts_rule.<locals>.pull_out_algebraicc                    r   )Nc                   sT   t  fddD r&fdd jD }|r(tdd |} | }||fS d S d S )Nc                 3      | ]}  |V  qd S rg   r  r   r   r   rn   ro   r   P  r:  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 rg   )r   )r   rw   r   rn   ro   r   R  r:  zT_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<listcomp>.<genexpr>)r   )r   	functionsr  ro   r   Q  s    
zJ_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<listcomp>c                 S  s   | | S rg   rn   )rB  rH  rn   rn   ro   rq   T  rE  zH_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rl.<locals>.<lambda>)r   r   r	   )r   r   r   r  r  r  ro   pull_out_u_rlO  s   z6_parts_rule.<locals>.pull_out_u.<locals>.pull_out_u_rlrn   )r  r  rn   r  ro   
pull_out_uN  s   	z_parts_rule.<locals>.pull_out_u	temporaryrS   Fr   Tc                 s  s     | ]}t |tttfV  qd S rg   )r   r,   r+   r   r   rB  rn   rn   ro   r     s    z_parts_rule.<locals>.<genexpr>r   )r   r  r,   r+   r   r   r   	enumerater   r  r   is_polynomialrX   is_Derivativer*   rP   r  r   r   _manualintegrater   allequalsr^   )r   r   r  r  liate_rulesdummyindexr   r,  r   r  rec_dvv_stepdur9  acceptlruler  rn   r   ro   _parts_ruleA  st   







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   rS   rs  z7Cyclic integration {} with v: {}, du: {}, integrand: {}c              	   S  s(   g | ]\}}}}}t |||d d d qS rg   )r   )r   r   r  r9  r  r  rn   rn   ro   r     s    zparts_rule.<locals>.<listcomp>rt  c                   sD   | r| d \}}}}}t ||| | dd  || |S t|S Nr   rS   )r   r  )stepsr   r   r  r9  r  r  make_second_stepr   rn   ro   r    s   
z$parts_rule.<locals>.make_second_stepr   )as_coeff_Mulr  re   r   r   r   rU   r,   r+   r   r#   r    xreplace_cache_dummy_parts_u_cacheranger   r   r   r   r{   r   )r  r   rz   r,  r  r   r  r9  r  r  cachekey_coefficientr   next_constantnext_integrandrn   r  ro   
parts_rule  sl   




r  c                 C  sh  | \}}t |ttfr(|jd }t |tsd S t |trd}nd}t||||S |t|d kr7td|||S |t|d krFtd|||S t |trVt|j t|j  }nTt |t	rft|j t|j  }nDt |tr|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   sec**2csc**2)r   r,   r+   r   r   r   r0   r/   r-   r.   r   r  )r  r   r   r   r   r  rn   rn   ro   	trig_rule  sB   








r  c                 C  s   | \}}t |t| }|| }||jvr,td|||}|dkr*|r*t|||||}|S t| t| }|| }||jvrUtd|||}|dkrS|rSt|||||}|S d S )Nsec*tanrS   csc*cot)r0   r-   r   r   r{   r/   r.   )r  r   r   sectanqr   csccotrn   rn   ro   trig_product_rule  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 |S t d|gd}||||d  ||  |  }|r|| || }}|jrd S td}||d|   }||||d|   }t||}|rt||d |||S d S t d|gd}||| | ||d  ||  |  }|r|| || || || || f\}}}}}|jrSd S ||d  ||  | }|d|  }d| | | }| | | }td}t|||t|d |||}|dkrt||| ||| | |}|jr|S t|| |}t	||g||}|| | ||  } t| |||S d S )NrB  rI  rH  rJ  r   r   rS   rt  FrN  r
  r   rK  )r   r   r   is_extended_realr   rb  r'   r   r{   r}   r   r   rw  r   r4  r   r   r  r   )!r  r   r   rB  rH  rJ  r   r}  positive_condr  rz   r1r2	log_stepsr  subnegative_stepr
  match2r   rl  
integrand2r  rK  match3denominatorconstnumer1numer2step1step2r~   rewritenrn   rn   ro   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      }t|st|d|||}t d}|t
jur|d
d	 |D }t||}t||f|t
jfg||}|S dS )z*
    Substitute common (a+b*x)**(1/n)
    rB  rI  rH  r   NrS   r   c                   s<   i | ]\}}}|t j|  |  t j|  |   qS rn   )r   One)r   r   rH  r  b0q0r   rn   ro   
<dictcomp>  s    .z$sqrt_linear_rule.<locals>.<dictcomp>c                 S  s   i | ]}|d qS r   rn   )r   rH  rn   rn   ro   r    s    )r   r   r   r   r   r   r   is_Rationalr   rL  r   rb  r   r  rY   r   r   zipr  r   r   r   r   )r  r   r   rB  rH  a0basesqsbspow_r   r  r   a1b1u_xr   r  r|  ra  
simplifiedre  rn   r  ro   ru  u  sT   






ru  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 )NrB  rI  rH  rJ  r   r   r   c                 S  s   | j o| jS rg   )r   is_oddr   rn   rn   ro   rq     rr   z%sqrt_quadratic_rule.<locals>.<lambda>r   r   
numer_polyr[   r   r   c                   s  t    d   }|  }|dkr|dkr|  ntj|  f\}}|d  }||  }d   | }d  }	}
|dkrwtd}t|tj dt | |}t	|   d   d ||}
|dkrwt
|||
|| }
|dkrttd| dd}	|dkrt
|d| |	|| }	|
r|	rt|| || dd}t|t|
|	g||}|S |
p|	}|S |  }t ||}|S )Nr   rS   r   r   Frz  rN  )r'   rX   
all_coeffsr   rr  as_exprr   r|   ry  r   r{   r  r   r
   r   r}   r   )r  r   denomdegrK  r
  ABpre_substituteconstant_steplinear_stepr   pow_ruleaddr|  coeffsrB  rH  rJ  r   rn   ro   sqrt_quadratic_denom_rule  s6   "&z6sqrt_quadratic_rule.<locals>.sqrt_quadratic_denom_rulerS   rt  )r  r[   r   r   )r   r   r'   as_polyr   r   rb  r4  r  ru  r   r  r   rf  )r  rz  r   r   r   r   f_polyra  re  r  r  r  r  rd  rn   r  ro   sqrt_quadratic_rule  s:   *,

"($
r  tuple[Expr, Symbol]c                 C  s  | \}}t |tr|jd |kr|jtkrtd|||S |jtkr(td|||S td}|jtkrOt|t| }t	|t
|t|d t|d| |||||S |jtkrrt|t| }t	|t
|t|d t|d| |||||S |t}|jtkrt	|t
|t|d d ttdtjtjd|d d  |||||S |jtkrt	|t
|t|d d t|d| |||||S d S d S d S )Nr   r#   r    r   rS   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  rn   rn   ro   hyperbolic_rule  sP   





&
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 )
NrB  rI  rH  mc                 S  
   t | tS rg   r   r   r   rn   rn   ro   rq        
 zmake_wilds.<locals>.<lambda>rF  r   c                 S  r  rg   r  r   rn   rn   ro   rq     r  )r   )r   rB  rH  r  r   rn   rn   ro   
make_wilds   s
   r  c                 C  >   t | \}}}}t||  | t||  |  }|||||fS rg   )r  r,   r+   r   rB  rH  r  r   rZ  rn   rn   ro   sincos_pattern	      r  c                 C  r  rg   )r  r-   r0   r  rn   rn   ro   tansec_pattern  r  r   c                 C  r  rg   )r  r.   r/   r  rn   rn   ro   cotcsc_pattern  r  r  c                 C  sD   t d| gd}t d| gd}t d}t||  | | }||||fS )Nr  rI  rH  r  )r   r8   )r   r  rH  r  rZ  rn   rn   ro   heaviside_pattern  s
   r  c                   r   )Nc                   s    |  S rg   rn   r   r   rn   ro   
uncurry_rl(     zuncurry.<locals>.uncurry_rlrn   )r   r  rn   r   ro   uncurry'  s   r  c                   r   )Nc                   sB   | \}}}}}} ||||||}||krt |t||||S d S rg   )r   r  )r   rB  rH  r  r   r   r   r  r  rn   ro   trig_rewriter_rl-  s   z'trig_rewriter.<locals>.trig_rewriter_rlrn   )r  r  rn   r  ro   trig_rewriter,  s   r	  c                 C  s   |j o|j o|jo|jS rg   )is_evenrL  rB  rH  r  r   r   srn   rn   ro   rq   8  s    rq   c                 C  s@   dt d|  |  d |d  dt d| |  d |d   S NrS   r   )r+   rB  rH  r  r   r   r   rn   rn   ro   rq   <  s    c                 C  s   |j o|dkS Nr  r  r  rn   rn   ro   rq   ?  r   c                 C  s<   dt | | d  |d d  t| |  t || |  S r  )r+   r,   r  rn   rn   ro   rq   B  
    
c                 C     |j o|dkS r  r  r  rn   rn   ro   rq   F  r   c                 C  s<   dt || d  |d d  t||  t | | |  S r  )r,   r+   r  rn   rn   ro   rq   I  r  c                 C  r  Nrs  r
  r  rn   rn   ro   rq   M  r   c                 C  @   dt || d  |d d  t|| d  t | | |  S r  )r-   r0   r  rn   rn   ro   rq   O  
    c                 C     |j S rg   r  r  rn   rn   ro   rq   S  r   c                 C  <   t | | d d |d d  t| |  t || |  S Nr   rS   )r0   r-   r  rn   rn   ro   rq   U  r  c                 C  s   |dko|dkS )Nr   r   rn   r  rn   rn   ro   rq   Y  r   c                 C  s   t | | d d S r  )r0   r  rn   rn   ro   rq   [  s    c                 C  r  r  r  r  rn   rn   ro   rq   ]  r   c                 C  r  r  )r.   r/   r  rn   rn   ro   rq   _  r  c                 C  r  rg   r  r  rn   rn   ro   rq   c  r   c                 C  r  r  )r/   r.   r  rn   rn   ro   rq   e  r  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  rg   r  r  r  rn   ro   r   l  r:  z#trig_sincos_rule.<locals>.<genexpr>c                   rp  rn   rq  r   rS  rn   ro   r   w  r  z$trig_sincos_rule.<locals>.<listcomp>)r   r,   r+   r  r   r(  sincos_botheven_conditionsincos_bothevensincos_sinodd_conditionsincos_sinoddsincos_cosodd_conditionsincos_cosoddri   r  r   rZ  rB  rH  r  r   rn   r   r   ro   trig_sincos_rulei  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 )NrS   c                 3  r  rg   r  r  r  rn   ro   r     r:  z#trig_tansec_rule.<locals>.<genexpr>c                   rp  rn   rq  r   rS  rn   ro   r     r  z$trig_tansec_rule.<locals>.<listcomp>)r   r+   r0   r   r-   r   r   r(  tansec_tanodd_conditiontansec_tanoddtansec_seceven_conditiontansec_seceventan_tansquared_conditiontan_tansquaredri   r   rn   r!  ro   trig_tansec_rulez  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 )NrS   c                 3  r  rg   r  r  r  rn   ro   r     r:  z#trig_cotcsc_rule.<locals>.<genexpr>c                   rp  rn   rq  r   rS  rn   ro   r     r  z$trig_cotcsc_rule.<locals>.<listcomp>)r   r,   r/   r-   r.   r+   r   r  r   r(  cotcsc_cotodd_conditioncotcsc_cotoddcotcsc_csceven_conditioncotcsc_cscevenri   r   rn   r!  ro   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 )NrB  r   rI  )r   r,   r   r+   r  )r  r   r   rB  r   
sin_doublern   rn   ro   trig_sindouble_rule  s    r0  c                 C  s"   t tttttttt| S rg   )ra   rb   r"  r)  r.  r0  r1  rn   rn   ro   trig_powers_products_rule  s   r1  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||}t|s,t|||||||  S q'd S )
NrB  r   rI  rH  thetar   TrS   )r   r   r   r   r;  r   rr  	is_numberrx  is_negativer'   r-   r,   rV   r0   r+   r/   r.   r   trigsimpr   r  r   r  r  r   r   )r  r   r   r  r  r2  target_patternmatchesr   r   rB  rH  
a_positive
b_positive
a_negative
b_negativex_funcrestrictionrz   substitutionsr   replacedsecantsr  rn   rn   ro   trig_substitution_rule  sf   






rB  c           
      C  s   | \}}t |\}}}}||}|r<d|| kr>t|| |}t|}	|| || }}t|| | | | |	||S d S d S r7  )r  r   r  r  r   )
r  r   r   rZ  r  rH  r  r   r  r,  rn   rn   ro   heaviside_rule  s   
rC  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 )NrS   r   rB  rI  rH  )r   r   r   rr  r   r   r   r   rb  ry   r9   r   rf  )
r  r   r   r   rB  rH  r   ra  re  rd  rn   rn   ro   dirac_delta_rule  s"   
 

rD  c              
   C  sf  | \}}t d}t|||}d}|rtd g }|D ]\}}}	t|	|}
|d }td||
 t|
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 RulesrS   r)  T)r   r  re   r  r   r   r^   r   r{   r   r   r   r   r   r   r4  r   r   r   r   r   r   )r  r   r   r   r?  r+  waysrl  rJ  r   subruler  r  	piecewisecould_be_zeror   r  rn   rn   ro   substitution_rule  sX   



+rI  c                 C     |   S rg   )r   rn  rn   rn   ro   rq   G  rE  c                 C  s
   |  |S rg   )apartrn  rn   rn   ro   rq   H  r  c                 C  s   dS )NTrn   rn  rn   rn   ro   rq   M  s    c                 C  rJ  rg   )r   rn  rn   rn   ro   rq   N  rE  c                   s,   t  fdd| jD pt| tpt| tS )Nc                 3  s     | ]}|j p| V  qd S rg   )r   r  r   r   rn   ro   r   R  s    z<lambda>.<locals>.<genexpr>)r  r   r   r   r   rn  rn   r   ro   rq   Q  s   
c                 C  rJ  rg   expandrn  rn   rn   ro   rq   U  rE  c                 C  s   t dd | tD dkS )Nc                 S  s   h | ]}|j d  qS r  r  r  rn   rn   ro   	<setcomp>Z  r   z<lambda>.<locals>.<setcomp>rS   )r   atomsr*   rn  rn   rn   ro   rq   Y  s   c                 C  s   | j ddS )NT)trigrL  rn  rn   rn   ro   rq   [  rr   c                 C  sT   | d }|j }|j}|j}| j|v r!| j|v rt|  S t|| jS t| jg| R  S r7  )	variablesr   r   r   r   r   ry   r   )r  r   diff_variablesundifferentiated_functionintegrand_variablesrn   rn   ro   derivative_rule]  s   

rU  c                 C  sJ   | \}}| dt| r#|dt| t|}t|t||||S d S r  )r   r+   r   r0   r   r  )r  r   r   r  rn   rn   ro   rewrites_rulek  s
   rV  c                 C  s   t |  S rg   )r   r1  rn   rn   ro   fallback_ruler  r  rW  zdict[Expr, Expr | None]_integral_cachezdict[Expr, int]r  zc                   sd  |  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t0t1|}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(u_var=_u, u_func=exp(x), constant=1,
    substep=ArctanRule(a=1, b=1, c=1, context=1/(_u**2 + 1), symbol=_u),
    context=exp(x)/(exp(2*x) + 1), symbol=x)
    >>> print(repr(integral_steps(sin(x), x)))     # doctest: +NORMALIZE_WHITESPACE
    TrigRule(func='sin', arg=x, context=sin(x), symbol=x)
    >>> print(repr(integral_steps((x**2 + 3)**2, x)))     # doctest: +NORMALIZE_WHITESPACE
    RewriteRule(rewritten=x**4 + 6*x**2 + 9,
    substep=AddRule(substeps=[PowerRule(base=x, exp=4, context=x**4, symbol=x),
        ConstantTimesRule(constant=6, other=x**2,
            substep=PowerRule(base=x, exp=2, context=x**2, symbol=x),
                context=6*x**2, symbol=x),
        ConstantRule(constant=9, context=9, symbol=x)],
    context=x**4 + 6*x**2 + 9, symbol=x), context=(x**2 + 3)**2, symbol=x)


    Returns
    =======

    rule : namedtuple
        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 rg   )r   r   r   r   r*   rP   r   type)r  r   rw   r   rn   ro   r$    s   

zintegral_steps.<locals>.keyc                    s    fdd}|S )Nc                   s    | }|o
t |S rg   )
issubclass)r  r{  )r$  klassesrn   ro   _integral_is_subclass  s   zKintegral_steps.<locals>.integral_is_subclass.<locals>._integral_is_subclassrn   )r\  r]  )r$  )r\  ro   integral_is_subclass  s   z,integral_steps.<locals>.integral_is_subclass)2r  r  rX  r   r   ra   rb   r]  r`   r   r6  r  ru  r  r   r   r8  r
   r  r   r  r  rC  r  r   rU  r*   r  r8   r9   rD  rP   r@  r   r2  r  r   rV  rI  rc   partial_fractions_rulecancel_ruler   r  r  distribute_expand_ruler1  trig_expand_rulerB  rW  )r   r   optionsr  r  r^  r,  rn   )r$  r   ro   r  |  s   0


,-r  c                 C  s   | | S rg   rn   )rz   r   r   rn   rn   ro   eval_constant     rd  c                 C  s   | t | S rg   r  )rz   rm   r  r   r   rn   rn   ro   eval_constanttimes     rg  c                 C  s,   t | |d  |d  t|dft| dfS NrS   rt  T)r)   r   r   r   r   r   r   rn   rn   ro   
eval_power  s   
rk  c                 C  s   |t |  S rg   r   rj  rn   rn   ro   eval_exp
  rh  rm  c                 C  s   t tt| S rg   )r   mapr  r~   r   r   rn   rn   ro   eval_add  s   rp  c                 C  s<   t |}|jr|jdkr|t| t|j }|| |S )Nrt  )r  r   r   r   r   r   )r   rl  rz   r  r   r   r,  rn   rn   ro   eval_u  s   rq  c                 C  s   t |}| | t | S rg   rf  )r   r  r  second_stepr   r   r9  rn   rn   ro   
eval_parts  s   rs  c                 C  sH   d| }g }d}| D ]}| ||j t|j  |d9 }q
t| | S )NrS   rt  )r   r   r  r  r
   )parts_rulesr  r   r   r,  signr   rn   rn   ro   eval_cyclicparts   s   
rv  c                 C  sh   | dkr	t | S | dkrt|S | dkrt|S | dkr!t|S | dkr)t|S | dkr2t| S d S )Nr,   r+   r  r  r  r  )r+   r,   r0   r/   r-   r.   r   r   r   r   rn   rn   ro   	eval_trig,  s   

rx  strr   r   c                 C  s$   | dkrt |S | dkrt|S d S )Nr#   r    )r    r#   rw  rn   rn   ro   eval_hyperbolic<  s
   rz  c                 C  s,   | | d t ||  t|t ||   S r  )r'   r3   rB  rH  rJ  r   r   rn   rn   ro   eval_arctanD  s   ,r|  c                 C  s2   |  | d t | |  t|t | |   S r  )r'   r&   r{  rn   rn   ro   eval_arctanhI  s   2r}  c                 C  s   t | S rg   rl  )r   r   r   rn   rn   ro   eval_reciprocalM  re  r~  c                 C     t |S rg   )r2   rn  rn   rn   ro   eval_arcsinQ  re  r  c                 C  r  rg   )r%   )r   r   rn   rn   ro   eval_arcsinhV  re  r  c                 C  sD   t dt| t| ||  ||d    | d| |  t| S )Nr   )r   r'   )rB  rH  rJ  r   r   rn   rn   ro   eval_reciprocal_sqrt_quadratic[  s   Dr  r  
list[Expr]c                   s>  g     tt d D ];}t d | } | ||  }|  |d   d| d | d | 8  <  |d   |d |  | 8  < q d  d }	}
t| |  |d   }|	||
 d|   }|dkrrd}nttd| dd}|t| }tg  fdd	ttD |
| R  | | S )
Nr   rS   rt  r3  r   Fr  c                 3  s,    | ]}| t  d  |   V  qdS )r   N)r   r   r  result_coeffsr   rn   ro   r   x  s    $z,eval_sqrt_quadratic_denom.<locals>.<genexpr>)	copyr  r   r   r'   r  r   r  r
   )rB  rH  rJ  r  r   r   r   r   r  r
  rK  r  rz   I0r|  rn   r  ro   eval_sqrt_quadratic_denom`  s0   
("
r  c                 C  s   t t||dd}t|S )NFr  )r  r   r  )rB  rH  rJ  r   r   r|  rn   rn   ro   eval_sqrt_quadratic|  s   r  c                 C  s   t | d S r7  rf  )r   r   r   rn   rn   ro   eval_alternative  rh  r  c                 C  r  rg   rf  )r  r  r   r   rn   rn   ro   eval_rewrite  s   r  c                 C  s   t dd | D  S )Nc                 S  s   g | ]
\}}t ||fqS rn   rf  r^  rn   rn   ro   r     s    z"eval_piecewise.<locals>.<listcomp>r(   ro  rn   rn   ro   eval_piecewise  s   r  c                 C  s  | t| dt|  }| t| dt|  }| t| dt|  }t|t	}t
|dks3J |d }t|| |}t
|dksFJ t|d \}	}
t|trh|	}|
}t|
d |	d  }t|d }n3t|tr|	}|
}t|
d |	d  }t|d }nt|tr|	}|
}t|
d |	d  }t|d }t| || ft| || ft| || f| |fg}tt| | |fS )NrS   r   r   )r   r0   r+   r/   r,   r.   r-   r   r   r*   r   r_   r\   r   r'   r2   r1   r3   r)   r  r6  )r2  r   r  r  r>  r   r   trig_functionrelationnumerr  opposite
hypotenuseadjacentinverser  rn   rn   ro   eval_trigsubstitution  s@   


r  c                 C  sN   t | j}t|D ]\}\}}||kr||d f||<  nq	t| jg|R  S r  )r   variable_countr  r   r   )r   r   r  r   varr+  rn   rn   ro   eval_derivativerule  s   
r  c                 C  s   t | ||||  S rg   )r8   r   )hargibndr  r   r   rn   rn   ro   eval_heaviside  s   r  c                 C  s6   | dkrt |||  | S t|||  | d | S r  r7   )r   rB  rH  r   r   rn   rn   ro   eval_dirac_delta  s   r  c                 C  s|   t dt| d |d |d | | | |  t| | | df|t| df|| d |d  d || | d  t| dfS )Nr   rS   r   rs  )r)   rO   r   r   )r   rB  rH  r   r   rn   rn   ro   eval_jacobi  s
   :0r  c                 C  sT   t t| d |d |d|d   t|dft| d || d  t| dftjdfS )NrS   r   rt  T)r)   rN   r   rH   r   rr  r   rB  r   r   rn   rn   ro   eval_gegenbauer  s
   (r  c                 C  sP   t t| d || d  t| d || d   d tt| df|d d dfS )NrS   r   T)r)   rH   r   r   r   r   r   rn   rn   ro   eval_chebyshevt  s   r  c                 C  s,   t t| d || d  t| dftjdfS ri  )r)   rH   r   r   rr  r  rn   rn   ro   eval_chebyshevu  s   r  c                 C  s(   t | d |t | d | d|  d  S r  )rJ   r  rn   rn   ro   eval_legendre     (r  c                 C  s   t | d |d| d   S r  )rK   r  rn   rn   ro   eval_hermite  s   r  c                 C  s   t | |t | d | S r  )rL   r  rn   rn   ro   eval_laguerre  s   r  c                 C  s   t | d |d | S r  )rM   r  rn   rn   ro   eval_assoclaguerre     r  c                 C  s(   t |t| |  t|t| |   S rg   )r+   r>   r,   r@   rB  rH  r   r   rn   rn   ro   eval_ci  r  r  c                 C  (   t |t| |  t|t| |   S rg   )r    r?   r#   rA   r  rn   rn   ro   eval_chi  r  r  c                 C  s   t |t| |  S rg   )r   rB   r  rn   rn   ro   eval_ei     r  c                 C  r  rg   )r,   r>   r+   r@   r  rn   rn   ro   eval_si	  r  r  c                 C  r  rg   )r#   r?   r    rA   r  rn   rn   ro   eval_shi  r  r  c                 C  s   | j rVt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   rs  r3  r   T)r  r)   r'   r   Pir   r:   r;   r{  rn   rn   ro   eval_erf  s    * ((r  c                 C  s   t tjd|   t|d d|   | td|  | | t d|  tj   t|d d|   | td|  | | t d|  tj     S Nr   rs  )r'   r   r  r+   r<   r,   r=   r{  rn   rn   ro   eval_fresnelc  
   <<r  c                 C  s   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  r+   r=   r,   r<   r{  rn   rn   ro   eval_fresnels#  r  r  c                 C  s   t | | | |  S rg   )rC   r  rn   rn   ro   eval_li)  r  r  c                 C  s   t |d | | S r  rQ   r  rn   rn   ro   eval_polylog-  s   r  c                 C  s0   || |  | |   t |d |  |  |  S r  rD   )rB  rK  r   r   rn   rn   ro   eval_uppergamma1  s   0r  c                 C  s   t |||  t|  S rg   )rG   r'   rB  r
  r   r   rn   rn   ro   eval_elliptic_f5  r  r  c                 C  s   t |||  t|  S rg   )rF   r'   r  rn   rn   ro   eval_elliptic_e9  r  r  c                 C  s
   t | |S rg   rT   rn  rn   rn   ro   eval_dontknowrule=  s   
r  c                 C  s(   t | j}|stdt|  ||  S )NzCannot evaluate rule %s)r   r;  rh   r   repr)r   	evaluatorrn   rn   ro   r  A  s   r  c                 C  s   t 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   rS   T)r  r  r  clearr   r)   r   r   r   r   r   )r   r  r,  r_  rn   rn   ro   manualintegrateH  s   1r  N)rf   )T)r  r   )r  r  )r   ry  r   r   )r  r  (4  __doc__
__future__r   typingr   r   r   collectionsr   r   collections.abcr   	functoolsr	   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   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%   r&   (sympy.functions.elementary.miscellaneousr'   $sympy.functions.elementary.piecewiser)   (sympy.functions.elementary.trigonometricr*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   'sympy.functions.special.delta_functionsr8   r9   'sympy.functions.special.error_functionsr:   r;   r<   r=   r>   r?   r@   rA   rB   rC   'sympy.functions.special.gamma_functionsrE   *sympy.functions.special.elliptic_integralsrF   rG   #sympy.functions.special.polynomialsrH   rI   rJ   rK   rL   rM   rN   rO   rP   &sympy.functions.special.zeta_functionsrR   	integralsrU   sympy.logic.boolalgrV   sympy.ntheory.factor_rW   sympy.polys.polytoolsrX   rY   rZ   r[   sympy.simplify.radsimpr\   sympy.simplify.simplifyr^   sympy.solvers.solversr_   sympy.strategies.corer`   ra   rb   rc   sympy.utilities.iterablesrd   sympy.utilities.miscre   rx   ry   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   r2  r6  r8  r@  rA  r   rU  rT  r]  rf  r  r  r  r  r  r  r  r  ru  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  rB  rC  rD  rI  r_  r`  ra  rb  rU  rV  rW  rX  intr  r  r  rd  rg  rk  rm  rp  rq  rs  rv  rx  rz  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  rn   rn   rn   ro   <module>   sT   ,<0,











































!W,F]G)O,B




=5

&	