Evd() public method

public Evd ( Symmetricity symmetricity = Symmetricity.Unknown ) : Evd
symmetricity Symmetricity
return Evd
Exemplo n.º 1
0
        public void CanCheckRankOfSquareSingular(int order)
        {
            var matrixA = new DenseMatrix(order, order);
            matrixA[0, 0] = 1;
            matrixA[order - 1, order - 1] = 1;
            for (var i = 1; i < order - 1; i++)
            {
                matrixA[i, i - 1] = 1;
                matrixA[i, i + 1] = 1;
                matrixA[i - 1, i] = 1;
                matrixA[i + 1, i] = 1;
            }

            var factorEvd = matrixA.Evd();

            Assert.AreEqual(factorEvd.Determinant, 0);
            Assert.AreEqual(factorEvd.Rank, order - 1);
        }
Exemplo n.º 2
0
        public void CanCheckRankOfSquareSingular([Values(10, 50, 100)] int order)
        {
            var A = new DenseMatrix(order, order);
            A[0, 0] = 1;
            A[order - 1, order - 1] = 1;
            for (var i = 1; i < order - 1; i++)
            {
                A[i, i - 1] = 1;
                A[i, i + 1] = 1;
                A[i - 1, i] = 1;
                A[i + 1, i] = 1;
            }

            var factorEvd = A.Evd();

            Assert.AreEqual(factorEvd.Determinant, 0);
            Assert.AreEqual(factorEvd.Rank, order - 1);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Eigenvalue,_eigenvector_and_eigenspace">EVD decomposition</seealso>
        public void Run()
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Create square symmetric matrix
            var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 2.0, 1.0, 4.0 }, { 3.0, 4.0, 1.0 } });
            Console.WriteLine(@"Initial square symmetric matrix");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Perform eigenvalue decomposition of symmetric matrix
            var evd = matrix.Evd();
            Console.WriteLine(@"Perform eigenvalue decomposition of symmetric matrix");

            // 1. Eigen vectors
            Console.WriteLine(@"1. Eigen vectors");
            Console.WriteLine(evd.EigenVectors().ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 2. Eigen values as a complex vector
            Console.WriteLine(@"2. Eigen values as a complex vector");
            Console.WriteLine(evd.EigenValues().ToString("N", formatProvider));
            Console.WriteLine();

            // 3. Eigen values as the block diagonal matrix
            Console.WriteLine(@"3. Eigen values as the block diagonal matrix");
            Console.WriteLine(evd.D().ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 4. Multiply V by its transpose VT
            var identity = evd.EigenVectors().TransposeAndMultiply(evd.EigenVectors());
            Console.WriteLine(@"4. Multiply V by its transpose VT: V*VT = I");
            Console.WriteLine(identity.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 5. Reconstruct initial matrix: A = V*D*V'
            var reconstruct = evd.EigenVectors() * evd.D() * evd.EigenVectors().Transpose();
            Console.WriteLine(@"5. Reconstruct initial matrix: A = V*D*V'");
            Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 6. Determinant of the matrix
            Console.WriteLine(@"6. Determinant of the matrix");
            Console.WriteLine(evd.Determinant);
            Console.WriteLine();

            // 7. Rank of the matrix
            Console.WriteLine(@"7. Rank of the matrix");
            Console.WriteLine(evd.Rank);
            Console.WriteLine();

            // Fill matrix by random values
            var rnd = new Random(1);
            for (var i = 0; i < matrix.RowCount; i++)
            {
                for (var j = 0; j < matrix.ColumnCount; j++)
                {
                    matrix[i, j] = rnd.NextDouble();
                }
            }

            Console.WriteLine(@"Fill matrix by random values");
            Console.WriteLine(matrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Perform eigenvalue decomposition of non-symmetric matrix
            evd = matrix.Evd();
            Console.WriteLine(@"Perform eigenvalue decomposition of non-symmetric matrix");

            // 8. Eigen vectors
            Console.WriteLine(@"8. Eigen vectors");
            Console.WriteLine(evd.EigenVectors().ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 9. Eigen values as a complex vector
            Console.WriteLine(@"9. Eigen values as a complex vector");
            Console.WriteLine(evd.EigenValues().ToString("N", formatProvider));
            Console.WriteLine();

            // 10. Eigen values as the block diagonal matrix
            Console.WriteLine(@"10. Eigen values as the block diagonal matrix");
            Console.WriteLine(evd.D().ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 11. Multiply A * V
            var av = matrix * evd.EigenVectors();
            Console.WriteLine(@"11. Multiply A * V");
            Console.WriteLine(av.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 12. Multiply V * D
            var vd = evd.EigenVectors() * evd.D();
            Console.WriteLine(@"12. Multiply V * D");
            Console.WriteLine(vd.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 13. Reconstruct non-symmetriv matrix A = V * D * Vinverse
            reconstruct = evd.EigenVectors() * evd.D() * evd.EigenVectors().Inverse();
            Console.WriteLine(@"13. Reconstruct non-symmetriv matrix A = V * D * Vinverse");
            Console.WriteLine(reconstruct.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 14. Determinant of the matrix
            Console.WriteLine(@"14. Determinant of the matrix");
            Console.WriteLine(evd.Determinant);
            Console.WriteLine();

            // 15. Rank of the matrix
            Console.WriteLine(@"15. Rank of the matrix");
            Console.WriteLine(evd.Rank);
            Console.WriteLine();
        }
Exemplo n.º 4
0
        // Method to rate mode goodness and then sort (use first mode as base)
        private void ComputeModeGoodnessRatio()
        {
            // Use no more than Max_N (which is 5 I believe)
            int n = lstCentroids.Count;     // find as many Gaussians as modes.
            // Don't do too many Gaussians because it will take a long time and perform poorly
            if (n > GCount)
            {
                n = GCount;
            }

            // Multiple dist map and diff map if necessary
            //DateTime startTime = DateTime.Now;
            ComputeRealMap();
            //DateTime stopTime = DateTime.Now;
            //TimeSpan duration = stopTime - startTime;
            //double RunTime = duration.TotalSeconds;
            //System.Windows.Forms.MessageBox.Show("ComputeRealMap Run time " + RunTime + " seconds!");

            // Allocate memory for output matrices
            Array arrModes = new double[n];
            Array arrProportions = new double[n];
            Array arrMUs = new double[n, 2];
            Array arrSigmaXSigmaY = new double[n];
            Array junkModes = new double[n];
            Array junkMUs = new double[n, 2];
            Array junkSigmaXSigmaY = new double[n];

            #region Using MATLAB for GMM

            ////startTime = DateTime.Now;
            //double[,] arrSamplesR;
            //double[,] arrSamplesI;

            //// Generate samples from map to get ready to perform mixed Gaussian fitting
            //PrepareSamples(out arrSamplesR, out arrSamplesI);
            ////stopTime = DateTime.Now;
            ////duration = stopTime - startTime;
            ////RunTime = duration.TotalSeconds;
            ////System.Windows.Forms.MessageBox.Show("PrepareSamples Run time " + RunTime + " seconds!");

            //// Perform mixed Gaussian fitting and get parameters
            ////startTime = DateTime.Now;
            //// Using MATLAB to do GMM
            //GaussianFitting(n, arrSamplesR, arrSamplesI, ref arrModes, ref arrMUs, ref arrSigmaXSigmaY, ref junkModes, ref junkMUs, ref junkSigmaXSigmaY);
            ////stopTime = DateTime.Now;
            ////duration = stopTime - startTime;
            ////RunTime = duration.TotalSeconds;
            ////System.Windows.Forms.MessageBox.Show("GaussianFitting Run time " + RunTime + " seconds!");
            //// Debug
            //// curRequest.SetLog("\nMATLAB GMM results\n");

            #endregion

            #region Using C# for GMM

            // Prepare samples into the format needed
            double[][] arrSamples;

            // Generate samples from map to get ready to perform mixed Gaussian fitting
            PrepareSamplesAccord(out arrSamples);

            // Using Accord.net library to do GMM
            GaussianMixtureModel gmm = new GaussianMixtureModel(n);
            List<MapMode> lstGaussians = new List<MapMode>();           // Results stored in a list of MapModes for sorting

            // If Accord.net library fails, try it again up to 3 times
            for (int ii = 0; ii < ProjectConstants.MaxAccordRun; ii++)
            {
                try
                {
                    gmm.Compute(arrSamples, 10);

                    //// Debug code
                    //// Print out means and covariances and proportions so we can plot
                    //Console.WriteLine("Accord.net run number " + ii);
                    //for (int i = 0; i < n; i++)
                    //{
                    //    Console.WriteLine("Gaussian number " + i);
                    //    // Means
                    //    Console.Write("Mean: (" + gmm.Gaussians[i].Mean[0] + " " + gmm.Gaussians[i].Mean[1] + ") ");
                    //    // Area
                    //    Console.Write("Covariance Matrix [" + gmm.Gaussians[i].Covariance[0, 0] + " "
                    //        + gmm.Gaussians[i].Covariance[0, 1] + "; "
                    //        + gmm.Gaussians[i].Covariance[1, 0] + " "
                    //        + gmm.Gaussians[i].Covariance[1, 1] + "] ");
                    //    Console.Write("Proportion: " + gmm.Gaussians[i].Proportion + "\n");
                    //}

                    // Getting arrays ready
                    for (int i = 0; i < n; i++)
                    {
                        // Means
                        arrMUs.SetValue(gmm.Gaussians[i].Mean[0], i, 0);
                        arrMUs.SetValue(gmm.Gaussians[i].Mean[1], i, 1);
                        // Area
                        DenseMatrix m = new DenseMatrix(gmm.Gaussians[i].Covariance);
                        System.Numerics.Complex[] d = m.Evd().EigenValues().ToArray();
                        double SigmaXSigmaY = Math.Sqrt(d[0].Real) * Math.Sqrt(d[1].Real);
                        arrSigmaXSigmaY.SetValue(SigmaXSigmaY, i);
                        // Modes
                        arrModes.SetValue(gmm.Gaussians[i].GetDistribution().ProbabilityDensityFunction(gmm.Gaussians[i].Mean), i);
                        // Scales
                        arrProportions.SetValue(gmm.Gaussians[i].Proportion, i);
                    }
                    // Debug
                    // curRequest.SetLog("\nAccord GMM results\n");

                    ////Debug code
                    //for (int i = 0; i < arrMUs.Length / 2; i++)
                    //{
                    //    curRequest.SetLog(arrMUs.GetValue(i, 0) + "," + arrMUs.GetValue(i, 1) + "  ");
                    //}
                    //curRequest.SetLog("\n");
                    //for (int i = 0; i < arrSigmaXSigmaY.Length; i++)
                    //{
                    //    curRequest.SetLog(arrSigmaXSigmaY.GetValue(i) + "  ");
                    //}
                    //curRequest.SetLog("\n");
                    //for (int i=0; i< arrModes.Length; i++)
                    //{
                    //    curRequest.SetLog(arrModes.GetValue(i) + " ");
                    //}
                    //curRequest.SetLog("\n");

                    //startTime = DateTime.Now;
                    // Match centroids to Gaussians
                    MatchCentroidsToGaussians(arrMUs, lstGaussians);
                    //stopTime = DateTime.Now;
                    //duration = stopTime - startTime;
                    //RunTime = duration.TotalSeconds;
                    //System.Windows.Forms.MessageBox.Show("MatchCentroidsToGaussians Run time " + RunTime + " seconds!");
                    ii = ProjectConstants.MaxAccordRun;
                }
                catch
                {
                    Console.WriteLine("Something went wrong with Accord.net library.");
                    // System.Windows.Forms.MessageBox.Show("Something went wrong with Accord.net library.");
                }
            }

            #endregion

            //startTime = DateTime.Now;
            // Evaluate Goodness Rating
            EvaluateGoodnessRatings(arrModes, arrSigmaXSigmaY, lstGaussians, arrProportions);
            //stopTime = DateTime.Now;
            //duration = stopTime - startTime;
            //RunTime = duration.TotalSeconds;
            //System.Windows.Forms.MessageBox.Show("EvaluateGoodnessRatings Run time " + RunTime + " seconds!");

            //// Debug code
            //// Print out all Gaussians found
            //Console.WriteLine("Goodness Ratio:");
            //for (int ii = 0; ii < lstGaussians.Count; ii++)
            //{
            //    Console.Write("Gaussian number " + ii + ": " + lstGaussians[ii].GoodnessRating + ", ");
            //}
            //Console.Write("\n");

            // Find top N modes
            lstGaussians.Sort();
            lstGaussians.Reverse();
            ////Debug
            //for (int i = 0; i < n; i++)
            //{
            //    curRequest.SetLog("Mode (x,y): " + lstGaussians[i].Mode.X + "," + lstGaussians[i].Mode.Y + " MGR:" + lstGaussians[i].GoodnessRating + "\n");
            //}
            lstGaussians.RemoveRange(N, lstGaussians.Count - N);

            // Rebuild lstCentroids
            lstCentroids.Clear();
            foreach (MapMode mm in lstGaussians)
            {
                lstCentroids.Add(mm.Mode);
            }
        }