Esempio n. 1
0
        public void TestCalculatingTheInverseOfAMatrix()
        {
            var a = new Matrix4x4(
                -5, 2, 6, -8,
                1, -5, 1, 8,
                7, 7, -6, -7,
                1, -3, 7, 4);

            var b = a.Inverse();

            var expected = new Matrix4x4(
                0.21805, 0.45113, 0.24060, -0.04511,
                -0.80827, -1.45677, -0.44361, 0.52068,
                -0.07895, -0.22368, -0.05263, 0.19737,
                -0.52256, -0.81391, -0.30075, 0.30639);

            Assert.Equal(532, a.Determinant());
            Assert.Equal(-160, a.Cofactor(2, 3));
            Assert.Equal(-160.0 / 532, b[3, 2]);
            Assert.Equal(105, a.Cofactor(3, 2));
            Assert.Equal(105.0 / 532, b[2, 3]);

            var comparer = Matrix4x4.GetEqualityComparer(0.00001);

            Assert.Equal(expected, b, comparer);
        }
Esempio n. 2
0
 public static T Determinant <T>(this Matrix4x4 <T> a)
     where T : IComparable <T> =>
 Operations.Add(
     Operations.Add(
         Operations.Multiply(a[0, 0], a.Cofactor(0, 0)),
         Operations.Multiply(a[0, 1], a.Cofactor(0, 1))),
     Operations.Add(
         Operations.Multiply(a[0, 2], a.Cofactor(0, 2)),
         Operations.Multiply(a[0, 3], a.Cofactor(0, 3))));
Esempio n. 3
0
        public void Inverse()
        {
            var matrix = new Matrix4x4(
                -5, 2, 6, -8,
                1, -5, 1, 8,
                7, 7, -6, -7,
                1, -3, 7, 4);

            var inverse = Matrix4x4.Inverse(matrix);

            Assert.Equal(532, Matrix4x4.Determinate(matrix));
            Assert.Equal(-160, Matrix4x4.Cofactor(matrix, 2, 3));
            Assert.Equal(-160 / 532f, inverse[3, 2]);
            Assert.Equal(105, Matrix4x4.Cofactor(matrix, 3, 2));
            Assert.Equal(105 / 532f, inverse[2, 3]);

            var expected = new Matrix4x4(
                0.21805f, 0.45113f, 0.24060f, -0.04511f,
                -0.80827f, -1.45677f, -0.44361f, 0.52068f,
                -0.07895f, -0.22368f, -0.05263f, 0.19737f,
                -0.52256f, -0.81391f, -0.30075f, 0.30639f);

            Assert.Equal(expected, inverse);

            matrix = new Matrix4x4(
                8, -5, 9, 2,
                7, 5, 6, 1,
                -6, 0, 9, 6,
                -3, 0, -9, -4);

            expected = new Matrix4x4(
                -0.15385f, -0.15385f, -0.28205f, -0.53846f,
                -0.07692f, 0.12308f, 0.02564f, 0.03077f,
                0.35897f, 0.35897f, 0.43590f, 0.92308f,
                -0.69231f, -0.69231f, -0.76923f, -1.92308f);

            Assert.Equal(expected, Matrix4x4.Inverse(matrix));

            matrix = new Matrix4x4(
                9, 3, 0, 9,
                -5, -2, -6, -3,
                -4, 9, 6, 4,
                -7, 6, 6, 2);

            expected = new Matrix4x4(
                -0.04074f, -0.07778f, 0.14444f, -0.22222f,
                -0.07778f, 0.03333f, 0.36667f, -0.33333f,
                -0.02901f, -0.14630f, -0.10926f, 0.12963f,
                0.17778f, 0.06667f, -0.26667f, 0.33333f);

            Assert.Equal(expected, Matrix4x4.Inverse(matrix));
        }
Esempio n. 4
0
        public void Determinate()
        {
            var matrix = new Matrix4x4(
                -2, -8, 3, 5,
                -3, 1, 7, 3,
                1, 2, -9, 6,
                -6, 7, 7, -9);

            Assert.Equal(690, Matrix4x4.Cofactor(matrix, 0, 0));
            Assert.Equal(447, Matrix4x4.Cofactor(matrix, 0, 1));
            Assert.Equal(210, Matrix4x4.Cofactor(matrix, 0, 2));
            Assert.Equal(51, Matrix4x4.Cofactor(matrix, 0, 3));
            Assert.Equal(-4071, Matrix4x4.Determinate(matrix));
        }
Esempio n. 5
0
        /// <summary>
        /// Returns the inverse of the given matrix. Note that it is up to the
        /// client to make sure the matrix specified by argument `a` is
        /// invertible at all. The `IsInvertible` method is provided for this
        /// purpose.
        /// </summary>
        public static Matrix4x4 Inverse(this Matrix4x4 a)
        {
            var m = new Matrix4x4(0);
            var d = a.Determinant();

            for (var i = 0; i < 4; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    m[j, i] = a.Cofactor(i, j) / d;
                }
            }

            return(m);
        }
Esempio n. 6
0
        public static Matrix4x4 <T> Inverse <T>(this Matrix4x4 <T> a)
            where T : IComparable <T>
        {
            var m = new Matrix4x4 <T>(default(T));
            var d = a.Determinant();

            for (var i = 0; i < 4; i++)
            {
                for (var j = 0; j < 4; j++)
                {
                    m[j, i] = Operations.Divide(a.Cofactor(i, j), d);
                }
            }

            return(m);
        }
Esempio n. 7
0
        public void Cofactor_should_return_the_minor_of_the_matrix_which_is_the_determinant_of_the_submatrix()
        {
            var matrix = new Matrix4x4(
                3, 5, 0, 4,
                2, -1, -7, 9,
                6, -1, 5, 2,
                3, 7, -4, 6);

            matrix.Cofactor(0, 0).Should().Be(-457);
            matrix.Cofactor(0, 1).Should().Be(65);
            matrix.Cofactor(0, 2).Should().Be(395);
            matrix.Cofactor(0, 3).Should().Be(416);

            matrix.Cofactor(1, 0).Should().Be(-66);
            matrix.Cofactor(1, 1).Should().Be(-42);
            matrix.Cofactor(1, 2).Should().Be(30);
            matrix.Cofactor(1, 3).Should().Be(102);

            matrix.Cofactor(2, 0).Should().Be(182);
            matrix.Cofactor(2, 1).Should().Be(-34);
            matrix.Cofactor(2, 2).Should().Be(-64);
            matrix.Cofactor(2, 3).Should().Be(-94);
        }
Esempio n. 8
0
 /// <summary>
 /// Calculates the determinant of given matrix `a` using Laplace
 /// expansion.
 /// </summary>
 public static double Determinant(this Matrix4x4 a) =>
 (a[0, 0] * a.Cofactor(0, 0)) +
 (a[0, 1] * a.Cofactor(0, 1)) +
 (a[0, 2] * a.Cofactor(0, 2)) +
 (a[0, 3] * a.Cofactor(0, 3));