public static bool TryGaussJordanElimination(NMatrix a, NMatrix b, out NMatrix result) { if (a.NoRows != b.NoRows) throw new ArgumentException(); result = null; NMatrix lhs = a.Copy(); NMatrix rhs = b.Copy(); int rowCount = lhs.NoRows; int colCount = rhs.NoCols + lhs.NoCols; // augment lhs array with rhs array and store in arr2 var matrixArray = new double[rowCount, colCount]; for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < lhs.NoCols; ++col) { matrixArray[row, col] = lhs[row, col]; } for (int col = 0; col < rhs.NoCols; ++col) { matrixArray[row, lhs.NoCols + col] = rhs[row, col]; } } // perform forward elimination to get arr2 in row-echelon form for (int row = 0; row < rowCount; ++row) { // run along diagonal, swapping rows to move zeros in working position // (along the diagonal) downwards if (IsZero(matrixArray[row, row])) if (row == (rowCount - 1)) return false; // no solution for (int insideRow = row + 1; insideRow < rowCount; insideRow++) { if (!IsZero(matrixArray[insideRow, row])) { SwapRows(matrixArray, row, insideRow); break; } } // divide working row by value of working position to get a 1 on the // diagonal if (IsZero(matrixArray[row, row])) return false; double diagonal = matrixArray[row, row]; for (int col = 0; col < colCount; ++col) matrixArray[row, col] /= diagonal; // eliminate value below working position by subtracting a multiple of // the current row for (int insideRow = row + 1; insideRow < rowCount; ++insideRow) { double coefficient = matrixArray[insideRow, insideRow]; for (int col = 0; col < colCount; ++col) matrixArray[insideRow, col] -= coefficient * matrixArray[insideRow, col]; } } // backward substitution steps for (int dindex = rowCount - 1; dindex >= 0; --dindex) { // eliminate value above working position by subtracting a multiple of // the current row for (int row = dindex - 1; row >= 0; --row) { double coefficient = matrixArray[row, dindex]; for (int col = 0; col < colCount; ++col) matrixArray[row, col] -= coefficient * matrixArray[dindex, col]; } } result = new NMatrix(matrixArray); return true; }
public static bool TryGaussJordanElimination(NMatrix a, NMatrix b, out NMatrix result) { if (a.NoRows != b.NoRows) { throw new ArgumentException(); } result = null; NMatrix lhs = a.Copy(); NMatrix rhs = b.Copy(); int rowCount = lhs.NoRows; int colCount = rhs.NoCols + lhs.NoCols; // augment lhs array with rhs array and store in arr2 var matrixArray = new double[rowCount, colCount]; for (int row = 0; row < rowCount; ++row) { for (int col = 0; col < lhs.NoCols; ++col) { matrixArray[row, col] = lhs[row, col]; } for (int col = 0; col < rhs.NoCols; ++col) { matrixArray[row, lhs.NoCols + col] = rhs[row, col]; } } // perform forward elimination to get arr2 in row-echelon form for (int row = 0; row < rowCount; ++row) { // run along diagonal, swapping rows to move zeros in working position // (along the diagonal) downwards if (IsZero(matrixArray[row, row])) { if (row == (rowCount - 1)) { return(false); // no solution } } for (int insideRow = row + 1; insideRow < rowCount; insideRow++) { if (!IsZero(matrixArray[insideRow, row])) { SwapRows(matrixArray, row, insideRow); break; } } // divide working row by value of working position to get a 1 on the // diagonal if (IsZero(matrixArray[row, row])) { return(false); } double diagonal = matrixArray[row, row]; for (int col = 0; col < colCount; ++col) { matrixArray[row, col] /= diagonal; } // eliminate value below working position by subtracting a multiple of // the current row for (int insideRow = row + 1; insideRow < rowCount; ++insideRow) { double coefficient = matrixArray[insideRow, insideRow]; for (int col = 0; col < colCount; ++col) { matrixArray[insideRow, col] -= coefficient * matrixArray[insideRow, col]; } } } // backward substitution steps for (int dindex = rowCount - 1; dindex >= 0; --dindex) { // eliminate value above working position by subtracting a multiple of // the current row for (int row = dindex - 1; row >= 0; --row) { double coefficient = matrixArray[row, dindex]; for (int col = 0; col < colCount; ++col) { matrixArray[row, col] -= coefficient * matrixArray[dindex, col]; } } } result = new NMatrix(matrixArray); return(true); }