Beispiel #1
0
        public static void Test()
        {
            System.Console.WriteLine(String.Empty.PadRight(78, '#'));
            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of MatrixF64");
            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine(String.Empty.PadRight(78, '#'));
            System.Console.WriteLine(Environment.NewLine);

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of matrix initialization:");
            System.Console.WriteLine(Environment.NewLine);

            // A 5x3 matrix (5 rows x 3 columns) with
            // 64-bit floating-point entries:
            MatrixF64 m =
                new MatrixF64
                (
                5, 3,
                 0.0, 1.0, 2.0, // row 0
                 3.0, 4.0, 5.0, // row 1
                 6.0, 7.0, 8.0, // row 2
                 9.0, 10.0, 11.0, // row 3
                12.0, 13.0, 14.0  // row 4
                );
            m.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  0,  1,  2 ]  // row 0
            // [  3,  4,  5 ]  // row 1
            // [  6,  7,  8 ]  // row 2
            // [  9, 10, 11 ]  // row 3
            // [ 12, 13, 14 ]  // row 4

            // An 8x2 matrix (8 rows x 2 columns) with all 16
            // 64-bit floating-point entries set to zero:
            MatrixF64 z = MatrixF64.Zero(8, 2);
            z.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ 0, 0 ]  // row 0
            // [ 0, 0 ]  // row 1
            // [ 0, 0 ]  // row 2
            // [ 0, 0 ]  // row 3
            // [ 0, 0 ]  // row 4
            // [ 0, 0 ]  // row 5
            // [ 0, 0 ]  // row 6
            // [ 0, 0 ]  // row 7

            // Examples of matrix addition, subtraction, and multiplication:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of matrix addition, subtraction, and multiplication:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 a =
                new MatrixF64
                (
                3, 5,
                 0.0, 1.0, 2.0, 3.0, 4.0, // row 0
                 5.0, 6.0, 7.0, 8.0, 9.0, // row 1
                10.0, 11.0, 12.0, 13.0, 14.0  // row 2
                );

            MatrixF64 b =
                new MatrixF64
                (
                3, 5,
                 4.0, -3.0, 2.0, -1.0, 0.0, // row 0
                -9.0, 8.0, -7.0, 6.0, -5.0, // row 1
                14.0, -13.0, 12.0, -11.0, 10.0  // row 2
                );

            MatrixF64 c =
                new MatrixF64
                (
                5, 3,
                 0.0, 1.0, 2.0, // row 0
                 3.0, 4.0, 5.0, // row 1
                 6.0, 7.0, 8.0, // row 2
                 9.0, 10.0, 11.0, // row 3
                12.0, 13.0, 14.0  // row 4
                );

            MatrixF64 result = new MatrixF64();

            result = a + b;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  4, -2,  4,  2,  4 ]
            // [ -4, 14,  0, 14,  4 ]
            // [ 24, -2, 24,  2, 24 ]

            result = a - b;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ -4,  4,  0,  4,  4 ]
            // [ 14, -2, 14,  2, 14 ]
            // [ -4, 24,  0, 24,  4 ]

            result = -a;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [   0,  -1,  -2,  -3,  -4 ]
            // [  -5,  -6,  -7,  -8,  -9 ]
            // [ -10, -11, -12, -13, -14 ]

            result = 3.0 * a;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  0,  3,  6,  9, 12 ]
            // [ 15, 18, 21, 24, 27 ]
            // [ 30, 33, 36, 39, 42 ]

            result = a * c; // (3x5) * (5x3) = (3x3)
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  90, 100, 110 ]
            // [ 240, 275, 310 ]
            // [ 390, 450, 510 ]

            result = c * a; // (5x3) * (3x5) = (5x5)
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  25,  28,  31,  34,  37 ]
            // [  70,  82,  94, 106, 118 ]
            // [ 115, 136, 157, 178, 199 ]
            // [ 160, 190, 220, 250, 280 ]
            // [ 205, 244, 283, 322, 361 ]

            // Examples of multiplying by an identity matrix:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of multiplying by an identity matrix:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 identity3x3 = MatrixF64.Identity(3);
            identity3x3.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ 1, 0, 0 ]
            // [ 0, 1, 0 ]
            // [ 0, 0, 1 ]

            MatrixF64 s3x3 =
                new MatrixF64
                (
                3, 3,
                0.0, 1.0, 2.0, // row 0
                3.0, 4.0, 5.0, // row 1
                6.0, 7.0, 8.0  // row 2
                );

            result = s3x3 * identity3x3;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ 0, 1, 2 ]
            // [ 3, 4, 5 ]
            // [ 6, 7, 8 ]
            // (i.e., the same as s3x3)

            result = identity3x3 * s3x3;
            result.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ 0, 1, 2 ]
            // [ 3, 4, 5 ]
            // [ 6, 7, 8 ]
            // (i.e., the same as s3x3)

            // Example of LU factoring and back-substitution:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of LU factoring and back-substitution:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 a3x3 =
                new MatrixF64
                (
                    3, 3,
                    0.0, 1.0, 2.0, // row 0
                    3.0, 4.0, 5.0, // row 1
                    6.0, 7.0, 8.0  // row 2
                );

            int[] rowPermutationOfOriginalMatrix = null;
            bool rowInterchangeParityIsOdd = false;
            MatrixF64 a3x3LU = null;
            MatrixF64.FindLUFactorsOfARowPermutedVersionOfAnOriginalMatrix
            (
                a3x3,
                ref a3x3LU,
                ref rowPermutationOfOriginalMatrix,
                ref rowInterchangeParityIsOdd
            );

            a3x3LU.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [     6,     7,     8 ]
            // [     0,     1,     2 ]
            // [   0.5,   0.5, 1e-19 ]
            // The matrix above is a combination of the L and U
            // matrices. The L matrix is 0 above the diagonal,
            // 1 on the diagonal, and the shown values below the
            // diagonal.  The U matrix is 0 below the diagonal,
            // and the shown values on and above the diagonal.
            // This LU factoring is of a ROW-PERMUTED version
            // of the original matrix.

            // The following L and U matrices are manually formed
            // by inspecting the LU factoring matrix above.
            MatrixF64 L3x3 =
                new MatrixF64
                (
                    3, 3,
                    1.0, 0.0, 0.0, // row 0
                    0.0, 1.0, 0.0, // row 1
                    0.5, 0.5, 1.0  // row 2
                );

            MatrixF64 U3x3 =
                new MatrixF64
                (
                    3, 3,
                    6.0, 7.0, 8.0, // row 0
                    0.0, 1.0, 2.0, // row 1
                    0.0, 0.0, 0.0  // row 2
                );

            // The product (L)*(U) produces a ROW-PERMUTED version
            // of the original matrix:
            result = L3x3 * U3x3;
            result.WriteLine(4);
            System.Console.WriteLine(Environment.NewLine);
            // [ 6, 7, 8 ]
            // [ 0, 1, 2 ]
            // [ 3, 4, 5 ]

            VectorF64 b3x1 = new VectorF64(1.0, 2.0, 3.0);
            VectorF64 x3x1 = null;

            MatrixF64.LUBacksubstitution
            (
                a3x3LU,
                rowPermutationOfOriginalMatrix,
                b3x1,
                ref x3x1
            );

            // Solution vector
            x3x1.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( -0.66666667,           1,           0 )
            // Both the given b3x1 and this x3x1 are relative to the
            // original matrix, a3x3.  The a3x3LU for a ROW-PERMUTED
            // version of a3x3, but LUBacksubstitution accepts a
            // non-permuted product vector (e.g., b3x1) and produces
            // a non-permuted solution vector (e.g., x3x1).

            // Check solution by multiplying the original matrix a3x3
            // by the solution vector x3x1, to get a product vector.
            VectorF64 checkb3x1 = a3x3 * x3x1;
            checkb3x1.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( 1, 2, 3 )
            // This matches the b3x1 vector we specified above, so
            // the solution is valid.

            // Examples of matrix determinants:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of matrix determinants:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 d2x2 =
                new MatrixF64
                (
                2, 2,
                 2.0, 5.0, // row 0
                -4.0, -7.0  // row 1
                );
            double detd2x2 = d2x2.Determinant();
            System.Console.WriteLine(detd2x2);
            System.Console.WriteLine(Environment.NewLine);
            // detd2x2 = 6

            MatrixF64 d3x3 =
                new MatrixF64
                (
                3, 3,
                 2.0, 5.0, 7.0, // row 0
                -4.0, -1.0, 6.0, // row 1
                 9.0, 8.0, 3.0  // row 2
                );
            double detd3x3 = d3x3.Determinant();
            System.Console.WriteLine(detd3x3);
            System.Console.WriteLine(Environment.NewLine);
            // detd3x3 = 67

            MatrixF64 d4x4 =
                new MatrixF64
                (
                4, 4,
                  7.0, -5.0, 2.0, 4.0, // row 0
                  3.0, 2.0, 6.0, 3.0, // row 1
                 -9.0, 8.0, -3.0, 2.0, // row 2
                  5.0, 3.0, 2.0, 5.0  // row 3
                );
            double detd4x4 = d4x4.Determinant();
            System.Console.WriteLine(detd4x4);
            System.Console.WriteLine(Environment.NewLine);
            // detd4x4 = 1457

            // Examples of matrix inverses:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of matrix inverses:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 m2x2 =
                new MatrixF64
                (
                2, 2,
                 2.0, 5.0, // row 0
                -4.0, -7.0  // row 1
                );
            MatrixF64 inversem2x2 = m2x2.Inverse();
            inversem2x2.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  -1.1666667, -0.83333333 ]
            // [  0.66666667,  0.33333333 ]

            MatrixF64 prodinvm2x2andm2x2 = inversem2x2 * m2x2;
            prodinvm2x2andm2x2.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [             1, 8.8817842e-16 ]
            // [             0,             1 ]
            // This product is very close to a
            // 2x2 identity matrix, as it should be.

            MatrixF64 prodm2x2andinvm2x2 = m2x2 * inversem2x2;
            prodm2x2andinvm2x2.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ 1, 0 ]
            // [ 0, 1 ]
            // This product is the 2x2 identity matrix,
            // as it should be.

            MatrixF64 m3x3 =
                new MatrixF64
                (
                3, 3,
                 2.0, 5.0, 7.0, // row 0
                -4.0, -1.0, 6.0, // row 1
                 9.0, 8.0, 3.0  // row 2
                );
            MatrixF64 inversem3x3 = m3x3.Inverse();
            inversem3x3.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [ -0.76119403,   0.6119403,  0.55223881 ]
            // [  0.98507463, -0.85074627, -0.59701493 ]
            // [ -0.34328358,  0.43283582,  0.26865672 ]

            MatrixF64 prodinvm3x3andm3x3 = inversem3x3 * m3x3;
            prodinvm3x3andm3x3.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [             1,             0, 4.4408921e-16 ]
            // [             0,             1,             0 ]
            // [             0,             0,             1 ]
            // This product is very close to a 3x3 identity matrix,
            // as it should be.

            MatrixF64 prodm3x3andinvm3x3 = m3x3 * inversem3x3;
            prodm3x3andinvm3x3.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [              1, -4.4408921e-16,  4.4408921e-16 ]
            // [              0,              1,  -2.220446e-16 ]
            // [ -4.4408921e-16,              0,              1 ]
            // This product is very close to a 3x3 identity matrix,
            // as it should be.

            // Examples of a product of a matrix and a vector:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of a product of a matrix and a vector:");
            System.Console.WriteLine(Environment.NewLine);

            MatrixF64 t3x3 =
                new MatrixF64
                (
                3, 3,
                 2.0, 5.0, 7.0, // row 0
                -4.0, -1.0, 6.0, // row 1
                 9.0, 8.0, 3.0  // row 2
                );

            VectorF64 p3x1 = new VectorF64(1.0, 2.0, 3.0);

            VectorF64 q3x1 = t3x3 * p3x1;
            q3x1.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( 33, 12, 34 )

            // The multiplicative product of the inverse
            // of the matrix and the result vector computed
            // above should produce the original vector.
            MatrixF64 inverset3x3 = t3x3.Inverse();

            VectorF64 prodinvt3x3andq3x1 = inverset3x3 * q3x1;
            prodinvt3x3andq3x1.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( 1, 2, 3 )
            // This vector is equal to the original vector,
            // as expected.

            // Example of using a homogeneous matrix to
            // translate a homogeneous vector:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of using a homogeneous matrix to translate a homogeneous vector:");
            System.Console.WriteLine(Environment.NewLine);

            // Forming a homogeneous matrix that represents
            // a translation:

            VectorF64 homogeneousTranslationVector4x1 =
                new VectorF64(10.0, 20.0, 30.0, 1.0);

            MatrixF64 translation4x4 =
                MatrixF64.FormHomogeneousTranslationMatrix
                    (homogeneousTranslationVector4x1);

            translation4x4.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // [  1,  0,  0, 10 ]
            // [  0,  1,  0, 20 ]
            // [  0,  0,  1, 30 ]
            // [  0,  0,  0,  1 ]

            // Using the homogeneous matrix to translate
            // a homogeneous vector:

            VectorF64 homogeneousVector =
                new VectorF64(5.0, 6.0, 7.0, 1.0);

            VectorF64 translatedVector =
                translation4x4 * homogeneousVector;

            translatedVector.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( 15, 26, 37,  1 )
            // The relevant vector components (5,6,7)
            // were translated by (10,20,30) by the
            // homogeneous translation matrix.

            // Example of using a homogeneous matrix
            // to rotate a homogeneous vector:

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine("Test of using a homogeneous matrix to rotate a homogeneous vector:");
            System.Console.WriteLine(Environment.NewLine);

            // Forming a homogeneous rotation matrix that
            // rotates a positive component along coordinate
            // axis 0 to a positive component along
            // cooridnate axis 1, in 3-dimensional space.
            // (The homogeneous matrix must be (3+1)=4
            // rows by (3+1)=4 columns.)

            MatrixF64 rotation4x4 =
                MatrixF64.Rab(4, 0, 1, (0.5 * Math.PI));

            rotation4x4.WriteLine(3);
            System.Console.WriteLine(Environment.NewLine);
            // [ 6.12e-17,       -1,        0,        0 ]
            // [        1, 6.12e-17,        0,        0 ]
            // [        0,        0,        1,        0 ]
            // [        0,        0,        0,        1 ]
            // (The entries '6.12e-17' are negligible.)
            // The upper-left 3x3 part of the matrix represents
            // a counterclockwise rotation of a quarter-turn
            // such that a positive component along axis 0
            // would be rotated to a positive component along
            // axis 1, in 3-dimensional space.

            // Using the homogeneous rotation matrix to
            // rotate a homogeneous vector:

            VectorF64 homogeneousVector2 =
                new VectorF64(5.0, 6.0, 7.0, 1.0);

            VectorF64 rotatedVector =
                rotation4x4 * homogeneousVector2;

            rotatedVector.WriteLine();
            System.Console.WriteLine(Environment.NewLine);
            // ( -6,  5,  7,  1 )
            // The relevant vector components (5,6,7) were
            // rotated to (-6,5,7), which is consistent with
            // a counterclockwise quarter-turn rotation
            // with a rotation plane parallel to axes 0 and 1.

            System.Console.WriteLine(Environment.NewLine);
            System.Console.WriteLine(String.Empty.PadRight(78, '#'));
            System.Console.WriteLine(Environment.NewLine);
        }