Skip to content
This repository was archived by the owner on Apr 24, 2020. It is now read-only.

Commit c218556

Browse files
jez-wjstac
authored andcommitted
Numpy polynomial correction (#689)
* Changed the implementation of exercise 3 in python essentials to make it faster, more pythonic and use techniques explained in the text * Corrected a bug in Exercise 1 of the numpy chapter The np.poly1d() method takes the coefficients in decreasing powers so they need to be flipped (np.flip()) before making the call Improved the test case as this error wasn't detected when using identical coefficients. Moved the tested value to a variable rather than hard coded Made the code more pythonic using np.ones_like() rather than np.empty(len(coef)) * typo coeffs -> coef * Revert "Changed the implementation of exercise 3 in python essentials to make it" This reverts commit a8c4795.
1 parent d2d0dea commit c218556

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

source/rst/numpy.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,7 @@ This code does the job
919919
.. code-block:: python3
920920
921921
def p(x, coef):
922-
X = np.empty(len(coef))
923-
X[0] = 1
922+
X = np.ones_like(coef)
924923
X[1:] = x
925924
y = np.cumprod(X) # y = [1, x, x**2,...]
926925
return coef @ y
@@ -929,12 +928,13 @@ Let's test it
929928

930929
.. code-block:: python3
931930
932-
coef = np.ones(3)
931+
x = 2
932+
coef = np.linspace(2, 4, 3)
933933
print(coef)
934-
print(p(1, coef))
934+
print(p(x, coef))
935935
# For comparison
936-
q = np.poly1d(coef)
937-
print(q(1))
936+
q = np.poly1d(np.flip(coef))
937+
print(q(x))
938938
939939
940940
Exercise 2

0 commit comments

Comments
 (0)