예제 #1
0
        public void testQRDecomposition()
        {
            // Testing QR decomposition...

            setup();

            double tol = 1.0e-12;

            Matrix[] testMatrices = { M1, M2,                   I,
                                      M3, Matrix.transpose(M3), M4,Matrix.transpose(M4), M5 };

            for (int j = 0; j < testMatrices.Length; j++)
            {
                Matrix     Q = new Matrix(), R = new Matrix();
                bool       pivot = true;
                Matrix     A     = testMatrices[j];
                List <int> ipvt  = MatrixUtilities.qrDecomposition(A, ref Q, ref R, pivot);

                Matrix P = new Matrix(A.columns(), A.columns(), 0.0);

                // reverse column pivoting
                for (int i = 0; i < P.columns(); ++i)
                {
                    P[ipvt[i], i] = 1.0;
                }

                if (norm(Q * R - A * P) > tol)
                {
                    QAssert.Fail("Q*R does not match matrix A*P (norm = "
                                 + norm(Q * R - A * P) + ")");
                }

                pivot = false;
                MatrixUtilities.qrDecomposition(A, ref Q, ref R, pivot);

                if (norm(Q * R - A) > tol)
                {
                    QAssert.Fail("Q*R does not match matrix A (norm = "
                                 + norm(Q * R - A) + ")");
                }
            }
        }