Posted By

jatkins on 06/04/15


Tagged

math Maths matrix matrices


Versions (?)

3x3 matrix algebra


 / Published in: JavaScript
 

Released into the public domain by Josh Atkins, 2015.

  1. function findInverseOf3x3Matrix(matrix) {
  2. var A = matrix.e * matrix.i - matrix.f * matrix.h,
  3. B = matrix.f * matrix.g - matrix.d * matrix.i,
  4. C = matrix.d * matrix.h - matrix.e * matrix.g,
  5. determinant = matrix.a * A + matrix.b * B + matrix.c * C,
  6. oneOverDeterminant;
  7.  
  8. if(determinant === 0) {
  9. return false; // singular; inverse does not exist
  10. }
  11. else {
  12. oneOverDeterminant = 1 / determinant;
  13.  
  14. return {
  15. a: A * oneOverDeterminant,
  16. b: (matrix.c * matrix.h - matrix.b * matrix.i) * oneOverDeterminant,
  17. c: (matrix.b * matrix.f - matrix.c * matrix.e) * oneOverDeterminant,
  18. d: B * oneOverDeterminant,
  19. e: (matrix.a * matrix.i - matrix.c * matrix.g) * oneOverDeterminant,
  20. f: (matrix.c * matrix.d - matrix.a * matrix.f) * oneOverDeterminant,
  21. g: C * oneOverDeterminant,
  22. h: (matrix.b * matrix.g - matrix.a * matrix.h) * oneOverDeterminant,
  23. i: (matrix.a * matrix.e - matrix.b * matrix.d) * oneOverDeterminant
  24. };
  25. }
  26. };
  27.  
  28. function findDeterminantOf2x2Matrix(matrix) {
  29. return matrix.a * matrix.d - matrix.b * matrix.c;
  30. }
  31.  
  32. function findDeterminantOf3x3Matrix(matrix) {
  33. return matrix.a * findDeterminantOf2x2Matrix({a: matrix.e, b: matrix.f, c: matrix.h, d: matrix.i})
  34. - matrix.b * findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.f, c: matrix.g, d: matrix.i})
  35. + matrix.c * findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.e, c: matrix.g, d: matrix.h});
  36. }
  37.  
  38. function formMatrixOfMinorsOf3x3Matrix(matrix) {
  39. return {
  40. a: findDeterminantOf2x2Matrix({a: matrix.e, b: matrix.f, c: matrix.h, d: matrix.i}),
  41. b: findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.f, c: matrix.g, d: matrix.i}),
  42. c: findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.e, c: matrix.g, d: matrix.h}),
  43. d: findDeterminantOf2x2Matrix({a: matrix.b, b: matrix.c, c: matrix.h, d: matrix.i}),
  44. e: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.c, c: matrix.g, d: matrix.i}),
  45. f: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.b, c: matrix.g, d: matrix.h}),
  46. g: findDeterminantOf2x2Matrix({a: matrix.b, b: matrix.c, c: matrix.e, d: matrix.f}),
  47. h: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.c, c: matrix.d, d: matrix.f}),
  48. i: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.b, c: matrix.d, d: matrix.e})
  49. };
  50. }
  51.  
  52. function formMatrixOfCofactorsOf3x3Matrix(matrix) {
  53. return {
  54. a: matrix.a, b: -matrix.b, c: matrix.c,
  55. d: -matrix.d, e: matrix.e, f: -matrix.f,
  56. g: matrix.g, h: -matrix.h, i: matrix.i
  57. };
  58. }
  59.  
  60. function getTransposeOf3x3Matrix(matrix) {
  61. return {
  62. a: matrix.a, b: matrix.d, c: matrix.g,
  63. d: matrix.b, e: matrix.e, f: matrix.h,
  64. g: matrix.c, h: matrix.f, i: matrix.i
  65. };
  66. }
  67.  
  68. function multiply3x3Matrices(matrix1, matrix2) {
  69. /* this calculates
  70. * (A B C) (a b c)
  71. * (D E F) X (d e f)
  72. * (G H I) (g h i)
  73. *
  74. * i.e. matrix1 * matrix2
  75. */
  76.  
  77. var A = matrix1.a, B = matrix1.b, C = matrix1.c,
  78. D = matrix1.d, E = matrix1.e, F = matrix1.f,
  79. G = matrix1.g, H = matrix1.h, I = matrix1.i;
  80.  
  81. var a = matrix2.a, b = matrix2.b, c = matrix2.c,
  82. d = matrix2.d, e = matrix2.e, f = matrix2.f,
  83. g = matrix2.g, h = matrix2.h, i = matrix2.i;
  84.  
  85. return {
  86. a: A*a + B*d + C*g, b: A*b + B*e + C*h, c: A*c + B*f + C*i,
  87. d: D*a + E*d + F*g, e: D*b + E*e + F*h, f: D*c + E*f + F*i,
  88. g: G*a + H*d + I*g, h: G*b + H*e + I*h, i: G*c + H*f + I*i
  89. };
  90. }

Report this snippet  

You need to login to post a comment.