Saturday, November 21, 2020

Numworks: 3 x 3 Matrices

 Numworks:   3 x 3 Matrices


The script invthree.py calculates the inverse and determinant of a 3 x 3 matrix.  


Matrices:


[[ a1,  a2,  a3 ]

[ b1, b2, b3 ]

[ c1, c2, c3 ]]



Numworks script invthree.py:

(701 bytes)


from math import *


# 2020-11-12 EWS

print("3x3 Matrix Inverse")

print("[[a1,a2,a3]")

print("[b1,b2,b3]")

print("[c1,c2,c3]]")

a1=float(input('a1: '))

a2=float(input('a2: '))

a3=float(input('a3: '))

b1=float(input('b1: '))

b2=float(input('b2: '))

b3=float(input('b3: '))

c1=float(input('c1: '))

c2=float(input('c2: '))

c3=float(input('c3: '))

# determinant

d=a1*(b2*c3-b3*c2)-a2*(b1*c3-b3*c1)+a3*(b1*c2-b2*c1)

# inverse

d1=(b2*c3-c2*b3)/d

d2=-(a2*c3-c2*a3)/d

d3=(a2*b3-a3*b2)/d

e1=-(b1*c3-c1*b3)/d

e2=(a1*c3-c1*a3)/d

e3=-(a1*b3-a3*b1)/d

f1=(b1*c2-b2*c1)/d

f2=-(a1*c2-a2*c1)/d

f3=(a1*b2-a2*b1)/d

print("det=")

print(d)

print("inv=")

print([d1,d2,d3])

print([e1,e2,e3])

print([f1,f2,f3])


Numworks page:  https://workshop.numworks.com/python/ews31415/invthree


Example:


[[ -5.4, 3.3, -1.7 ], [ 0.6, 8.3, 5.3 ] [ 5.5, 5.4, 1.9 ]] 


returns 


[ -0.05493...,  -0.06604..., 0.13508... ]

[ 0.11974... ,  -0.00389..., 0.11798... ]

[ -0.18130..., 0.20224..., -0.20006... ]


Source:


wikiHow Staff "How to Find the Inverse of a 3x3 Matrix"  WikiHow.   Last Updated November 5, 2020.  https://www.wikihow.com/Find-the-Inverse-of-a-3x3-Matrix  Retrieved November 12, 2020.


Eddie


All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.