Skip to content

Instantly share code, notes, and snippets.

View defencedog's full-sized avatar

defencedog

  • Pakistan
View GitHub Profile
@maxiimilian
maxiimilian / multi_root.py
Last active January 4, 2024 01:20
Function to look for all roots within given interval/bracket based on scipy's `root_scalar`. Resolution `n` of this function only needs to be high enough so that all sign changes of roots are covered.
import warnings
from typing import Callable, Iterable
import numpy as np
from scipy.optimize import root_scalar
def multi_root(f: Callable, bracket: Iterable[float], args: Iterable = (), n: int = 30) -> np.ndarray:
""" Find all roots of f in `bracket`, given that resolution `n` covers the sign change.
Fine-grained root finding is performed with `scipy.optimize.root_scalar`.