Advertisement
libli

Untitled

Feb 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.99 KB | None | 0 0
  1. package ru.omsu.imit.kr.Matrix;
  2.  
  3. import com.sun.javafx.geom.transform.SingularMatrixException;
  4.  
  5. import java.util.Arrays;
  6.  
  7. /**
  8.  * Created by User on 15.02.2017.
  9.  */
  10. public class Matrix implements IMatrix {
  11.     private int n; // размер
  12.     private double matr[]; // сам массив
  13.  
  14.  
  15.     public Matrix(int n) throws SingularMatrixException { // конструктор по размеру
  16.         if (n < 0) {
  17.             throw new SingularMatrixException();
  18.         }
  19.         this.n = n;
  20.         matr = new double[n * n];
  21.     }
  22.  
  23.     public Matrix(double[] arr) throws SingularMatrixException { //конструктор копирования
  24.             if(Math.sqrt(arr.length) % 1 == 0){
  25.                 n = new Double(Math.sqrt(arr.length)).intValue();
  26.                 System.arraycopy(arr,0,matr, 0,arr.length);
  27.             }
  28.         }
  29.  
  30.     @Override
  31.     public boolean equals(Object o) {
  32.         if (this == o) return true;
  33.         if (o == null || getClass() != o.getClass()) return false;
  34.  
  35.         Matrix matrix = (Matrix) o;
  36.  
  37.         if (n != matrix.n) return false;
  38.         return Arrays.equals(matr, matrix.matr);
  39.  
  40.     }
  41.  
  42.     @Override
  43.     public int getLength() {
  44.         return matr.length;
  45.     }
  46.  
  47.     @Override
  48.     public int hashCode() {
  49.         int result = n;
  50.         result = 31 * result + Arrays.hashCode(matr);
  51.         return result;
  52.     }
  53.  
  54.     @Override
  55.     public double getElem(int i, int j) {
  56.         return matr[n * i + j];
  57.     }
  58.  
  59.     public void setN(int n) {
  60.         this.n = n;
  61.     }
  62.  
  63.     @Override
  64.     public void setElem(int i, int j, double elem) throws SingularMatrixException {
  65.         if ((i < 0) || (i > n) || (j < 0) || (j > n)) {
  66.             throw new SingularMatrixException();
  67.         }
  68.         this.matr[n * i + j] = elem;
  69.     }
  70.  
  71.  
  72.     public double getDeg() {
  73.         double deg = 1;
  74.         Matrix resmatr = new Matrix(matr);
  75.         //копия матрицы
  76.  
  77.         for (int k = 0; k < matr.length; k++) { //кол-во зануленных столбцов
  78.             //проверка на 0
  79.             for (int i = k + 1; i < matr.length; i++) { //по строке
  80.                 for (int j = k; j < matr.length; j++) {  // по столбцу
  81.                     double res = resmatr.getElem(i, j) - ((resmatr.getElem(i, k) * resmatr.getElem(k, j)) / resmatr.getElem(k, k));
  82.                     resmatr.setElem(i, j, res);
  83.                 }
  84.             }
  85.         }
  86.         for (int x = 0; x < matr.length; x++) {
  87.             deg = deg * resmatr.getElem(x, x); // элементики по диагонали
  88.         }
  89.         return deg;
  90.     }
  91.  
  92.  
  93.   /*  функция умножения строки на модуль
  94.     public double[] Coef(int x, int y, double[]str, double[]str1) {
  95.         double elem1=x/y;
  96.         return
  97.     }
  98.     @Override
  99.     public double getDeg() {
  100.         double deg;
  101.         for (int i = 0; i < (matr.length-1); i++){
  102.  
  103.         }
  104.        return deg;
  105.     }
  106. */
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement