Advertisement
Guest User

Untitled

a guest
Sep 27th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.39 KB | None | 0 0
  1. package ml.svischuk.matrix;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Matrix {
  6.     private double array[][];
  7.     private int columns;
  8.     private int rows;
  9.     private int size;
  10.    
  11.     public Matrix(double src[][]) {
  12.        
  13.         if (src == null) {
  14.             throw new IllegalArgumentException("src == null");
  15.         }
  16.         if (src.length == 0) {
  17.             throw new IllegalArgumentException("src.length == 0");
  18.         }
  19.         for (int i = 0; i < src.length - 1; i++) {
  20.             if (src[i] == null || src[i + 1] == null) {
  21.                 throw new IllegalArgumentException("src[" + i + "] == null");
  22.             }
  23.             if (src[i].length == 0) {
  24.                 throw new IllegalArgumentException("src" + i + ".length == 0");
  25.             }
  26.             if (src[i].length != src[i + 1].length) {
  27.                 throw new IllegalArgumentException(
  28.                         "src[" + i + "].length != " + "src[" + (i + 1) +
  29.                                 "].length");
  30.             }
  31.         }
  32.        
  33.         array = new double[src.length][];
  34.         rows = src.length;
  35.         columns = src[0].length;
  36.         size = rows * columns;
  37.        
  38.         for (int i = 0; i < src.length; i++) {
  39.             array[i] = src[i].clone();
  40.         }
  41.     }
  42.    
  43.     public double findMin() {
  44.         double result = array[0][0];
  45.        
  46.         for (double row[] : array) {
  47.             for (double element : row) {
  48.                 if (result > element) {
  49.                     result = element;
  50.                 }
  51.             }
  52.         }
  53.         return result;
  54.     }
  55.    
  56.     public double findMax() {
  57.         double result = array[0][0];
  58.        
  59.         for (double row[] : array) {
  60.             for (double element : row) {
  61.                 if (result < element) {
  62.                     result = element;
  63.                 }
  64.             }
  65.         }
  66.         return result;
  67.     }
  68.    
  69.     public double findArithMean() {
  70.         double result = 0;
  71.        
  72.         for (double row[] : array) {
  73.             for (double element : row) {
  74.                 result += element;
  75.             }
  76.         }
  77.         return result / size;
  78.     }
  79.    
  80.     public double findGeomMean() {
  81.         double result = 1;
  82.        
  83.         for (double row[] : array) {
  84.             for (double element : row) {
  85.                 result *= element;
  86.             }
  87.         }
  88.         return Math.pow(result, 1.0 / size);
  89.     }
  90.    
  91.     public int findLocalMin() {
  92.         return findLocal(this::isLessThen);
  93.     }
  94.    
  95.     public int findLocalMax() {
  96.         return findLocal(this::isGreaterThen);
  97.     }
  98.    
  99.     public int findLocal(LocalComparator comp) {
  100.         int result;
  101.        
  102.         for (int row = 0; row < rows; row++) {
  103.             for (int col = 0; col < columns; col++) {
  104.                 result = row * columns + col;
  105.                 double value = array[row][col];
  106.                 int rowP = row - 1;
  107.                 int rowN = row + 1;
  108.                 int colP = col - 1;
  109.                 int colN = col + 1;
  110.                 boolean lastRow = row == rowN;
  111.                 boolean lastCol = col == colN;
  112.                
  113.                 if (row == 0) {
  114.                     if (col == 0) {
  115.                         if (lastRow) {
  116.                             if (lastCol) {
  117.                                 return result;
  118.                             } else if (comp
  119.                                     .compareAll(value, array[row][colN])) {
  120.                                 return result;
  121.                             }
  122.                         } else if (lastCol) {
  123.                             if (comp.compareAll(value, array[rowN][col])) {
  124.                                 return result;
  125.                             }
  126.                         } else if (comp.compareAll(value, array[row][colN],
  127.                                 array[rowN][col], array[rowN][colN])) {
  128.                             return result;
  129.                         }
  130.                     } else if (lastRow) {
  131.                         if (lastCol) {
  132.                             if (comp.compareAll(value, array[row][colP])) {
  133.                                 return result;
  134.                             }
  135.                         } else if (comp.compareAll(value, array[row][colN],
  136.                                 array[row][colP])) {
  137.                             return result;
  138.                         }
  139.                     } else if (lastCol) {
  140.                         if (comp.compareAll(value, array[row][colP],
  141.                                 array[rowN][col], array[rowN][colP])) {
  142.                             return result;
  143.                         }
  144.                     } else if (comp.compareAll(value, array[row][colP],
  145.                             array[rowN][col], array[rowN][colP],
  146.                             array[row][colN], array[rowN][colN])) {
  147.                         return result;
  148.                     }
  149.                 } else if (col == 0) {
  150.                     if (lastRow) {
  151.                         if (lastCol) {
  152.                             if (comp.compareAll(value, array[rowP][col])) {
  153.                                 return result;
  154.                             }
  155.                         } else if (comp.compareAll(value, array[row][colN],
  156.                                 array[rowP][col], array[rowP][colN])) {
  157.                             return result;
  158.                         }
  159.                     } else if (lastCol) {
  160.                         if (comp.compareAll(value, array[rowP][col],
  161.                                 array[rowN][col])) {
  162.                             return result;
  163.                         }
  164.                     } else if (comp.compareAll(value, array[row][colN],
  165.                             array[rowN][col], array[rowN][colN],
  166.                             array[rowP][col], array[rowP][colN])) {
  167.                         return result;
  168.                     }
  169.                 } else if (lastRow) {
  170.                     if (lastCol) {
  171.                         if (comp.compareAll(value, array[row][colP],
  172.                                 array[rowP][colP], array[rowP][col])) {
  173.                             return result;
  174.                         }
  175.                     } else if (comp.compareAll(value, array[row][colP],
  176.                             array[rowP][colP], array[rowP][colP],
  177.                             array[rowP][col], array[rowP][colN],
  178.                             array[row][colN])) {
  179.                         return result;
  180.                     }
  181.                 } else if (lastCol) {
  182.                     if (comp.compareAll(value, array[rowP][col],
  183.                             array[rowN][col], array[rowP][colP],
  184.                             array[row][colP], array[rowN][colP])) {
  185.                         return result;
  186.                     }
  187.                 } else if (comp
  188.                         .compareAll(value, array[rowP][col], array[rowN][col],
  189.                                 array[rowP][colP], array[row][colP],
  190.                                 array[rowN][colP], array[rowN][colN],
  191.                                 array[row][colN], array[rowP][colN])) {
  192.                     return result;
  193.                 }
  194.             }
  195.         }
  196.         return -1;
  197.     }
  198.    
  199.     public Matrix transpose() {
  200.         double temp[][] = new double[columns][rows];
  201.         for (int i = 0; i < rows; i++) {
  202.             for (int j = 0; j < columns; j++) {
  203.                 temp[j][i] = array[i][j];
  204.             }
  205.         }
  206.         array = temp;
  207.         return this;
  208.     }
  209.    
  210.     public int getColumns() {
  211.         return columns;
  212.     }
  213.    
  214.     public int getRows() {
  215.         return rows;
  216.     }
  217.    
  218.     public int getSize() {
  219.         return size;
  220.     }
  221.    
  222.     @Override
  223.     public String toString() {
  224.         StringBuilder result = new StringBuilder();
  225.         for (double row[] : array) {
  226.             result.append(Arrays.toString(row)).append("\n");
  227.         }
  228.         return result.toString();
  229.     }
  230.    
  231.     private boolean isLessThen(double... args) {
  232.         for (int i = 1; i < args.length; i++) {
  233.             if (args[0] >= args[i]) {
  234.                 return false;
  235.             }
  236.         }
  237.         return true;
  238.     }
  239.    
  240.     private boolean isGreaterThen(double... args) {
  241.         for (int i = 1; i < args.length; i++) {
  242.             if (args[0] <= args[i]) {
  243.                 return false;
  244.             }
  245.         }
  246.         return true;
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement