o
    8Va+?                     @   s   d dl mZmZ d dlmZ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mZmZ d dlmZ d dlmZ d dlm Z  d dl!m"Z"m#Z# dd Z$dddZ%dddZ&dS )    )SPow)iterableis_sequence)
DerivativeAppliedUndefdiff)EqualityEq)Dummy)sympify)BooleanAtom)exp)Order)simplifyposify
besselsimp)trigsimp)
sqrtdenest)solve)_preprocess	ode_orderc                 C   sX   ||i}|  tD ]}|j|kr|j|j ||< q	|||ijdd||< q	| |S )aa  
    When replacing the func with something else, we usually want the
    derivative evaluated, so this function helps in making that happen.

    Examples
    ========

    >>> from sympy import Derivative, symbols, Function
    >>> from sympy.solvers.ode.subscheck import sub_func_doit
    >>> x, z = symbols('x, z')
    >>> y = Function('y')

    >>> sub_func_doit(3*Derivative(y(x), x) - 1, y(x), x)
    2

    >>> sub_func_doit(x*Derivative(y(x), x) - y(x)**2 + y(x), y(x),
    ... 1/(x*(z + 1/x)))
    x*(-1/(x**2*(z + 1/x)) + 1/(x**3*(z + 1/x)**2)) + 1/(x*(z + 1/x))
    ...- 1/(x**2*(z + 1/x)**2)
    F)Zdeep)atomsr   exprr   Zvariable_countxreplacedoit)eqfuncnewrepsd r!   =/usr/lib/python3/dist-packages/sympy/solvers/ode/subscheck.pysub_func_doit   s   

r#   NautoTc              	      sh  t rt| dS ttstd du rOz	tj\} W n, tyN   dd t|t	r3|n|gD }t	 j
| }t|dkrHtd|  Y nw t tr[t jdkratd  t|t	rut|fd	d|D S t|tst |}n|j kr|j}d
krt |j ko|j  }r|st| }|rÇ fdd|D }	t|dkr|	d }	t|	ddS  jd }
|tr#|j ksJ |j }|j }|j}t|tsJ |j}|t|
| ksJ jj  |  }t|
|  }|| }| |ksJ | }|dk|fS d}d}|r|dkrfjj }|j krGt| |j}t |}n|d7 }q't!|"t}|r^|jdd}nd}|d7 }n+|dkrt!t#t$|j|
t$|j|
 t#j t#j }|d7 }n|dkr|j kr|j sd|ji}n|j kr|j sd|ji}ni }|j|j }t%dd D ]@}|dkr|$|
}zt| $|
|}|st&W n t&y   |d7 }Y  nw |d ||< q||d  $|
||< q|dkrq'jj}}t%ddD ]H}|dkr*d|vr* n;t| $|
||| }t| $|
||| }t||}t!|}t|t't(fr]|r[t)j* }}q|j}|j}qt#|| + d }t,d}| |}t-|\}}t!|.|.| i}|d7 }nn|s*|sd|fS |du rt&dt/| d t/ d d|fS )a  
    Substitutes ``sol`` into ``ode`` and checks that the result is ``0``.

    This works when ``func`` is one function, like `f(x)` or a list of
    functions like `[f(x), g(x)]` when `ode` is a system of ODEs.  ``sol`` can
    be a single solution or a list of solutions.  Each solution may be an
    :py:class:`~sympy.core.relational.Equality` that the solution satisfies,
    e.g. ``Eq(f(x), C1), Eq(f(x) + C1, 0)``; or simply an
    :py:class:`~sympy.core.expr.Expr`, e.g. ``f(x) - C1``. In most cases it
    will not be necessary to explicitly identify the function, but if the
    function cannot be inferred from the original equation it can be supplied
    through the ``func`` argument.

    If a sequence of solutions is passed, the same sort of container will be
    used to return the result for each solution.

    It tries the following methods, in order, until it finds zero equivalence:

    1. Substitute the solution for `f` in the original equation.  This only
       works if ``ode`` is solved for `f`.  It will attempt to solve it first
       unless ``solve_for_func == False``.
    2. Take `n` derivatives of the solution, where `n` is the order of
       ``ode``, and check to see if that is equal to the solution.  This only
       works on exact ODEs.
    3. Take the 1st, 2nd, ..., `n`\th derivatives of the solution, each time
       solving for the derivative of `f` of that order (this will always be
       possible because `f` is a linear operator). Then back substitute each
       derivative into ``ode`` in reverse order.

    This function returns a tuple.  The first item in the tuple is ``True`` if
    the substitution results in ``0``, and ``False`` otherwise. The second
    item in the tuple is what the substitution results in.  It should always
    be ``0`` if the first item is ``True``. Sometimes this function will
    return ``False`` even when an expression is identically equal to ``0``.
    This happens when :py:meth:`~sympy.simplify.simplify.simplify` does not
    reduce the expression to ``0``.  If an expression returned by this
    function vanishes identically, then ``sol`` really is a solution to
    the ``ode``.

    If this function seems to hang, it is probably because of a hard
    simplification.

    To use this function to test, test the first item of the tuple.

    Examples
    ========

    >>> from sympy import (Eq, Function, checkodesol, symbols,
    ...     Derivative, exp)
    >>> x, C1, C2 = symbols('x,C1,C2')
    >>> f, g = symbols('f g', cls=Function)
    >>> checkodesol(f(x).diff(x), Eq(f(x), C1))
    (True, 0)
    >>> assert checkodesol(f(x).diff(x), C1)[0]
    >>> assert not checkodesol(f(x).diff(x), x)[0]
    >>> checkodesol(f(x).diff(x, 2), x**2)
    (False, 2)

    >>> eqs = [Eq(Derivative(f(x), x), f(x)), Eq(Derivative(g(x), x), g(x))]
    >>> sol = [Eq(f(x), C1*exp(x)), Eq(g(x), C2*exp(x))]
    >>> checkodesol(eqs, sol)
    (True, [0, 0])

    r   r   Nc                 S      g | ]}| tqS r!   r   r   ).0sr!   r!   r"   
