§
    JŠtj´&  ã                   óŒ   — d dl m Z  ddlmZ  G d„ de¦  «        Zd„ Zdd
„Zee_        edk    rd dlZ ej	        ¦   «          dS dS )é    )Úbisecté   )Úxrangec                   ó   — e Zd ZdS )Ú
ODEMethodsN)Ú__name__Ú
__module__Ú__qualname__© ó    úR/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/mpmath/calculus/odes.pyr   r      s   € € € € € Ø€Dr   r   c                 óL  ‡‡‡— |                       d| ¦  «        xŠ}t          |¦  «        }|g}|g}	|}
|Š| j        }	 |d|z   z  | _        t          |¦  «        D ]g} ||
‰¦  «        Šˆˆˆfd„t	          t          ‰¦  «        ¦  «        D ¦   «         Š|
‰z  }
|                     |
¦  «         |	                     ‰¦  «         Œhd„ t          |¦  «        D ¦   «         }t          |dz   ¦  «        D ]É}dg|z  }d|dz  z  }d}t          |dz   ¦  «        D ]G}t          |¦  «        D ]!}||xx         ||	|         |         z  z  cc<   Œ"|||z
  dz   z  | z  }|dz  }ŒH‰| z  |                      |¦  «        z  }t          |¦  «        D ]1}||         |z  ||<   ||                              ||         ¦  «         Œ2ŒÊ	 || _        n# || _        w xY w| j        }|D ]D}|d         r:t          ||  	                    |t          |d         ¦  «        z  |¦  «        ¦  «        }ŒE|dz  }|||z   fS )Né   c                 ó8   •— g | ]}‰|         ‰‰|         z  z   ‘ŒS r   r   )Ú.0ÚiÚfxyÚhÚys     €€€r   ú
<listcomp>zode_taylor.<locals>.<listcomp>   s)   ø€ Ð7Ð7Ð7 1��1”�a˜˜Aœ‘h‘Ð7Ð7Ð7r   c                 ó   — g | ]}g ‘ŒS r   r   )r   Úds     r   r   zode_taylor.<locals>.<listcomp>   s   € Ð&Ð&Ð&�aˆrÐ&Ð&Ð&r   r   éÿÿÿÿr   )ÚldexpÚlenÚprecÚranger   ÚappendÚfacÚoneÚminÚnthrootÚabs)ÚctxÚderivsÚx0Úy0Útol_precÚnÚtolÚdimÚxsÚysÚxÚorigr   ÚserÚjÚsÚbÚkr   ÚscaleÚradiusÚtsr   r   r   s                         @@@r   Ú
ode_taylorr8      s†  øøø€ Ø�iŠi˜˜H˜9Ñ%Ô%Ð%€AˆÝ
ˆb‰'Œ'€CØ
ˆ€BØ
ˆ€BØ
€AØ
€AØŒ8€DðØ˜˜1™‘:ˆŒõ �q‘”ð 	ð 	ˆAØ�&˜˜A‘,”,ˆCØ7Ð7Ð7Ð7Ð7Ð7­­s°1©v¬v©¬Ð7Ñ7Ô7ˆAØ�‰FˆAØ�IŠI�a‰LŒLˆLØ�IŠI�a‰LŒLˆLˆLà&Ð&�5 ™:œ:Ð&Ñ&Ô&ˆÝ�q˜‘s‘”ð 	$ð 	$ˆAØ��C‘ˆAØ˜˜Q™‘ˆAØˆAÝ˜1˜Q™3‘Z”Zð ð �Ý˜s™œð )ð )�AØ�a�D�D”D˜A  1¤ a¤™LÑ(�D�D‘D�DØ˜!˜A™#˜a™%‘[ q bÑ)�Ø�Q‘��Ø˜˜‘G˜cŸgšg a™jœjÑ(ˆEÝ˜3‘Z”Zð $ð $�Ø˜”t˜e‘|��!‘Ø�A”—’˜a œdÑ#Ô#Ð#Ð#ð$ð	$ð ˆŒˆø�4ˆŒˆˆˆˆð ŒW€FØð Bð BˆØˆbŒ6ð 	BÝ˜ §¢¨Sµ°R¸´V±´©_¸aÑ!@Ô!@ÑAÔAˆFøØ
ˆa�K€FØ��6‘	ˆ>Ðs   ¾E9G  Ç 	G	NÚtaylorFc           	      ó°  ‡ ‡‡‡‡‡‡‡‡‡‡‡‡— |r(t          ‰                      |d¦  «         ¦  «        dz   Šn
‰ j        dz   Š‰pdt          d‰ j        z  dz  ¦  «        z   Š‰ j        dz   Š	 t	          |¦  «         dŠn# t
          $ r ‰Šˆfd„Š|g}dŠY nw xY wt          ‰ ‰‰|‰‰¦  «        \  }}	‰|	gŠ|‰|	fgŠˆ fd	„Šˆˆ ˆˆˆˆˆˆˆf	d
„Šˆ ˆˆˆˆfd„}
|
S )aû  
    Returns a function `y(x) = [y_0(x), y_1(x), \ldots, y_n(x)]`
    that is a numerical solution of the `n+1`-dimensional first-order
    ordinary differential equation (ODE) system

    .. math ::

        y_0'(x) = F_0(x, [y_0(x), y_1(x), \ldots, y_n(x)])

        y_1'(x) = F_1(x, [y_0(x), y_1(x), \ldots, y_n(x)])

        \vdots

        y_n'(x) = F_n(x, [y_0(x), y_1(x), \ldots, y_n(x)])

    The derivatives are specified by the vector-valued function
    *F* that evaluates
    `[y_0', \ldots, y_n'] = F(x, [y_0, \ldots, y_n])`.
    The initial point `x_0` is specified by the scalar argument *x0*,
    and the initial value `y(x_0) =  [y_0(x_0), \ldots, y_n(x_0)]` is
    specified by the vector argument *y0*.

    For convenience, if the system is one-dimensional, you may optionally
    provide just a scalar value for *y0*. In this case, *F* should accept
    a scalar *y* argument and return a scalar. The solution function
    *y* will return scalar values instead of length-1 vectors.

    Evaluation of the solution function `y(x)` is permitted
    for any `x \ge x_0`.

    A high-order ODE can be solved by transforming it into first-order
    vector form. This transformation is described in standard texts
    on ODEs. Examples will also be given below.

    **Options, speed and accuracy**

    By default, :func:`~mpmath.odefun` uses a high-order Taylor series
    method. For reasonably well-behaved problems, the solution will
    be fully accurate to within the working precision. Note that
    *F* must be possible to evaluate to very high precision
    for the generation of Taylor series to work.

    To get a faster but less accurate solution, you can set a large
    value for *tol* (which defaults roughly to *eps*). If you just
    want to plot the solution or perform a basic simulation,
    *tol = 0.01* is likely sufficient.

    The *degree* argument controls the degree of the solver (with
    *method='taylor'*, this is the degree of the Taylor series
    expansion). A higher degree means that a longer step can be taken
    before a new local solution must be generated from *F*,
    meaning that fewer steps are required to get from `x_0` to a given
    `x_1`. On the other hand, a higher degree also means that each
    local solution becomes more expensive (i.e., more evaluations of
    *F* are required per step, and at higher precision).

    The optimal setting therefore involves a tradeoff. Generally,
    decreasing the *degree* for Taylor series is likely to give faster
    solution at low precision, while increasing is likely to be better
    at higher precision.

    The function
    object returned by :func:`~mpmath.odefun` caches the solutions at all step
    points and uses polynomial interpolation between step points.
    Therefore, once `y(x_1)` has been evaluated for some `x_1`,
    `y(x)` can be evaluated very quickly for any `x_0 \le x \le x_1`.
    and continuing the evaluation up to `x_2 > x_1` is also fast.

    **Examples of first-order ODEs**

    We will solve the standard test problem `y'(x) = y(x), y(0) = 1`
    which has explicit solution `y(x) = \exp(x)`::

        >>> from mpmath import *
        >>> mp.dps = 15; mp.pretty = True
        >>> f = odefun(lambda x, y: y, 0, 1)
        >>> for x in [0, 1, 2.5]:
        ...     print((f(x), exp(x)))
        ...
        (1.0, 1.0)
        (2.71828182845905, 2.71828182845905)
        (12.1824939607035, 12.1824939607035)

    The solution with high precision::

        >>> mp.dps = 50
        >>> f = odefun(lambda x, y: y, 0, 1)
        >>> f(1)
        2.7182818284590452353602874713526624977572470937
        >>> exp(1)
        2.7182818284590452353602874713526624977572470937

    Using the more general vectorized form, the test problem
    can be input as (note that *f* returns a 1-element vector)::

        >>> mp.dps = 15
        >>> f = odefun(lambda x, y: [y[0]], 0, [1])
        >>> f(1)
        [2.71828182845905]

    :func:`~mpmath.odefun` can solve nonlinear ODEs, which are generally
    impossible (and at best difficult) to solve analytically. As
    an example of a nonlinear ODE, we will solve `y'(x) = x \sin(y(x))`
    for `y(0) = \pi/2`. An exact solution happens to be known
    for this problem, and is given by
    `y(x) = 2 \tan^{-1}\left(\exp\left(x^2/2\right)\right)`::

        >>> f = odefun(lambda x, y: x*sin(y), 0, pi/2)
        >>> for x in [2, 5, 10]:
        ...     print((f(x), 2*atan(exp(mpf(x)**2/2))))
        ...
        (2.87255666284091, 2.87255666284091)
        (3.14158520028345, 3.14158520028345)
        (3.14159265358979, 3.14159265358979)

    If `F` is independent of `y`, an ODE can be solved using direct
    integration. We can therefore obtain a reference solution with
    :func:`~mpmath.quad`::

        >>> f = lambda x: (1+x**2)/(1+x**3)
        >>> g = odefun(lambda x, y: f(x), pi, 0)
        >>> g(2*pi)
        0.72128263801696
        >>> quad(f, [pi, 2*pi])
        0.72128263801696

    **Examples of second-order ODEs**

    We will solve the harmonic oscillator equation `y''(x) + y(x) = 0`.
    To do this, we introduce the helper functions `y_0 = y, y_1 = y_0'`
    whereby the original equation can be written as `y_1' + y_0' = 0`. Put
    together, we get the first-order, two-dimensional vector ODE

    .. math ::

        \begin{cases}
        y_0' = y_1 \\
        y_1' = -y_0
        \end{cases}

    To get a well-defined IVP, we need two initial values. With
    `y(0) = y_0(0) = 1` and `-y'(0) = y_1(0) = 0`, the problem will of
    course be solved by `y(x) = y_0(x) = \cos(x)` and
    `-y'(x) = y_1(x) = \sin(x)`. We check this::

        >>> f = odefun(lambda x, y: [-y[1], y[0]], 0, [1, 0])
        >>> for x in [0, 1, 2.5, 10]:
        ...     nprint(f(x), 15)
        ...     nprint([cos(x), sin(x)], 15)
        ...     print("---")
        ...
        [1.0, 0.0]
        [1.0, 0.0]
        ---
        [0.54030230586814, 0.841470984807897]
        [0.54030230586814, 0.841470984807897]
        ---
        [-0.801143615546934, 0.598472144103957]
        [-0.801143615546934, 0.598472144103957]
        ---
        [-0.839071529076452, -0.54402111088937]
        [-0.839071529076452, -0.54402111088937]
        ---

    Note that we get both the sine and the cosine solutions
    simultaneously.

    **TODO**

    * Better automatic choice of degree and step size
    * Make determination of Taylor series convergence radius
      more robust
    * Allow solution for `x < x_0`
    * Allow solution for complex `x`
    * Test for difficult (ill-conditioned) problems
    * Implement Runge-Kutta and other algorithms

    r   é
   é   g       @é(   Tc                 ó*   •—  ‰| |d         ¦  «        gS )Nr   r   )r.   r   ÚF_s     €r   ú<lambda>zodefun.<locals>.<lambda>ñ   s   ø€ ˜"˜"˜Q  !¤™+œ+˜€ r   Fc                 ó$   •‡— ˆˆfd„| D ¦   «         S )Nc                 óN   •— g | ]!}‰                      |d d d…         ‰¦  «        ‘Œ"S )Nr   )Úpolyval)r   r2   Úar$   s     €€r   r   z,odefun.<locals>.mpolyval.<locals>.<listcomp>ù   s1   ø€ Ð5Ð5Ð5¨A�—’˜A˜d˜d ˜dœG QÑ'Ô'Ð5Ð5Ð5r   r   )r0   rD   r$   s    `€r   Úmpolyvalzodefun.<locals>.mpolyvalø   s!   øø€ Ø5Ð5Ð5Ð5Ð5°Ð5Ñ5Ô5Ð5r   c                 óˆ  •	— | ‰k     rt           ‚t          ‰
