Module ImportFrom alias ImportFrom alias ClassDef bases NameLoad body Pass decorator_list FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam defaults body Assign NameStore NameStore Call AttributeLoad NameLoad attr Num UnaryOpUSub NameLoad Assign NameStore Call NameLoad NameLoad Assign NameStore ListLoad NameLoad Assign NameStore ListLoad NameLoad Assign NameStore NameLoad Assign NameStore NameLoad Assign NameStore AttributeLoad NameLoad attr TryFinally body Assign AttributeStore NameLoad attr BinOpMult NameLoad BinOpAdd Num NameLoad For NameStore Call NameLoad NameLoad body Assign NameStore Call NameLoad NameLoad NameLoad Assign NameStore ListComp BinOpAdd SubscriptLoad NameLoad Index NameLoad BinOpMult NameLoad SubscriptLoad NameLoad Index NameLoad comprehension NameStore Call NameLoad Call NameLoad NameLoad AugAssignAdd NameStore NameLoad Expr Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr NameLoad Assign NameStore ListComp ListLoad comprehension NameStore Call NameLoad NameLoad For NameStore Call NameLoad BinOpAdd NameLoad Num body Assign NameStore BinOpMult ListLoad Num NameLoad Assign NameStore BinOpPow Num BinOpBitAnd NameLoad Num Assign NameStore Num For NameStore Call NameLoad BinOpAdd NameLoad Num body For NameStore Call NameLoad NameLoad body AugAssignAdd SubscriptStore NameLoad Index NameLoad BinOpMult NameLoad SubscriptLoad SubscriptLoad NameLoad Index NameLoad Index NameLoad Assign NameStore BinOpFloorDiv BinOpMult NameLoad BinOpAdd BinOpSub NameLoad NameLoad Num UnaryOpUSub NameLoad AugAssignAdd NameStore Num Assign NameStore BinOpDiv BinOpPow NameLoad UnaryOpUSub NameLoad Call AttributeLoad NameLoad attr NameLoad For NameStore Call NameLoad NameLoad body Assign SubscriptStore NameLoad Index NameLoad BinOpMult SubscriptLoad NameLoad Index NameLoad NameLoad Expr Call AttributeLoad SubscriptLoad NameLoad Index NameLoad attr SubscriptLoad NameLoad Index NameLoad finalbody Assign AttributeStore NameLoad attr NameLoad Assign NameStore AttributeLoad NameLoad attr For NameStore NameLoad body If SubscriptLoad NameLoad Index Num body Assign NameStore Call NameLoad NameLoad Call AttributeLoad NameLoad attr BinOpDiv NameLoad Call NameLoad SubscriptLoad NameLoad Index Num NameLoad AugAssignDiv NameStore Num Return TupleLoad NameLoad BinOpAdd NameLoad NameLoad decorator_list FunctionDef arguments args NameParam NameParam NameParam NameParam NameParam NameParam NameParam NameParam defaults NameLoad NameLoad Str NameLoad body Expr Str If NameLoad body Assign NameStore BinOpAdd Call NameLoad UnaryOpUSub Call AttributeLoad NameLoad attr NameLoad Num Num orelse Assign NameStore BinOpAdd AttributeLoad NameLoad attr Num Assign NameStore BoolOpOr NameLoad BinOpAdd Num Call NameLoad BinOpDiv BinOpMult Num AttributeLoad NameLoad attr Num Assign NameStore BinOpAdd AttributeLoad NameLoad attr Num TryExcept body Expr Call NameLoad NameLoad Assign NameStore NameLoad handlers ExceptHandler type NameLoad body Assign NameStore NameLoad Assign NameStore Lambda arguments args NameParam NameParam defaults ListLoad Call NameLoad NameLoad SubscriptLoad NameLoad Index Num Assign NameStore ListLoad NameLoad Assign NameStore NameLoad Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad Assign NameStore ListLoad NameLoad NameLoad Assign NameStore ListLoad TupleLoad NameLoad NameLoad NameLoad FunctionDef arguments args NameParam NameParam defaults body Return ListComp Call AttributeLoad NameLoad attr SubscriptLoad NameLoad Slice Num NameLoad comprehension NameStore NameLoad decorator_list FunctionDef arguments args NameParam defaults body If CompareLt NameLoad NameLoad body Raise NameLoad Assign NameStore Call NameLoad NameLoad NameLoad If CompareLt NameLoad Call NameLoad NameLoad body Return SubscriptLoad NameLoad Index BinOpSub NameLoad Num While Num body Assign TupleStore NameStore NameStore NameStore SubscriptLoad NameLoad Index Num If NameLoad body Print BinOpMod Str TupleLoad NameLoad NameLoad Assign NameStore Call NameLoad NameLoad BinOpSub NameLoad NameLoad Assign NameStore NameLoad Assign TupleStore NameStore NameStore Call NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr TupleLoad NameLoad NameLoad NameLoad If CompareLtE NameLoad NameLoad body Return SubscriptLoad NameLoad Index Num decorator_list FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr NameLoad Assign NameStore AttributeLoad NameLoad attr TryFinally body Assign AttributeStore NameLoad attr NameLoad Assign TupleStore NameStore NameStore NameStore Call NameLoad NameLoad Assign NameStore Call NameLoad NameLoad BinOpSub NameLoad NameLoad finalbody Assign AttributeStore NameLoad attr NameLoad If NameLoad body Return ListComp UnaryOpUAdd NameLoad comprehension NameStore NameLoad orelse Return UnaryOpUAdd SubscriptLoad NameLoad Index Num decorator_list Return NameLoad decorator_list Assign AttributeStore NameLoad attr NameLoad If CompareEq NameLoad Str body Import alias Expr Call AttributeLoad NameLoad attr
N/A bisect bisect libmp.backend xrange ODEMethods N/A object N/A N/A N/A ode_taylor N/A N/A ctx derivs x0 y0 tol_prec n N/A N/A N/A h tol N/A N/A ctx ldexp 1 N/A tol_prec N/A dim N/A len y0 N/A xs N/A x0 N/A ys N/A y0 N/A x x0 N/A y y0 N/A orig N/A ctx prec N/A N/A N/A N/A ctx prec N/A orig N/A 1 n N/A i N/A range n N/A N/A fxy N/A derivs x y N/A y N/A N/A N/A y N/A i N/A h N/A fxy N/A i N/A i N/A xrange N/A len y N/A x h N/A N/A N/A xs append x N/A N/A N/A ys append y N/A ser N/A N/A N/A d N/A range dim N/A j N/A range N/A n 1 N/A N/A s N/A N/A 0 dim N/A b N/A -1 N/A j 1 N/A k 1 N/A i N/A range N/A j 1 N/A N/A d N/A range dim N/A N/A N/A s N/A d N/A b N/A N/A ys N/A i N/A d N/A b N/A N/A b N/A N/A j k 1 N/A k N/A k 1 N/A scale N/A N/A h N/A j N/A N/A ctx fac j N/A d N/A range dim N/A N/A N/A s N/A d N/A N/A s N/A d scale N/A N/A N/A N/A ser N/A d append N/A s N/A d N/A N/A N/A ctx prec orig N/A radius N/A ctx one N/A ts ser N/A N/A N/A ts N/A -1 N/A N/A radius N/A min radius N/A N/A ctx nthroot N/A tol N/A abs N/A ts N/A -1 n N/A radius 2 N/A N/A ser N/A x0 radius N/A odefun N/A N/A ctx F x0 y0 tol degree method verbose N/A None None taylor False N/A N/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

     N/A tol N/A N/A tol_prec N/A N/A int N/A N/A N/A ctx log tol 2 10 N/A N/A tol_prec N/A N/A ctx prec 10 N/A degree N/A degree N/A 3 N/A int N/A N/A 3 N/A ctx dps 2.0 N/A workprec N/A N/A ctx prec 40 N/A N/A N/A N/A len y0 N/A return_vector True N/A N/A N/A TypeError N/A N/A F_ F N/A F N/A N/A N/A x y N/A N/A N/A F_ x N/A y N/A 0 N/A y0 N/A y0 N/A return_vector False N/A N/A ser xb N/A ode_taylor ctx F x0 y0 tol_prec degree N/A series_boundaries N/A x0 xb N/A series_data N/A N/A ser x0 xb mpolyval N/A N/A ser a N/A N/A N/A N/A N/A N/A ctx polyval N/A s N/A -1 a N/A s ser N/A get_series N/A N/A x N/A N/A N/A N/A x x0 N/A N/A ValueError N/A n N/A bisect series_boundaries x N/A N/A n N/A len series_boundaries N/A N/A N/A series_data N/A N/A n 1 N/A 1 N/A N/A N/A ser xa xb N/A series_data N/A -1 N/A verbose N/A N/A N/A Computing Taylor series for [%f, %f] N/A xa xb N/A y N/A mpolyval ser N/A xb xa N/A xa xb N/A N/A ser xb N/A ode_taylor ctx F xb y tol_prec degree N/A N/A N/A series_boundaries append xb N/A N/A N/A series_data append N/A ser xa xb N/A N/A x xb N/A N/A N/A series_data N/A -1 N/A interpolant N/A N/A x N/A N/A N/A x N/A N/A ctx convert x N/A orig N/A ctx prec N/A N/A N/A N/A ctx prec workprec N/A N/A ser xa xb N/A get_series x N/A y N/A mpolyval ser N/A x xa N/A N/A N/A ctx prec orig N/A return_vector N/A N/A N/A N/A yk N/A yk y N/A N/A N/A N/A y N/A 0 N/A N/A interpolant N/A N/A N/A ODEMethods odefun odefun N/A N/A __name__ __main__ N/A N/A doctest N/A N/A N/A doctest testmod