Skip to content

root_finder

Implicit methods require solving systems of equations. This module provides an interface (wrapper) for such solvers.

RootFinder

Bases: ABC

An interface for root-finders used to solve systems in implicit methods.

Source code in src/odeiter/root_finder.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class RootFinder(ABC):
    """An interface for root-finders used to solve systems in
    implicit methods.
    """

    @abstractstaticmethod
    def solve(
        func: Callable[[np.ndarray[float]], np.ndarray[float]],
        u0: np.ndarray[float],
    ) -> np.ndarray[float]:
        """
        Parameters:
            func: A function `f(u)`.
            u0: An approximate solution to `f(u) = 0`.

        Returns:
            The solution `u` to `f(u) = 0`.

        Raises:
            ValueError: The solver fails to find a solution.
        """
        ...

solve(func, u0)

Parameters:

Name Type Description Default
func Callable[[ndarray[float]], ndarray[float]]

A function f(u).

required
u0 ndarray[float]

An approximate solution to f(u) = 0.

required

Returns:

Type Description
ndarray[float]

The solution u to f(u) = 0.

Raises:

Type Description
ValueError

The solver fails to find a solution.

Source code in src/odeiter/root_finder.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@abstractstaticmethod
def solve(
    func: Callable[[np.ndarray[float]], np.ndarray[float]],
    u0: np.ndarray[float],
) -> np.ndarray[float]:
    """
    Parameters:
        func: A function `f(u)`.
        u0: An approximate solution to `f(u) = 0`.

    Returns:
        The solution `u` to `f(u) = 0`.

    Raises:
        ValueError: The solver fails to find a solution.
    """
    ...

DefaultRootFinder

Bases: RootFinder

A wrapper for scipy.optimize.root.

Source code in src/odeiter/root_finder.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class DefaultRootFinder(RootFinder):
    """A wrapper for [`scipy.optimize.root`](https://docs.scipy.org/doc/scipy/
    reference/generated/scipy.optimize.root.html).
    """

    @staticmethod
    def solve(
        func: Callable[[np.ndarray[float]], np.ndarray[float]],
        u0: np.ndarray[float],
    ):
        sol = root(fun=func, x0=u0)
        if not sol.success:
            raise ValueError(sol)
        return sol.x