public void EigenvalueDecompositionConstructorTest() { // Symmetric test double[,] A = { { 4, 2 }, { 2, 4 } }; EigenvalueDecomposition target = new EigenvalueDecomposition(A); var D = target.DiagonalMatrix; var Q = target.Eigenvectors; double[,] expectedD = { { 2, 0 }, { 0, 6 } }; double[,] expectedQ = { { 0.7071, 0.7071 }, { -0.7071, 0.7071 } }; Assert.IsTrue(Matrix.IsEqual(expectedD, D, 0.00001)); Assert.IsTrue(Matrix.IsEqual(expectedQ, Q, 0.0001)); // Decomposition identity var actualA = Q.Multiply(D).Multiply(Q.Inverse()); Assert.IsTrue(Matrix.IsEqual(expectedD, D, 0.00001)); Assert.IsTrue(Matrix.IsEqual(A, actualA, 0.0001)); }
public Matrix GetEigenVectorsMatrix(Matrix covarianceMatrix) { EigenvalueDecomposition obj = new EigenvalueDecomposition(covarianceMatrix); //getting the eigen values of the covariance matrix double[] eigenValues = obj.RealEigenvalues; double[] eigenValues_Magnitudes = new double[eigenValues.Length]; //to save the positions of the eigenvalues Dictionary <int, double> lookup = new Dictionary <int, double>(); for (int i = 0; i < eigenValues.Length; i++) { eigenValues_Magnitudes[i] = Math.Pow(eigenValues[i], 2); lookup.Add(i, eigenValues_Magnitudes[i]); } //sort eigen valuesby magnitude Array.Sort(eigenValues_Magnitudes); //getting the eigenvectors Matrix eigenVectors = obj.EigenvectorMatrix; //keeping the first 30 eigenvectors Matrix eigenVectors_Matrix = new Matrix(covarianceMatrix.Rows, 30); for (int j = 0; j < eigenVectors_Matrix.Columns; j++) { for (int i = 0; i < eigenVectors_Matrix.Rows; i++) { eigenVectors_Matrix[i, j] = eigenVectors[i, lookup.FirstOrDefault(x => x.Value == eigenValues_Magnitudes[j]).Key]; } } return(eigenVectors_Matrix); }
public void Random_NByN_Symetric(int n) { Rectangular rand = new Rectangular(); Matrix3 A = rand.Randomfloat(n, n); A = A.Add(Matrix3.Transpose(A)); /// Any matrix added to its transpose will be symetric Assert.That(StandardMatrixTests.IsSymetric(A), Is.True); EigenvalueDecomposition EofA = new EigenvalueDecomposition(A); Matrix3 V = EofA.V; // V is orthogonal V times V transpose is the identity Assert.That(Matrix3.Mult(V, Matrix3.Transpose(V)).ToFloatArray(), Is.EqualTo(Matrix3.Identity.ToFloatArray()).Within(.0000001)); Matrix3 D = EofA.getD(); Matrix3 test = Matrix3.Mult(D, Matrix3.Transpose(V)); test = Matrix3.Mult(V, test); Assert.That(test.ToFloatArray(), Is.EqualTo(A.ToFloatArray()).Within(.0000001).Percent); }
//function used for the faces' database : called just one time public EigenFace(double[,] data1) { // //INPUT : data -> matrix of all face's coordinates //Using the ACCORD framework. For all matrix operations // data = data1; distanceData = new double[data.GetLongLength(1), n]; distanceMouth = new double[data.GetLongLength(1), coupleDistanceMouth.Length]; distanceEyeLeft = new double[data.GetLongLength(1), coupledistanceEyeLeft.Length]; distanceEyeRight = new double[data.GetLongLength(1), coupledistanceEyeRight.Length]; double[,] mReduce = new double[data.GetLowerBound(1), data.GetLowerBound(2)]; double[,] mReduceM = new double[distanceMouth.GetLowerBound(1), distanceMouth.GetLowerBound(2)]; double[,] mReduceEyeL = new double[distanceEyeLeft.GetLowerBound(1), distanceEyeLeft.GetLowerBound(2)]; double[,] mReduceEyeR = new double[distanceEyeRight.GetLowerBound(1), distanceEyeRight.GetLowerBound(2)]; vectorMean = new double[data.GetLowerBound(2)]; vectorMeanM = new double[distanceMouth.GetLowerBound(2)]; vectorMeanEyeL = new double[distanceEyeLeft.GetLowerBound(2)]; vectorMeanEyeR = new double[distanceEyeRight.GetLowerBound(2)]; Cov = new double[data.GetLowerBound(2), data.GetLowerBound(2)]; CovM = new double[distanceMouth.GetLowerBound(2), distanceMouth.GetLowerBound(2)]; CovEyeL = new double[distanceEyeLeft.GetLowerBound(2), distanceEyeLeft.GetLowerBound(2)]; CovEyeR = new double[distanceEyeRight.GetLowerBound(2), distanceEyeRight.GetLowerBound(2)]; eigenVectors = new double[Cov.GetLowerBound(1), Cov.GetLowerBound(2)]; eigenVectorsM = new double[CovM.GetLowerBound(1), CovM.GetLowerBound(2)]; eigenVectorsEyeL = new double[CovEyeL.GetLowerBound(1), CovEyeL.GetLowerBound(2)]; eigenVectorsEyeR = new double[CovEyeR.GetLowerBound(1), CovEyeR.GetLowerBound(2)]; epsilon = new double[nEigenValue, data.GetLowerBound(1)]; epsilonM = new double[nEigenValue, data.GetLowerBound(1)]; epsilonEyeL = new double[nEigenValue, data.GetLowerBound(1)]; epsilonEyeR = new double[nEigenValue, data.GetLowerBound(1)]; for (int j = 0; j < data.GetLongLength(1); j++) { for (int i = 0; i < coupleDistanceMouth.Length; i++) { xa = data[j, coupleDistanceMouth[i, 0]]; xb = data[j, coupleDistanceMouth[i, 1]]; ya = data[j, (coupleDistanceMouth[i, 0]) + 1]; yb = data[j, (coupleDistanceMouth[i, 1]) + 1]; distanceData[j, i] = distance(xa, xb, ya, yb, 0, 0); distanceMouth[j, i] = distance(xa, xb, ya, yb, 0, 0); } for (int i = 0; i < coupledistanceEyeLeft.Length; i++) { xa = data[j, coupledistanceEyeLeft[i, 0]]; xb = data[j, coupledistanceEyeLeft[i, 1]]; ya = data[j, (coupledistanceEyeLeft[i, 0]) + 1]; yb = data[j, (coupledistanceEyeLeft[i, 1]) + 1]; distanceData[j, i] = distance(xa, xb, ya, yb, 0, 0); distanceEyeLeft[j, i] = distance(xa, xb, ya, yb, 0, 0); } for (int i = 0; i < coupledistanceEyeRight.Length; i++) { xa = data[j, coupledistanceEyeRight[i, 0]]; xb = data[j, coupledistanceEyeRight[i, 1]]; ya = data[j, (coupledistanceEyeRight[i, 0]) + 1]; yb = data[j, (coupledistanceEyeRight[i, 1]) + 1]; distanceData[j, i] = distance(xa, xb, ya, yb, 0, 0); distanceEyeRight[j, i] = distance(xa, xb, ya, yb, 0, 0); } } //calculations of mean for each data matrix vectorMean = data.Mean(2); vectorMeanM = distanceMouth.Mean(2); vectorMeanEyeL = distanceEyeLeft.Mean(2); vectorMeanEyeR = distanceEyeRight.Mean(2); //calculations of reduced matrix mReduce = data.Subtract(vectorMean, 1); mReduceM = distanceMouth.Subtract(vectorMeanM, 1); mReduceEyeL = distanceEyeLeft.Subtract(vectorMeanEyeL, 1); mReduceEyeR = distanceEyeRight.Subtract(vectorMeanEyeR, 1); //calculations of covariance matrix Cov = mReduce.Covariance(); CovM = mReduceM.Covariance(); CovEyeL = mReduceEyeL.Covariance(); CovEyeR = mReduceEyeR.Covariance(); //calculations of eigenvectors : is equivalent to the matlab function [V,D] = eig(A) eigenVectors = new EigenvalueDecomposition(Cov).Eigenvectors; eigenVectorsM = new EigenvalueDecomposition(CovM).Eigenvectors; eigenVectorsEyeL = new EigenvalueDecomposition(CovEyeL).Eigenvectors; eigenVectorsEyeR = new EigenvalueDecomposition(CovEyeR).Eigenvectors; //with the three last eigenvalues indexOflastEigenValues[0] = eigenVectors.GetLowerBound(2) - 2; indexOflastEigenValues[1] = eigenVectors.GetLowerBound(2) - 1; indexOflastEigenValues[2] = eigenVectors.GetLowerBound(2); indexOflastEigenValuesM[0] = eigenVectorsM.GetLowerBound(2) - 2; indexOflastEigenValuesM[1] = eigenVectorsM.GetLowerBound(2) - 1; indexOflastEigenValuesM[2] = eigenVectorsM.GetLowerBound(2); indexOflastEigenValuesEyeL[0] = eigenVectorsEyeL.GetLowerBound(2) - 2; indexOflastEigenValuesEyeL[1] = eigenVectorsEyeL.GetLowerBound(2) - 1; indexOflastEigenValuesEyeL[2] = eigenVectorsEyeL.GetLowerBound(2); indexOflastEigenValuesEyeR[0] = eigenVectorsEyeR.GetLowerBound(2) - 2; indexOflastEigenValuesEyeR[1] = eigenVectorsEyeR.GetLowerBound(2) - 1; indexOflastEigenValuesEyeR[2] = eigenVectorsEyeR.GetLowerBound(2); epsilon = eigenVectors.GetColumns(indexOflastEigenValues).Transpose().Multiply(mReduce.Transpose()); epsilonM = eigenVectorsM.GetColumns(indexOflastEigenValuesM).Transpose().Multiply(mReduceM.Transpose()); epsilonEyeL = eigenVectorsEyeL.GetColumns(indexOflastEigenValuesEyeL).Transpose().Multiply(mReduceEyeL.Transpose()); epsilonEyeR = eigenVectorsEyeR.GetColumns(indexOflastEigenValuesEyeR).Transpose().Multiply(mReduceEyeR.Transpose()); }
public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { if (weights == null) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else { this.Means = x.WeightedMean(weights: weights); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.WeightedStandardDeviation(weights, Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } double[,] cov = x.WeightedCovariance(weights, Means); var evd = new EigenvalueDecomposition(cov, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = Jagged.Transpose(evd.Eigenvectors); } } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { if (weights != null) { throw new Exception(); } var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: (VectorType)1, result: ComponentVectors); } CreateComponents(); return(CreateRegression()); }
/// <summary> /// Computes the Kernel Principal Component Analysis algorithm. /// </summary> /// public void Compute(int components) { if (components < 0 || components > Source.GetLength(0)) { throw new ArgumentException( "The number of components must be between 0 and " + "the number of rows in your source data matrix."); } int dimension = Source.GetLength(0); // If needed, center the source matrix sourceCentered = Adjust(Source, Overwrite); // Create the Gram (Kernel) Matrix this.kernelMatrix = new double[dimension, dimension]; for (int i = 0; i < dimension; i++) { double[] row = sourceCentered.GetRow(i); for (int j = i; j < dimension; j++) { double k = kernel.Function(row, sourceCentered.GetRow(j)); kernelMatrix[i, j] = k; // Kernel matrix is symmetric kernelMatrix[j, i] = k; } } // Center the Gram (Kernel) Matrix if requested double[,] Kc = centerFeatureSpace ? centerKernel(kernelMatrix) : kernelMatrix; // Perform the Eigenvalue Decomposition (EVD) of the Kernel matrix EigenvalueDecomposition evd = new EigenvalueDecomposition(Kc, assumeSymmetric: true); // Gets the Eigenvalues and corresponding Eigenvectors double[] evals = evd.RealEigenvalues; double[,] eigs = evd.Eigenvectors; // Sort eigenvalues and vectors in descending order eigs = Matrix.Sort(evals, eigs, new GeneralComparer(ComparerDirection.Descending, true)); // Eliminate unwanted components if (components != Source.GetLength(0)) { eigs = eigs.Submatrix(null, 0, components - 1); evals = evals.Submatrix(0, components - 1); } if (threshold > 0) { // We will be discarding less important // eigenvectors to conserve memory. // Calculate component proportions double sum = 0.0; // total variance for (int i = 0; i < evals.Length; i++) { sum += Math.Abs(evals[i]); } if (sum > 0) { int keep = 0; // Now we will detect how many components we have // have to keep in order to achieve the level of // explained variance specified by the threshold. while (keep < components) { // Get the variance explained by the component double explainedVariance = Math.Abs(evals[keep]); // Check its proportion double proportion = explainedVariance / sum; // Now, if the component explains an // enough proportion of the variance, if (proportion > threshold) { keep++; // We can keep it. } else { break; // Otherwise we can stop, since the } // components are ordered by variance. } if (keep != components) { if (keep > 0) { // Resize the vectors keeping only needed components eigs = eigs.Submatrix(0, components - 1, 0, keep - 1); evals = evals.Submatrix(0, keep - 1); } else { // No component will be kept. eigs = new double[dimension, 0]; evals = new double[0]; } } } } // Normalize eigenvectors if (centerFeatureSpace) { for (int j = 0; j < evals.Length; j++) { double val = Math.Sqrt(Math.Abs(evals[j])); for (int i = 0; i < eigs.GetLength(0); i++) { eigs[i, j] = eigs[i, j] / val; } } } // Set analysis properties this.SingularValues = new double[evals.Length]; this.Eigenvalues = evals; this.ComponentMatrix = eigs; // Project the original data into principal component space this.Result = Kc.Multiply(eigs); // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); }
internal EigenElement(EigenvalueDecomposition data, int idx) { Value = data.RealEigenvalues[idx]; Vector = data.EigenVectors.GetColumnVector(idx).CopyToArray(); }
/// <summary> /// Learns a model that can map the given inputs to the desired outputs. /// </summary> /// /// <param name="x">The model inputs.</param> /// <param name="weights">The weight of importance for each input sample.</param> /// /// <returns> /// A model that has learned how to produce suitable outputs /// given the input data <paramref name="x" />. /// </returns> /// public MultivariateLinearRegression Learn(double[][] x, double[] weights = null) { this.NumberOfInputs = x.Columns(); if (Method == PrincipalComponentMethod.Center || Method == PrincipalComponentMethod.Standardize) { if (weights == null) { this.Means = x.Mean(dimension: 0); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.StandardDeviation(Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: false, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; Eigenvalues = SingularValues.Pow(2); Eigenvalues.Divide(x.Rows() - 1, result: Eigenvalues); ComponentVectors = svd.RightSingularVectors.Transpose(); } else { this.Means = x.WeightedMean(weights: weights); double[][] matrix = Overwrite ? x : Jagged.CreateAs(x); x.Subtract(Means, dimension: (VectorType)0, result: matrix); if (Method == PrincipalComponentMethod.Standardize) { this.StandardDeviations = x.WeightedStandardDeviation(weights, Means); matrix.Divide(StandardDeviations, dimension: (VectorType)0, result: matrix); } double[,] cov = x.WeightedCovariance(weights, Means); // Perform the Eigenvalue Decomposition of the covariance // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new EigenvalueDecomposition(cov, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = Jagged.Transpose(evd.Eigenvectors); } } else if (Method == PrincipalComponentMethod.CovarianceMatrix || Method == PrincipalComponentMethod.CorrelationMatrix) { if (weights != null) { throw new Exception(); } // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new JaggedEigenvalueDecomposition(x, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; SingularValues = Eigenvalues.Sqrt(); ComponentVectors = evd.Eigenvectors.Transpose(); } else { // The method type should have been validated before we even entered this section throw new InvalidOperationException("Invalid method, this should never happen: {0}".Format(Method)); } if (Whiten) { ComponentVectors.Divide(SingularValues, dimension: (VectorType)1, result: ComponentVectors); } // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); return(CreateRegression()); }
//--------------------------------------------- #region Public Methods /// <summary>Computes the Kernel Principal Component Analysis algorithm.</summary> public override void Compute() { int rows = Source.GetLength(0); // Center (adjust) the source matrix sourceCentered = Adjust(Source, Overwrite); // Create the Gram (Kernel) Matrix double[,] K = new double[rows, rows]; for (int i = 0; i < rows; i++) { for (int j = i; j < rows; j++) { double k = kernel.Function(sourceCentered.GetRow(i), sourceCentered.GetRow(j)); K[i, j] = k; // Kernel matrix is symmetric K[j, i] = k; } } // Center the Gram (Kernel) Matrix if (centerFeatureSpace) { K = centerKernel(K); } // Perform the Eigenvalue Decomposition (EVD) of the Kernel matrix EigenvalueDecomposition evd = new EigenvalueDecomposition(K, true); // Gets the eigenvalues and corresponding eigenvectors double[] evals = evd.RealEigenvalues; double[,] eigs = evd.Eigenvectors; // Sort eigenvalues and vectors in descending order eigs = Matrix.Sort(evals, eigs, new GeneralComparer(ComparerDirection.Descending, true)); if (threshold > 0) { // Calculate proportions in advance double sum = 0.0; for (int i = 0; i < evals.Length; i++) { sum += System.Math.Abs(evals[i]); } if (sum > 0) { sum = 1.0 / sum; // Discard less important eigenvectors to conserve memory int keep = 0; while (keep < evals.Length && System.Math.Abs(evals[keep]) * sum > threshold) { keep++; } eigs = eigs.Submatrix(0, evals.Length - 1, 0, keep - 1); evals = evals.Submatrix(0, keep - 1); } } // Normalize eigenvectors if (centerFeatureSpace) { for (int j = 0; j < evals.Length; j++) { double eig = System.Math.Sqrt(System.Math.Abs(evals[j])); for (int i = 0; i < eigs.GetLength(0); i++) { eigs[i, j] = eigs[i, j] / eig; } } } // Set analysis properties this.SingularValues = new double[evals.Length]; this.Eigenvalues = evals; this.ComponentMatrix = eigs; // Project the original data into principal component space this.Result = K.Multiply(eigs); // Computes additional information about the analysis and creates the // object-oriented structure to hold the principal components found. CreateComponents(); }
public static double[,] GetEigenMatrix(this double[,] matrix) { var dec = new EigenvalueDecomposition(matrix); return(dec.Eigenvectors.Multiply(dec.RealEigenvalues.Select(Math.Sqrt).ToArray().ToDiagonalMatrix())); }
//--------------------------------------------- #region Public Methods /// <summary> /// Computes the Multi-Class Kernel Discriminant Analysis algorithm. /// </summary> public override void Compute() { // Get some initial information int dimension = Source.GetLength(0); double[,] source = Source; double total = dimension; // Create the Gram (Kernel) Matrix double[,] K = new double[dimension, dimension]; for (int i = 0; i < dimension; i++) { for (int j = i; j < dimension; j++) { double s = kernel.Function(source.GetRow(i), source.GetRow(j)); K[i, j] = s; K[j, i] = s; } } // Compute entire data set measures base.Means = Tools.Mean(K); base.StandardDeviations = Tools.StandardDeviation(K, Means); // Initialize the kernel analogue scatter matrices double[,] Sb = new double[dimension, dimension]; double[,] Sw = new double[dimension, dimension]; // For each class for (int c = 0; c < Classes.Count; c++) { // Get the Kernel matrix class subset double[,] Kc = K.Submatrix(Classes[c].Indexes); int count = Kc.GetLength(0); // Get the Kernel matrix class mean double[] mean = Tools.Mean(Kc); // Construct the Kernel equivalent of the Within-Class Scatter matrix double[,] Swi = Tools.Scatter(Kc, mean, (double)count); // Sw = Sw + Swi for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { Sw[i, j] += Swi[i, j]; } } // Construct the Kernel equivalent of the Between-Class Scatter matrix double[] d = mean.Subtract(base.Means); double[,] Sbi = d.Multiply(d.Transpose()).Multiply(total); // Sb = Sb + Sbi for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { Sb[i, j] += Sbi[i, j]; } } // Store additional information base.ClassScatter[c] = Swi; base.ClassCount[c] = count; base.ClassMeans[c] = mean; base.ClassStandardDeviations[c] = Tools.StandardDeviation(Kc, mean); } // Add regularization for (int i = 0; i < dimension; i++) { Sw[i, i] += regularization; } // Compute eigen value decomposition double[,] C = Matrix.Inverse(Sw).Multiply(Sb); EigenvalueDecomposition evd = new EigenvalueDecomposition(C); // Gets the eigenvalues and corresponding eigenvectors double[] evals = evd.RealEigenvalues; double[,] eigs = evd.Eigenvectors; // Sort eigen values and vectors in ascending order eigs = Matrix.Sort(evals, eigs, new GeneralComparer(ComparerDirection.Descending, true)); if (threshold > 0) { // Calculate proportions earlier double sum = 0.0; for (int i = 0; i < dimension; i++) { sum += System.Math.Abs(evals[i]); } if (sum > 0) { sum = 1.0 / sum; // Discard less important eigenvectors to conserve memory int keep = 0; while (keep < dimension && System.Math.Abs(evals[keep]) * sum > threshold) { keep++; } eigs = eigs.Submatrix(0, dimension - 1, 0, keep - 1); evals = evals.Submatrix(0, keep - 1); } } // Store information base.Eigenvalues = evals; base.DiscriminantMatrix = eigs; base.ScatterBetweenClass = Sb; base.ScatterWithinClass = Sw; // Compute feature space means for later classification for (int c = 0; c < Classes.Count; c++) { double[] mean = new double[eigs.GetLength(1)]; for (int i = 0; i < eigs.GetLength(0); i++) { for (int j = 0; j < eigs.GetLength(1); j++) { mean[j] += ClassMeans[c][i] * eigs[i, j]; } } kernelClassMeans[c] = mean; } // Computes additional information about the analysis and creates the // object-oriented structure to hold the discriminants found. createDiscriminants(); }
/// <summary>An exception is thrown at the end of the process, /// if any error is encountered.</summary> [Test] public void AllTests() { Matrix A, B, C, Z, O, I, R, S, X, SUB, M, T, SQ, DEF, SOL; int errorCount = 0; int warningCount = 0; double tmp; double[] columnwise = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 }; double[] rowwise = { 1.0, 4.0, 7.0, 10.0, 2.0, 5.0, 8.0, 11.0, 3.0, 6.0, 9.0, 12.0 }; double[,] avals = { { 1.0, 4.0, 7.0, 10.0 }, { 2.0, 5.0, 8.0, 11.0 }, { 3.0, 6.0, 9.0, 12.0 } }; double[,] rankdef = avals; double[,] tvals = { { 1.0, 2.0, 3.0 }, { 4.0, 5.0, 6.0 }, { 7.0, 8.0, 9.0 }, { 10.0, 11.0, 12.0 } }; double[,] subavals = { { 5.0, 8.0, 11.0 }, { 6.0, 9.0, 12.0 } }; double[,] pvals = { { 1.0, 1.0, 1.0 }, { 1.0, 2.0, 3.0 }, { 1.0, 3.0, 6.0 } }; double[,] ivals = { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 } }; double[,] evals = { { 0.0, 1.0, 0.0, 0.0 }, { 1.0, 0.0, 2e-7, 0.0 }, { 0.0, -2e-7, 0.0, 1.0 }, { 0.0, 0.0, 1.0, 0.0 } }; double[,] square = { { 166.0, 188.0, 210.0 }, { 188.0, 214.0, 240.0 }, { 210.0, 240.0, 270.0 } }; double[,] sqSolution = { { 13.0 }, { 15.0 } }; double[,] condmat = { { 1.0, 3.0 }, { 7.0, 9.0 } }; int rows = 3, cols = 4; int invalidld = 5; /* should trigger bad shape for construction with val */ int validld = 3; /* leading dimension of intended test Matrices */ int nonconformld = 4; /* leading dimension which is valid, but nonconforming */ int ib = 1, ie = 2, jb = 1, je = 3; /* index ranges for sub Matrix */ int[] rowindexset = new int[] { 1, 2 }; int[] badrowindexset = new int[] { 1, 3 }; int[] columnindexset = new int[] { 1, 2, 3 }; int[] badcolumnindexset = new int[] { 1, 2, 4 }; double columnsummax = 33.0; double rowsummax = 30.0; double sumofdiagonals = 15; double sumofsquares = 650; // Constructors and constructor-like methods: // double[], int // double[,] // int, int // int, int, double // int, int, double[,] // Create(double[,]) // Random(int,int) // Identity(int) print("\nTesting constructors and constructor-like methods...\n"); try { // check that exception is thrown in packed constructor with invalid length A = new Matrix(columnwise, invalidld); errorCount = try_failure(errorCount, "Catch invalid length in packed constructor... ", "exception not thrown for invalid input"); } catch (System.ArgumentException e) { try_success("Catch invalid length in packed constructor... ", e.Message); } A = new Matrix(columnwise, validld); B = new Matrix(avals); tmp = B[0, 0]; avals[0, 0] = 0.0; C = B - A; avals[0, 0] = tmp; B = Matrix.Create(avals); tmp = B[0, 0]; avals[0, 0] = 0.0; if ((tmp - B[0, 0]) != 0.0) { // check that Create behaves properly errorCount = try_failure(errorCount, "Create... ", "Copy not effected... data visible outside"); } else { try_success("Create... ", ""); } avals[0, 0] = columnwise[0]; I = new Matrix(ivals); try { check(I, Matrix.Identity(3, 4)); try_success("Identity... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Identity... ", "Identity Matrix not successfully created"); System.Console.Out.WriteLine(e.Message); } // Access Methods: // getColumnDimension() // getRowDimension() // getArray() // getArrayCopy() // getColumnPackedCopy() // getRowPackedCopy() // get(int,int) // GetMatrix(int,int,int,int) // GetMatrix(int,int,int[]) // GetMatrix(int[],int,int) // GetMatrix(int[],int[]) // set(int,int,double) // SetMatrix(int,int,int,int,Matrix) // SetMatrix(int,int,int[],Matrix) // SetMatrix(int[],int,int,Matrix) // SetMatrix(int[],int[],Matrix) print("\nTesting access methods...\n"); // Various get methods B = new Matrix(avals); if (B.RowCount != rows) { errorCount = try_failure(errorCount, "getRowDimension... ", ""); } else { try_success("getRowDimension... ", ""); } if (B.ColumnCount != cols) { errorCount = try_failure(errorCount, "getColumnDimension... ", ""); } else { try_success("getColumnDimension... ", ""); } B = new Matrix(avals); double[,] barray = (Matrix)B; if (barray != avals) { errorCount = try_failure(errorCount, "getArray... ", ""); } else { try_success("getArray... ", ""); } barray = (Matrix)B.Clone(); if (barray == avals) { errorCount = try_failure(errorCount, "getArrayCopy... ", "data not (deep) copied"); } try { check(barray, avals); try_success("getArrayCopy... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "getArrayCopy... ", "data not successfully (deep) copied"); System.Console.Out.WriteLine(e.Message); } // double[] bpacked = B.ColumnPackedCopy; // try // { // check(bpacked, columnwise); // try_success("getColumnPackedCopy... ", ""); // } // catch (System.SystemException e) // { // errorCount = try_failure(errorCount, "getColumnPackedCopy... ", "data not successfully (deep) copied by columns"); // System.Console.Out.WriteLine(e.Message); // } // bpacked = B.RowPackedCopy; // try // { // check(bpacked, rowwise); // try_success("getRowPackedCopy... ", ""); // } // catch (System.SystemException e) // { // errorCount = try_failure(errorCount, "getRowPackedCopy... ", "data not successfully (deep) copied by rows"); // System.Console.Out.WriteLine(e.Message); // } try { tmp = B[B.RowCount, B.ColumnCount - 1]; errorCount = try_failure(errorCount, "get(int,int)... ", "OutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { tmp = B[B.RowCount - 1, B.ColumnCount]; errorCount = try_failure(errorCount, "get(int,int)... ", "OutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("get(int,int)... OutofBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "get(int,int)... ", "OutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { if (B[B.RowCount - 1, B.ColumnCount - 1] != avals[B.RowCount - 1, B.ColumnCount - 1]) { errorCount = try_failure(errorCount, "get(int,int)... ", "Matrix entry (i,j) not successfully retreived"); } else { try_success("get(int,int)... ", ""); } } catch (System.IndexOutOfRangeException e) { errorCount = try_failure(errorCount, "get(int,int)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e.Message); } SUB = new Matrix(subavals); try { M = B.GetMatrix(ib, ie + B.RowCount + 1, jb, je); errorCount = try_failure(errorCount, "GetMatrix(int,int,int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { M = B.GetMatrix(ib, ie, jb, je + B.ColumnCount + 1); errorCount = try_failure(errorCount, "GetMatrix(int,int,int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("GetMatrix(int,int,int,int)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { M = B.GetMatrix(ib, ie, jb, je); try { check(SUB, M); try_success("GetMatrix(int,int,int,int)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int,int)... ", "submatrix not successfully retreived"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int,int)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e.Message); } try { M = B.GetMatrix(ib, ie, badcolumnindexset); errorCount = try_failure(errorCount, "GetMatrix(int,int,int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { M = B.GetMatrix(ib, ie + B.RowCount + 1, columnindexset); errorCount = try_failure(errorCount, "GetMatrix(int,int,int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("GetMatrix(int,int,int[])... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { M = B.GetMatrix(ib, ie, columnindexset); try { check(SUB, M); try_success("GetMatrix(int,int,int[])... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int[])... ", "submatrix not successfully retreived"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e) { errorCount = try_failure(errorCount, "GetMatrix(int,int,int[])... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e.Message); } try { M = B.GetMatrix(badrowindexset, jb, je); errorCount = try_failure(errorCount, "GetMatrix(int[],int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { M = B.GetMatrix(rowindexset, jb, je + B.ColumnCount + 1); errorCount = try_failure(errorCount, "GetMatrix(int[],int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("GetMatrix(int[],int,int)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "GetMatrix(int[],int,int)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { M = B.GetMatrix(rowindexset, jb, je); try { check(SUB, M); try_success("GetMatrix(int[],int,int)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "GetMatrix(int[],int,int)... ", "submatrix not successfully retreived"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e) { errorCount = try_failure(errorCount, "GetMatrix(int[],int,int)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e.Message); } try { M = B.GetMatrix(badrowindexset, columnindexset); errorCount = try_failure(errorCount, "GetMatrix(int[],int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { M = B.GetMatrix(rowindexset, badcolumnindexset); errorCount = try_failure(errorCount, "GetMatrix(int[],int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("GetMatrix(int[],int[])... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "GetMatrix(int[],int[])... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { M = B.GetMatrix(rowindexset, columnindexset); try { check(SUB, M); try_success("GetMatrix(int[],int[])... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "GetMatrix(int[],int[])... ", "submatrix not successfully retreived"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e) { errorCount = try_failure(errorCount, "GetMatrix(int[],int[])... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e.Message); } // Various set methods: try { B[B.RowCount, B.ColumnCount - 1] = 0.0; errorCount = try_failure(errorCount, "set(int,int,double)... ", "OutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { B[B.RowCount - 1, B.ColumnCount] = 0.0; errorCount = try_failure(errorCount, "set(int,int,double)... ", "OutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("set(int,int,double)... OutofBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "set(int,int,double)... ", "OutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { B[ib, jb] = 0.0; tmp = B[ib, jb]; try { check(tmp, 0.0); try_success("set(int,int,double)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "set(int,int,double)... ", "Matrix element not successfully set"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e1) { errorCount = try_failure(errorCount, "set(int,int,double)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e1.Message); } M = new Matrix(2, 3, 0.0); try { B.SetMatrix(ib, ie + B.RowCount + 1, jb, je, M); errorCount = try_failure(errorCount, "SetMatrix(int,int,int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { B.SetMatrix(ib, ie, jb, je + B.ColumnCount + 1, M); errorCount = try_failure(errorCount, "SetMatrix(int,int,int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("SetMatrix(int,int,int,int,Matrix)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(ib, ie, jb, je, M); try { check(M - B.GetMatrix(ib, ie, jb, je), M); try_success("SetMatrix(int,int,int,int,Matrix)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int,int,Matrix)... ", "submatrix not successfully set"); System.Console.Out.WriteLine(e.Message); } B.SetMatrix(ib, ie, jb, je, SUB); } catch (System.IndexOutOfRangeException e1) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int,int,Matrix)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(ib, ie + B.RowCount + 1, columnindexset, M); errorCount = try_failure(errorCount, "SetMatrix(int,int,int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { B.SetMatrix(ib, ie, badcolumnindexset, M); errorCount = try_failure(errorCount, "SetMatrix(int,int,int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("SetMatrix(int,int,int[],Matrix)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(ib, ie, columnindexset, M); try { check(M - B.GetMatrix(ib, ie, columnindexset), M); try_success("SetMatrix(int,int,int[],Matrix)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int[],Matrix)... ", "submatrix not successfully set"); System.Console.Out.WriteLine(e.Message); } B.SetMatrix(ib, ie, jb, je, SUB); } catch (System.IndexOutOfRangeException e1) { errorCount = try_failure(errorCount, "SetMatrix(int,int,int[],Matrix)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(rowindexset, jb, je + B.ColumnCount + 1, M); errorCount = try_failure(errorCount, "SetMatrix(int[],int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { B.SetMatrix(badrowindexset, jb, je, M); errorCount = try_failure(errorCount, "SetMatrix(int[],int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("SetMatrix(int[],int,int,Matrix)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "SetMatrix(int[],int,int,Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(rowindexset, jb, je, M); try { check(M - B.GetMatrix(rowindexset, jb, je), M); try_success("SetMatrix(int[],int,int,Matrix)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "SetMatrix(int[],int,int,Matrix)... ", "submatrix not successfully set"); System.Console.Out.WriteLine(e.Message); } B.SetMatrix(ib, ie, jb, je, SUB); } catch (System.IndexOutOfRangeException e1) { errorCount = try_failure(errorCount, "SetMatrix(int[],int,int,Matrix)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(rowindexset, badcolumnindexset, M); errorCount = try_failure(errorCount, "SetMatrix(int[],int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e) { System.Console.Out.WriteLine(e.Message); try { B.SetMatrix(badrowindexset, columnindexset, M); errorCount = try_failure(errorCount, "SetMatrix(int[],int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); } catch (System.IndexOutOfRangeException e1) { try_success("SetMatrix(int[],int[],Matrix)... ArrayIndexOutOfBoundsException... ", ""); System.Console.Out.WriteLine(e1.Message); } } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "SetMatrix(int[],int[],Matrix)... ", "ArrayIndexOutOfBoundsException expected but not thrown"); System.Console.Out.WriteLine(e1.Message); } try { B.SetMatrix(rowindexset, columnindexset, M); try { check(M - B.GetMatrix(rowindexset, columnindexset), M); try_success("SetMatrix(int[],int[],Matrix)... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "SetMatrix(int[],int[],Matrix)... ", "submatrix not successfully set"); System.Console.Out.WriteLine(e.Message); } } catch (System.IndexOutOfRangeException e1) { errorCount = try_failure(errorCount, "SetMatrix(int[],int[],Matrix)... ", "Unexpected ArrayIndexOutOfBoundsException"); System.Console.Out.WriteLine(e1.Message); } // Array-like methods: // Subtract // SubtractEquals // Add // AddEquals // ArrayLeftDivide // ArrayLeftDivideEquals // ArrayRightDivide // ArrayRightDivideEquals // arrayTimes // ArrayMultiplyEquals // uminus print("\nTesting array-like methods...\n"); S = new Matrix(columnwise, nonconformld); R = Matrix.Random(A.RowCount, A.ColumnCount); A = R; try { S = A - S; errorCount = try_failure(errorCount, "Subtract conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("Subtract conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } if ((A - R).Norm1() != 0.0) { errorCount = try_failure(errorCount, "Subtract... ", "(difference of identical Matrices is nonzero,\nSubsequent use of Subtract should be suspect)"); } else { try_success("Subtract... ", ""); } A = (Matrix)R.Clone(); A.Subtract(R); Z = new Matrix(A.RowCount, A.ColumnCount); try { A.Subtract(S); errorCount = try_failure(errorCount, "SubtractEquals conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("SubtractEquals conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } if ((A - Z).Norm1() != 0.0) { errorCount = try_failure(errorCount, "SubtractEquals... ", "(difference of identical Matrices is nonzero,\nSubsequent use of Subtract should be suspect)"); } else { try_success("SubtractEquals... ", ""); } A = (Matrix)R.Clone(); B = Matrix.Random(A.RowCount, A.ColumnCount); C = A - B; try { S = A + S; errorCount = try_failure(errorCount, "Add conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("Add conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } try { check(C + B, A); try_success("Add... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Add... ", "(C = A - B, but C + B != A)"); System.Console.Out.WriteLine(e.Message); } C = A - B; C.Add(B); try { A.Add(S); errorCount = try_failure(errorCount, "AddEquals conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("AddEquals conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } try { check(C, A); try_success("AddEquals... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "AddEquals... ", "(C = A - B, but C = C + B != A)"); System.Console.Out.WriteLine(e.Message); } A = ((Matrix)R.Clone()); A.UnaryMinus(); try { check(A + R, Z); try_success("UnaryMinus... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "uminus... ", "(-A + A != zeros)"); System.Console.Out.WriteLine(e.Message); } A = (Matrix)R.Clone(); O = new Matrix(A.RowCount, A.ColumnCount, 1.0); try { Matrix.ArrayDivide(A, S); errorCount = try_failure(errorCount, "ArrayRightDivide conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("ArrayRightDivide conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } C = Matrix.ArrayDivide(A, R); try { check(C, O); try_success("ArrayRightDivide... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "ArrayRightDivide... ", "(M./M != ones)"); System.Console.Out.WriteLine(e.Message); } try { A.ArrayDivide(S); errorCount = try_failure(errorCount, "ArrayRightDivideEquals conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("ArrayRightDivideEquals conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } A.ArrayDivide(R); try { check(A, O); try_success("ArrayRightDivideEquals... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "ArrayRightDivideEquals... ", "(M./M != ones)"); System.Console.Out.WriteLine(e.Message); } A = (Matrix)R.Clone(); B = Matrix.Random(A.RowCount, A.ColumnCount); try { S = Matrix.ArrayMultiply(A, S); errorCount = try_failure(errorCount, "arrayTimes conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("arrayTimes conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } C = Matrix.ArrayMultiply(A, B); try { C.ArrayDivide(B); check(C, A); try_success("arrayTimes... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "arrayTimes... ", "(A = R, C = A.*B, but C./B != A)"); System.Console.Out.WriteLine(e.Message); } try { A.ArrayMultiply(S); errorCount = try_failure(errorCount, "ArrayMultiplyEquals conformance check... ", "nonconformance not raised"); } catch (System.ArgumentException e) { try_success("ArrayMultiplyEquals conformance check... ", ""); System.Console.Out.WriteLine(e.Message); } A.ArrayMultiply(B); try { A.ArrayDivide(B); check(A, R); try_success("ArrayMultiplyEquals... ", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "ArrayMultiplyEquals... ", "(A = R, A = A.*B, but A./B != R)"); System.Console.Out.WriteLine(e.Message); } // LA methods: // Transpose // Multiply // Condition // Rank // Determinant // trace // Norm1 // norm2 // normF // normInf // Solve // solveTranspose // Inverse // chol // Eigen // lu // qr // svd print("\nTesting linear algebra methods...\n"); A = new Matrix(columnwise, 3); T = new Matrix(tvals); T = Matrix.Transpose(A); try { check(Matrix.Transpose(A), T); try_success("Transpose...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Transpose()...", "Transpose unsuccessful"); System.Console.Out.WriteLine(e.Message); } Matrix.Transpose(A); try { check(A.Norm1(), columnsummax); try_success("Norm1...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Norm1()...", "incorrect norm calculation"); System.Console.Out.WriteLine(e.Message); } try { check(A.NormInf(), rowsummax); try_success("normInf()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "normInf()...", "incorrect norm calculation"); System.Console.Out.WriteLine(e.Message); } try { check(A.NormF(), System.Math.Sqrt(sumofsquares)); try_success("normF...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "normF()...", "incorrect norm calculation"); System.Console.Out.WriteLine(e.Message); } try { check(A.Trace(), sumofdiagonals); try_success("trace()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "trace()...", "incorrect trace calculation"); System.Console.Out.WriteLine(e.Message); } try { check(A.GetMatrix(0, A.RowCount - 1, 0, A.RowCount - 1).Determinant(), 0.0); try_success("Determinant()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Determinant()...", "incorrect determinant calculation"); System.Console.Out.WriteLine(e.Message); } SQ = new Matrix(square); try { check(A * Matrix.Transpose(A), SQ); try_success("Multiply(Matrix)...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Multiply(Matrix)...", "incorrect Matrix-Matrix product calculation"); System.Console.Out.WriteLine(e.Message); } try { check(0.0 * A, Z); try_success("Multiply(double)...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Multiply(double)...", "incorrect Matrix-scalar product calculation"); System.Console.Out.WriteLine(e.Message); } A = new Matrix(columnwise, 4); QRDecomposition QR = A.QRD(); R = QR.R; try { check(A, QR.Q * R); try_success("QRDecomposition...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "QRDecomposition...", "incorrect QR decomposition calculation"); System.Console.Out.WriteLine(e.Message); } SingularValueDecomposition SVD = A.SVD(); try { check(A, SVD.LeftSingularVectors * (SVD.S * Matrix.Transpose(SVD.RightSingularVectors))); try_success("SingularValueDecomposition...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "SingularValueDecomposition...", "incorrect singular value decomposition calculation"); System.Console.Out.WriteLine(e.Message); } DEF = new Matrix(rankdef); try { check(DEF.Rank(), System.Math.Min(DEF.RowCount, DEF.ColumnCount) - 1); try_success("Rank()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Rank()...", "incorrect Rank calculation"); System.Console.Out.WriteLine(e.Message); } B = new Matrix(condmat); SVD = B.SVD(); double[] singularvalues = SVD.SingularValues; try { check(B.Condition(), singularvalues[0] / singularvalues[System.Math.Min(B.RowCount, B.ColumnCount) - 1]); try_success("Condition()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Condition()...", "incorrect condition number calculation"); System.Console.Out.WriteLine(e.Message); } int n = A.ColumnCount; A = A.GetMatrix(0, n - 1, 0, n - 1); A[0, 0] = 0.0; LUDecomposition LU = A.LUD(); try { check(A.GetMatrix(LU.Pivot, 0, n - 1), LU.L * LU.U); try_success("LUDecomposition...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "LUDecomposition...", "incorrect LU decomposition calculation"); System.Console.Out.WriteLine(e.Message); } X = A.Inverse(); try { check(A * X, Matrix.Identity(3, 3)); try_success("Inverse()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Inverse()...", "incorrect Inverse calculation"); System.Console.Out.WriteLine(e.Message); } O = new Matrix(SUB.RowCount, 1, 1.0); SOL = new Matrix(sqSolution); SQ = SUB.GetMatrix(0, SUB.RowCount - 1, 0, SUB.RowCount - 1); try { check(SQ.Solve(SOL), O); try_success("Solve()...", ""); } catch (System.ArgumentException e1) { errorCount = try_failure(errorCount, "Solve()...", e1.Message); System.Console.Out.WriteLine(e1.Message); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "Solve()...", e.Message); System.Console.Out.WriteLine(e.Message); } A = new Matrix(pvals); CholeskyDecomposition Chol = A.chol(); Matrix L = Chol.GetL(); try { check(A, L * Matrix.Transpose(L)); try_success("CholeskyDecomposition...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "CholeskyDecomposition...", "incorrect Cholesky decomposition calculation"); System.Console.Out.WriteLine(e.Message); } X = Chol.Solve(Matrix.Identity(3, 3)); try { check(A * X, Matrix.Identity(3, 3)); try_success("CholeskyDecomposition Solve()...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "CholeskyDecomposition Solve()...", "incorrect Choleskydecomposition Solve calculation"); System.Console.Out.WriteLine(e.Message); } EigenvalueDecomposition Eig = A.Eigen(); Matrix D = Eig.BlockDiagonal; Matrix V = Eig.EigenVectors; try { check(A * V, V * D); try_success("EigenvalueDecomposition (symmetric)...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "EigenvalueDecomposition (symmetric)...", "incorrect symmetric Eigenvalue decomposition calculation"); System.Console.Out.WriteLine(e.Message); } A = new Matrix(evals); Eig = A.Eigen(); D = Eig.BlockDiagonal; V = Eig.EigenVectors; try { check(A * V, V * D); try_success("EigenvalueDecomposition (nonsymmetric)...", ""); } catch (System.SystemException e) { errorCount = try_failure(errorCount, "EigenvalueDecomposition (nonsymmetric)...", "incorrect nonsymmetric Eigenvalue decomposition calculation"); System.Console.Out.WriteLine(e.Message); } print("\nTestMatrix completed.\n"); print("Total errors reported: " + System.Convert.ToString(errorCount) + "\n"); print("Total warnings reported: " + System.Convert.ToString(warningCount) + "\n"); if (errorCount > 0) { throw new Exception("Errors reported."); } }
private static void МетодАнализаИерархий(List <List <double> > matrix) { Matrix A = GetMatrixFromListOfLists(matrix); Console.WriteLine("Введенная матрица:"); PrintMatrix(A); Console.WriteLine(); var eigen = new EigenvalueDecomposition(A); double Lmax = 0; Console.WriteLine("Корни характеристического уравнения:"); foreach (var item in eigen.RealEigenvalues) { Console.WriteLine(item.ToString("0.00")); Lmax = item > Lmax ? item : Lmax; } Console.WriteLine("Максимальный корень из найденных: " + Lmax.ToString("0.00")); Console.WriteLine(); var CI = (Lmax - A.Columns) / (A.Columns - 1); Console.WriteLine("Индекс совместности: " + CI.ToString("0.00")); Console.WriteLine(); if (CI > 0.1) { Console.WriteLine("Ошибка: Индекс совместности системы > 0.1"); return; } // новое значение диагонального элемента матрицы for (int d = 0; d < A.Columns; d++) { A[d, d] = 1 - Lmax; } Console.WriteLine("Полученная матрица для составления однородной СЛАУ"); PrintMatrix(A); // решить систему уравнений var B = new Matrix(A.Rows, 1); for (int i = 0; i < A.Rows; i++) { B[i, 0] = Math.Pow(0.1, 2); } // нормировать полученные значения var W = A.Solve(B); var summa = 0.0; for (int i = 0; i < A.Rows; i++) { summa += Math.Round(W[i, 0], 2); } for (int i = 0; i < A.Rows; i++) { W[i, 0] = Math.Round(W[i, 0] / summa, 2); } Console.WriteLine("Искомый нормированный весовой вектор:"); PrintMatrix(W); }
public void TwoByTwo_Symetric() { string strA = @"2 1 1 2"; Matrix3 A = new Matrix3(); A.Parse(strA); string strExpectedD = @"1 0 0 3"; Matrix3 ExpectedD = new Matrix3(); ExpectedD.Parse(strExpectedD); string strExpectedV = @" -0.7071 0.7071 0.7071 0.7071"; Matrix3 ExpectedV = new Matrix3(); ExpectedV.Parse(strExpectedV); //This is the value that one will get from Matlab. The eigenvalue Decomposition returns //the following 0.7071 0.7071 // -0.7071 0.7071" // (Warning Proof ahead ) //The problem stems from the fact that the Eigenvalue decomposition is not entirely unique. // The A is decomposed such that A = V * D * V'; // Consider the diagonal matrix J such that all of its diagonal values are eiher 1 or -1. J*J = I. // (V *J) * D * (V * J)' = V * J * D * J' * V' using (AB)' = B'A' The transpose of the product is the transpose of the elements reversed. // V * J * D * J' * V' = V * J * D * J * V' as J is diagonal // V * J * D * J * V' = V * J * J * D * V' as J and D are diagonal. // V * J * J * D * V' = V * D * V' since J*J = I. // (Proof finished) // In practical terms this means that Assert.That(V, Is.EqualTo(ExpectedV) ); // is not a good test. and I will need to test if it is equivalent instead. Assert.That(StandardMatrixTests.IsSymetric(A), Is.True); EigenvalueDecomposition EofA = new EigenvalueDecomposition(A); float[] realEigenValues = EofA.EV; float[] imaginaryEigenValues = EofA.EV; Matrix3 V = EofA.V; Debug.WriteLine(V.ToString()); // Assert.That(V, Is.EqualTo(ExpectedV) ); Not enough uniqueness so does not work //TestEigenvalueVEquivalent(V, ExpectedV); // V is orthogonal V times V transpose is the identity Assert.That(Matrix3.Mult(V, Matrix3.Transpose(V)).ToFloatArray(), Is.EqualTo(Matrix3.Identity.ToFloatArray()).Within(.0000001)); Matrix3 D = EofA.getD(); Assert.That(StandardMatrixTests.IsDiagonal(D), Is.True); //Diagonal which for 2x2 is diagonal Assert.That(D.ToFloatArray(), Is.EqualTo(ExpectedD.ToFloatArray()).Within(10).Ulps); //V * D * V,transpose = A Matrix3 test = Matrix3.Mult(D, Matrix3.Transpose(V)); test = Matrix3.Mult(V, test); Assert.That(test.ToFloatArray(), Is.EqualTo(A.ToFloatArray()).Within(.0000001)); }
/// <summary> /// Render the 5-sigma ellipse of gaussian. /// </summary> /// <param name="gaussian">Gaussian to be rendered.</param> /// <param name="camera">Camera 4d transform matrix.</param> /// <param name="incolor">Color to be used for filling the gaussian.</param> public void RenderGaussian(Gaussian gaussian, double[][] camera, Color incolor) { Color outcolor = Color.Blue; incolor.A = 200; outcolor.A = 200; double weight; if (gaussian.Weight < 1.0) { weight = 0.04 * gaussian.Weight; incolor.A = (byte)(200 * gaussian.Weight); outcolor.A = (byte)(200 * gaussian.Weight); } else if (gaussian.Weight < 2.0) { weight = 0.01 * (gaussian.Weight - 1) + 0.04; outcolor = Color.Black; } else { weight = 0.04; outcolor = Color.Red; } if (ShowVisible) { outcolor = (BestEstimate.Visible(gaussian.Mean)) ? Color.Blue : Color.Red; } double[] camloc = camera.TransformH(gaussian.Mean); double[][] camrot = camera.Submatrix(0, 2, 0, 2); double camzoom = 1.0 / camera[3][3]; double[][] covariance = camrot.Multiply(gaussian.Covariance).MultiplyByTranspose(camrot).Submatrix(0, 1, 0, 1); var decomp = new EigenvalueDecomposition(covariance.ToMatrix()); double[,] stddev = decomp.DiagonalMatrix; for (int i = 0; i < stddev.GetLength(0); i++) { stddev[i, i] = (stddev[i, i] > 0) ? camzoom * Math.Sqrt(stddev[i, i]) : 0; } double[,] rotation = decomp.Eigenvectors; double[][] linear = rotation.Multiply(stddev).ToArray(); if (linear.Determinant() < 0) { linear = linear.ReverseColumns(); } double[][] points = new double[pinterval.Length][]; VertexPositionColor[] vertices = new VertexPositionColor[pinterval.Length]; for (int i = 0; i < points.Length; i++) { points [i] = linear.Multiply(pinterval[i]); points [i] = new double[3] { points[i][0], points[i][1], 0 }.Add(camloc); vertices[i] = new VertexPositionColor(points[i].ToVector3(), incolor); } short[] index = new short[vertices.Length]; for (int i = 0, k = 0; k < vertices.Length; i++, k += 2) { index[k] = (short)i; } for (int i = vertices.Length - 1, k = 1; k < vertices.Length; i--, k += 2) { index[k] = (short)i; } Graphics.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices, 0, vertices.Length, index, 0, vertices.Length - 2); Graphics.DrawUser2DPolygon(points, (float)weight, outcolor, true); }
//--------------------------------------------- #region Public Methods /// <summary> /// Computes the Multi-Class Linear Discriminant Analysis algorithm. /// </summary> public virtual void Compute() { // Compute entire data set measures Means = Tools.Mean(source); StandardDeviations = Tools.StandardDeviation(source, totalMeans); double total = dimension; // Initialize the scatter matrices this.Sw = new double[dimension, dimension]; this.Sb = new double[dimension, dimension]; // For each class for (int c = 0; c < Classes.Count; c++) { // Get the class subset double[,] subset = Classes[c].Subset; int count = subset.GetLength(0); // Get the class mean double[] mean = Tools.Mean(subset); // Continue constructing the Within-Class Scatter Matrix double[,] Swi = Tools.Scatter(subset, mean, (double)count); // Sw = Sw + Swi for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { Sw[i, j] += Swi[i, j]; } } // Continue constructing the Between-Class Scatter Matrix double[] d = mean.Subtract(totalMeans); double[,] Sbi = d.Multiply(d.Transpose()).Multiply(total); // Sb = Sb + Sbi for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { Sb[i, j] += Sbi[i, j]; } } // Store some additional information this.classScatter[c] = Swi; this.classCount[c] = count; this.classMeans[c] = mean; this.classStdDevs[c] = Tools.StandardDeviation(subset, mean); } // Compute eigen value decomposition EigenvalueDecomposition evd = new EigenvalueDecomposition(Matrix.Inverse(Sw).Multiply(Sb)); // Gets the eigenvalues and corresponding eigenvectors double[] evals = evd.RealEigenvalues; double[,] eigs = evd.Eigenvectors; // Sort eigen values and vectors in ascending order eigs = Matrix.Sort(evals, eigs, new GeneralComparer(ComparerDirection.Descending, true)); // Store information this.Eigenvalues = evals; this.DiscriminantMatrix = eigs; // Create discriminant functions bias bias = new double[classes]; for (int i = 0; i < classes; i++) { bias[i] = (-0.5).Multiply(classMeans[i]).Multiply( eigs.Multiply(classMeans[i])) + System.Math.Log(classCount[i] / total); } // Create projections this.result = new double[dimension, dimension]; for (int i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { for (int k = 0; k < dimension; k++) { result[i, j] += source[i, k] * eigenVectors[k, j]; } } } // Computes additional information about the analysis and creates the // object-oriented structure to hold the discriminants found. createDiscriminants(); }
private void CreateGridPoints() { currentResolution = resolution; points = new ParticleSystem.Particle[resolution * resolution + resolution]; float increment = 1f / (resolution - 1); int i = 0; // cartesian grid if (gridOption == GridOption.Cartesian) { for (int x = 0; x < resolution; x++) { for (int z = 0; z < resolution; z++) { Vector3 p = new Vector3(increment * (x - resolution / 2.0f), 0f, increment * (z - resolution / 2.0f)); points[i].position = p; points[i].color = new Color(p.x + increment * resolution / 2.0f, 0f, p.z + increment * resolution / 2.0f); points[i++].size = 0.1f; } } } //polar grid else if (gridOption == GridOption.Polar) { float thetaIncBy = (2.0f * Mathf.PI / (resolution - 1)); float radiusIncBy = 20.0f / (resolution - 1); for (int thetaInc = 0; thetaInc < resolution; thetaInc++) { for (int radiusInc = 0; radiusInc < resolution; radiusInc++) { Vector3 p = new Vector3(radiusInc * radiusIncBy * Mathf.Cos(thetaInc * thetaIncBy), 0f, radiusInc * radiusIncBy * Mathf.Sin(thetaInc * thetaIncBy)); points[i].position = p; points[i].color = new Color(p.x + increment * resolution / 2.0f, 0f, p.z + increment * resolution / 2.0f); points[i++].size = 0.1f; } } } //polar ellipse grid TODO:: doesn't work yet!!!! else if (gridOption == GridOption.PolarEllipse) { float t = Time.timeSinceLevelLoad; Matrix a = QuadraticFormMatrix(t); float thetaIncBy = (2.0f * Mathf.PI / (resolution - 1)); float radiusIncBy = 1.0f / (resolution - 1); EigenvalueDecomposition eigen = a.EigenvalueDecomposition; //Complex[] eigenValues = eigen.EigenValues; // eigenvalues: 1, -2 Matrix eigenVectors = eigen.EigenVectors; for (int thetaInc = 0; thetaInc < resolution; thetaInc++) { Matrix currentAngleVector = new Matrix(new double[][] { new double[] { Mathf.Cos(thetaInc * thetaIncBy) }, new double[] { Mathf.Sin(thetaInc * thetaIncBy) } }); Matrix evscale = eigenVectors * currentAngleVector; float radiusScale = (float)(a * currentAngleVector).Norm2(); for (int radiusInc = 0; radiusInc < resolution; radiusInc++) { Vector3 p = new Vector3(radiusScale * radiusInc * radiusIncBy * Mathf.Cos(thetaInc * thetaIncBy), 0f, radiusScale * radiusInc * radiusIncBy * Mathf.Sin(thetaInc * thetaIncBy)); points[i].position = p; points[i].color = new Color(p.x + increment * resolution / 2.0f, 0f, p.z + increment * resolution / 2.0f); points[i++].size = 0.1f; } } } currentGridOption = gridOption; for (int t = 0; t < resolution; t++) { Vector3 p = new Vector3(0f, 0f, 0f); points[i].position = p; points[i].color = new Color(increment * resolution / 2, increment * resolution / 2, increment * resolution / 2); points[i++].size = 0.15f; } }
// End AddupChisqContributions(ChisqFirstandSecond[] SubTotal, Desertwind TotalSolution) public static void FindQlimits(Desertwind Solution, ref double Qhigh, ref double Qlow, ref int ReasontoStop1, ref int ReasontoStop2) { if (Hotsun.FullSecondDerivative) { FindTraceandNorm(Solution.ExactFullMatrix, ref Hotsun.ChisqMatrixTrace, ref Hotsun.ChisqMatrixNorm); } else { FindTraceandNorm(Solution.FullMatrix, ref Hotsun.ChisqMatrixTrace, ref Hotsun.ChisqMatrixNorm); } var ConventionalMatrix = new double[Hotsun.npar, Hotsun.npar]; // Set Up Matrix to find eigenvalues // Scale but do NOT add Q double[, ][,] Matrix; if (Hotsun.FullSecondDerivative) { Matrix = Solution.ExactFullMatrix; } else { Matrix = Solution.FullMatrix; } // Set actual matrix for (int GlobalIndex1 = 0; GlobalIndex1 < Hotsun.npar; GlobalIndex1++) { for (int GlobalIndex2 = 0; GlobalIndex2 < Hotsun.npar; GlobalIndex2++) { double MatrixElement = Matrix[GlobalIndex1, GlobalIndex2][0, 0]; if (Hotsun.UseDiagonalScaling) { MatrixElement *= Hotsun.sqdginv[GlobalIndex1][0] * Hotsun.sqdginv[GlobalIndex2][0]; } ConventionalMatrix[GlobalIndex1, GlobalIndex2] = MatrixElement; } // End GlobalIndex2 } // End GlobalIndex1 // Find Minimum and Maximum eigenvalue of ConventionalMatrix // Begin Added by smbeason 5/17/2009 LMatrix lmatrix = LMatrix.Create(ConventionalMatrix); EigenvalueDecomposition eigenValueDecomp = lmatrix.EigenvalueDecomposition; double minEigenValue = double.MaxValue; double maxEigenValue = double.MinValue; //Assuming you want on the real part... for (int i = 0; i < eigenValueDecomp.RealEigenvalues.Length; i++) { minEigenValue = Math.Min(minEigenValue, eigenValueDecomp.RealEigenvalues[i]); maxEigenValue = Math.Max(maxEigenValue, eigenValueDecomp.RealEigenvalues[i]); } // End Added by smbeason 5/17/2009 ReasontoStop1 = 1; ReasontoStop2 = 1; Qlow = minEigenValue; Qhigh = maxEigenValue; return; }
private double[][] getFeature(double[][] input, int K) { //var activationContext = Type.GetTypeFromProgID("matlab.application.single"); //var matlab = (MLApp.MLApp)Activator.CreateInstance(activationContext); //matlab.Visible = 0; int i, j; int row = input[0].Length; int column = input.Length; double[][] X = new double[column][]; for (i = 0; i < column; i++) { X[i] = input[i].Subtract(this.meanMatrix); } // matlab.PutWorkspaceData("X", "base", X.ToMatrix()); // MessageBox.Show(matlab.Execute("XTX=X'*X;")); // MessageBox.Show(matlab.Execute("size(XTX)")); // var t = matlab.GetVariable("XTX", "base"); // matlab.Quit(); double[][] XT = X.Transpose(); double[,] XTX = X.Multiply(XT).ToMatrix(); var feature = new EigenvalueDecomposition(XTX); //EigenvalueDecomposition feature = XTX.eig(); double[] d = feature.RealEigenvalues; // assert d.length >= K : "number of eigenvalues is less than K"; int[] indexes; d.StableSort(out indexes); indexes = indexes.Reverse().ToArray(); indexes = indexes.Submatrix(0, K - 1); double[][] eigenVectors = XT.Multiply(feature.Eigenvectors.ToArray()); double[][] selectedEigenVectors = eigenVectors.Submatrix(0, eigenVectors.Length - 1, indexes); // normalize the eigenvectors row = selectedEigenVectors.Length; column = selectedEigenVectors[0].Length; for (i = 0; i < column; i++) { double temp = 0; for (j = 0; j < row; j++) { temp += Math.Pow(selectedEigenVectors[j][i], 2); } temp = Math.Sqrt(temp); for (j = 0; j < row; j++) { selectedEigenVectors[j][i] = selectedEigenVectors[j][i] / temp; } } return(selectedEigenVectors); }
public LDA(double[][] trainingSet, List <string> labels, int numOfComponents) { int n = trainingSet.Length; // sample size HashSet <String> tempSet = new HashSet <String>(labels); int c = tempSet.Count; // class size // process in PCA PCA pca = new PCA(trainingSet, labels, n - c); // classify double[][] meanTotal = new double[n - c][]; for (int i = 0; i < n - c; i++) { meanTotal[i] = new double[1]; } Dictionary <String, List <double[]> > dict = new Dictionary <String, List <double[]> >(); List <projectedTrainingMatrix> pcaTrain = pca.getProjectedTrainingSet(); for (int i = 0; i < pcaTrain.Count; i++) { String key = pcaTrain[i].label; meanTotal = meanTotal.Add(pcaTrain[i].matrix); if (!dict.ContainsKey(key)) { List <double[]> temp = new List <double[]>(); temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict.Add(key, temp); } else { List <double[]> temp = dict[key]; temp.Add(pcaTrain[i].matrix.Transpose()[0]); dict[key] = temp; } } meanTotal.ToMatrix().Multiply((double)1 / n); // calculate Sw, Sb double[][] Sw = new double[n - c][]; double[][] Sb = new double[n - c][]; for (int i = 0; i < n - c; i++) { Sw[i] = new double[n - c]; Sb[i] = new double[n - c]; } List <String> labelSet = dict.Keys.ToList(); foreach (string label in labelSet) { List <double[]> tempMatrix = dict[label]; double[][] matrixWithinThatClass = tempMatrix.ToArray(); double[] meanOfCurrentClass = Accord.Statistics.Tools.Mean(matrixWithinThatClass); for (int i = 0; i < matrixWithinThatClass.Length; i++) { double[][] temp1 = matrixWithinThatClass[i].ToArray().Subtract(meanOfCurrentClass.ToArray()); temp1 = temp1.Multiply(temp1.Transpose()); Sw = Sw.Add(temp1); } double[][] temp = meanOfCurrentClass.ToArray().Subtract(meanTotal); temp = temp.Multiply(temp.Transpose()).ToMatrix().Multiply((double)matrixWithinThatClass.Length).ToArray(); Sb = Sb.Add(temp); } // calculate the eigenvalues and vectors of Sw^-1 * Sb double [][] targetForEigen = Sw.Inverse().Multiply(Sb); var feature = new EigenvalueDecomposition(targetForEigen.ToMatrix()); double[] d = feature.RealEigenvalues; int[] indexes; d.StableSort(out indexes); indexes = indexes.Reverse().ToArray(); indexes = indexes.Submatrix(0, c - 1); //int[] indexes = getIndexesOfKEigenvalues(d, c - 1); double[][] eigenVectors = feature.Eigenvectors.ToArray(); double[][] selectedEigenVectors = eigenVectors.Submatrix(0, eigenVectors.Length - 1, indexes); this.W = pca.getW().Multiply(selectedEigenVectors); // Construct projectedTrainingMatrix this.projectedTrainingSet = new List <projectedTrainingMatrix>(); for (int i = 0; i < trainingSet.Length; i++) { projectedTrainingMatrix ptm = new projectedTrainingMatrix(this.W.Transpose().Multiply(trainingSet[i].Subtract(pca.meanMatrix).ToArray()), labels[i]); this.projectedTrainingSet.Add(ptm); } this.meanMatrix = pca.meanMatrix; GC.Collect(); }
public void CalculateEigenvalueDecomposition() { double[] i = new double[3]; double[] j = new double[3]; double[] k = new double[3]; double[] u = new double[3]; double[] t_vol = new double[3]; double[] vol = new double[3]; vol[0] = 0; vol[1] = 0; vol[2] = 0; double[] func_sum = new double[3]; func_sum[0] = 0; func_sum[1] = 0; func_sum[2] = 0; double[] func_sum_inertia = new double[3]; func_sum_inertia[0] = 0; func_sum_inertia[1] = 0; func_sum_inertia[2] = 0; double func_sum_xy = 0; double func_sum_xz = 0; double func_sum_yz = 0; double surfaceArea = 0; //loop through all the triangles for (int count = 0; count < _connections.Length; count++) { //get the 3 points of the triangle double[] p1 = new double[3]; double[] p2 = new double[3]; double[] p3 = new double[3]; //Fix so its relative to the centroid center of mass p1[0] = (double)_pts[_connections[count][0]][0] - _centroid[0]; //fix x's first p2[0] = (double)_pts[_connections[count][1]][0] - _centroid[0]; p3[0] = (double)_pts[_connections[count][2]][0] - _centroid[0]; p1[1] = (double)_pts[_connections[count][0]][1] - _centroid[1]; //fix y's p2[1] = (double)_pts[_connections[count][1]][1] - _centroid[1]; p3[1] = (double)_pts[_connections[count][2]][1] - _centroid[1]; p1[2] = (double)_pts[_connections[count][0]][2] - _centroid[2]; //fix z's p2[2] = (double)_pts[_connections[count][1]][2] - _centroid[2]; p3[2] = (double)_pts[_connections[count][2]][2] - _centroid[2]; //calculate the i, j, k vectors i[0] = p2[0] - p1[0]; j[0] = p2[1] - p1[1]; k[0] = p2[2] - p1[2]; i[1] = p3[0] - p1[0]; j[1] = p3[1] - p1[1]; k[1] = p3[2] - p1[2]; i[2] = p3[0] - p2[0]; j[2] = p3[1] - p2[1]; k[2] = p3[2] - p2[2]; //cross product between two vectors, to determine normal vector u[0] = j[0] * k[1] - k[0] * j[1]; u[1] = k[0] * i[1] - i[0] * k[1]; u[2] = i[0] * j[1] - j[0] * i[1]; //Normalize vector to 1 double norm = Math.Sqrt(u[0] * u[0] + u[1] * u[1] + u[2] * u[2]); if (norm != 0.0) { u[0] = u[0] / norm; u[1] = u[1] / norm; u[2] = u[2] / norm; } else { u[0] = 0.0; u[1] = 0.0; u[2] = 0.0; } //This is reduced to ... //area of a triangle... double a = Math.Sqrt(i[1] * i[1] + j[1] * j[1] + k[1] * k[1]); double b = Math.Sqrt(i[0] * i[0] + j[0] * j[0] + k[0] * k[0]); double c = Math.Sqrt(i[2] * i[2] + j[2] * j[2] + k[2] * k[2]); double s = 0.5 * (a + b + c); double area = Math.Sqrt(Math.Abs(s * (s - a) * (s - b) * (s - c))); //patches(count,1) = area // surfaceArea += area; //volume elements ... double zavg = (p1[2] + p2[2] + p3[2]) / 3.0; double yavg = (p1[1] + p2[1] + p3[1]) / 3.0; double xavg = (p1[0] + p2[0] + p3[0]) / 3.0; //sum of function for centroid calculation func_sum[0] += t_vol[0] * xavg; func_sum[1] += t_vol[1] * yavg; func_sum[2] += t_vol[2] * zavg; //sum of function for inertia calculation func_sum_inertia[0] += area * u[0] * xavg * xavg * xavg; func_sum_inertia[1] += area * u[1] * yavg * yavg * yavg; func_sum_inertia[2] += area * u[2] * zavg * zavg * zavg; //sum of function for products of inertia calculation func_sum_xz += area * u[0] * xavg * xavg * zavg; func_sum_xy += area * u[1] * yavg * yavg * xavg; func_sum_yz += area * u[2] * zavg * zavg * yavg; } func_sum_inertia[0] /= 3; func_sum_inertia[1] /= 3; func_sum_inertia[2] /= 3; double Ixy = -1 * func_sum_xy / 2; double Ixz = -1 * func_sum_xz / 2; double Iyz = -1 * func_sum_yz / 2; double Iyx = Ixy; double Izx = Ixz; double Izy = Iyz; double Ixx = func_sum_inertia[1] + func_sum_inertia[2]; double Iyy = func_sum_inertia[0] + func_sum_inertia[2]; double Izz = func_sum_inertia[0] + func_sum_inertia[1]; GeneralMatrix i_CoM = new GeneralMatrix(3, 3); i_CoM.Array[0][0] = Ixx; i_CoM.Array[0][1] = Ixy; i_CoM.Array[0][2] = Ixz; i_CoM.Array[1][0] = Iyx; i_CoM.Array[1][1] = Iyy; i_CoM.Array[1][2] = Iyz; i_CoM.Array[2][0] = Izx; i_CoM.Array[2][1] = Izy; i_CoM.Array[2][2] = Izz; EigenvalueDecomposition eig = i_CoM.Eigen(); _eigenvalues = eig.D; _eigenvectors = eig.GetV(); //make sure this is a right handed matrix if (_eigenvectors.Determinant() < 0) { _eigenvectors = _eigenvectors.Multiply(-1); } }
public static void Main(string[] argv) { MagicSquareExample.print("\n Test of Matrix Class, using magic squares.\n"); MagicSquareExample.print(" See MagicSquareExample.main() for an explanation.\n"); MagicSquareExample.print("\n n trace max_eig rank cond lu_res qr_res\n\n"); DateTime now = DateTime.Now; double num = Math.Pow(2.0, -52.0); for (int i = 3; i <= 32; i++) { MagicSquareExample.print(MagicSquareExample.fixedWidthIntegertoString(i, 7)); JamaMatrix jamaMatrix = MagicSquareExample.magic(i); int n = (int)jamaMatrix.trace(); MagicSquareExample.print(MagicSquareExample.fixedWidthIntegertoString(n, 10)); EigenvalueDecomposition eigenvalueDecomposition = new EigenvalueDecomposition(jamaMatrix.plus(jamaMatrix.transpose()).times(0.5)); double[] realEigenvalues = eigenvalueDecomposition.RealEigenvalues; MagicSquareExample.print(MagicSquareExample.fixedWidthDoubletoString(realEigenvalues[i - 1], 14, 3)); int n2 = jamaMatrix.rank(); MagicSquareExample.print(MagicSquareExample.fixedWidthIntegertoString(n2, 7)); double num2 = jamaMatrix.cond(); MagicSquareExample.print((num2 < 1.0 / num) ? MagicSquareExample.fixedWidthDoubletoString(num2, 12, 3) : " Inf"); LUDecomposition lUDecomposition = new LUDecomposition(jamaMatrix); JamaMatrix l = lUDecomposition.L; JamaMatrix u = lUDecomposition.U; int[] pivot = lUDecomposition.Pivot; JamaMatrix jamaMatrix2 = l.times(u).minus(jamaMatrix.getMatrix(pivot, 0, i - 1)); double x = jamaMatrix2.norm1() / ((double)i * num); MagicSquareExample.print(MagicSquareExample.fixedWidthDoubletoString(x, 12, 3)); QRDecomposition qRDecomposition = new QRDecomposition(jamaMatrix); JamaMatrix q = qRDecomposition.Q; jamaMatrix2 = qRDecomposition.R; jamaMatrix2 = q.times(jamaMatrix2).minus(jamaMatrix); x = jamaMatrix2.norm1() / ((double)i * num); MagicSquareExample.print(MagicSquareExample.fixedWidthDoubletoString(x, 12, 3)); MagicSquareExample.print("\n"); } double x2 = (double)(DateTime.Now.Ticks - now.Ticks) / 1000.0; MagicSquareExample.print("\nElapsed Time = " + MagicSquareExample.fixedWidthDoubletoString(x2, 12, 3) + " seconds\n"); MagicSquareExample.print("Adios\n"); }
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 static void Run(int pllproc) { double[] @params = new double[6]; double maxgrad = 0; double stpthrsh = 0; double maxeigen; double mineigen; double wr = 0; double[] alpha = { 0.50, 1.00 }; double rfMax = 2.75; int td = 0; int[] prtls = { -1, -1, -1, -1 }; int iterint = 1; long sn = (long)Math.Pow(10.0, 7); int prec = (int)Math.Pow(10.0, 4); string type = ""; string alg = ""; const string rootdir = Config.ConfigsRootDir; // Read setup details from control file. try { var getParams = File.ReadAllText(Path.Combine(rootdir, Config.ControlFile)); var s = getParams.Split(new[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); @params = s.Take(6).Select(i => double.Parse(i, CultureInfo.InvariantCulture)).ToArray(); td = int.Parse(s[6]); wr = double.Parse(s[7]); stpthrsh = double.Parse(s[8]); alg = s[9]; type = s[10]; if (type.Equals("sim")) { sn = int.Parse(s[11]); alpha[0] = double.Parse(s[12]); alpha[1] = double.Parse(s[13]); } else if (type.Equals("dp")) { prec = int.Parse(s[11]); rfMax = double.Parse(s[12]); } } catch (Exception ex) { Trace.Write("ERROR: Could not read file: "); Trace.WriteLine(Path.Combine(rootdir, Config.ControlFile) + $". {ex.Message}"); Trace.WriteLine("EXITING...main()..."); Console.Read(); Environment.Exit(1); } // Read initial glide-path from file. var gp = new double[td]; var gpPath = Path.Combine(rootdir, Config.InitGladepathFile); try { gp = File.ReadAllLines(gpPath).Select(double.Parse) .Take(td).ToArray(); } catch (Exception ex) { Trace.Write("ERROR: Could not read file: "); Trace.WriteLine(gpPath + ". Error: " + ex.Message); Trace.WriteLine("EXITING...main()..."); Console.Read(); Environment.Exit(1); } if (gp.Length != td) { Trace.Write("ERROR: File: "); Trace.Write(gpPath); Trace.Write(" needs "); Trace.Write(td); Trace.WriteLine(" initial asset allocations, but has fewer."); Trace.WriteLine("EXITING...main()..."); Console.Read(); Environment.Exit(1); } // Display optimization algorithm. Trace.Write(@"===> Optimization algorithm: "); if (alg == "nr") { Trace.WriteLine(@"Newton's Method"); } else if (alg == "ga") { Trace.WriteLine(@"Gradient Ascent"); } // Display estimation method. Trace.WriteLine(""); Trace.Write(@"===> Estimation method: "); if (type == "sim") { Trace.WriteLine(@"Simulation"); } else if (type == "dp") { Trace.WriteLine(@"Dynamic Program"); pllproc = 4 * pllproc; } // Declare variables that depend on data read from the control file for sizing. double[,] hess; double[] ngrdnt = new double[td]; EigenvalueDecomposition hevals; var grad = new double[td]; // Take some steps (1 full iteration but no more than 50 steps) in the direction of steepest ascent. This can move us off // the boundary region where computations may be unstable (infinite), especially when constructing the Hessian for Newton's method. // Also, this initial stepping usually makes improvements very quickly before proceeding with the optimization routine. double probnr = GetPNR.Run(type, @params, gp, td, wr, 4 * sn, (int)(rfMax * prec), prec, prtls, pllproc); Trace.WriteLine(""); Trace.WriteLine("Initial Glide-Path (w/Success Probability):"); WrtAry.Run(probnr, gp, "GP", td); for (int s = 1; s <= 2; ++s) { maxgrad = BldGrad.Run(type, @params, gp, td, wr, sn, (int)(rfMax * prec), prec, probnr, 4 * sn, alpha[1], pllproc, grad); if (maxgrad <= stpthrsh) { Trace.Write("The glide-path supplied satisfies the EPSILON convergence criteria: "); Trace.WriteLine($"{maxgrad:F15} vs. {stpthrsh:F15}"); s = s + 1; } else if (s != 2) { probnr = Climb.Run(type, @params, gp, td, wr, 2 * sn, (int)(rfMax * prec), prec, pllproc, maxgrad, probnr, 4 * sn, grad, alpha[0], 50); Trace.WriteLine(""); Trace.WriteLine("New (Post Initial Climb) Glide-Path (w/Success Probability):"); WrtAry.Run(probnr, gp, "GP", td); } else if (maxgrad <= stpthrsh) { Trace.Write("The glide-path supplied satisfies the EPSILON convergence criteria after intial climb without iterating: "); Trace.WriteLine($"{maxgrad:F15} vs. {stpthrsh:F15}"); } } // Negate the gradient if using NR method. if (alg == "nr") { for (int y = 0; y < td; ++y) { ngrdnt[y] = -1.00 * grad[y]; } } // If convergence is not achieved after initial climb then launch into full iteration mode. while (maxgrad > stpthrsh) { Trace.WriteLine(""); Trace.WriteLine("========================="); Trace.WriteLine($"Start Iteration #{iterint}"); Trace.WriteLine("========================="); if (alg == "nr") { // Record the probability before iterating. double strtpnr = probnr; // Build the Hessian matrix for this glide-path and derive its eigenvalues. (Display the largest & smallest value.) // This is required when method=nr. When either procedure ends with convergence we recompute the Hessian matrix to // ensure we are at a local/global maximum (done below after convergence). hess = DrvHess.Run(type, @params, gp, td, wr, sn, (int)(rfMax * prec), prec, pllproc, grad, probnr); //hevals.compute(hess, false); hevals = new EigenvalueDecomposition(hess); var reals = hevals.RealEigenvalues; maxeigen = reals.Max(); mineigen = reals.Min(); // Display the smallest/largest eigenvalues. Trace.WriteLine(""); Trace.Write("Min Hessian eigenvalue for this iteration (>=0.00 --> convex region): "); Trace.WriteLine(mineigen); Trace.WriteLine(""); Trace.Write("Max Hessian eigenvalue for this iteration (<=0.00 --> concave region): "); Trace.WriteLine(maxeigen); // Update the glidepath and recompute the probability using the new glidepath. //sol = hess.colPivHouseholderQr().solve(ngrdnt); var qr = new QrDecomposition(hess); var sol = qr.Solve(ngrdnt); for (int y = 0; y < td; ++y) { gp[y] += sol[y]; } probnr = GetPNR.Run(type, @params, gp, td, wr, 4 * sn, (int)(rfMax * prec), prec, prtls, pllproc); // If success probability has worsened alert the user. if (probnr < strtpnr) { Trace.WriteLine(""); Trace.WriteLine("NOTE: The success probability has worsened during the last iteration. This could happen for different reasons:"); Trace.WriteLine(" 1.) The difference in probabilities is beyond the system's ability to measure accurately (i.e., beyond 15 significant digits)."); Trace.WriteLine(" 2.) The difference is due to estimation/approximation error."); Trace.WriteLine(" 3.) You may be operating along the boundary region. In general the procedure is not well defined on the boundaries. (Try gradient ascent.)"); } } else if (alg == "ga") { // Update the glide-path and recompute the probability using the new glide-path. probnr = Climb.Run(type, @params, gp, td, wr, 2 * sn, (int)(rfMax * prec), prec, pllproc, maxgrad, probnr, 4 * sn, grad, alpha[0]); } // Display the new glide-path. Trace.WriteLine(""); Trace.Write("New Glide-Path:"); WrtAry.Run(probnr, gp, "GP", td); // Rebuild the gradient and negate it when using NR. maxgrad = BldGrad.Run(type, @params, gp, td, wr, 1 * sn, (int)(rfMax * prec), prec, probnr, 4 * sn, alpha[1], pllproc, grad); if (alg == "nr") { for (int y = 0; y < td; ++y) { ngrdnt[y] = -1.00 * grad[y]; } } // Report the convergence status. Trace.WriteLine(""); Trace.WriteLine($"EPSILON Convergence Criteria: {maxgrad:F15} vs. {stpthrsh:F15}"); if (maxgrad <= stpthrsh) { Trace.WriteLine(""); Trace.WriteLine("==========> EPSILON Convergence criteria satisfied. <=========="); } Trace.WriteLine(""); Trace.WriteLine(new String('=', 25)); Trace.Write("End Iteration #"); Trace.WriteLine(iterint); Trace.WriteLine(new String('=', 25)); iterint++; } // Build Hessian and confirm we are at a maximum, not a saddle-point or plateau for example. Trace.WriteLine(""); Trace.WriteLine("Convergence Achieved: Final step is to confirm we are at a local/global maximum. Hessian is being built."); hess = DrvHess.Run(type, @params, gp, td, wr, sn, (int)(rfMax * prec), prec, pllproc, grad, probnr); hevals = new EigenvalueDecomposition(hess); var r = hevals.RealEigenvalues; maxeigen = r.Max(); mineigen = r.Min(); // Display the smallest/largest eigenvalues. Trace.WriteLine(""); Trace.Write("Min Hessian eigenvalue at solution [>=0.00 --> convex region --> (local/global) minimum]: "); Trace.WriteLine(mineigen); Trace.WriteLine(""); Trace.Write("Max Hessian eigenvalue at solution [<=0.00 --> concave region --> (local/global) maximum]: "); Trace.WriteLine(maxeigen); // Write final GP to the output file. Trace.WriteLine(""); if (maxeigen <= 0 || mineigen >= 0) { Trace.Write("(Local/Global) Optimal "); } Trace.WriteLine("Glide-Path:"); WrtAry.Run(probnr, gp, "GP", td, Path.Combine(rootdir, Config.Outfile)); Trace.WriteLine(""); }
public LDA(List <Matrix> trainingSet, List <String> labels, int numOfComponents) { int n = trainingSet.Count(); // sample size HashSet <string> tempSet = new HashSet <string>(labels); int c = tempSet.Count(); // class size /// deh mfrod used for debugging issues, so fakes for nw ////////////////////////// //assert numOfComponents >= n - c : "the input components is smaller than n - c!"; //assert n >= 2 * c : "n is smaller than 2c!"; // process in PCA EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, n - c); // classify Matrix meanTotal = new Matrix(n - c, 1); Dictionary <string, List <Matrix> > map = new Dictionary <string, List <Matrix> >(); List <ProjectMatrix> pcaTrain = pca.getProjectSet(); for (int i = 0; i < pcaTrain.Count(); i++) { string key = pcaTrain[i].getLabel(); meanTotal.AddEquals(pcaTrain[i].getImgMat()); if (!map.ContainsKey(key)) { List <Matrix> temp = new List <Matrix>(); temp.Add(pcaTrain[i].getImgMat()); map.Add(key, temp); } else { List <Matrix> temp = map[key]; temp.Add(pcaTrain[i].getImgMat()); map[key] = temp; } } meanTotal.Multiply((double)1 / n); // calculate Sw, Sb Matrix Sw = new Matrix(n - c, n - c); Matrix Sb = new Matrix(n - c, n - c); /*** !!! **/ tempSet = new HashSet <string>(map.Keys); /*** !!! **/ foreach (string s in tempSet) { //iterator<string> it = tempSet.iterator(); //while (it.hasNext()) { //String s = (String)it.next(); List <Matrix> matrixWithinThatClass = map[s]; Matrix meanOfCurrentClass = getMean(matrixWithinThatClass); for (int i = 0; i < matrixWithinThatClass.Count(); i++) { Matrix temp1 = matrixWithinThatClass[i].Subtract(meanOfCurrentClass); temp1 = temp1.Multiply(temp1.Transpose()); Sw.AddEquals(temp1); } Matrix temp = meanOfCurrentClass.Subtract(meanTotal); temp = temp.Multiply(temp.Transpose()).Multiply(matrixWithinThatClass.Count()); Sb.AddEquals(temp); } // calculate the eigenvalues and vectors of Sw^-1 * Sb Matrix targetForEigen = Sw.Inverse().Multiply(Sb); EigenvalueDecomposition feature = targetForEigen.Eigen(); double[] d = feature.RealEigenvalues; //assert d.length >= c - 1 : "Ensure that the number of eigenvalues is larger than c - 1"; int[] indexes = getIndOfHigherEV(d, c - 1); Matrix eigenVectors = feature.GetV(); Matrix selectedEigenVectors = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexes); this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors); // Construct projectedTrainingMatrix this.projectSet = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count(); i++) { ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix .Transpose() .Multiply(trainingSet[i].Subtract(pca.getMeanMatrix())), labels[i]); this.projectSet.Add(ptm); } this.meanMatrix = pca.getMeanMatrix(); }
public void ComputeKernel() { double[,] distanceMatrix = new double[0, 0]; for (int i = 0; i < initialData.Rows(); i++) { distanceMatrix = distanceMatrix.InsertRow(getDistances(initialData.GetRow(i)[0], initialData.GetRow(i)[1])); } int nr = distanceMatrix.Rows(); double[,] ones = new double[nr, nr]; double[,] k = new double[nr, nr]; for (int i = 0; i < nr; i++) { for (int j = 0; j < nr; j++) { //double x = (-Gamma * Math.Pow(distanceMatrix[i, j], 2)); double x = (-Gamma * distanceMatrix[i, j]); k[i, j] = Math.Pow(Math.E, x); ones[i, j] = 1.0 / nr; } } var p1 = ones.Dot(k); var p2 = k.Dot(ones); //var final = k.Subtract(p1).Subtract(p2).Add(p1.Dot(ones)); var final = k.Subtract(ones.Dot(k)).Subtract(k.Dot(ones)).Add(ones.Dot(k).Dot(ones)); //final = final.Transpose(); var e = new EigenvalueDecomposition(final); var values = e.RealEigenvalues; var vectors = e.Eigenvectors; StreamWriter sw = new StreamWriter("eigenvectorsunsorted.txt"); StreamWriter sw1 = new StreamWriter("eigenvaluesunsorted.txt"); for (int i = 0; i < vectors.Rows(); i++) { sw.WriteLine(vectors.GetRow(i).ToString("+0.00000;-0.00000")); sw1.WriteLine(values[i]); } sw.Close(); sw1.Close(); vectors = Matrix.Sort(values, vectors, new GeneralComparer(ComparerDirection.Descending, true)); //var coloane = vectors.GetColumns(0, 1); //var kernelData = dataAdjusted.Dot(coloane); //for (int i = 0; i < dataAdjusted.Rows(); i++) // Console.WriteLine(dataAdjusted[i, 0] + " " + vectors[i, 0] + " " + kernelData[i, 0] + " " + kernelData[i, 1]); KernelData = vectors.GetColumns(0, 1); allKernelValues = values; allKernelVectors = vectors; allKernelData = vectors; KernelValues = values[0]; KernelVectors = vectors.GetColumn(0); sw = new StreamWriter("kerneldata.txt"); sw1 = new StreamWriter("eigenvaluessorted.txt"); for (int i = 0; i < vectors.Rows(); i++) { sw.WriteLine(KernelData.GetRow(i).ToString("+0.00000;-0.00000")); sw1.WriteLine(values[i]); } sw.Close(); sw1.Close(); //Console.WriteLine(distanceMatrix.ToString("+0.00;-0.00")); //Console.WriteLine(); //Console.WriteLine(k.ToString("+0.00;-0.00")); //Console.WriteLine(); //Console.WriteLine(vectors.ToString("+0.00;-0.00")); //ScatterplotBox.Show("kernelData", kernelData); }
public virtual void Compute() { if (!onlyCovarianceMatrixAvailable) { int rows; if (this.array != null) { rows = array.Length; double[][] matrix = Adjust(array, Overwrite); var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; ComponentVectors = svd.RightSingularVectors.Transpose(); } else { rows = source.GetLength(0); #pragma warning disable 612, 618 double[,] matrix = Adjust(source, Overwrite); #pragma warning restore 612, 618 var svd = new SingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; ComponentVectors = svd.RightSingularVectors.ToArray().Transpose(); } Eigenvalues = new double[SingularValues.Length]; for (int i = 0; i < SingularValues.Length; i++) { Eigenvalues[i] = SingularValues[i] * SingularValues[i] / (rows - 1); } } else { var evd = new EigenvalueDecomposition(covarianceMatrix, assumeSymmetric: true, sort: true); Eigenvalues = evd.RealEigenvalues; var eigenvectors = evd.Eigenvectors.ToJagged(); SingularValues = Eigenvalues.Sqrt(); ComponentVectors = eigenvectors.Transpose(); } if (Whiten) { ComponentVectors = ComponentVectors.Transpose().Divide(Eigenvalues, dimension: 0).Transpose(); } CreateComponents(); if (!onlyCovarianceMatrixAvailable) { if (array != null) { result = Transform(array).ToMatrix(); } else if (source != null) { result = Transform(source.ToJagged()).ToMatrix(); } } }
public LinearDescriminantAnalysis(List <Matrix> trainingSet, List <String> labels) { this.trainingSet = trainingSet; this.imgRows = trainingSet[0].RowDimension; int n = trainingSet.Count(); // sample size HashSet <string> uniqueLabels = new HashSet <string>(labels); int c = uniqueLabels.Count(); // class size this.numOfComponents = c - 1; EigenFaceRecognizer pca = new EigenFaceRecognizer(trainingSet, labels, n - c); Matrix meanTotal = new Matrix(n - c, 1); Dictionary <string, List <Matrix> > dic = new Dictionary <string, List <Matrix> >(); List <ProjectMatrix> pcaWeights = pca.getWeights(); for (int i = 0; i < pcaWeights.Count(); i++) { string key = pcaWeights[i].getLabel(); meanTotal.AddEquals(pcaWeights[i].getImgMat()); if (!dic.ContainsKey(key)) { List <Matrix> temp = new List <Matrix>(); temp.Add(pcaWeights[i].getImgMat()); dic.Add(key, temp); } else { List <Matrix> temp = dic[key]; temp.Add(pcaWeights[i].getImgMat()); dic[key] = temp; } } meanTotal.Multiply((double)1 / n); // calculate Sw, Sb Matrix Sw = new Matrix(n - c, n - c); Matrix Sb = new Matrix(n - c, n - c); uniqueLabels = new HashSet <string>(dic.Keys); foreach (string s in uniqueLabels) { List <Matrix> matrixWithinThatClass = dic[s]; Matrix meanOfCurrentClass = getMean(matrixWithinThatClass); for (int i = 0; i < matrixWithinThatClass.Count(); i++) { Matrix mat = matrixWithinThatClass[i].Subtract(meanOfCurrentClass); mat = mat.Multiply(mat.Transpose()); Sw.AddEquals(mat); } Matrix temp = meanOfCurrentClass.Subtract(meanTotal); temp = temp.Multiply(temp.Transpose()).Multiply(matrixWithinThatClass.Count()); Sb.AddEquals(temp); } // calculate the eigenvalues and vectors of Sw^-1 * Sb Matrix targetForEigen = Sw.Inverse().Multiply(Sb); EigenvalueDecomposition feature = targetForEigen.Eigen(); double[] eigenValues = feature.RealEigenvalues; int[] indexOfChosenEigenValues = getIndOfHigherEV(eigenValues, c - 1); Matrix eigenVectors = feature.GetV(); Matrix selectedEigenVectors = eigenVectors.GetMatrix(0, eigenVectors.RowDimension - 1, indexOfChosenEigenValues); this.weightMatrix = pca.getWeightMatrix().Multiply(selectedEigenVectors); // Construct weights this.weights = new List <ProjectMatrix>(); for (int i = 0; i < trainingSet.Count(); i++) { ProjectMatrix ptm = new ProjectMatrix(this.weightMatrix.Transpose().Multiply(trainingSet[i].Subtract(pca.getMeanMatrix())) , labels[i]); this.weights.Add(ptm); } this.meanMatrix = pca.getMeanMatrix(); }
public virtual void Compute() { if (!onlyCovarianceMatrixAvailable) { int rows; if (this.array != null) { rows = array.Length; // Center and standardize the source matrix double[][] matrix = Adjust(array, Overwrite); // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new JaggedSingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // The right singular vectors contains the principal components of the data matrix ComponentVectors = svd.RightSingularVectors.Transpose(); } else { rows = source.GetLength(0); // Center and standardize the source matrix #pragma warning disable 612, 618 double[,] matrix = Adjust(source, Overwrite); #pragma warning restore 612, 618 // Perform the Singular Value Decomposition (SVD) of the matrix var svd = new SingularValueDecomposition(matrix, computeLeftSingularVectors: true, computeRightSingularVectors: true, autoTranspose: true, inPlace: true); SingularValues = svd.Diagonal; // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // The right singular vectors contains the principal components of the data matrix ComponentVectors = svd.RightSingularVectors.ToArray().Transpose(); // The left singular vectors contains the factor scores for the principal components } // Eigenvalues are the square of the singular values Eigenvalues = new double[SingularValues.Length]; for (int i = 0; i < SingularValues.Length; i++) { Eigenvalues[i] = SingularValues[i] * SingularValues[i] / (rows - 1); } } else { // We only have the covariance matrix. Compute the Eigenvalue decomposition var evd = new EigenvalueDecomposition(covarianceMatrix, assumeSymmetric: true, sort: true); // Gets the Eigenvalues and corresponding Eigenvectors Eigenvalues = evd.RealEigenvalues; var eigenvectors = evd.Eigenvectors.ToJagged(); SingularValues = Eigenvalues.Sqrt(); ComponentVectors = eigenvectors.Transpose(); } if (Whiten) { ComponentVectors = ComponentVectors.Transpose().Divide(Eigenvalues, dimension: 0).Transpose(); } CreateComponents(); if (!onlyCovarianceMatrixAvailable) { if (array != null) { result = Transform(array).ToMatrix(); } else if (source != null) { result = Transform(source.ToJagged()).ToMatrix(); } } }
public static void Main(String[] args) { Matrix A = new Matrix(3, 3); A[0, 0] = 2.0; A[0, 1] = 1.0; A[0, 2] = 2.0; A[1, 0] = 1.0; A[1, 1] = 4.0; A[1, 2] = 0.0; A[2, 0] = 2.0; A[2, 1] = 0.0; A[2, 2] = 8.0; Console.WriteLine("A = "); Console.WriteLine(A.ToString()); Console.WriteLine("A.Determinant = " + A.Determinant); Console.WriteLine("A.Trace = " + A.Trace); Console.WriteLine("A.Norm1 = " + A.Norm1); Console.WriteLine("A.NormInfinite = " + A.InfinityNorm); Console.WriteLine("A.NormFrobenius = " + A.FrobeniusNorm); SingularValueDecomposition svg = new SingularValueDecomposition(A); Console.WriteLine("A.Norm2 = " + svg.Norm2); Console.WriteLine("A.Condition = " + svg.Condition); Console.WriteLine("A.Rank = " + svg.Rank); Console.WriteLine(); Console.WriteLine("A.Transpose = "); Console.WriteLine(A.Transpose().ToString()); Console.WriteLine("A.Inverse = "); Console.WriteLine(A.Inverse.ToString()); Matrix I = A * A.Inverse; Console.WriteLine("I = A * A.Inverse = "); Console.WriteLine(I.ToString()); Matrix B = new Matrix(3, 3); Console.WriteLine("B = "); B[0, 0] = 2.0; B[0, 1] = 0.0; B[0, 2] = 0.0; B[1, 0] = 1.0; B[1, 1] = 0.0; B[1, 2] = 0.0; B[2, 0] = 2.0; B[2, 1] = 0.0; B[2, 2] = 0.0; Console.WriteLine(B.ToString()); Matrix X = A.Solve(B); Console.WriteLine("A.Solve(B)"); Console.WriteLine(X.ToString()); Matrix T = A * X; Console.WriteLine("A * A.Solve(B) = B = "); Console.WriteLine(T.ToString()); Console.WriteLine("A = V * D * V"); EigenvalueDecomposition eigen = new EigenvalueDecomposition(A); Console.WriteLine("D = "); Console.WriteLine(eigen.DiagonalMatrix.ToString()); Console.WriteLine("lambda = "); foreach (double eigenvalue in eigen.RealEigenvalues) { Console.WriteLine(eigenvalue.ToString()); } Console.WriteLine(); Console.WriteLine("V = "); Console.WriteLine(eigen.EigenvectorMatrix); Console.WriteLine("V * D * V' = "); Console.WriteLine(eigen.EigenvectorMatrix * (eigen.DiagonalMatrix * eigen.EigenvectorMatrix.Transpose())); Console.WriteLine("A * V = "); Console.WriteLine(A * eigen.EigenvectorMatrix); Console.WriteLine("V * D = "); Console.WriteLine(eigen.EigenvectorMatrix * eigen.DiagonalMatrix); }
static void Main(string[] args) { #region 1. Declaring matrices // 1.1 Using standard .NET declaration double[,] A = { { 1, 2, 3 }, { 6, 2, 0 }, { 0, 0, 1 } }; double[,] B = { { 2, 0, 0 }, { 0, 2, 0 }, { 0, 0, 2 } }; { // 1.2 Using Accord extension methods double[,] Bi = Matrix.Identity(3).Multiply(2); double[,] Bj = Matrix.Diagonal(3, 2.0); // both are equal to B // 1.2 Using Accord extension methods with implicit typing var I = Matrix.Identity(3); } #endregion #region 2. Matrix Operations { // 2.1 Addition var C = A.Add(B); // 2.2 Subtraction var D = A.Subtract(B); // 2.3 Multiplication { // 2.3.1 By a scalar var halfM = A.Multiply(0.5); // 2.3.2 By a vector double[] m = A.Multiply(new double[] { 1, 2, 3 }); // 2.3.3 By a matrix var M = A.Multiply(B); // 2.4 Transposing var At = A.Transpose(); } } // 2.5 Elementwise operations // 2.5.1 Elementwise multiplication A.ElementwiseMultiply(B); // A.*B // 2.5.1 Elementwise division A.ElementwiseDivide(B); // A./B #endregion #region 3. Matrix characteristics { // 3.1 Calculating the determinant double det = A.Determinant(); // 3.2 Calculating the trace double tr = A.Trace(); // 3.3 Computing the sum vector { double[] sumVector = A.Sum(); // 3.3.1 Computing the total sum of elements double sum = sumVector.Sum(); // 3.3.2 Computing the sum along the rows sumVector = A.Sum(0); // Equivalent to Octave's sum(A, 1) // 3.3.2 Computing the sum along the columns sumVector = A.Sum(1); // Equivalent to Octave's sum(A, 2) } } #endregion #region 4. Linear Algebra { // 4.1 Computing the inverse var invA = A.Inverse(); // 4.2 Computing the pseudo-inverse var pinvA = A.PseudoInverse(); // 4.3 Solving a linear system (Ax = B) var x = A.Solve(B); } #endregion #region 5. Special operators { // 5.1 Finding the indices of elements double[] v = { 5, 2, 2, 7, 1, 0 }; int[] idx = v.Find(e => e > 2); // finding the index of every element in v higher than 2. // 5.2 Selecting elements by index double[] u = v.Submatrix(idx); // u is { 5, 7 } // 5.3 Converting between different matrix representations double[][] jaggedA = A.ToArray(); // from multidimensional to jagged array // 5.4 Extracting a column or row from the matrix double[] a = A.GetColumn(0); // retrieves the first column double[] b = B.GetRow(1); // retrieves the second row // 5.5 Taking the absolute of a matrix var absA = A.Abs(); // 5.6 Applying some function to every element var newv = v.Apply(e => e + 1); } #endregion #region 7. Vector operations { double[] u = { 1, 2, 3 }; double[] v = { 4, 5, 6 }; var w1 = u.InnerProduct(v); var w2 = u.OuterProduct(v); var w3 = u.CartesianProduct(v); double[] m = { 1, 2, 3, 4 }; double[,] M = Matrix.Reshape(m, 2, 2); } #endregion #region Decompositions { // Singular value decomposition { SingularValueDecomposition svd = new SingularValueDecomposition(A); var U = svd.LeftSingularVectors; var S = svd.Diagonal; var V = svd.RightSingularVectors; } // or (please see documentation for details) { SingularValueDecomposition svd = new SingularValueDecomposition(A.Transpose()); var U = svd.RightSingularVectors; var S = svd.Diagonal; var V = svd.LeftSingularVectors; } // Eigenvalue decomposition { EigenvalueDecomposition eig = new EigenvalueDecomposition(A); var V = eig.Eigenvectors; var D = eig.DiagonalMatrix; } // QR decomposition { QrDecomposition qr = new QrDecomposition(A); var Q = qr.OrthogonalFactor; var R = qr.UpperTriangularFactor; } // Cholesky decomposition { CholeskyDecomposition chol = new CholeskyDecomposition(A); var R = chol.LeftTriangularFactor; } // LU decomposition { LuDecomposition lu = new LuDecomposition(A); var L = lu.LowerTriangularFactor; var U = lu.UpperTriangularFactor; } } #endregion }
/// <summary> /// Indexer For a Vector Matrix. i is the row number. /// </summary> public virtual double this[int iRow] { get { return this._MatrixData[iRow, 0]; } set { this._MatrixData[iRow, 0] = value; this.LUDecomp = null; this.QRDecomp = null; this.EigenDecomp = null; } }