public void InsertColumn(int column, double[] values) { if (this.NumberOfColumns <= column) { throw new OutOfBoundIndexException(); } Matrix resultMatrix = new Matrix(this.NumberOfRows, this.NumberOfColumns + 1); int counter = 0; for (int i = 0; i < this.NumberOfColumns + 1; i++) { if (i == column) { resultMatrix.SetColumn(i, values); continue; } resultMatrix.SetColumn(i, this.GetColumn(counter)); counter += 1; } this.Reconstruct(resultMatrix); }
public void RemoveColumn(int column) { if (this.NumberOfColumns <= column) { throw new OutOfBoundIndexException(); } Matrix resultMatrix = new Matrix(this.NumberOfRows, this.NumberOfColumns - 1); int counter = 0; for (int i = 0; i < this.NumberOfColumns; i++) { if (i == column) { continue; } resultMatrix.SetColumn(counter, this.GetColumn(i)); counter += 1; } this.Reconstruct(resultMatrix); }
public Matrix Transpose() { Matrix result = new Matrix(this.NumberOfColumns, this.NumberOfRows); for (int i = 0; i < this.NumberOfRows; i++) { result.SetColumn(i, this.GetRow(i)); } return(result); }
public static void LU(Matrix matrix, out Matrix lowerMatrix, out Matrix upperMatrix) { if (!matrix.IsSquare()) { throw new NonSquareMatrixException(); } lowerMatrix = new Matrix(matrix.NumberOfRows, matrix.NumberOfColumns); lowerMatrix.SetColumn(0, matrix.GetColumn(0)); upperMatrix = Matrix.Identity(matrix.NumberOfRows); for (int i = 0; i < matrix.NumberOfRows; i++) { for (int j = i + 1; j < matrix.NumberOfColumns; j++) { for (int k = 0; k <= i - 1; k++) { upperMatrix[i, j] += lowerMatrix[i, k] * upperMatrix[k, j]; } upperMatrix[i, j] = (matrix[i, j] - upperMatrix[i, j]) / lowerMatrix[i, i]; } if (i == matrix.NumberOfColumns - 1) { continue; } for (int j = i + 1; j < matrix.NumberOfColumns; j++) { for (int k = 0; k <= i + 1 - 1; k++) { lowerMatrix[j, i + 1] += lowerMatrix[j, k] * upperMatrix[k, i + 1]; } lowerMatrix[j, i + 1] = matrix[j, i + 1] - lowerMatrix[j, i + 1]; } } }
public EigenvaluesEigenvectors GetEigenvaluesEigenvectors() { if (!IsSymmetric()) { throw new NotImplementedException(); } //Q=wT * this * w Matrix Q = this.Clone(); Matrix eigenvectors = Identity(this.NumberOfColumns);; double threshold = Q.CalculateSumOfNonDiagonalElemets(); do { DoSweep(ref Q, ref eigenvectors); threshold = Q.CalculateAbsoluteSumOfNonDiagonalElements(); } while (threshold > 0.0000000001); //return W; double[] eigenvalues = new double[eigenvectors.NumberOfColumns]; for (int i = 0; i < eigenvectors.NumberOfColumns; i++) { Matrix tempEigenvector = new Matrix(NumberOfRows, 1); tempEigenvector.SetColumn(0, eigenvectors.GetColumn(i)); Matrix tempMultiplication = this * tempEigenvector; eigenvalues[i] = tempMultiplication[0, 0] / tempEigenvector[0, 0]; } return(new EigenvaluesEigenvectors(eigenvalues, eigenvectors)); }