The idea is after we do operation to make the lower-triangle have zero value, we continue the operation until all the component in the upper-triangle have zero value too, and the diagonal have value of one.
Basically, the matrix becomes identity matrix. This way, we didn't need subtitution at all since all variables already has the exact value on the right side, :)
Here's the modificatian
from random import uniform def showIt(a): print 'The matrix' print a[0][0], a[0][1],a[0][2], a[0][3] print a[1][0], a[1][1],a[1][2], a[1][3] print a[2][0], a[2][1],a[2][2], a[2][3] a = [] x = [] for i in range (3): a.append([]) x.append(0.) for j in range (4): a[i].append(uniform(.1,10)) showIt(a) #manual operation on lower triangle temp = a[0][0] a[0][0] /= temp a[0][1] /= temp a[0][2] /= temp a[0][3] /= temp temp = a[1][0] a[1][0] = a[1][0]/temp - a[0][0] a[1][1] = a[1][1]/temp - a[0][1] a[1][2] = a[1][2]/temp - a[0][2] a[1][3] = a[1][3]/temp - a[0][3] temp = a[1][1] a[1][1] /= temp a[1][2] /= temp a[1][3] /= temp temp = a[2][0] a[2][0] = a[2][0]/temp - a[0][0] a[2][1] = a[2][1]/temp - a[0][1] a[2][2] = a[2][2]/temp - a[0][2] a[2][3] = a[2][3]/temp - a[0][3] temp = a[2][1] a[2][1] = a[2][1]/temp - a[1][1] a[2][2] = a[2][2]/temp - a[1][2] a[2][3] = a[2][3]/temp - a[1][3] temp = a[2][2] a[2][2] /= temp a[2][3] /= temp print '' print 'Manual Operation on lower-triangle Result' showIt(a) print '' temp = a[1][2] a[1][3] = a[1][3]/temp-a[2][3] a[1][2] = a[1][2]/temp-a[2][2] a[1][1] = a[1][1]/temp-a[2][1] temp = a[1][1] a[1][1] /= temp a[1][3] /= temp temp = a[0][2] a[0][3] = a[0][3]/temp - a[2][3] a[0][2] = a[0][2]/temp - a[2][2] a[0][1] = a[0][1]/temp - a[2][1] a[0][0] = a[0][0]/temp - a[2][0] temp = a[0][1] a[0][3] = a[0][3]/temp - a[1][3] a[0][2] = a[0][2]/temp - a[1][2] a[0][1] = a[0][1]/temp - a[1][1] a[0][0] = a[0][0]/temp - a[1][0] temp = a[0][0] a[0][0] /= temp a[0][3] /= temp print 'operation on upper-triangle result' showIt(a) print 'the result' x[0] = a[0][3] x[1] = a[1][3] x[2] = a[2][3] print x
And the result is here
Nugrohos-MacBook-Air:blog nugroho$ python gaussJordanManual.py The matrix 0.325152138961 4.70653216428 5.62293611004 0.355298420178 3.77212266843 5.01372896476 2.44600829577 5.16166288251 6.01821316098 3.642737365 1.98942519502 0.891669926919 Manual Operation on lower tringle Result The matrix 1.0 14.4748614581 17.2932465645 1.09271438691 0.0 1.0 1.26617773562 -0.0209693160516 0.0 0.0 1.0 -2.06355734268 operation on upper triangle result The matrix 1.0 0.0 0.0 -0.7385092664 0.0 1.0 0.0 2.59186104743 0.0 0.0 1.0 -2.06355734268 the result [-0.7385092664002817, 2.5918610474291373, -2.063557342678102] Nugrohos-MacBook-Air:blog nugroho$
No comments:
Post a Comment