Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.Scanner;
- public class LowerRelaxationMethod {
- private double[][] augmentedMatrix;
- private double W;
- private double[][] F;
- private double[][] H;
- private double[] x;
- private double[] discrepancy;
- private double normDiscrepancy;
- private int row;
- private int col;
- private int countOfIterations;
- public LowerRelaxationMethod(int row, int col, double W){
- this.W = W;
- this.row = row;
- this.col = col;
- augmentedMatrix = new double[row][col];
- x = new double[row];
- }
- public void formAugmentedMatrix(String fileName) throws IOException {
- try(Scanner sc = new Scanner(new FileReader(fileName))){
- for(int i = 0; i < row; ++i){
- for(int j = 0; j < col; ++j){
- augmentedMatrix[i][j] = sc.nextDouble();
- }
- }
- }
- }
- public void formFandH() {
- F = new double[row][col];
- H = new double[row][col];
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- if (j == col - 1) {
- F[i][j] = augmentedMatrix[i][j] / augmentedMatrix[i][i];
- }
- else {
- if (i == j) {
- F[i][j] = 0;
- H[i][j] = 0;
- }
- else {
- if (i < j) {
- F[i][j] = -augmentedMatrix[i][j] / augmentedMatrix[i][i];
- } else {
- H[i][j] = -augmentedMatrix[i][j] / augmentedMatrix[i][i];
- }
- }
- }
- }
- }
- }
- public void firstIteration(){
- countOfIterations++;
- for(int i = 0; i < row; i++){
- x[i] = F[i][col - 1];
- }
- }
- public void iterations(){
- double[] xPrevious = new double[row];
- while(!stop(xPrevious, x)){
- copyArray(x, xPrevious);
- oneIteration(xPrevious);
- countOfIterations++;
- }
- }
- private void oneIteration(double[] previous){
- for (int i = 0; i < row; i++) {
- x[i] = 0;
- x[i] += (1 - W) * previous[i];
- for(int j = 0; j < i; j++) {
- x[i] += W * H[i][j] * x[j];
- }
- for(int j = i + 1; j < col - 1; j++) {
- x[i] += W * F[i][j] * previous[j];
- }
- x[i] += W * F[i][col - 1];
- }
- }
- private boolean stop(double[] previous, double[] current){
- for(int i = 0; i < row; i++){
- if(previous[i] - current[i] > 0.00001){
- return false;
- }
- }
- return true;
- }
- private void copyArray(double[] from, double[] to){
- for(int i = 0; i < row; i++){
- to[i] = from[i];
- }
- }
- public void findDiscrepancy(){
- discrepancy = new double [row];
- for(int i = 0; i < row; ++i){
- for(int j = 0; j < col - 1; ++j){
- discrepancy[i] += augmentedMatrix[i][j] * x[j];
- }
- discrepancy[i] -= augmentedMatrix[i][col - 1];
- }
- }
- public void discrepancyNorm(){
- normDiscrepancy = 0;
- for(int i = 0; i < row; ++i){
- normDiscrepancy = normDiscrepancy + Math.abs(discrepancy[i]);
- }
- System.out.printf("Норма r = %.5e\n", normDiscrepancy);
- }
- public void printAugmentedMatrix(){
- System.out.println("Матрица системы:");
- for(int i = 0; i < row; ++i){
- for(int j = 0; j < col; ++j){
- if(j == col - 1){
- System.out.printf("|%8.4f", augmentedMatrix[i][j]);
- }
- else {
- System.out.printf("%8.4f", augmentedMatrix[i][j]);
- }
- }
- System.out.println();
- }
- }
- public void printX() {
- System.out.println("Вектор решений:");
- Arrays.stream(x).forEach(item -> System.out.printf("%8.5f ", item));
- System.out.println();
- }
- public void printDiscrepancy(){
- System.out.println("Вектор невязки:");
- Arrays.stream(discrepancy).forEach(item -> System.out.printf("%15.5e\n", item));
- }
- public void printCountOfIterations(){
- System.out.println("Количество итераций:");
- System.out.println(countOfIterations);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement