Skip to content

Commit

Permalink
Merge pull request #27234 from charris/backport-25984
Browse files Browse the repository at this point in the history
BUG: Allow fitting of degree zero polynomials with Polynomial.fit
  • Loading branch information
charris authored Aug 17, 2024
2 parents 7443dcc + 85b1cab commit 3cf9394
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions numpy/polynomial/_polybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,9 @@ class domain in NumPy 1.4 and ``None`` in later versions.
"""
if domain is None:
domain = pu.getdomain(x)
if domain[0] == domain[1]:
domain[0] -= 1
domain[1] += 1
elif type(domain) is list and len(domain) == 0:
domain = cls.domain

Expand Down
11 changes: 10 additions & 1 deletion numpy/polynomial/tests/test_polynomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
from fractions import Fraction
import numpy as np
import numpy.polynomial.polynomial as poly
import numpy.polynomial.polyutils as pu
import pickle
from copy import deepcopy
from numpy.testing import (
assert_almost_equal, assert_raises, assert_equal, assert_,
assert_array_equal, assert_raises_regex)
assert_array_equal, assert_raises_regex, assert_warns)


def trim(x):
Expand Down Expand Up @@ -628,6 +629,14 @@ def test_polyline(self):
def test_polyline_zero(self):
assert_equal(poly.polyline(3, 0), [3])

def test_fit_degenerate_domain(self):
p = poly.Polynomial.fit([1], [2], deg=0)
assert_equal(p.coef, [2.])
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=0)
assert_almost_equal(p.coef, [2.05])
with assert_warns(pu.RankWarning):
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=1)

def test_result_type(self):
w = np.array([-1, 1], dtype=np.float32)
p = np.polynomial.Polynomial(w, domain=w, window=w)
Expand Down

0 comments on commit 3cf9394

Please sign in to comment.