/// <summary> /// Replace column with vector that same size /// </summary> /// <param name="vector">Vector</param> /// <param name="rowIndex">Row index</param> public static void WriteOnColumn(this RealMatrix matrix, ColumnVector vector, int columnIndex, bool isAugmentedRow = false) { //check that matrix column length (row count) is equal to vector length //and column index is valid if (!isAugmentedRow) { if (vector.V.Count != matrix.RowCount && columnIndex >= matrix.ColumnCount) { return; } for (int i = 0; i < vector.V.Count; i++) { matrix.M[i][columnIndex] = vector.V[i]; } } else { if (vector.V.Count != matrix.AugmentedRowCount && columnIndex >= matrix.AugmentedColumnCount) { return; } for (int i = 0; i < vector.V.Count; i++) { matrix.B[i][columnIndex] = vector.V[i]; } } matrix.MatrixElementsChanged(); }
/// <summary> /// Replace row with vector that same size /// </summary> /// <param name="vector">Vector</param> /// <param name="rowIndex">Row index</param> public static void WriteOnRow(this RealMatrix matrix, RowVector rowVector, int rowIndex, bool isAugmentedRow = false) { //check that matrix row length (column count) is equal to vector length //and row index is valid if (!isAugmentedRow) { if (rowVector.V.Count != matrix.ColumnCount && rowIndex >= matrix.RowCount) { return; } for (int i = 0; i < rowVector.V.Count; i++) { matrix.M[rowIndex][i] = rowVector.V[i]; } } else { if (rowVector.V.Count != matrix.AugmentedColumnCount && rowIndex >= matrix.AugmentedRowCount) { return; } for (int i = 0; i < rowVector.V.Count; i++) { matrix.B[rowIndex][i] = rowVector.V[i]; } } matrix.MatrixElementsChanged(); }
/// <summary> /// Elementary row operation one, interchange two rows /// </summary> /// <param name="rowOneIndex">Row one index</param> /// <param name="rowTwoIndex">Row two index</param> public static void EROOne(this RealMatrix matrix, int rowOneIndex, int rowTwoIndex) { var rowOne = matrix.GetRow(rowOneIndex); var rowTwo = matrix.GetRow(rowTwoIndex); for (int i = 0; i < matrix.ColumnCount; i++) { matrix.M[rowOneIndex][i] = rowTwo.V[i]; matrix.M[rowTwoIndex][i] = rowOne.V[i]; } if (matrix.IsAugmentedMatrix) { var augmentedRowOne = matrix.GetAugmentedRow(rowOneIndex); var augmentedRowTwo = matrix.GetAugmentedRow(rowTwoIndex); for (int i = 0; i < matrix.AugmentedColumnCount; i++) { matrix.B[rowOneIndex][i] = augmentedRowOne.V[i]; matrix.B[rowTwoIndex][i] = augmentedRowTwo.V[i]; } } matrix.MatrixElementsChanged(); //TODO //If two rows of A are interchanged to produce a matrix B, then det(B) = //− det(A). }
/// <summary> /// Elemental row operation three, multiply one row to add another row /// </summary> /// <param name="rowToMultiply">Multiply this index row</param> /// <param name="constant">Multiply with this constant row</param> /// <param name="rowToAddIndex">Add multiple of a row to this indexex row</param> public static void EROThree(this RealMatrix matrix, int rowToMultiply, IR.RealNumber constant, int rowToAddIndex) { if (constant == 0) { return; } for (int i = 0; i < matrix.ColumnCount; i++) { matrix.M[rowToAddIndex][i] += matrix.M[rowToMultiply][i] * -constant; } if (matrix.IsAugmentedMatrix) { for (int i = 0; i < matrix.AugmentedColumnCount; i++) { matrix.B[rowToAddIndex][i] += matrix.B[rowToMultiply][i] * -constant; } } matrix.MatrixElementsChanged(); }
/// <summary> /// Elemental row operation two, multiply one row with a constant /// </summary> /// <param name="rowIndex">Row to multiplt</param> /// <param name="constant">Multiply row with this constant</param> public static void EROTwo(this RealMatrix matrix, int rowIndex, IR.RealNumber constant) { if (constant == 0) { return; } var row = matrix.GetRow(rowIndex); row.ElementMultiplication(constant); matrix.WriteOnRow(row, rowIndex); if (matrix.IsAugmentedMatrix) { var augmentedRow = matrix.GetAugmentedRow(rowIndex); augmentedRow.ElementMultiplication(constant); matrix.WriteOnRow(augmentedRow, rowIndex, isAugmentedRow: true); } matrix.MatrixElementsChanged(); //TODO //If a row of A is multiplied by a real number α to produce a matrix B, then //det(B) = αdet(A) }