<listcomp>{       zcheckodesol.<locals>.<listcomp>   z0must pass func arg to checkodesol for this case./func must be a function of one variable, not %sc                    s   g | ]
}t  |d qS )ordersolve_for_func)checkodesol)r(   i)oder/   r0   r!   r"   r*      s    r$   c                    s   g | ]}t  |qS r!   )r
   )r(   tr%   r!   r"   r*      r+   Fr.   TZforce   r   zUnable to test if z is a solution to .)0r   checksysodesol
isinstancer	   r
   r   lhs
ValueErrorr   setunionlenpopr   argstyperhsreversedr   hasr   r1   r   ZgetOZremoveOr   r   r   Zsubsr   expandr#   r   r   Zrewriter   r   rangeNotImplementedErrorboolr   r   ZZeroZas_numer_denomr   r   r   str)r3   solr   r/   r0   _funcssolvedrC   eqsxZOtermZsolrhsZOexprZsorderZodesubsZneworderZresidualr)   ZtestnumZode_diffssZdiffsolsr2   ZdsZsdfr;   Zode_or_boolZnumZ_funcr   r!   )r   r3   r/   r0   r"   r1   1   s  A




















j
r1   c                 C   s(  dd }|| } t t| D ]}t| | tr#| | j| | j | |< q|du rOg }| D ]}|t}t j	dd |D  }|D ]}|
| q@q,tt|}tdd |D sitdd	 |D d
kritd| |D ]}	t|	td
krztdqkt|tdd	 |D krtdt }
|D ]2}	t|	td }|	j|kr|	j}	|	j|ko|	j| }|st|	|}|stn|	j}||
|< qg }| D ]/}|D ]
}t|||
| }qt|}|dkr|jdd}|dkrt| }nd}|
| qtt|d
krtt|d dkrd|fS d|fS )aw
  
    Substitutes corresponding ``sols`` for each functions into each ``eqs`` and
    checks that the result of substitutions for each equation is ``0``. The
    equations and solutions passed can be any iterable.

    This only works when each ``sols`` have one function only, like `x(t)` or `y(t)`.
    For each function, ``sols`` can have a single solution or a list of solutions.
    In most cases it will not be necessary to explicitly identify the function,
    but if the function cannot be inferred from the original equation it
    can be supplied through the ``func`` argument.

    When a sequence of equations is passed, the same sequence is used to return
    the result for each equation with each function substituted with corresponding
    solutions.

    It tries the following method to find zero equivalence for each equation:

    Substitute the solutions for functions, like `x(t)` and `y(t)` into the
    original equations containing those functions.
    This function returns a tuple.  The first item in the tuple is ``True`` if
    the substitution results for each equation is ``0``, and ``False`` otherwise.
    The second item in the tuple is what the substitution results in.  Each element
    of the ``list`` should always be ``0`` corresponding to each equation if the
    first item is ``True``. Note that sometimes this function may return ``False``,
    but with an expression that is identically equal to ``0``, instead of returning
    ``True``.  This is because :py:meth:`~sympy.simplify.simplify.simplify` cannot
    reduce the expression to ``0``.  If an expression returned by each function
    vanishes identically, then ``sols`` really is a solution to ``eqs``.

    If this function seems to hang, it is probably because of a difficult simplification.

    Examples
    ========

    >>> from sympy import Eq, diff, symbols, sin, cos, exp, sqrt, S, Function
    >>> from sympy.solvers.ode.subscheck import checksysodesol
    >>> C1, C2 = symbols('C1:3')
    >>> t = symbols('t')
    >>> x, y = symbols('x, y', cls=Function)
    >>> eq = (Eq(diff(x(t),t), x(t) + y(t) + 17), Eq(diff(y(t),t), -2*x(t) + y(t) + 12))
    >>> sol = [Eq(x(t), (C1*sin(sqrt(2)*t) + C2*cos(sqrt(2)*t))*exp(t) - S(5)/3),
    ... Eq(y(t), (sqrt(2)*C1*cos(sqrt(2)*t) - sqrt(2)*C2*sin(sqrt(2)*t))*exp(t) - S(46)/3)]
    >>> checksysodesol(eq, sol)
    (True, [0, 0])
    >>> eq = (Eq(diff(x(t),t),x(t)*y(t)**4), Eq(diff(y(t),t),y(t)**3))
    >>> sol = [Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), -sqrt(2)*sqrt(-1/(C2 + t))/2),
    ... Eq(x(t), C1*exp(-1/(4*(C2 + t)))), Eq(y(t), sqrt(2)*sqrt(-1/(C2 + t))/2)]
    >>> checksysodesol(eq, sol)
    (True, [0, 0])

    c                 S   s    t ttt| r| S | gS N)listmapr   r   )r   r!   r!   r"   _sympifyX  s    z checksysodesol.<locals>._sympifyNc                 S   r&   r!   r'   )r(   r    r!   r!   r"   r*   b  r+   z"checksysodesol.<locals>.<listcomp>c                 s   s(    | ]}t |tot|jd kV  qdS )r,   N)r:   r   r?   rA   r(   r   r!   r!   r"   	<genexpr>f  s   & z!checksysodesol.<locals>.<genexpr>c                 S      h | ]}|j qS r!   )rA   rV   r!   r!   r"   	<setcomp>g      z!checksysodesol.<locals>.<setcomp>r,   r-   z'solutions should have one function onlyc                 S   rX   r!   )r;   )r(   rK   r!   r!   r"   rY   l  rZ   zCnumber of solutions provided does not match the number of equationsr   Tr5   F)rG   r?   r:   r	   r;   rC   r   r   r=   r>   appendrS   allr<   r   dictrD   rE   r   rH   r#   r   rF   r   )rO   Zsolsr   rU   r2   rM   r   ZderivsZfunc_rK   ZdictsolrN   rC   ZcheckeqrQ   r!   r!   r"   r9   $  sj   4



(r9   )Nr$   TrR   )'Z
sympy.corer   r   Zsympy.core.compatibilityr   r   Zsympy.core.functionr   r   r   Zsympy.core.relationalr	   r
   Zsympy.core.symbolr   Zsympy.core.sympifyr   Zsympy.logic.boolalgr   Zsympy.functionsr   Zsympy.seriesr   Zsympy.simplify.simplifyr   r   r   Zsympy.simplify.trigsimpr   Zsympy.simplify.sqrtdenestr   Zsympy.solversr   Zsympy.solvers.deutilsr   r   r#   r1   r9   r!   r!   r!   r"   <module>   s$    
 t