Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions scipy/interpolate/_polyint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import numpy as np
from scipy.special import factorial
from scipy._lib._util import _asarray_validated, float_factorial, check_random_state
from scipy._lib._util import (_asarray_validated, float_factorial, check_random_state,
_transition_to_rng)


__all__ = ["KroghInterpolator", "krogh_interpolate",
Expand Down Expand Up @@ -564,13 +565,29 @@ class BarycentricInterpolator(_Interpolator1DWithDerivatives):
If absent or None, the weights will be computed from `xi` (default).
This allows for the reuse of the weights `wi` if several interpolants
are being calculated using the same nodes `xi`, without re-computation.
random_state : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
If `seed` is None (or `numpy.random`), the `numpy.random.RandomState`
singleton is used.
If `seed` is an int, a new ``RandomState`` instance is used,
seeded with `seed`.
If `seed` is already a ``Generator`` or ``RandomState`` instance then
that instance is used.
rng : {None, int, `numpy.random.Generator`}, optional
If `rng` is passed by keyword, types other than `numpy.random.Generator` are
passed to `numpy.random.default_rng` to instantiate a ``Generator``.
If `rng` is already a ``Generator`` instance, then the provided instance is
used. Specify `rng` for repeatable interpolation.

If this argument `random_state` is passed by keyword,
legacy behavior for the argument `random_state` applies:

- If `random_state` is None (or `numpy.random`), the `numpy.random.RandomState`
singleton is used.
- If `random_state` is an int, a new ``RandomState`` instance is used,
seeded with `random_state`.
- If `random_state` is already a ``Generator`` or ``RandomState`` instance then
that instance is used.

.. versionchanged:: 1.15.0
As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
transition from use of `numpy.random.RandomState` to
`numpy.random.Generator` this keyword was changed from `random_state` to `rng`.
For an interim period, both keywords will continue to work (only specify
one of them). After the interim period using the `random_state` keyword will emit
warnings. The behavior of the `random_state` and `rng` keywords is outlined above.

Notes
-----
Expand Down Expand Up @@ -619,10 +636,11 @@ class BarycentricInterpolator(_Interpolator1DWithDerivatives):
>>> plt.show()
""" # numpy/numpydoc#87 # noqa: E501

def __init__(self, xi, yi=None, axis=0, *, wi=None, random_state=None):
@_transition_to_rng("random_state", replace_doc=False)
def __init__(self, xi, yi=None, axis=0, *, wi=None, rng=None):
super().__init__(xi, yi, axis)

random_state = check_random_state(random_state)
rng = check_random_state(rng)

self.xi = np.asarray(xi, dtype=np.float64)
self.set_yi(yi)
Expand All @@ -643,7 +661,7 @@ def __init__(self, xi, yi=None, axis=0, *, wi=None, random_state=None):
# these numerical stability improvements will be able to provide all
# the points to the constructor.
self._inv_capacity = 4.0 / (np.max(self.xi) - np.min(self.xi))
permute = random_state.permutation(self.n, )
permute = rng.permutation(self.n, )
inv_permute = np.zeros(self.n, dtype=np.int32)
inv_permute[permute] = np.arange(self.n)
self.wi = np.zeros(self.n)
Expand Down
5 changes: 3 additions & 2 deletions scipy/interpolate/tests/test_polyint.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,12 @@ def setup_method(self):
self.ys = self.true_poly(self.xs)

def test_lagrange(self):
P = BarycentricInterpolator(self.xs, self.ys)
# Ensure backwards compatible post SPEC7
P = BarycentricInterpolator(self.xs, self.ys, random_state=1)
xp_assert_close(P(self.test_xs), self.true_poly(self.test_xs))

def test_scalar(self):
P = BarycentricInterpolator(self.xs, self.ys)
P = BarycentricInterpolator(self.xs, self.ys, rng=1)
xp_assert_close(P(7), self.true_poly(7), check_0d=False)
xp_assert_close(P(np.array(7)), self.true_poly(np.array(7)), check_0d=False)

Expand Down
Loading