Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/thealgorithms/maths/FFT.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ public double abs() {
*/
public Complex divide(Complex z) {
Complex temp = new Complex();
temp.real = (this.real * z.real + this.img * z.img) / (z.abs() * z.abs());
temp.img = (this.img * z.real - this.real * z.img) / (z.abs() * z.abs());
double d = z.abs() * z.abs();
d = (double)Math.round(d * 1000000000d) / 1000000000d;
temp.real = (this.real * z.real + this.img * z.img) / (d);
temp.img = (this.img * z.real - this.real * z.img) / (d);
return temp;
}

Expand Down
55 changes: 25 additions & 30 deletions src/main/java/com/thealgorithms/maths/Gaussian.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -65,8 +60,8 @@ public static void main(String[] args) {
} 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 changes: 32 additions & 0 deletions src/test/java/com/thealgorithms/maths/GaussianTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}