Skip to content

Instantly share code, notes, and snippets.

@olivierverdier
Created November 21, 2014 10:47
Show Gist options
  • Save olivierverdier/1596eb3f78e6aff7a1e9 to your computer and use it in GitHub Desktop.
Save olivierverdier/1596eb3f78e6aff7a1e9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"worksheets": [
{
"cells": [
{
"metadata": {},
"cell_type": "heading",
"source": "Exercise for the course [Python for MATLAB users](http://sese.nu/python-for-matlab-users/), by Olivier Verdier",
"level": 2
},
{
"metadata": {},
"cell_type": "code",
"input": "%pylab\n%matplotlib inline\nfrom __future__ import division",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": false
},
{
"metadata": {},
"cell_type": "markdown",
"source": "The goal of this exercise is to define a class `Polynomial`, which behaves like a polynomial. For instance\n\n```\np = Polynomial([1.,2.])\n```\nshould represent the polynomial $1 + 2X$.\n\nThe object `p` should be callable\n\n```\np(.2) # value of the polynomial at 0.2\n```\n\nOne should be able to add, multiply two polynomials.\n\nYou will be guided through by the following detailed tasks."
},
{
"metadata": {},
"cell_type": "code",
"input": "class Polynomial(object):\n pass # implement here",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"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`."
},
{
"metadata": {},
"cell_type": "code",
"input": "a = array([1.,2.])\np = Polynomial(a)\nassert not p.coeffs is a",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Implement the method `__getitem__`, which allows to give acess to the coefficients. An out of bound index should return zero, like in mathematics."
},
{
"metadata": {},
"cell_type": "code",
"input": "assert allclose(p[0], 1.)\nassert allclose(p[3], 0.)",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Implement the method `__add__`, which adds two polynomials."
},
{
"metadata": {},
"cell_type": "code",
"input": "q = Polynomial([1.,2.,3])\nz = p+q\nassert allclose((p+q)[0], 2.)\nassert allclose((p+q)[2], 3.)",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"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."
},
{
"metadata": {},
"cell_type": "code",
"input": "assert repr(p) == \"Polynomial(array([ 1., 2.]))\"",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"source": "Implement `differentiate` which returns the derivative of the polynomial."
},
{
"metadata": {},
"cell_type": "code",
"input": "assert allclose(p.differentiate().coeffs, array([2.]))",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "markdown",
"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`."
},
{
"metadata": {},
"cell_type": "code",
"input": "reduce?",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
},
{
"metadata": {},
"cell_type": "code",
"input": "assert allclose(p(0.), 1.)",
"outputs": [],
"language": "python",
"trusted": true,
"collapsed": true
}
],
"metadata": {}
}
],
"metadata": {
"name": "",
"signature": "sha256:b5597ff8332ccca177c1bc45833fa9de98e9b3f9c7b438211247f2c1155791b4"
},
"nbformat": 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment