Created
December 8, 2015 15:04
-
-
Save olivierverdier/008cc0b2f1ee3605f9fc to your computer and use it in GitHub Desktop.
This file contains 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users-ht15/), by Olivier Verdier" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"%matplotlib inline" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"The goal of this exercise is to define a class `Polynomial`, which behaves like a polynomial. For instance\n", | |
"\n", | |
"```\n", | |
"p = Polynomial([1.,2.])\n", | |
"```\n", | |
"should represent the polynomial $1 + 2X$.\n", | |
"\n", | |
"The object `p` should be callable\n", | |
"\n", | |
"```\n", | |
"p(.2) # value of the polynomial at 0.2\n", | |
"```\n", | |
"\n", | |
"One should be able to add, multiply two polynomials.\n", | |
"\n", | |
"You will be guided through by the following detailed tasks." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class Polynomial:\n", | |
" pass # implement here" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the `__init__` method, which stores a list of coefficients. Use the `array` function to copy the list, or array, of coefficiente which is passed. Store the coefficients in a property `coeffs`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"a = np.array([1.,2.])\n", | |
"p = Polynomial(a)\n", | |
"assert not p.coeffs is a" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the method `__getitem__`, which allows to give acess to the coefficients. An out of bound index should return zero, like in mathematics." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"assert np.allclose(p[0], 1.)\n", | |
"assert np.allclose(p[3], 0.)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the method `__add__`, which adds two polynomials." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"q = Polynomial([1.,2.,3])\n", | |
"z = p+q\n", | |
"assert np.allclose((p+q)[0], 2.)\n", | |
"assert np.allclose((p+q)[2], 3.)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the method `__repr__`, which returns a string such as `Polynomial(array([1.,2.]))`.\n", | |
"\n", | |
"**Hint**: use the function `repr` on the coefficient array of the polynomial." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"assert repr(p) == \"Polynomial(array([ 1., 2.]))\"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement `differentiate` which returns the derivative of the polynomial." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"assert np.allclose(p.differentiate().coeffs, array([2.]))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Implement the method `__call__`, which evaluates the polynomial at a given point.\n", | |
"\n", | |
"**Bonus** if the method works with array inputs, like `p(array([1.,2.,3.])`.\n", | |
"\n", | |
"**Hint**: Use the functions `reduce` and, possibly, the function `reversed`." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"reduce?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"assert allclose(p(0.), 1.)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment