Example #1
0
        public static void QRDecomposition()
        {
            SquareMatrix A = new SquareMatrix(new double[, ] {
                { 1, -2, 3 },
                { 2, -5, 12 },
                { 0, 2, -10 }
            });

            ColumnVector b = new ColumnVector(2, 8, -4);

            SquareQRDecomposition qrd = A.QRDecomposition();
            ColumnVector          x   = qrd.Solve(b);

            PrintMatrix("x", x);

            SquareMatrix Q = qrd.QMatrix;
            SquareMatrix R = qrd.RMatrix;

            PrintMatrix("QR", Q * R);

            PrintMatrix("Q  Q^T", Q.MultiplySelfByTranspose());

            SquareMatrix AI = qrd.Inverse();

            PrintMatrix("A^{-1}", AI);
            PrintMatrix("A^{-1} A", AI * A);
        }
Example #2
0
        public void SquareRandomMatrixQRDecomposition()
        {
            for (int d = 1; d <= 100; d += 11)
            {
                Console.WriteLine("d={0}", d);

                SquareMatrix M = CreateSquareRandomMatrix(d);

                // QR decompose the matrix.
                SquareQRDecomposition QRD = M.QRDecomposition();

                // The dimension should be right.
                Assert.IsTrue(QRD.Dimension == M.Dimension);

                // Test that the decomposition works.
                SquareMatrix Q = QRD.QMatrix;
                SquareMatrix R = QRD.RMatrix;
                Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * R, M));

                // Check that the inverse works.
                SquareMatrix MI = QRD.Inverse();
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M * MI, UnitMatrix.OfDimension(d)));

                // Test that a solution works.
                ColumnVector t = new ColumnVector(d);
                for (int i = 0; i < d; i++)
                {
                    t[i] = i;
                }
                ColumnVector s = QRD.Solve(t);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(M * s, t));
            }
        }