Advertisement
NyteOwlDave

Simultaneous Linear Eq.

Feb 8th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1.  
  2. # 2x2 matrix determinant
  3. def det2x2(m):
  4.   a,b = m[0]
  5.   c,d = m[1]
  6.   return 1 / (a*d - b*c)
  7.  
  8. # 2x2 matrix inverse
  9. def inv2x2(m):
  10.   det = det2x2(m)
  11.   a,b = m[0]
  12.   c,d = m[1]
  13.   return [[d*det,-b*det],[-c*det,a*det]]
  14.  
  15. # Vec2 times 2x2 matrix
  16. def vec2Mul(v,m):
  17.   x = v[0]*m[0][0] + v[1]*m[0][1]
  18.   y = v[0]*m[1][0] + v[1]*m[1][1]
  19.   return [x, y]
  20.  
  21. # Solve
  22. # 4p+q=6
  23. # 2p-q=–3
  24. v = [6, -3]
  25. m = [[4, 1], [2, -1]]
  26. u = vec2Mul(v,inv2x2(m))
  27. print("p = ",u[0])
  28. print("q = ",u[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement