Skip to content

Commit 3cf9394

Browse files
authored
Merge pull request #27234 from charris/backport-25984
BUG: Allow fitting of degree zero polynomials with Polynomial.fit
2 parents 7443dcc + 85b1cab commit 3cf9394

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

numpy/polynomial/_polybase.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,9 @@ class domain in NumPy 1.4 and ``None`` in later versions.
10411041
"""
10421042
if domain is None:
10431043
domain = pu.getdomain(x)
1044+
if domain[0] == domain[1]:
1045+
domain[0] -= 1
1046+
domain[1] += 1
10441047
elif type(domain) is list and len(domain) == 0:
10451048
domain = cls.domain
10461049

numpy/polynomial/tests/test_polynomial.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
from fractions import Fraction
66
import numpy as np
77
import numpy.polynomial.polynomial as poly
8+
import numpy.polynomial.polyutils as pu
89
import pickle
910
from copy import deepcopy
1011
from numpy.testing import (
1112
assert_almost_equal, assert_raises, assert_equal, assert_,
12-
assert_array_equal, assert_raises_regex)
13+
assert_array_equal, assert_raises_regex, assert_warns)
1314

1415

1516
def trim(x):
@@ -628,6 +629,14 @@ def test_polyline(self):
628629
def test_polyline_zero(self):
629630
assert_equal(poly.polyline(3, 0), [3])
630631

632+
def test_fit_degenerate_domain(self):
633+
p = poly.Polynomial.fit([1], [2], deg=0)
634+
assert_equal(p.coef, [2.])
635+
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=0)
636+
assert_almost_equal(p.coef, [2.05])
637+
with assert_warns(pu.RankWarning):
638+
p = poly.Polynomial.fit([1, 1], [2, 2.1], deg=1)
639+
631640
def test_result_type(self):
632641
w = np.array([-1, 1], dtype=np.float32)
633642
p = np.polynomial.Polynomial(w, domain=w, window=w)

0 commit comments

Comments
 (0)