## Posted By

jatkins on 06/04/15

# 3x3 matrix algebra

/ Published in: JavaScript

Released into the public domain by Josh Atkins, 2015.

`function findInverseOf3x3Matrix(matrix) {	var A = matrix.e * matrix.i - matrix.f * matrix.h,		B = matrix.f * matrix.g - matrix.d * matrix.i,		C = matrix.d * matrix.h - matrix.e * matrix.g,		determinant = matrix.a * A + matrix.b * B + matrix.c * C,		oneOverDeterminant; 	if(determinant === 0) {		return false; // singular; inverse does not exist	}	else {		oneOverDeterminant = 1 / determinant; 		return {			a: A * oneOverDeterminant,			b: (matrix.c * matrix.h - matrix.b * matrix.i) * oneOverDeterminant,			c: (matrix.b * matrix.f - matrix.c * matrix.e) * oneOverDeterminant,			d: B * oneOverDeterminant,			e: (matrix.a * matrix.i - matrix.c * matrix.g) * oneOverDeterminant,			f: (matrix.c * matrix.d - matrix.a * matrix.f) * oneOverDeterminant,			g: C * oneOverDeterminant,			h: (matrix.b * matrix.g - matrix.a * matrix.h) * oneOverDeterminant,			i: (matrix.a * matrix.e - matrix.b * matrix.d) * oneOverDeterminant		};	}}; function findDeterminantOf2x2Matrix(matrix) {	return matrix.a * matrix.d - matrix.b * matrix.c;} function findDeterminantOf3x3Matrix(matrix) {	return matrix.a * findDeterminantOf2x2Matrix({a: matrix.e, b: matrix.f, c: matrix.h, d: matrix.i})		 - matrix.b * findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.f, c: matrix.g, d: matrix.i})		 + matrix.c * findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.e, c: matrix.g, d: matrix.h});} function formMatrixOfMinorsOf3x3Matrix(matrix) {	return {		a: findDeterminantOf2x2Matrix({a: matrix.e, b: matrix.f, c: matrix.h, d: matrix.i}),		b: findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.f, c: matrix.g, d: matrix.i}),		c: findDeterminantOf2x2Matrix({a: matrix.d, b: matrix.e, c: matrix.g, d: matrix.h}),		d: findDeterminantOf2x2Matrix({a: matrix.b, b: matrix.c, c: matrix.h, d: matrix.i}),		e: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.c, c: matrix.g, d: matrix.i}),		f: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.b, c: matrix.g, d: matrix.h}),		g: findDeterminantOf2x2Matrix({a: matrix.b, b: matrix.c, c: matrix.e, d: matrix.f}),		h: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.c, c: matrix.d, d: matrix.f}),		i: findDeterminantOf2x2Matrix({a: matrix.a, b: matrix.b, c: matrix.d, d: matrix.e})	};} function formMatrixOfCofactorsOf3x3Matrix(matrix) {	return {		a: matrix.a, b: -matrix.b, c: matrix.c,		d: -matrix.d, e: matrix.e, f: -matrix.f,		g: matrix.g, h: -matrix.h, i: matrix.i	};} function getTransposeOf3x3Matrix(matrix) {	return {		a: matrix.a, b: matrix.d, c: matrix.g,		d: matrix.b, e: matrix.e, f: matrix.h,		g: matrix.c, h: matrix.f, i: matrix.i	};} function multiply3x3Matrices(matrix1, matrix2) {	/* this calculates	 * (A B C)   (a b c)	 * (D E F) X (d e f)	 * (G H I)   (g h i)	 *	 * i.e. matrix1 * matrix2 	 */ 	var A = matrix1.a, B = matrix1.b, C = matrix1.c,		D = matrix1.d, E = matrix1.e, F = matrix1.f,		G = matrix1.g, H = matrix1.h, I = matrix1.i; 	var a = matrix2.a, b = matrix2.b, c = matrix2.c,		d = matrix2.d, e = matrix2.e, f = matrix2.f,		g = matrix2.g, h = matrix2.h, i = matrix2.i; 	return {		a: A*a + B*d + C*g, b: A*b + B*e + C*h, c: A*c + B*f + C*i,		d: D*a + E*d + F*g, e: D*b + E*e + F*h, f: D*c + E*f + F*i,		g: G*a + H*d + I*g, h: G*b + H*e + I*h, i: G*c + H*f + I*i	};}`