Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. import java.io.FileReader;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class LowerRelaxationMethod {
  7.     private double[][] augmentedMatrix;
  8.     private double W;
  9.     private double[][] F;
  10.     private double[][] H;
  11.     private double[] x;
  12.     private double[] discrepancy;
  13.     private double normDiscrepancy;
  14.     private int row;
  15.     private int col;
  16.     private int countOfIterations;
  17.  
  18.     public LowerRelaxationMethod(int row, int col, double W){
  19.         this.W = W;
  20.         this.row = row;
  21.         this.col = col;
  22.         augmentedMatrix = new double[row][col];
  23.         x = new double[row];
  24.     }
  25.  
  26.     public void formAugmentedMatrix(String fileName) throws IOException {
  27.         try(Scanner sc = new Scanner(new FileReader(fileName))){
  28.             for(int i = 0; i < row; ++i){
  29.                 for(int j = 0; j < col; ++j){
  30.                     augmentedMatrix[i][j] = sc.nextDouble();
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     public void formFandH() {
  37.         F = new double[row][col];
  38.         H = new double[row][col];
  39.         for (int i = 0; i < row; i++) {
  40.             for (int j = 0; j < col; j++) {
  41.                 if (j == col - 1) {
  42.                     F[i][j] = augmentedMatrix[i][j] / augmentedMatrix[i][i];
  43.                 }
  44.                 else {
  45.                     if (i == j) {
  46.                         F[i][j] = 0;
  47.                         H[i][j] = 0;
  48.                     }
  49.                     else {
  50.                         if (i < j) {
  51.                             F[i][j] = -augmentedMatrix[i][j] / augmentedMatrix[i][i];
  52.                         } else {
  53.                             H[i][j] = -augmentedMatrix[i][j] / augmentedMatrix[i][i];
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.         }
  59.     }
  60.  
  61.     public void firstIteration(){
  62.         countOfIterations++;
  63.         for(int i = 0; i < row; i++){
  64.             x[i] = F[i][col - 1];
  65.         }
  66.     }
  67.  
  68.     public void iterations(){
  69.         double[] xPrevious = new double[row];
  70.         while(!stop(xPrevious, x)){
  71.             copyArray(x, xPrevious);
  72.             oneIteration(xPrevious);
  73.             countOfIterations++;
  74.         }
  75.     }
  76.  
  77.     private void oneIteration(double[] previous){
  78.         for (int i = 0; i < row; i++) {
  79.             x[i] = 0;
  80.             x[i] += (1 - W) * previous[i];
  81.             for(int j = 0; j < i; j++) {
  82.                 x[i] += W * H[i][j] * x[j];
  83.             }
  84.             for(int j = i + 1; j < col - 1; j++) {
  85.                 x[i] += W * F[i][j] * previous[j];
  86.             }
  87.             x[i] += W * F[i][col - 1];
  88.         }
  89.     }
  90.  
  91.     private boolean stop(double[] previous, double[] current){
  92.         for(int i = 0; i < row; i++){
  93.             if(previous[i] - current[i] > 0.00001){
  94.                 return false;
  95.             }
  96.         }
  97.         return true;
  98.     }
  99.  
  100.     private void copyArray(double[] from, double[] to){
  101.         for(int i = 0; i < row; i++){
  102.             to[i] = from[i];
  103.         }
  104.     }
  105.  
  106.     public void findDiscrepancy(){
  107.         discrepancy = new double [row];
  108.         for(int i = 0; i < row; ++i){
  109.             for(int j = 0; j < col - 1; ++j){
  110.                 discrepancy[i] += augmentedMatrix[i][j] * x[j];
  111.             }
  112.             discrepancy[i] -= augmentedMatrix[i][col - 1];
  113.         }
  114.     }
  115.  
  116.     public void discrepancyNorm(){
  117.         normDiscrepancy = 0;
  118.         for(int i = 0; i < row; ++i){
  119.             normDiscrepancy = normDiscrepancy + Math.abs(discrepancy[i]);
  120.         }
  121.         System.out.printf("Норма r = %.5e\n", normDiscrepancy);
  122.     }
  123.  
  124.     public void printAugmentedMatrix(){
  125.         System.out.println("Матрица системы:");
  126.         for(int i = 0; i < row; ++i){
  127.             for(int j = 0; j < col; ++j){
  128.                 if(j == col - 1){
  129.                     System.out.printf("|%8.4f", augmentedMatrix[i][j]);
  130.                 }
  131.                 else {
  132.                     System.out.printf("%8.4f", augmentedMatrix[i][j]);
  133.                 }
  134.             }
  135.             System.out.println();
  136.         }
  137.     }
  138.  
  139.     public void printX() {
  140.         System.out.println("Вектор решений:");
  141.         Arrays.stream(x).forEach(item -> System.out.printf("%8.5f ", item));
  142.         System.out.println();
  143.     }
  144.  
  145.     public void printDiscrepancy(){
  146.         System.out.println("Вектор невязки:");
  147.         Arrays.stream(discrepancy).forEach(item -> System.out.printf("%15.5e\n", item));
  148.     }
  149.  
  150.  
  151.  
  152.     public void printCountOfIterations(){
  153.         System.out.println("Количество итераций:");
  154.         System.out.println(countOfIterations);
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement