public MatrixMethods(Fixed <Q>[,] matrix) { this.matrix = matrix; rows = matrix.GetLength(0); columns = matrix.GetLength(1); delta = (Fixed <Q>) 1 / 100; }
private static void addRowToRow(int row1, int row2, bool negative) { for (int i = 0; i < Matrix.GetLength(1); i++) { if (negative) { Matrix[row2, i] -= Matrix[row1, i]; } else { Matrix[row2, i] += Matrix[row1, i]; } } }
public static Fixed <Q>[,] MatrixMult(Fixed <Q>[,] A, Fixed <Q>[,] B) { Fixed <Q>[,] matrix = new Fixed <Q> [A.GetLength(0), B.GetLength(1)]; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { for (int i = 0; i < A.GetLength(1); i++) { matrix[row, col] += A[row, i] * B[i, col]; } } } return(matrix); }
public static void Eliminate(Fixed <Q>[,] matrix) { Matrix = matrix; for (int j = 0; j < Matrix.GetLength(1); j++) { for (int i = 1 + j; i < Matrix.GetLength(0); i++) { var multiplier = Matrix[j, j]; multiplyRow(j, Matrix[i, j]); multiplyRow(i, multiplier); addRowToRow(j, i, true); multiplyRow(j, 1 / multiplier); } } }
static void AddRow(int iTargetRow, int iSecondRow, Fixed <Q> iMultiple, ref Fixed <Q>[,] reducedMatrix) { for (int j = 0; j < reducedMatrix.GetLength(1); j++) { reducedMatrix[iTargetRow, j] += (reducedMatrix[iSecondRow, j] * iMultiple); } }
static void MultiplyRow(int iRow, Fixed <Q> num, ref Fixed <Q>[,] reducedMatrix) { for (int j = 0; j < reducedMatrix.GetLength(1); j++) { reducedMatrix[iRow, j] *= num; } }
static void InterchangeRow(int iRow1, int iRow2, ref Fixed <Q>[,] reducedMatrix) { for (int j = 0; j < reducedMatrix.GetLength(1); j++) { Fixed <Q> temp = reducedMatrix[iRow1, j]; reducedMatrix[iRow1, j] = reducedMatrix[iRow2, j]; reducedMatrix[iRow2, j] = temp; } }
public static Fixed <Q>[,] AddCol(Fixed <Q>[,] A, Fixed <Q>[,] b) { Fixed <Q>[,] matrix = new Fixed <Q> [A.GetLength(0), A.GetLength(1) + 1]; for (int row = 0; row < matrix.GetLength(0); row++) { for (int col = 0; col < matrix.GetLength(1); col++) { if (col < A.GetLength(1)) { matrix[row, col] = A[row, col]; } else { matrix[row, col] = b[row, 0]; } } } return(matrix); }
public static Fixed <Q>[,] ReducedEchelonForm(Fixed <Q>[,] M) { Fixed <Q>[,] reducedMatrix = M; for (int i = 0; i < M.GetLength(0); i++) { if (reducedMatrix[i, i] == 0) { for (int j = i + 1; j < reducedMatrix.GetLength(0); j++) { if (reducedMatrix[j, i] != 0) { InterchangeRow(i, j, ref reducedMatrix); } } } if (reducedMatrix[i, i] == 0) { continue; } if (reducedMatrix[i, i] != 1) { for (int j = i + 1; j < reducedMatrix.GetLength(0); j++) { if (reducedMatrix[j, i] == 1) { InterchangeRow(i, j, ref reducedMatrix); } } } MultiplyRow(i, 1 / reducedMatrix[i, i], ref reducedMatrix); for (int j = i + 1; j < reducedMatrix.GetLength(0); j++) { AddRow(j, i, -reducedMatrix[j, i], ref reducedMatrix); } for (int j = i - 1; j >= 0; j--) { AddRow(j, i, -reducedMatrix[j, i], ref reducedMatrix); } } return(reducedMatrix); }