Ejemplo n.º 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");
		}
Ejemplo n.º 2
0
        /// <summary>Initializes this Metric object from metric matrix (called by constructor) </summary>
        /// <param name="m">The NxN metric matrix</param> 
        private void init(DotNetMatrix.GeneralMatrix m)
        {
            if (!Util.IsSymmetric(m))
                throw new Exception("The metric matrix must be symmetric");

            m_matrix = m.Copy();

            //            System.Console.WriteLine("m_matrix: " + Util.ToString(m_matrix));

            // compute eigen value decomposition
            m_eig = new DotNetMatrix.EigenvalueDecomposition(m_matrix);

            //            System.Console.WriteLine("m_eig: " + Util.ToString(m_eig.GetV()));

            m_invEigMatrix = m_eig.GetV().Transpose();

            m_eigenMetric = m_eig.RealEigenvalues;

            //            {
              //              DotNetMatrix.GeneralMatrix D = Util.Diagonal(m_eigenMetric);
            //            DotNetMatrix.GeneralMatrix tmp = m_eig.GetV().Multiply(D).Multiply(m_invEigMatrix);
            //                System.Console.WriteLine("input_matrix = " + Util.ToString(tmp));
              //      }

            m_isDiagonal = Util.IsDiagonal(m_matrix);
            if (!m_isDiagonal)
            {
                m_isEuclidean = m_isAntiEuclidean = false;
            }
            else
            {
                m_isEuclidean = m_isAntiEuclidean = true;
                for (int i = 0; i < m.RowDimension; i++)
                {
                    if (m_matrix.GetElement(i, i) != 1.0)
                        m_isEuclidean = false;
                    if (m_matrix.GetElement(i, i) != -1.0)
                        m_isAntiEuclidean = false;
                }
            }
        }
Ejemplo n.º 3
0
        private void UseGeneralMatrix(out Matrix eigenvals, out Matrix eigenvecs)
        {
            if (AllElementsReal == false)
                throw new Exception("All matrix elements must be real to diagonalize.");

            double[][] eles = new double[Rows][];
            for (int i = 0; i < Rows; i++)
                eles[i] = new double[Columns];

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    eles[i][j] = this[i, j].RealPart;
                }
            }

            GeneralMatrix matrix = new GeneralMatrix(eles);

            EigenvalueDecomposition evs = new EigenvalueDecomposition(matrix);

            GeneralMatrix vectors = evs.GetV();

            eigenvecs = new Matrix(Rows, Columns);

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Columns; j++)
                {
                    eigenvecs[i, j] = vectors.GetElement(i, j);
                }
            }

            double[] values = evs.RealEigenvalues;
            double[] valimag = evs.ImagEigenvalues;

            eigenvals = new Matrix(Rows, 1);

            for (int i = 0; i < Rows; i++)
            {
                eigenvals[i, 0] = new Complex(values[i], valimag[i]);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor where caller determines the value of all fields (used internally by RoundEigenMetric())
 /// </summary>
 protected Metric(DotNetMatrix.GeneralMatrix matrix,
     DotNetMatrix.EigenvalueDecomposition eig, double[] eigenMetric,
     DotNetMatrix.GeneralMatrix invEigMatrix, bool isDiagonal,
     bool isEuclidean, bool isAntiEuclidean)
 {
     m_eigenMetric = eigenMetric;
     m_matrix = matrix;
     m_eig = eig;
     m_eigenMetric = eigenMetric;
     m_invEigMatrix = invEigMatrix;
     m_isDiagonal = isDiagonal;
     m_isEuclidean = isEuclidean;
     m_isAntiEuclidean = isAntiEuclidean;
 }