Beispiel #1
0
        public static void  Main(System.String[] argv)
        {
            /*
             | Tests LU, QR, SVD and symmetric Eig decompositions.
             |
             |   n       = order of magic square.
             |   trace   = diagonal sum, should be the magic sum, (n^3 + n)/2.
             |   max_eig = maximum eigenvalue of (A + A')/2, should equal trace.
             |   rank    = linear algebraic rank,
             |             should equal n if n is odd, be less than n if n is even.
             |   cond    = L_2 condition number, ratio of singular values.
             |   lu_res  = test of LU factorization, norm1(L*U-A(p,:))/(n*eps).
             |   qr_res  = test of QR factorization, norm1(Q*R-A)/(n*eps).
             */

            print("\n    Test of GeneralMatrix Class, using magic squares.\n");
            print("    See MagicSquareExample.main() for an explanation.\n");
            print("\n      n     trace       max_eig   rank        cond      lu_res      qr_res\n\n");

            System.DateTime start_time = System.DateTime.Now;
            double          eps        = System.Math.Pow(2.0, -52.0);

            for (int n = 3; n <= 32; n++)
            {
                print(fixedWidthIntegertoString(n, 7));

                GeneralMatrix M = magic(n);

                //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"'
                int t = (int)M.Trace();
                print(fixedWidthIntegertoString(t, 10));

                EigenvalueDecomposition E = new EigenvalueDecomposition(M.Add(M.Transpose()).Multiply(0.5));
                double[] d = E.RealEigenvalues;
                print(fixedWidthDoubletoString(d[n - 1], 14, 3));

                int r = M.Rank();
                print(fixedWidthIntegertoString(r, 7));

                double c = M.Condition();
                print(c < 1 / eps ? fixedWidthDoubletoString(c, 12, 3):"         Inf");

                LUDecomposition LU  = new LUDecomposition(M);
                GeneralMatrix   L   = LU.L;
                GeneralMatrix   U   = LU.U;
                int[]           p   = LU.Pivot;
                GeneralMatrix   R   = L.Multiply(U).Subtract(M.GetMatrix(p, 0, n - 1));
                double          res = R.Norm1() / (n * eps);
                print(fixedWidthDoubletoString(res, 12, 3));

                QRDecomposition QR = new QRDecomposition(M);
                GeneralMatrix   Q  = QR.Q;
                R   = QR.R;
                R   = Q.Multiply(R).Subtract(M);
                res = R.Norm1() / (n * eps);
                print(fixedWidthDoubletoString(res, 12, 3));

                print("\n");
            }

            System.DateTime stop_time = System.DateTime.Now;
            double          etime     = (stop_time.Ticks - start_time.Ticks) / 1000.0;

            print("\nElapsed Time = " + fixedWidthDoubletoString(etime, 12, 3) + " seconds\n");
            print("Adios\n");
        }
 public void Transpose5()
 {
     A.Transpose();
     Assert.IsTrue(GeneralTests.Check(A.Trace(), sumofdiagonals));
 }