Exemple #1
0
        static void Main(string[] args)
        {
            left  = 0;
            right = 1;
            n     = 4;
            h     = (right - left) / (n - 1);
            var deviation = 0.00000001;
            var A         = GetMatrixA();

            double lambdaPrev  = 32; //Initial approach
            double lambdaNext  = 0;
            double deltaLambda = double.MaxValue;

            Console.WriteLine($"Lambda = {lambdaPrev}");
            do
            {
                var D = GetMatrixD(A, lambdaPrev);
                deltaLambda = GetDeltaLambda(D, n);

                lambdaNext = lambdaPrev + deltaLambda;
                lambdaPrev = lambdaNext;
                Console.WriteLine($"Lambda = {lambdaNext}");
            } while (Math.Abs(deltaLambda) > deviation);

            Console.WriteLine($"\nLambda final = {lambdaNext}");
            var decA     = new Accord.Math.Decompositions.EigenvalueDecomposition(A, false, true);
            var eigValsA = decA.RealEigenvalues.OrderBy(c => c);

            Console.WriteLine($"\nReal values   = { string.Join("\t", eigValsA.Select(c => c))}");

            Console.ReadKey();
        }
Exemple #2
0
        public static object RotateCorrs(double[,] Data, object[] Labels, int EigenVectorIndex)
        {
            int nRows = Data.GetLength(0), nCols = Data.GetLength(1);

            double[,] data = (double[, ])Data.Clone();
            double[,] CorrelationMatrix = Accord.Statistics.Measures.Correlation(data);

            Accord.Math.Decompositions.EigenvalueDecomposition evd = new Accord.Math.Decompositions.EigenvalueDecomposition(CorrelationMatrix);
            double[,] values  = evd.DiagonalMatrix;
            double[,] vectors = evd.Eigenvectors;

            double[] eigenVector = vectors.GetColumn(nCols - EigenVectorIndex - 1);
            double[] indices     = Enumerable.Range(0, Labels.Length).Select(x => (double)x).ToArray();
            Array.Sort(eigenVector, indices);

            object[,] permutedData = new object[nRows + 1, nCols];
            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < nCols; ++j)
                {
                    if (i == 0)
                    {
                        permutedData[0, j] = Labels[(int)indices[j]];
                    }

                    permutedData[i + 1, j] = Data[i, (int)indices[j]];
                }
            }

            return(permutedData);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            left  = 0;
            right = 1;
            n     = 4;
            h     = (right - left) / (n - 1);
            var A        = GetMatrixA();
            var AInverse = A.Inverse();

            var decA     = new Accord.Math.Decompositions.EigenvalueDecomposition(A, false, true);
            var eigValsA = decA.RealEigenvalues.OrderBy(c => c);

            Console.WriteLine($"Real eigen values: {string.Join("\t", eigValsA.Select(c => c))}");

            var decAInverse     = new Accord.Math.Decompositions.EigenvalueDecomposition(AInverse, false, true);
            var eigValsAInverse = decAInverse.RealEigenvalues;

            for (int i = 0; i < eigValsAInverse.Length; ++i)
            {
                eigValsAInverse[i] = 1 / eigValsAInverse[i];
            }
            Console.WriteLine($"\nCalculated values: {string.Join("\t", eigValsAInverse.Select(c => c).OrderBy(c => c))}");

            Console.ReadKey();
        }
        public static double[,] RMT(double[,] CorrelationMatrix, int NumEigenValuesToKeep)
        {
            Accord.Math.Decompositions.EigenvalueDecomposition evd = new Accord.Math.Decompositions.EigenvalueDecomposition(CorrelationMatrix);

            double[] eValues = evd.RealEigenvalues;
            double[,] eVectors = evd.Eigenvectors;

            int nEigen = eValues.Length;

            int[]    keys   = Enumerable.Range(0, nEigen).ToArray();
            double[] values = (double[])eValues.Clone();
            Array.Sort(keys, values);

            int n = Math.Min(nEigen, NumEigenValuesToKeep);

            double x = 0;

            for (int i = nEigen - 1; i > nEigen - 1 - n; --i)
            {
                x += values[i];
            }

            double replacementValue = (nEigen - x) / (nEigen - NumEigenValuesToKeep);

            for (int i = nEigen - n - 1; i >= 0; --i)
            {
                values[i] = replacementValue;
            }

            double check = 0;

            for (int i = 0; i < nEigen; ++i)
            {
                check += values[i];
            }

            double[,] reconstructedMatrix = new double[nEigen, nEigen];
            for (int i = 0; i < nEigen; ++i)
            {
                for (int j = 0; j < nEigen; ++j)
                {
                    for (int k = 0; k < nEigen; ++k)
                    {
                        reconstructedMatrix[i, j] += values[k] * eVectors[i, keys[k]] * eVectors[j, keys[k]];
                    }
                }
            }

            double[,] ret = new double[nEigen, nEigen];
            for (int i = 0; i < nEigen; ++i)
            {
                for (int j = 0; j < nEigen; ++j)
                {
                    ret[i, j] = reconstructedMatrix[i, j] / Math.Sqrt(reconstructedMatrix[i, i] * reconstructedMatrix[j, j]);
                }
            }

            return(ret);
        }
Exemple #5
0
 private void calculate_eigendecomposition()
 {
     var e = new Accord.Math.Decompositions.EigenvalueDecomposition(L);
     X = new double[inputSize, NumofClusters];
     for (int i = 0; i < inputSize; i++)
     {
         for (int j = 0; j < NumofClusters; j++)
         {
             X[i,j] = e.Eigenvectors[i, j];
         }
     }
 }
Exemple #6
0
        private static double[] GetCalculatedEigenValues(double[,] B, double[,] C, int m)
        {
            var Cm = GetCm(C, m);
            var Am = new double[n, n];

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    Am[i, j] = B[i, j] + Cm[i, j];
                }
            }
            var dec = new Accord.Math.Decompositions.EigenvalueDecomposition(Am, false, true);

            return(dec.RealEigenvalues.OrderBy(c => c).ToList().ToArray());
        }
Exemple #7
0
        public static object EVD(double[,] Data)
        {
            int nRows = Data.GetLength(0), nCols = Data.GetLength(1);

            if (nRows != nCols)
            {
                throw new Exception("Error, eigendecomposition is only defined for square matrices!");
            }

            Accord.Math.Decompositions.EigenvalueDecomposition evd = new Accord.Math.Decompositions.EigenvalueDecomposition(Data);
            double[,] values  = evd.DiagonalMatrix;
            double[,] vectors = evd.Eigenvectors;

            nRows = Math.Max(values.GetLength(0), vectors.GetLength(0));
            nCols = values.GetLength(1) + 1 + vectors.GetLength(1);

            object[,] ret = new object[nRows, nCols];
            for (int i = 0; i < nRows; ++i)
            {
                for (int j = 0; j < values.GetLength(1); ++j)
                {
                    if (i < values.GetLength(0))
                    {
                        ret[i, j] = values[i, j];
                    }
                }

                ret[i, values.GetLength(1)] = "";

                for (int j = 0; j < vectors.GetLength(1); ++j)
                {
                    if (i < vectors.GetLength(0))
                    {
                        ret[i, j + values.GetLength(1) + 1] = vectors[i, j];
                    }
                }
            }

            return(ret);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            left  = 0;
            right = 1;
            n     = 4;
            h     = (right - left) / (n - 1);
            var deviation = 0.000001;
            var A         = GetMatrixA();

            double lambdaPrev  = 31;
            double lambdaNext  = 0;
            double deltaLambda = double.MaxValue;

            double m = 0;
            double deltaLambdaMain = Math.Abs(lambdaPrev - m);

            Console.WriteLine("First approach::::::::::::::::::::::::");
            Console.WriteLine($"Lambda = {lambdaPrev}");
            do
            {
                Approximation1(A, lambdaPrev, out deltaLambda);
                lambdaNext = lambdaPrev + deltaLambda;

                Console.WriteLine($"Lambda = {lambdaNext}");
                deltaLambdaMain = Math.Abs(lambdaNext - lambdaPrev);
                lambdaPrev      = lambdaNext;
            } while (deltaLambdaMain > deviation);
            Console.WriteLine($"Result = {lambdaNext}");

            double lambdaPrev1 = 31; //Initial left approximation
            double lambdaPrev2 = 34; //Initial right approximation

            double lambdaNext1 = 0;
            double lambdaNext2 = 0;

            double deltaLambda1 = double.MaxValue;
            double deltaLambda2 = double.MaxValue;

            deltaLambdaMain = double.MaxValue;

            Console.WriteLine("\nSecond approach::::::::::::::::::::::::");
            Console.WriteLine($"Lambda = {ArithmeticMean(lambdaPrev1, lambdaPrev2)}");
            do
            {
                Approximation2(A, lambdaPrev1, lambdaPrev2, out deltaLambda1, out deltaLambda2);

                lambdaNext1 = lambdaPrev1 + deltaLambda1;
                lambdaNext2 = lambdaPrev2 + deltaLambda2;

                Console.WriteLine($"Lambda = {ArithmeticMean(lambdaNext1, lambdaNext2)}");
                deltaLambdaMain = Math.Abs(lambdaNext1 - lambdaNext2);

                lambdaPrev1 = lambdaNext1;
                lambdaPrev2 = lambdaNext2;
            } while (deltaLambdaMain > deviation);
            Console.WriteLine($"Result = {ArithmeticMean(lambdaNext1, lambdaPrev2)}");

            Console.WriteLine("\nExpected result::::::::::::::::::::::::");
            var decA     = new Accord.Math.Decompositions.EigenvalueDecomposition(A, false, true);
            var eigValsA = decA.RealEigenvalues.OrderBy(c => c);

            Console.WriteLine($"Real values   = { string.Join("\t", eigValsA.Select(c => c))}");

            Console.ReadKey();
        }
Exemple #9
0
        private static double[] GetRealEigenValues(double[,] A)
        {
            var decA = new Accord.Math.Decompositions.EigenvalueDecomposition(A, false, true);

            return(decA.RealEigenvalues.OrderBy(c => c).ToList().ToArray());
        }