o
    8Vab(                     @   s^   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddd	Z
d
d ZdddddZdS )zUtility functions for classifying and solving
ordinary and partial differential equations.

Contains
========
_preprocess
ode_order
_desolve

    )Pow)
DerivativeAppliedUndef)Equality)WildN	_Integralc                    s   t | tr| jjr| j} | t} s.t jdd |D  }t	|dkr*t
d|  |  t jdu r;|  fS  fdd|D }| |}| fS )a  Prepare expr for solving by making sure that differentiation
    is done so that only func remains in unevaluated derivatives and
    (if hint doesn't end with _Integral) that doit is applied to all
    other derivatives. If hint is None, don't do any differentiation.
    (Currently this may cause some simple differential equations to
    fail.)

    In case func is None, an attempt will be made to autodetect the
    function to be solved for.

    >>> from sympy.solvers.deutils import _preprocess
    >>> from sympy import Derivative, Function
    >>> from sympy.abc import x, y, z
    >>> f, g = map(Function, 'fg')

    If f(x)**p == 0 and p>0 then we can solve for f(x)=0
    >>> _preprocess((f(x).diff(x)-4)**5, f(x))
    (Derivative(f(x), x) - 4, f(x))

    Apply doit to derivatives that contain more than the function
    of interest:

    >>> _preprocess(Derivative(f(x) + x, x))
    (Derivative(f(x), x) + 1, f(x))

    Do others if the differentiation variable(s) intersect with those
    of the function of interest or contain the function of interest:

    >>> _preprocess(Derivative(g(x), y, z), f(y))
    (0, f(y))
    >>> _preprocess(Derivative(f(y), z), f(y))
    (0, f(y))

    Do others if the hint doesn't end in '_Integral' (the default
    assumes that it does):

    >>> _preprocess(Derivative(g(x), y), f(x))
    (Derivative(g(x), y), f(x))
    >>> _preprocess(Derivative(f(x), y), f(x), hint='')
    (0, f(x))

    Don't do any derivatives if hint is None:

    >>> eq = Derivative(f(x) + 1, x) + Derivative(f(x), y)
    >>> _preprocess(eq, f(x), hint=None)
    (Derivative(f(x) + 1, x) + Derivative(f(x), y), f(x))

    If it's not clear what the function of interest is, it must be given:

    >>> eq = Derivative(f(x) + g(x), x)
    >>> _preprocess(eq, g(x))
    (Derivative(f(x), x) + Derivative(g(x), x), g(x))
    >>> try: _preprocess(eq)
    ... except ValueError: print("A ValueError was raised.")
    A ValueError was raised.

    c                 S   s   g | ]}| tqS  )atomsr   .0dr   r   7/usr/lib/python3/dist-packages/sympy/solvers/deutils.py
<listcomp>P   s    z_preprocess.<locals>.<listcomp>   z5The function cannot be automatically detected for %s.Nc                    s:   g | ]} d r| st|j@ r|| fqS )r   )endswithZhasset	variablesZdoitr
   funcZfvarshintr   r   r   X   s
    )
isinstancer   ZexpZis_positivebaser	   r   r   unionlen
ValueErrorpopargsZsubs)exprr   r   ZderivsZfuncsZrepseqr   r   r   _preprocess   s"   
:


r   c                 C   s   t d|gd}| |rdS t| tr9| jd |krt| jS d}| jd jD ]}t|t||t| j }q'|S d}| jD ]
}t|t||}q>|S )a  
    Returns the order of a given differential
    equation with respect to func.

    This function is implemented recursively.

    Examples
    ========

    >>> from sympy import Function
    >>> from sympy.solvers.deutils import ode_order
    >>> from sympy.abc import x
    >>> f, g = map(Function, ['f', 'g'])
    >>> ode_order(f(x).diff(x, 2) + f(x).diff(x)**2 +
    ... f(x).diff(x), f(x))
    2
    >>> ode_order(f(x).diff(x, 2) + g(x).diff(x, 3), f(x))
    2
    >>> ode_order(f(x).diff(x, 2) + g(x).diff(x, 3), g(x))
    3

    a)Zexcluder   )	r   matchr   r   r   r   r   max	ode_order)r   r   r    orderargr   r   r   r#   ]   s   



r#   defaultT)prepc                K   s  t | tr| j| j } |s|du rt| |\} }d}|dd}|d}|d}	|dd}
|d}|d	krHdd
lm}m} |}d}d}n|dkrZddl	m
}m} |}d}d}|ddro|| |d|||	||
||d
}n|dd|||d d|d i}|d s||vr|dkrtd| ||d vr|dkrt|t|  d | |d dkrtt| d t| t|d d t|  |dkrt| |||d |||
d|d ||d  ||	||dS |dv rFi }t|h d  }|d!kr|D ]}|d"r||dtd"   qd#D ]}||v r|| q|D ]}t| ||||
||d||d || |d$}|||< q d|d%< | |d&< |S ||vrQtd| ||vrbt|t|  d | ||d< ||| d' |S )(a^  This is a helper function to dsolve and pdsolve in the ode
    and pde modules.

    If the hint provided to the function is "default", then a dict with
    the following keys are returned

    'func'    - It provides the function for which the differential equation
                has to be solved. This is useful when the expression has
                more than one function in it.

    'default' - The default key as returned by classifier functions in ode
                and pde.py

    'hint'    - The hint given by the user for which the differential equation
                is to be solved. If the hint given by the user is 'default',
                then the value of 'hint' and 'default' is the same.

    'order'   - The order of the function as returned by ode_order

    'match'   - It returns the match as given by the classifier functions, for
                the default hint.

    If the hint provided to the function is not "default" and is not in
    ('all', 'all_Integral', 'best'), then a dict with the above mentioned keys
    is returned along with the keys which are returned when dict in
    classify_ode or classify_pde is set True

    If the hint given is in ('all', 'all_Integral', 'best'), then this function
    returns a nested dict, with the keys, being the set of classified hints
    returned by classifier functions, and the values being the dict of form
    as mentioned above.

    Key 'eq' is a common key to all the above mentioned hints which returns an
    expression if eq given by user is an Equality.

    See Also
    ========
    classify_ode(ode.py)
    classify_pde(pde.py)
    NFtypexietax0r   nZode)classify_odeallhintszODE  Zpde)classify_pder.   zPDE pclassifyT)dicticsr)   r*   r,   r+   r   r'   r   r&   r!   r$   zHint not recognized: ordered_hintsz does not match hint z, is not a solvable differential equation in Zsolvez: Cannot solve )r4   r   simplifyr'   r+   r2   r$   r!   r)   r*   r,   r(   )allall_IntegralZbest>   r$   r&   r5   r8   r   )Z1st_homogeneous_coeff_bestZ1st_power_seriesZ	lie_groupZ2nd_power_series_ordinaryZ2nd_power_series_regular)
r4   r   r+   r6   r'   r2   r,   r$   r!   r(   r7   r   )r   r   )r   r   ZlhsZrhsr   getZsympy.solvers.oder-   r.   Zsympy.solvers.pder0   r   strNotImplementedError_desolver   r   remover   update)r   r   r   r4   r6   r'   kwargsr(   r)   r*   r+   Ztermsr-   r.   Z
classifierstringZdummyr0   ZhintsZretdictZgethintsikZsolr   r   r   r<      s   
)








r<   )Nr   )Nr&   NT)__doc__Z
sympy.corer   Zsympy.core.functionr   r   Zsympy.core.relationalr   Zsympy.core.symbolr   r   r#   r<   r   r   r   r   <module>   s    

M)