| ¦  «        }|t          ‰
¦  «        k     r‰|dz
           S 	 ‰d         \  }}}‰rt          d||fz  ¦  «          ‰	|||z
  ¦  «        }|}t	          ‰‰||‰‰¦  «        \  }}‰
                     |¦  «         ‰                     |||f¦  «         | |k    r‰d         S Œ†)Nr   r   z$Computing Taylor series for [%f, %f])Ú
ValueErrorr   r   Úprintr8   r   )r.   r)   r0   ÚxaÚxbr   ÚFr$   ÚdegreerE   Úseries_boundariesÚseries_datar(   Úverboser&   s         €€€€€€€€€r   Ú
get_serieszodefun.<locals>.get_seriesû   sõ   ø€ ØˆrŠ6ˆ6ÝÐÝÐ$ aÑ(Ô(ˆØ�sÐ$Ñ%Ô%Ò%Ð%Ø˜q ™sÔ#Ð#ð
	'Ø% bœ/‰KˆC��RØð IÝÐ<ÀÀB¸xÑGÑHÔHÐHØ�˜˜b ™eÑ$Ô$ˆAØˆBÝ   a¨¨Q°¸&ÑAÔA‰GˆC�Ø×$Ò$ RÑ(Ô(Ð(Ø×Ò  R¨˜}Ñ-Ô-Ð-Ø�BŠwˆwØ" 2”Ð&ð
	'r   c                 óÞ   •— ‰                      | ¦  «        } ‰j        }	 ‰
‰_         ‰| ¦  «        \  }}} ‰|| |z
  ¦  «        }|‰_        n# |‰_        w xY w‰	rd„ |D ¦   «         S |d         
 S )Nc                 ó   — g | ]}|
 ‘ŒS r   r   )r   Úyks     r   r   z/odefun.<locals>.interpolant.<locals>.<listcomp>  s   € Ð$Ð$Ð$˜B�R�CÐ$Ð$Ð$r   r   )Úconvertr   )r.   r/   r0   rI   rJ   r   r$   rP   rE   Úreturn_vectorÚworkprecs         €€€€€r   Úinterpolantzodefun.<locals>.interpolant  sŒ   ø€ Ø�KŠK˜‰NŒNˆØŒxˆð	ØˆCŒHØ$˜* Q™-œ-‰KˆC��RØ�˜˜a ™dÑ#Ô#ˆAàˆCŒHˆHø�tˆCŒHˆOˆOˆOˆOØð 	Ø$Ð$ !Ð$Ñ$Ô$Ð$à�a”D�5ˆLs   Ÿ%A Á	A)ÚintÚlogr   Údpsr   Ú	TypeErrorr8   )r$   rK   r&   r'   r*   rL   ÚmethodrO   r0   rJ   rW   r?   rP   rE   rU   rM   rN   r(   rV   s   ```  ` `   @@@@@@@@r   Úodefunr]   3   sŒ  øøøøøøøøøøøøø€ ðf ð Ý˜Ÿš  Q™œÐ'Ñ(Ô(¨Ñ+ˆˆà”8˜B‘;ˆØÐ.˜�C  #¤'¡	¨"¡Ñ-Ô-Ñ-€FØŒx˜"‰}€HðÝˆB‰ŒˆØˆˆøÝð ð ð ØˆØ&Ð&Ð&Ð&ˆØˆTˆØˆˆˆð	øøøõ
 ˜˜a  R¨°6Ñ:Ô:�G€CˆØ˜R˜ÐØ˜˜R�=�/€Kð6ð 6ð 6ð 6ð 6ð'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð 'ð$ð ð ð ð ð ð ð ð ð Ðs   Á,A> Á>BÂBÚ__main__)NNr9   F)
r   Úlibmp.backendr   Úobjectr   r8   r]   r   ÚdoctestÚtestmodr   r   r   ú<module>rc      s¸   ðØ Ð Ð Ð Ð Ð Ø "Ð "Ð "Ð "Ð "Ð "ð	ð 	ð 	ð 	ð 	�ñ 	ô 	ð 	ð*ð *ð *ðXgð gð gð gðR €
Ô àˆzÒÐØ€N€N€NØ€G„OÑÔÐÐÐð Ðr   