Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package Algorithms;
  2.  
  3. public class NaiveGaussianAlgorithm {
  4.  
  5. public double[] Gaussian(double arr[][], double vector[]) {
  6.  
  7. int n = vector.length;
  8.  
  9. //forward elimination
  10. for(int k = 0; k < n; k++) {
  11. int max = k;
  12.  
  13. for(int i = (k + 1); i < n; i++) {
  14. if(Math.abs(arr[i][k]) > Math.abs(arr[max][k])) {
  15. max = i;
  16. }
  17. }
  18.  
  19. double[] temp = arr[k];
  20. arr[k] = arr[max];
  21. arr[max] = temp;
  22.  
  23. double a = vector[k];
  24. vector[k] = vector[max];
  25. vector[max] = a;
  26.  
  27. for (int i = k + 1; i < n; i++) {
  28. double factor = arr[i][k] / arr[k][k];
  29. vector[i] -= factor * vector[k];
  30. for(int j = k; j< n; j++) {
  31. arr[i][j] -= factor *arr[k][j];
  32. }
  33. }
  34. }
  35.  
  36. //back substitution
  37. double vectorSolution[] = new double[n];
  38. vectorSolution[n] = vector[n] / arr[n][n];
  39.  
  40. for(int i = n - 1;i >= 0; i--) {
  41. double sum = 0.0;
  42. for(int j = i + 1; j < n; j++) {
  43. sum += arr[i][j] * vectorSolution[j];
  44. }
  45. vectorSolution[i] =(vector[i] - sum) / arr[i][i];
  46. }
  47.  
  48. return vectorSolution;
  49. }
  50.  
  51. public void naiveGaussian(double arr[][], double vector[]) {
  52.  
  53. System.out.println(Gaussian(arr, vector));
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement