Beispiel #1
0
 public static double CalculateDeterminant(this Matrix matrix)
 {
     return(matrix[0, 0] * matrix.Cofactor(0, 0)
            + matrix[0, 1] * matrix.Cofactor(0, 1)
            + matrix[0, 2] * matrix.Cofactor(0, 2)
            + matrix[0, 3] * matrix.Cofactor(0, 3));
 }
Beispiel #2
0
        public static Matrix Invert(this Matrix matrix)
        {
            var determinant = matrix.CalculateDeterminant();

            if (determinant == 0f)
            {
                throw new Exception("Cannot invert a singular matrix.");
            }

            var cofactorMatrix = new Matrix(
                matrix.Cofactor(0, 0), matrix.Cofactor(1, 0), matrix.Cofactor(2, 0), matrix.Cofactor(3, 0),
                matrix.Cofactor(0, 1), matrix.Cofactor(1, 1), matrix.Cofactor(2, 1), matrix.Cofactor(3, 1),
                matrix.Cofactor(0, 2), matrix.Cofactor(1, 2), matrix.Cofactor(2, 2), matrix.Cofactor(3, 2),
                matrix.Cofactor(0, 3), matrix.Cofactor(1, 3), matrix.Cofactor(2, 3), matrix.Cofactor(3, 3)
                );

            return((1f / determinant) * cofactorMatrix);
        }