كود برمجي بلغة Java يقوم بتحويل المصفوفات رياضيا إلى صورة إشلون (Echlon Form)
الكود :
public class Matrix_Reduced_Echelon_Form {
public static void main(String[] args) {
double[][] matrix = {
{2, 1, -1, 8},
{-3, -1, 2, -11},
{-2, 1, 2, -3}
};
int rows = matrix.length;
int cols = matrix[0].length;
reduceToEchelonForm(matrix, rows, cols);
System.out.println("Reduced Echelon Form:");
printMatrix(matrix, rows, cols);
}
public static void reduceToEchelonForm(double[][] matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
// Find the pivot element
int pivotRow = i;
while (pivotRow < rows && matrix[pivotRow][i] == 0) {
pivotRow++;
}
if (pivotRow == rows) {
continue; // No pivot found in this column, move to the next column
}
// Swap rows to bring the pivot element to the current row
double[] temp = matrix[i];
matrix[i] = matrix[pivotRow];
matrix[pivotRow] = temp;
// Make the pivot element 1
double pivot = matrix[i][i];
for (int j = i; j < cols; j++) {
matrix[i][j] /= pivot;
}
// Eliminate other elements in the current column
for (int k = 0; k < rows; k++) {
if (k != i) {
double factor = matrix[k][i];
for (int j = i; j < cols; j++) {
matrix[k][j] -= factor * matrix[i][j];
}
}
}
}
}
public static void printMatrix(double[][] matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
}