Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Algorithms;
- public class NaiveGaussianAlgorithm {
- public double[] Gaussian(double arr[][], double vector[]) {
- int n = vector.length;
- //forward elimination
- for(int k = 0; k < n; k++) {
- int max = k;
- for(int i = (k + 1); i < n; i++) {
- if(Math.abs(arr[i][k]) > Math.abs(arr[max][k])) {
- max = i;
- }
- }
- double[] temp = arr[k];
- arr[k] = arr[max];
- arr[max] = temp;
- double a = vector[k];
- vector[k] = vector[max];
- vector[max] = a;
- for (int i = k + 1; i < n; i++) {
- double factor = arr[i][k] / arr[k][k];
- vector[i] -= factor * vector[k];
- for(int j = k; j< n; j++) {
- arr[i][j] -= factor *arr[k][j];
- }
- }
- }
- //back substitution
- double vectorSolution[] = new double[n];
- vectorSolution[n] = vector[n] / arr[n][n];
- for(int i = n - 1;i >= 0; i--) {
- double sum = 0.0;
- for(int j = i + 1; j < n; j++) {
- sum += arr[i][j] * vectorSolution[j];
- }
- vectorSolution[i] =(vector[i] - sum) / arr[i][i];
- }
- return vectorSolution;
- }
- public void naiveGaussian(double arr[][], double vector[]) {
- System.out.println(Gaussian(arr, vector));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement