Created
December 2, 2013 21:05
-
-
Save lucasad/7758961 to your computer and use it in GitHub Desktop.
Crammer's Rule
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
{ | |
"metadata": { | |
"name": "Crammer's Rule" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%pylab inline\n%precision 4", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "Populating the interactive namespace from numpy and matplotlib\n" | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "def solve(eqs):\n det = np.linalg.det(np.matrix(map(lambda eq:eq[:-1],eqs)))\n ans = []\n for i in range(len(eqs)):\n mat = []\n for eq in eqs:\n row = eq[:-1]\n row[i] = eq[-1]\n mat.append(row)\n ans.append(np.linalg.det(np.matrix(mat))/det)\n return ans", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "$x-2y+z=-3$ \n$x+y-2z=6$ \n$x-y+z=-2$ " | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "solve([[1,-2,1,-3],\n [1,1,-2,6],\n [1,-1,1,-2]])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": "[1.0000, 1.0000, -2.0000]" | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": "$x + 2y - 3z + 4w = 12$ \n$2x + 2y - 2z + 3w = 10$ \n$0 + y + z + 0 = -1$ \n$x - y + z - 2w = -4$ " | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "solve([[1,2,-3,4,12],\n [2,2,-2,3,10],\n [0,1,1,0,-1],\n [1,-1,1,-2,-4]])", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 6, | |
"text": "[1.0000, -0.0000, -1.0000, 2.0000]" | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment