o
    8Va                     @   s>  d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlZd dlZd dlmZ edZergd d	lmZmZ d d
lmZmZmZ d dlmZmZ edddd ZeeZ G dd dZ!edddd Z"edddd Z#edddddZ$edddddZ%edddd Z&dS )     )import_module)doctest_depends_on)IntegerFloat)PowAddIntegralMulSFunctionE)expN)powsimpmatchpy)ManyToOneReplacerManyToOneMatcher)rubi_exprubi_unevaluated_exprprocess_trig)op_iterop_len)r   )modulesc                  C   s  ddl m}  ddlm} ddlm} ddlm} ddlm	} ddl
m} ddlm} dd	lm} dd
lm} ddlm}	 ddlm}
 ddlm} ddlm} ddlm} ddlm} ddlm} ddl m!} g }||  7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }||	 7 }||
 7 }|| 7 }|| 7 }|| 7 }|| 7 }|| 7 }t"| }||fS )a9  
        Returns rubi ManyToOneReplacer by adding all rules from different modules.

        Uncomment the lines to add integration capabilities of that module.

        Currently, there are parsing issues with special_function,
        derivative and miscellaneous_integration. Hence they are commented.
        r   )integrand_simplification)linear_products)quadratic_products)binomial_products)trinomial_products)miscellaneous_algebraic)exponential)
logarithms)sine)tangent)secant)miscellaneous_trig)inverse_trig)
hyperbolic)inverse_hyperbolic)special_functions)miscellaneous_integration)#Z3sympy.integrals.rubi.rules.integrand_simplificationr   Z*sympy.integrals.rubi.rules.linear_productsr   Z-sympy.integrals.rubi.rules.quadratic_productsr   Z,sympy.integrals.rubi.rules.binomial_productsr   Z-sympy.integrals.rubi.rules.trinomial_productsr   Z2sympy.integrals.rubi.rules.miscellaneous_algebraicr   Z&sympy.integrals.rubi.rules.exponentialr   Z%sympy.integrals.rubi.rules.logarithmsr   Zsympy.integrals.rubi.rules.siner    Z"sympy.integrals.rubi.rules.tangentr!   Z!sympy.integrals.rubi.rules.secantr"   Z-sympy.integrals.rubi.rules.miscellaneous_trigr#   Z'sympy.integrals.rubi.rules.inverse_trigr$   Z%sympy.integrals.rubi.rules.hyperbolicr%   Z-sympy.integrals.rubi.rules.inverse_hyperbolicr&   Z,sympy.integrals.rubi.rules.special_functionsr'   Z4sympy.integrals.rubi.rules.miscellaneous_integrationr(   r   )r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   rulesrubi r+   ?/usr/lib/python3/dist-packages/sympy/integrals/rubi/rubimain.pyget_rubi_object   sJ   

















r-   c                   @   sD   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )LoadRubiReplacerz-
    Class trick to load RUBI only once.
    Nc                 C   sD   t d u r
td d S tjd urtjS t| }d |_d |_|t_|S )NzMatchPy library not found)r   printr.   	_instanceobject__new___rubi_rules)clsobjr+   r+   r,   r2   R   s   

zLoadRubiReplacer.__new__c                 C   s*   | j d ur| j S t \}}|| _ || _|S N)r3   r-   r4   )selfr*   r)   r+   r+   r,   load^   s   

zLoadRubiReplacer.loadc                 C   L   dd l }|  }t|d}||| W d    d S 1 sw   Y  d S Nr   wb)pickler9   opendump)r8   filenamer=   r*   foutr+   r+   r,   	to_picklef   
   "zLoadRubiReplacer.to_picklec                 C   r:   r;   )dillr9   r>   r?   )r8   r@   rD   r*   rA   r+   r+   r,   to_dilll   rC   zLoadRubiReplacer.to_dillc                 C   H   dd l }t|d}||| _W d    | jS 1 sw   Y  | jS Nr   rb)r=   r>   r9   r3   )r8   r@   r=   finr+   r+   r,   from_pickler      
zLoadRubiReplacer.from_picklec                 C   rF   rG   )rD   r>   r9   r3   )r8   r@   rD   rI   r+   r+   r,   	from_dillx   rK   zLoadRubiReplacer.from_dill)__name__
__module____qualname____doc__r0   r2   r9   rB   rE   rJ   rL   r+   r+   r+   r,   r.   K   s    r.   c                 C   s   |  tr| tt} | S )a  
    Rubi's `rubi_exp` need to be replaced back to SymPy's general `exp`.

    Examples
    ========
    >>> from sympy import Function, E, Integral
    >>> from sympy.integrals.rubi.rubimain import process_final_integral
    >>> from sympy.integrals.rubi.utility_function import rubi_unevaluated_expr
    >>> from sympy.abc import a, x
    >>> _E = rubi_unevaluated_expr(E)
    >>> process_final_integral(Integral(a, x))
    Integral(a, x)
    >>> process_final_integral(_E**5)
    exp(5)

    )Zhas_Ereplacer   )exprr+   r+   r,   process_final_integral   s   
rT   c                 C   s\   g }g }t | tr,| jD ]}t |tttfr|| q|| qtt| t|  S | S )a@  
    This function is needed to preprocess an expression as done in matchpy
    `x^a*x^b` in matchpy auotmatically transforms to `x^(a+b)`

    Examples
    ========

    >>> from sympy.integrals.rubi.rubimain import rubi_powsimp
    >>> from sympy.abc import a, b, x
    >>> rubi_powsimp(x**a*x**b)
    x**(a + b)

    )
isinstancer	   argsr   r   sym_expappendr   )rS   Zlst_powZlst_non_powir+   r+   r,   rubi_powsimp   s   

rZ   Fc                 C   s   t   }| tt} t| } t| } t| tt	fs!t| t
tfr't| | S t| trBd}| jD ]}||t||7 }q1t|S tt| |}t|S )aW  
    Rule based algorithm for integration. Integrates the expression by applying
    transformation rules to the expression.

    Returns `Integrate` if an expression cannot be integrated.

    Parameters
    ==========
    expr : integrand expression
    var : variable of integration

    Returns Integral object if unable to integrate.
    r   )r.   r9   rR   rW   r   r   rZ   rU   intr   floatr   r
   r   rV   r   rT   util_rubi_integrate)rS   var	showstepsr*   resultsexr+   r+   r,   rubi_integrate   s   


rb   
   c                    sX   t    t| } | tt} t|D ]}| dd  fdd}| |kr)|  S q|S )Nc                 S   s
   t | tS r7   )rU   r   xr+   r+   r,   <lambda>   s   
 z%util_rubi_integrate.<locals>.<lambda>c                    s    j | ddS )Nrc   )Z	max_count)rR   rd   r*   r+   r,   rf      s    )r.   r9   r   rR   rW   r   range)rS   r_   Zmax_looprY   r`   r+   rg   r,   r]      s   

r]   c           
      C   s   t  }|j}|t| |}|D ]C\}}td tt| t|\}}td| td| td t	t
d|d d}	t|j|	d   t| t  qdS )	z
    Prints the list or rules which match to `expr`.

    Parameters
    ==========
    expr : integrand expression
    var : variable of integration
    zRule matching: z	On line: 
zPattern matching: z^\s*rule(\d+)r      N)r.   matchermatchr   r/   inspectZgetsourcefileZgetsourcelinesjoinr[   regrouppatterns)
rS   r^   r*   rk   ZmiterZfunecodelinenoZpattnor+   r+   r,   get_matching_rule_definition   s   

ru   )F)Frc   )'Zsympy.externalr   Zsympy.utilities.decoratorr   Z
sympy.corer   r   Zsympyr   r   r   r	   r
   r   r   Zsympy.functionsr   rW   rm   ro   r   r   r   r   Z%sympy.integrals.rubi.utility_functionr   r   r   Z!sympy.utilities.matchpy_connectorr   r   r-   rQ   r.   rT   rZ   rb   r]   ru   r+   r+   r+   r,   <module>   s6    $
44

