/// <summary> /// Gets the leading (first non-zero) column in each row of the matrix. /// </summary> /// <param name="matrix">The <see cref="MatrixByRow"/> to determine leading columns for.</param> /// <returns>An array containing the leading columns of each row.</returns> public static int[] GetLeadingColumns(MatrixByRow matrix) { int[] leadingPositions = new int[matrix.Height]; for (int i = 0; i < matrix.Height; i++) { int pos = GetLeadingColumn(matrix[i]); leadingPositions[i] = pos; } return(leadingPositions); }
/// <inheritdoc/> public override ExpNode Execute(RowElimOperNode node) { // Ensure matrix. if (node.Child is TensorNode tensorNode && tensorNode.TensorType == TensorType.Matrix) { MatrixByRow matrix = new MatrixByRow(tensorNode); int[] leadingPositions = RefHelpers.GetLeadingColumns(matrix); // Put in row-echelon form for (int i = 0; i < matrix.Height; i++) { int leftMostCol = RefHelpers.GetLeftMostColumn(leadingPositions, i); matrix.SwapRows(i, leftMostCol); Common.Swap(ref leadingPositions[i], ref leadingPositions[leftMostCol]); if (leadingPositions[i] == -1) { continue; } matrix[i].MultiplyRow(QuickOpers.Reciprical(matrix[i][leadingPositions[i]])); for (int j = i + 1; j < matrix.Height; j++) { matrix[j].AddRowToRow(matrix[i], QuickOpers.Negative(matrix[j][leadingPositions[i]])); leadingPositions[j] = RefHelpers.GetLeadingColumn(matrix[j]); } } if (node.EliminationMethod == RowElimMethod.GaussJordan) { // Put in reduced row-echelon form for (int i = matrix.Height - 1; i > 0; i--) { for (int j = i - 1; j >= 0; j--) { matrix[j].AddRowToRow(matrix[i], QuickOpers.Negative(matrix[j][leadingPositions[i]])); leadingPositions[j] = RefHelpers.GetLeadingColumn(matrix[j]); } } } return(matrix.AsExpNode()); } return(HandleError(new CannotReduceNonMatrix(this, node.Child))); }