Nugroho's blog.: Manual Gauss Jordan in Python.

Wednesday, April 26, 2017

Manual Gauss Jordan in Python.

What if we didn't do back substitution on Gauss Naif method but eliminate the rest instead? Nah, we get the Gauss Jordan here.

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:

323f (5) amp (1) android (12) apple (7) arduino (18) art (1) assembler (21) astina (4) ATTiny (23) blackberry (4) camera (3) canon (2) cerita (2) computer (106) crazyness (11) debian (1) delphi (39) diary (286) flash (8) fortran (6) freebsd (6) google apps script (8) guitar (2) HTML5 (10) IFTTT (7) Instagram (7) internet (12) iOS (5) iPad (6) iPhone (5) java (1) javascript (1) keynote (2) LaTeX (6) lazarus (1) linux (29) lion (15) mac (28) macbook air (8) macbook pro (3) macOS (1) Math (3) mathematica (1) maverick (6) mazda (4) microcontroler (35) mountain lion (2) music (37) netbook (1) nugnux (6) os x (36) php (1) Physicist (29) Picture (3) programming (189) Python (109) S2 (13) software (7) Soliloquy (125) Ubuntu (5) unix (4) Video (8) wayang (3) yosemite (3)