public static CellArray Multiply(CellArray A, CellArray B) { if (!CellArray.IsMatrix(A) || !CellArray.IsMatrix(B)) { throw new Exception("Both A and B must be matrixes"); } int A_Rows = A.Count, B_Rows = B.Count; int A_Cols = CellArray.ColumnCount(A), B_Cols = CellArray.ColumnCount(B); if (A_Cols != B_Rows) { throw new Exception(string.Format("Dimension mismatch A {0}:{1} B {2}:{3}", A_Rows, A_Cols, B_Rows, B_Cols)); } CellArray C = CellArray.Matrix(A_Rows, B_Cols); // Main Loop // for (int i = 0; i < A_Rows; i++) { // Sub Loop One // for (int j = 0; j < B_Cols; j++) { // Sub Loop Two // for (int k = 0; k < A_Cols; k++) { C[i].valueARRAY[j] = (C[i].valueARRAY[j].IsNull ? A[i].valueARRAY[k] * B[k].valueARRAY[j] : A[i].valueARRAY[k] * B[k].valueARRAY[j] + C[i].valueARRAY[j]); } } } // Return C // return(C); }
public CellArray solve(CellArray B) { if (B.Count != m) { throw new Exception("Matrix row dimensions must agree."); } if (!this.isNonsingular()) { throw new Exception("Matrix is singular."); } // Copy right hand side with pivoting int nx = CellArray.ColumnCount(B); CellArray X = this.getMatrix(B, piv, 0, nx - 1); // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { for (int i = k + 1; i < n; i++) { for (int j = 0; j < nx; j++) { X[i].ARRAY[j] -= X[k].ARRAY[j] * LU[i].ARRAY[k]; } } } // Solve U*X = Y; for (int k = n - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { X[k].ARRAY[j] /= LU[k].ARRAY[k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { X[i].ARRAY[j] -= X[k].ARRAY[j] * LU[i].ARRAY[k]; } } } return(X); }
public static Cell Determinant(CellArray A) { if (!CellArray.IsMatrix(A)) { throw new Exception("A must be a matrix"); } int A_Rows = A.Count; int A_Cols = CellArray.ColumnCount(A); if (A_Rows != A_Cols) { throw new Exception(string.Format("Matrix must be square {0}:{1}", A_Rows, A_Cols)); } LUDecomposition engine = new LUDecomposition(A); return(engine.det()); }
public static CellArray Inverse(CellArray A) { if (!CellArray.IsMatrix(A)) { throw new Exception("A must be a matrix"); } int A_Rows = A.Count; int A_Cols = CellArray.ColumnCount(A); if (A_Rows != A_Cols) { throw new Exception(string.Format("Matrix must be square {0}:{1}", A_Rows, A_Cols)); } LUDecomposition engine = new LUDecomposition(A); CellArray B = CellArray.Identity(A_Rows); return(engine.solve(B)); }