Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Gauss_solution;
- import java.io.*;
- import java.util.Arrays;
- class Gauss
- {
- double[][] A; //коэффициенты при Х
- double[] B; //свободные члены
- double[] X; //сами неизвестные
- int n; //кол-во уравнений
- int m; //кол-во Х
- public Gauss (double[][]A, double[]B, double[]X)
- {
- this.A = A.clone();
- this.B = B.clone();
- this.X = X;
- this.n = A.length;
- this.m = A[0].length;
- if (n>20) throw new ArithmeticException("No more than 20 lines");
- }
- public void TriangMatr() //приведение матрицы к трегуольному виду
- {
- for (int i=0;i<m;i++)
- {
- for(int k=i; k<n-1;k++)
- {
- if (0!= A[i][i] ) //операции с матрицей коэффициентов
- {
- double tt = A[k+1][i]/A[i][i];
- if (tt==0) continue;
- for (int j=0; j<m; j++)
- {
- A[i][j] *=tt;
- A[k+1][j]-=A[i][j];
- }
- A[k+1][i]=0;
- B[i]*=tt; //операции с матрицей свободных членов
- B[k+1]-=B[i];
- }
- else if (i!= n-1)
- {
- for (int j=0; j<m; j++)
- {
- double temp = A[i][j];
- A[i][j] = A[i+1][j];
- A[i+1][j] = temp;
- }
- double temp = B[i];
- B[i] = B[i+1];
- B[i+1] = temp;
- i--;
- }
- }
- }
- }
- public double Determinant(double[][] matrix) throws ArithmeticException
- {
- double res = 0;
- if (matrix.length == 1)
- {
- res = matrix [0][0];
- if (res == 0) throw new ArithmeticException("Determinant is 0. No solutions");
- return res;
- }
- if(matrix.length == 2)
- {
- res = matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0];
- if (res == 0) throw new ArithmeticException("Determinant is 0. No solutions");
- return res;
- }
- for (int i=0; i<matrix[0].length; i++)
- {
- double temp[][] = new double[matrix.length-1][matrix[0].length-1];
- for (int j=1; j < matrix.length; j++)
- {
- System.arraycopy(matrix[j], 0, temp[j-1], 0, i);
- System.arraycopy(matrix[j], i+1, temp[j-1], i, matrix[0].length-i-1);
- }
- res +=matrix[0][i]*Math.pow(-1, i)*Determinant(temp);
- }
- if (res == 0) throw new ArithmeticException("Determinant is 0. No solutions");
- return res;
- }
- public double[] solution() throws ArithmeticException
- {
- B[m-1] /= A[n-1][m-1];
- A[n-1][m-1] /= A[n-1][m-1];
- for (int i = m-2; i >= 0; i--)
- {
- if (A[i][i] == 0)
- {throw new ArithmeticException("No solutions");}
- for (int j=i; j >= 0; j--)
- {
- B[j] -= (B[i+1]*A[j][i+1]);
- A[j][i+1] = 0.0;
- }
- B[i] /= A[i][i];
- A[i][i]=1;
- }
- X = B;
- return this.X;
- }
- }
- public class Gauss_Solution
- {
- static double[][] A1;
- static double[] B;
- public static double[][] arraycopy(double[][] array)
- {
- double[][] Newarr = new double [array.length][];
- for (int i=0; i< array.length; i++)
- {
- Newarr[i] = Arrays.copyOf(array[i], array[i].length);
- }
- return Newarr;
- }
- public static double rounding(double k, int accuracy)
- {
- accuracy = (int) Math.pow(10, accuracy);
- //accuracy = 10^accuracy;
- k = k*accuracy;
- int i = (int) Math.round(k);
- return (double) i/accuracy;
- }
- // @SuppressWarnings("empty-statement")
- public static void main (String[] args)
- throws IOException, FileNotFoundException
- {
- try
- {
- System.out.println("Enter 'P' to write path to matrix or\n "
- + "'I' to enter a matrix by yourself\n"
- + "press 'Q' to exit:");
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String str = stdin.readLine();
- if (str.equals("P"))
- {
- readFile(str);
- }
- else if (str.equals("I"))
- {
- String line = null;
- String text = "";
- System.out.println("Enter expanded matrix:");
- line = stdin.readLine();
- while (!line.equals("Q"))
- {
- text+=line+"\n";
- line = stdin.readLine();
- }
- ToMatrix(text);
- }
- else
- System.out.println("Incorrect character!");
- double[] X = new double[B.length];
- Gauss abc = new Gauss(arraycopy(A1),B,X);
- try
- {
- double Det = abc.Determinant(abc.A);
- System.out.println("Matrix' determinant = "+ Det);
- }
- catch (ArithmeticException e)
- {
- System.out.println(e.getMessage());
- System.exit(0);
- }
- abc.TriangMatr();
- System.out.println("Triangle matrix:");
- for (int i = 0; i < abc.A.length; i++)
- {
- double[] elem = abc.A[i];
- for (double n: elem)
- {
- System.out.print(rounding(n,2)+"\t");
- }
- System.out.print(rounding(B[i],2));
- System.out.println();
- }
- System.out.println("Solutions: ");
- try
- {
- X = abc.solution();
- }
- catch(ArithmeticException e)
- {
- System.out.println(e.getMessage());
- }
- for(double elem : X)
- {
- System.out.println(rounding(elem, 2));
- }
- System.out.println("Column of discrepancies ");
- for (int i=0; i < X.length;i++)
- {
- double n = 0;
- for(int j=0 ; j< X.length;j++)
- {
- n += (X[j]*A1[i][j]);
- }
- System.out.println(B[i]-n);
- }
- }
- catch(ArithmeticException e)
- {
- System.out.println(e);;
- System.exit(0);
- }
- catch(NumberFormatException e)
- {
- System.out.println("Incorrect input");
- System.exit(0);
- }
- }
- public static void ToMatrix(String str)
- throws ArithmeticException
- {
- String[] lines = str.split("\\r?\\n");
- B = new double[lines.length];
- A1 = new double[lines.length][lines.length];
- for(int i = 0; i < lines.length; i++)
- {
- String[] ch = lines[i].split(" ");
- if(lines.length != ch.length-1) throw new ArithmeticException
- ("Number of lines must be equal to variables");
- for(int k=0; k < ch.length; k++)
- {
- if( k < lines.length)
- {
- A1[i][k] = Double.parseDouble(ch[k].replaceAll("", ""));
- }
- else
- {
- B[i] = Double.parseDouble(ch[k].replaceAll("", ""));
- }
- }
- }
- }
- public static void readFile(String path)
- throws FileNotFoundException, IOException
- {
- File file = new File( path );
- BufferedReader br = new BufferedReader (new InputStreamReader
- (new FileInputStream( file ), "UTF-8"));
- String line = null;
- String text = "";
- while ((line = br.readLine()) != null)
- {
- text += line +"\n";
- }
- br.close();
- ToMatrix(text);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement