public RMatrix Clone() { RMatrix m = new RMatrix(matrix); m.matrix = (double[, ])matrix.Clone(); return(m); }
public RMatrix GetTranspose() { RMatrix m = this; m.Transpose(); return(m); }
public static RMatrix Minor(RMatrix mat, int row, int col) { RMatrix mm = new RMatrix(mat.GetnRows - 1, mat.GetnCols - 1); int ii = 0, jj = 0; for (int i = 0; i < mat.GetnRows; i++) { if (i == row) { continue; } jj = 0; for (int j = 0; j < mat.GetnCols; j++) { if (j == col) { continue; } mm[ii, jj] = mat[i, j]; jj++; } ii++; } return(mm); }
public static RMatrix operator *(RMatrix m1, RMatrix m2) { if (m1.GetnCols != m2.GetnRows) { throw new Exception("The numbers of columns of the" + " first matrix must be equal to the number of " + " rows of the second matrix!"); } double tmp; RMatrix result = new RMatrix(m1.GetnRows, m2.GetnCols); for (int i = 0; i < m1.GetnRows; i++) { for (int j = 0; j < m2.GetnCols; j++) { tmp = result[i, j]; for (int k = 0; k < result.GetnRows; k++) { tmp += m1[i, k] * m2[k, j]; } result[i, j] = tmp; } } return(result); }
public static RMatrix Inverse(RMatrix mat) { if (Determinant(mat) == 0) { throw new Exception("Cannot inverse a matrix with a zero determinant!"); } return(Adjoint(mat) / Determinant(mat)); }
public static bool CompareDimension(RMatrix m1, RMatrix m2) { if (m1.GetnRows == m2.GetnRows && m1.GetnCols == m2.GetnCols) { return(true); } else { return(false); } }
public void Transpose() { RMatrix m = new RMatrix(nCols, nRows); for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { m[j, i] = matrix[i, j]; } } this = m; }
public static RMatrix operator /(double d, RMatrix m) { RMatrix result = new RMatrix(m.GetnRows, m.GetnCols); for (int i = 0; i < m.GetnRows; i++) { for (int j = 0; j < m.GetnCols; j++) { result[i, j] = m[i, j] / d; } } return(result); }
public RMatrix IdentityMatrix() { RMatrix m = new RMatrix(nRows, nCols); for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { if (i == j) { m[i, j] = 1; } } } return(m); }
public static RMatrix Adjoint(RMatrix mat) { if (!mat.IsSquared()) { throw new Exception("The matrix must be squared!"); } RMatrix ma = new RMatrix(mat.GetnRows, mat.GetnCols); for (int i = 0; i < mat.GetnRows; i++) { for (int j = 0; j < mat.GetnCols; j++) { ma[i, j] = Math.Pow(-1, i + j) * (Determinant(Minor(mat, i, j))); } } return(ma.GetTranspose()); }
public static RMatrix Transform(RVector v1, RVector v2) { if (v1.GetVectorSize != v2.GetVectorSize) { throw new Exception("The vectors must have the same ndim!"); } RMatrix result = new RMatrix(v1.GetVectorSize, v1.GetVectorSize); for (int i = 0; i < v1.GetVectorSize; i++) { for (int j = 0; j < v1.GetVectorSize; j++) { result[j, i] = v1[i] * v2[j]; } } return(result); }
public static RMatrix operator -(RMatrix m1, RMatrix m2) { if (!RMatrix.CompareDimension(m1, m2)) { throw new Exception("The dimensions of two matrices must be the same!"); } RMatrix result = new RMatrix(m1.GetnRows, m1.GetnCols); for (int i = 0; i < m1.GetnRows; i++) { for (int j = 0; j < m1.GetnCols; j++) { result[i, j] = m1[i, j] - m2[i, j]; } } return(result); }
public static double Determinant(RMatrix mat) { double result = 0.0; if (!mat.IsSquared()) { throw new Exception("The matrix must be squared!"); } if (mat.GetnRows == 1) { result = mat[0, 0]; } else { for (int i = 0; i < mat.GetnRows; i++) { result += Math.Pow(-1, i) * mat[0, i] * Determinant(RMatrix.Minor(mat, 0, i)); } } return(result); }
public static RVector Transform(RVector vec, RMatrix mat) { RVector result = new RVector(vec.GetVectorSize); if (!mat.IsSquared()) { throw new Exception("The matrix must be squared!"); } if (mat.GetnRows != vec.GetVectorSize) { throw new Exception("The ndim of the vector must be equal" + " to the number of rows of the matrix!"); } for (int i = 0; i < mat.GetnRows; i++) { result[i] = 0.0; for (int j = 0; j < mat.GetnCols; j++) { result[i] += vec[j] * mat[j, i]; } } return(result); }
public RMatrix(RMatrix m) { nRows = m.GetnRows; nCols = m.GetnCols; matrix = m.matrix; }
public bool Equals(RMatrix m) { return(matrix == m.matrix); }