-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Add polyvalfromroots
to numpy.polynomial
#7542
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ Basics | |
polygrid3d | ||
polyroots | ||
polyfromroots | ||
polyvalfromroots | ||
|
||
Fitting | ||
------- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,65 @@ def test_polyval(self): | |
assert_equal(poly.polyval(x, [1, 0]).shape, dims) | ||
assert_equal(poly.polyval(x, [1, 0, 0]).shape, dims) | ||
|
||
def test_polyvalfromroots(self): | ||
#check empty input | ||
assert_equal(poly.polyvalfromroots([], [1]).size, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need tests for multidimensional array of roots. |
||
assert_(poly.polyvalfromroots([], [1]).shape == (0,)) | ||
|
||
#check empty input + multidimensional roots | ||
assert_equal(poly.polyvalfromroots([], [[1] * 5]).size, 0) | ||
assert_(poly.polyvalfromroots([], [[1] * 5]).shape == (5, 0)) | ||
|
||
#check scalar input | ||
assert_equal(poly.polyvalfromroots(1, 1), 0) | ||
assert_(poly.polyvalfromroots(1, np.ones((3, 3))).shape == (3, 1)) | ||
|
||
#check normal input) | ||
x = np.linspace(-1, 1) | ||
y = [x**i for i in range(5)] | ||
for i in range(1, 5): | ||
tgt = y[i] | ||
res = poly.polyvalfromroots(x, [0]*i) | ||
assert_almost_equal(res, tgt) | ||
tgt = x*(x - 1)*(x + 1) | ||
res = poly.polyvalfromroots(x, [-1, 0, 1]) | ||
assert_almost_equal(res, tgt) | ||
|
||
#check that shape is preserved | ||
for i in range(3): | ||
dims = [2]*i | ||
x = np.zeros(dims) | ||
assert_equal(poly.polyvalfromroots(x, [1]).shape, dims) | ||
assert_equal(poly.polyvalfromroots(x, [1, 0]).shape, dims) | ||
assert_equal(poly.polyvalfromroots(x, [1, 0, 0]).shape, dims) | ||
|
||
#check compatibility with factorization | ||
ptest = [15, 2, -16, -2, 1] | ||
r = poly.polyroots(ptest) | ||
x = np.linspace(-1, 1) | ||
assert_almost_equal(poly.polyval(x, ptest), | ||
poly.polyvalfromroots(x, r)) | ||
|
||
#check multidimensional arrays of roots and values | ||
#check tensor=False | ||
rshape = (3, 5) | ||
x = np.arange(-3, 2) | ||
r = np.random.randint(-5, 5, size=rshape) | ||
res = poly.polyvalfromroots(x, r, tensor=False) | ||
tgt = np.empty(r.shape[1:]) | ||
for ii in range(tgt.size): | ||
tgt[ii] = poly.polyvalfromroots(x[ii], r[:, ii]) | ||
assert_equal(res, tgt) | ||
|
||
#check tensor=True | ||
x = np.vstack([x, 2*x]) | ||
res = poly.polyvalfromroots(x, r, tensor=True) | ||
tgt = np.empty(r.shape[1:] + x.shape) | ||
for ii in range(r.shape[1]): | ||
for jj in range(x.shape[0]): | ||
tgt[ii, jj, :] = poly.polyvalfromroots(x[jj], r[:, ii]) | ||
assert_equal(res, tgt) | ||
|
||
def test_polyval2d(self): | ||
x1, x2, x3 = self.x | ||
y1, y2, y3 = self.y | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be like
The error situation is actually a bit tricky, as sometimes broadcasting empty x does work, but it is a sometimes thing, for instance
r = [1]
works, butr = [1,2]
does not. The first only works because the number of elements in the row is 1, which is a special "broadcasting" value. So probably better to just error out in that case. Theprod
function already returns 1 for empty first axis, so no need to special case that. For testing you can reshape empty arrays to get various other empty arrays. For instanceThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make that