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

Commit

Permalink
Numpy polynomial correction (#689)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jez-w authored and jstac committed Sep 25, 2019
1 parent d2d0dea commit c218556
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions source/rst/numpy.rst
Expand Up @@ -919,8 +919,7 @@ This code does the job
.. code-block:: python3
def p(x, coef):
X = np.empty(len(coef))
X[0] = 1
X = np.ones_like(coef)
X[1:] = x
y = np.cumprod(X) # y = [1, x, x**2,...]
return coef @ y
Expand All @@ -929,12 +928,13 @@ Let's test it

.. code-block:: python3
coef = np.ones(3)
x = 2
coef = np.linspace(2, 4, 3)
print(coef)
print(p(1, coef))
print(p(x, coef))
# For comparison
q = np.poly1d(coef)
print(q(1))
q = np.poly1d(np.flip(coef))
print(q(x))
Exercise 2
Expand Down

0 comments on commit c218556

Please sign in to comment.