Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ml.svischuk.matrix;
- import java.util.Arrays;
- public class Matrix {
- private double array[][];
- private int columns;
- private int rows;
- private int size;
- public Matrix(double src[][]) {
- if (src == null) {
- throw new IllegalArgumentException("src == null");
- }
- if (src.length == 0) {
- throw new IllegalArgumentException("src.length == 0");
- }
- for (int i = 0; i < src.length - 1; i++) {
- if (src[i] == null || src[i + 1] == null) {
- throw new IllegalArgumentException("src[" + i + "] == null");
- }
- if (src[i].length == 0) {
- throw new IllegalArgumentException("src" + i + ".length == 0");
- }
- if (src[i].length != src[i + 1].length) {
- throw new IllegalArgumentException(
- "src[" + i + "].length != " + "src[" + (i + 1) +
- "].length");
- }
- }
- array = new double[src.length][];
- rows = src.length;
- columns = src[0].length;
- size = rows * columns;
- for (int i = 0; i < src.length; i++) {
- array[i] = src[i].clone();
- }
- }
- public double findMin() {
- double result = array[0][0];
- for (double row[] : array) {
- for (double element : row) {
- if (result > element) {
- result = element;
- }
- }
- }
- return result;
- }
- public double findMax() {
- double result = array[0][0];
- for (double row[] : array) {
- for (double element : row) {
- if (result < element) {
- result = element;
- }
- }
- }
- return result;
- }
- public double findArithMean() {
- double result = 0;
- for (double row[] : array) {
- for (double element : row) {
- result += element;
- }
- }
- return result / size;
- }
- public double findGeomMean() {
- double result = 1;
- for (double row[] : array) {
- for (double element : row) {
- result *= element;
- }
- }
- return Math.pow(result, 1.0 / size);
- }
- public int findLocalMin() {
- return findLocal(this::isLessThen);
- }
- public int findLocalMax() {
- return findLocal(this::isGreaterThen);
- }
- public int findLocal(LocalComparator comp) {
- int result;
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < columns; col++) {
- result = row * columns + col;
- double value = array[row][col];
- int rowP = row - 1;
- int rowN = row + 1;
- int colP = col - 1;
- int colN = col + 1;
- boolean lastRow = row == rowN;
- boolean lastCol = col == colN;
- if (row == 0) {
- if (col == 0) {
- if (lastRow) {
- if (lastCol) {
- return result;
- } else if (comp
- .compareAll(value, array[row][colN])) {
- return result;
- }
- } else if (lastCol) {
- if (comp.compareAll(value, array[rowN][col])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colN],
- array[rowN][col], array[rowN][colN])) {
- return result;
- }
- } else if (lastRow) {
- if (lastCol) {
- if (comp.compareAll(value, array[row][colP])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colN],
- array[row][colP])) {
- return result;
- }
- } else if (lastCol) {
- if (comp.compareAll(value, array[row][colP],
- array[rowN][col], array[rowN][colP])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colP],
- array[rowN][col], array[rowN][colP],
- array[row][colN], array[rowN][colN])) {
- return result;
- }
- } else if (col == 0) {
- if (lastRow) {
- if (lastCol) {
- if (comp.compareAll(value, array[rowP][col])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colN],
- array[rowP][col], array[rowP][colN])) {
- return result;
- }
- } else if (lastCol) {
- if (comp.compareAll(value, array[rowP][col],
- array[rowN][col])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colN],
- array[rowN][col], array[rowN][colN],
- array[rowP][col], array[rowP][colN])) {
- return result;
- }
- } else if (lastRow) {
- if (lastCol) {
- if (comp.compareAll(value, array[row][colP],
- array[rowP][colP], array[rowP][col])) {
- return result;
- }
- } else if (comp.compareAll(value, array[row][colP],
- array[rowP][colP], array[rowP][colP],
- array[rowP][col], array[rowP][colN],
- array[row][colN])) {
- return result;
- }
- } else if (lastCol) {
- if (comp.compareAll(value, array[rowP][col],
- array[rowN][col], array[rowP][colP],
- array[row][colP], array[rowN][colP])) {
- return result;
- }
- } else if (comp
- .compareAll(value, array[rowP][col], array[rowN][col],
- array[rowP][colP], array[row][colP],
- array[rowN][colP], array[rowN][colN],
- array[row][colN], array[rowP][colN])) {
- return result;
- }
- }
- }
- return -1;
- }
- public Matrix transpose() {
- double temp[][] = new double[columns][rows];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- temp[j][i] = array[i][j];
- }
- }
- array = temp;
- return this;
- }
- public int getColumns() {
- return columns;
- }
- public int getRows() {
- return rows;
- }
- public int getSize() {
- return size;
- }
- @Override
- public String toString() {
- StringBuilder result = new StringBuilder();
- for (double row[] : array) {
- result.append(Arrays.toString(row)).append("\n");
- }
- return result.toString();
- }
- private boolean isLessThen(double... args) {
- for (int i = 1; i < args.length; i++) {
- if (args[0] >= args[i]) {
- return false;
- }
- }
- return true;
- }
- private boolean isGreaterThen(double... args) {
- for (int i = 1; i < args.length; i++) {
- if (args[0] <= args[i]) {
- return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement