Finding the matrix row reduction using Gaussian elimination.
Here’s an example Java program that implements Gaussian elimination to find the row-reduced echelon form of a matrix:
import java.util.Arrays; public class GaussianElimination { public static void main(String[] args) { double[][] matrix = { {2, -3, 1, 4}, {1, 2, -1, 3}, {4, -1, 3, 2} }; int rows = matrix.length; int cols = matrix[0].length; System.out.println("Original Matrix:"); printMatrix(matrix); rowReduction(matrix, rows, cols); System.out.println("Row-Reduced Echelon Form:"); printMatrix(matrix); } public static void rowReduction(double[][] matrix, int rows, int cols) { int lead = 0; for (int r = 0; r < rows; r++) { if (lead >= cols) break; int i = r; while (matrix[i][lead] == 0) { i++; if (i == rows) { i = r; lead++; if (lead == cols) return; } } double[] temp = matrix[r]; matrix[r] = matrix[i]; matrix[i] = temp; double lv = matrix[r][lead]; for (int j = 0; j < cols; j++) matrix[r][j] /= lv; for (int k = 0; k < rows; k++) { if (k != r) { double factor = matrix[k][lead]; for (int j = 0; j < cols; j++) matrix[k][j] -= factor * matrix[r][j]; } } lead++; } } public static void printMatrix(double[][] matrix) { for (double[] row : matrix) { System.out.println(Arrays.toString(row)); } System.out.println(); } }
In this example, the program takes a 2D array representing the matrix and performs Gaussian elimination to obtain the row-reduced echelon form. The `rowReduction` method implements the algorithm, and the `printMatrix` method is used to display the matrix before and after the row reduction.
The program uses a hardcoded matrix for simplicity, but you can modify it to accept user input or generate random matrices if needed.
No comments yet! You be the first to comment.