mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
@ -1,58 +1,53 @@
|
||||
/**
|
||||
* \file
|
||||
* \brief [Gaussian elimination
|
||||
* method](https://en.wikipedia.org/wiki/Gaussian_elimination)
|
||||
* @author [Sachwin Kohli](https://github.com/Sachwin-Kohli)
|
||||
*/
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
public class Gaussian {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int mat_size, i, j, step;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
System.out.println("Matrix Size : ");
|
||||
mat_size = sc.nextInt();
|
||||
public static ArrayList<Double> gaussian(int mat_size, ArrayList<Double> matrix) {
|
||||
ArrayList<Double> answerArray = new ArrayList<Double>();
|
||||
int i, j = 0;
|
||||
|
||||
double[][] mat = new double[mat_size + 1][mat_size + 1];
|
||||
double[][] x = new double[mat_size][mat_size + 1];
|
||||
|
||||
System.out.println("Enter value of the matrix");
|
||||
System.out.println(' ');
|
||||
// Values from arraylist to matrix
|
||||
for (i = 0; i < mat_size; i++) {
|
||||
for (j = 0; j <= mat_size; j++) {
|
||||
mat[i][j] = sc.nextInt();
|
||||
mat[i][j] = matrix.get(i);
|
||||
}
|
||||
}
|
||||
|
||||
// perform Gaussian elimination
|
||||
mat = gaussianElimination(mat_size, i, mat);
|
||||
answerArray = valueOfGaussian(mat_size, x, mat);
|
||||
return answerArray;
|
||||
}
|
||||
|
||||
// Perform Gaussian elimination
|
||||
public static double[][] gaussianElimination(int mat_size, int i, double[][] mat) {
|
||||
int step = 0;
|
||||
for (step = 0; step < mat_size - 1; step++) {
|
||||
for (i = step; i < mat_size - 1; i++) {
|
||||
double a = (mat[i + 1][step] / mat[step][step]);
|
||||
|
||||
for (j = step; j <= mat_size; j++) {
|
||||
for (int j = step; j <= mat_size; j++) {
|
||||
mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return mat;
|
||||
}
|
||||
|
||||
// calcilate the x_1, x_2,... values of the gaussian and save it in an arraylist.
|
||||
public static ArrayList<Double> valueOfGaussian(int mat_size, double[][] x, double[][] mat) {
|
||||
ArrayList<Double> answerArray = new ArrayList<Double>();
|
||||
int i, j;
|
||||
|
||||
System.out.println("Matrix using Gaussian Elimination method: ");
|
||||
System.out.println(" ");
|
||||
for (i = 0; i < mat_size; i++) {
|
||||
for (j = 0; j <= mat_size; j++) {
|
||||
x[i][j] = mat[i][j];
|
||||
System.out.print(mat[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
System.out.println("Value of the Gaussian Elimination method: ");
|
||||
System.out.println(" ");
|
||||
|
||||
for (i = mat_size - 1; i >= 0; i--) {
|
||||
double sum = 0;
|
||||
@ -65,8 +60,8 @@ public class Gaussian {
|
||||
} else {
|
||||
x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
|
||||
}
|
||||
System.out.print("x" + i + "=" + x[i][i]);
|
||||
System.out.println(" ");
|
||||
answerArray.add(x[i][j]);
|
||||
}
|
||||
return answerArray;
|
||||
}
|
||||
}
|
||||
}
|
32
src/test/java/com/thealgorithms/maths/GaussianTest.java
Normal file
32
src/test/java/com/thealgorithms/maths/GaussianTest.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static com.thealgorithms.maths.Gaussian.gaussian;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class GaussianTest {
|
||||
|
||||
// easy pass test for the whole class. Matrix of 2*3.
|
||||
@Test
|
||||
void passTest1()
|
||||
{
|
||||
ArrayList<Double> list = new ArrayList<Double>();
|
||||
ArrayList<Double> gaussian = new ArrayList<Double>();
|
||||
ArrayList<Double> answer = new ArrayList<Double>();
|
||||
answer.add(0.0);
|
||||
answer.add(1.0);
|
||||
|
||||
int matrixSize = 2;
|
||||
list.add(1.0);
|
||||
list.add(1.0);
|
||||
list.add(1.0);
|
||||
list.add(2.0);
|
||||
list.add(1.0);
|
||||
list.add(1.0);
|
||||
gaussian=gaussian(matrixSize,list);
|
||||
|
||||
assertEquals(answer,gaussian);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user