Last active
August 29, 2015 14:06
-
-
Save ketch/6be81962868fa9e4fb4f to your computer and use it in GitHub Desktop.
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
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:a5255a7564db12b450621ea68453364adf7b4bd423d95039ced644b73e2cf5d9" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Bug in numpy.poly" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import numpy as np\n", | |
"print np.__version__" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"1.9.0\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Here we will compute the coefficients of the Wilkinson polynomial\n", | |
"$$P(x) = \\prod_{i=1}^{20} (x-i)$$\n", | |
"in two ways, both using the `np.poly` function. First, we just give the vector of coefficients; second, we give a diagonal matrix with those coefficients on the diagonal. The output of `np.poly` should be the same in both cases, but it's not:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"N = 20\n", | |
"v = np.arange(1,N+1)\n", | |
"c1 = np.poly(v)\n", | |
"c2 = np.poly(np.diag(v))\n", | |
"print c1-c2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n", | |
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n", | |
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n", | |
" 0.00000000e+00 0.00000000e+00 0.00000000e+00 0.00000000e+00\n", | |
" -1.02400000e+03 1.84467441e+19 -1.84467441e+19 0.00000000e+00\n", | |
" 0.00000000e+00]\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"So at least one of the answers is wrong; which?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print c1" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 1 -210 20615\n", | |
" -1256850 53327946 -1672280820\n", | |
" 40171771630 -756111184500 11310276995381\n", | |
" -135585182899530 1307535010540395 -10142299865511450\n", | |
" 63030812099294896 -311333643161390640 1206647803780373360\n", | |
" -3599979517947607200 8037811822645051776 5575812828558562816\n", | |
" -4642984320068847616 -8752948036761600000 2432902008176640000]\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Clearly `c1` is wrong, since the coefficients ought to have alternating signs." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print c2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 1.00000000e+00 -2.10000000e+02 2.06150000e+04 -1.25685000e+06\n", | |
" 5.33279460e+07 -1.67228082e+09 4.01717716e+10 -7.56111184e+11\n", | |
" 1.13102770e+13 -1.35585183e+14 1.30753501e+15 -1.01422999e+16\n", | |
" 6.30308121e+16 -3.11333643e+17 1.20664780e+18 -3.59997952e+18\n", | |
" 8.03781182e+18 -1.28709312e+19 1.38037598e+19 -8.75294804e+18\n", | |
" 2.43290201e+18]\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"For $N=19$, no such error occurs:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"N = 19\n", | |
"v = np.arange(1,N+1)\n", | |
"c1 = np.poly(v)\n", | |
"c2 = np.poly(np.diag(v))\n", | |
"print c1-c2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.\n", | |
" 0. 0.]\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment