Advertisement
AlexandrP

Matrix.java

Apr 4th, 2024
753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.67 KB | None | 0 0
  1. package com.epam.rd.qa.basicio;
  2.  
  3. /**
  4.  * Encapsulates two-dimensional array-matrix block of real ({@code double}) type.
  5.  * {@code Matrix} is the cover for two-dimensional array of real values, storing matrix
  6.  * values with operations of matrix addition, deduction, and multiplication.
  7.  */
  8. public class Matrix {
  9.  
  10.     private final double[][] matrix;
  11.     private final int rows;
  12.     private final int columns;
  13.  
  14.     /**
  15.      * Creates an empty matrix with predetermined number
  16.      * of rows and columns (all values in matrix equal to 0)
  17.      *
  18.      * @param rows number of rows
  19.      * @param cols number of columns
  20.      * @throws MatrixException if {@code rows} or {@code cols} less than 1
  21.      */
  22.     public Matrix(int rows, int cols) throws MatrixException {
  23.         if (rows < 1 || cols < 1) {
  24.             throw new MatrixException("Rows and columns must be greater than 0");
  25.         }
  26.         this.rows = rows;
  27.         this.columns = cols;
  28.         this.matrix = new double[rows][cols];
  29.     }
  30.  
  31.     /**
  32.      * Creates a matrix based on existing two-dimensional array
  33.      *
  34.      * @param values two-dimensional array
  35.      * @throws MatrixException if {@code rows} or {@code cols} less than 1
  36.      */
  37.     public Matrix(double[][] values) throws MatrixException {
  38.         if (values.length < 1 || values[0].length < 1) {
  39.             throw new MatrixException("Rows and columns must be greater than 0");
  40.         }
  41.         this.rows = values.length;
  42.         this.columns = values[0].length;
  43.         this.matrix = new double[rows][columns];
  44.         for (int i = 0; i < rows; i++) {
  45.             if (values[i].length != columns) {
  46.                 throw new MatrixException("All rows must have the same number of columns");
  47.             }
  48.             System.arraycopy(values[i], 0, this.matrix[i], 0, columns);
  49.         }
  50.     }
  51.  
  52.     /**
  53.      * Returns count of matrix rows.
  54.      *
  55.      * @return count of rows in the matrix
  56.      */
  57.     public int getRows() {
  58.         return rows;
  59.     }
  60.  
  61.     /**
  62.      * Returns count of matrix columns
  63.      *
  64.      * @return count of columns in the matrix
  65.      */
  66.     public int getColumns() {
  67.         return columns;
  68.     }
  69.  
  70.     /**
  71.      * Returns an element via predetermined correct indexes.
  72.      *
  73.      * @param row row index
  74.      * @param col column index
  75.      * @return the element via indexes
  76.      * @throws MatrixException if index out of bounds
  77.      */
  78.     public double get(int row, int col) throws MatrixException {
  79.         if (row < 0 || row >= rows || col < 0 || col >= columns) {
  80.             throw new MatrixException("Index out of bounds");
  81.         }
  82.         return matrix[row][col];
  83.     }
  84.  
  85.     /**
  86.      * Sets new value via predetermined correct indexes.
  87.      *
  88.      * @param row   row index
  89.      * @param col   column index
  90.      * @param value value to set
  91.      * @throws MatrixException if index out of bounds
  92.      */
  93.     public void set(int row, int col, double value) throws MatrixException {
  94.         if (row < 0 || row >= rows || col < 0 || col >= columns) {
  95.             throw new MatrixException("Index out of bounds");
  96.         }
  97.         matrix[row][col] = value;
  98.     }
  99.  
  100.     /**
  101.      * Returns standard two-dimensional array out of matrix.
  102.      * Any changes in the returned array will be reflected to internal array.
  103.      *
  104.      * @return matrix values
  105.      */
  106.     public double[][] toArray() {
  107.         double[][] result = new double[rows][columns];
  108.         for (int i = 0; i < rows; i++) {
  109.             System.arraycopy(matrix[i], 0, result[i], 0, columns);
  110.         }
  111.         return result;
  112.     }
  113.  
  114.     /**
  115.      * Adds all elements of {@code other} matrix to corresponding elements
  116.      * of this matrix and creates new {@code Matrix} with resulting two-dimensional array
  117.      *
  118.      * @param other another {@code Matrix} object
  119.      * @return new matrix
  120.      * @throws MatrixException if matrices have different size
  121.      */
  122.     public Matrix add(Matrix other) throws MatrixException {
  123.         if (this.rows != other.rows || this.columns != other.columns) {
  124.             throw new MatrixException("Matrices must have the same dimensions");
  125.         }
  126.         double[][] result = new double[rows][columns];
  127.         for (int i = 0; i < rows; i++) {
  128.             for (int j = 0; j < columns; j++) {
  129.                 result[i][j] = this.matrix[i][j] + other.matrix[i][j];
  130.             }
  131.         }
  132.         return new Matrix(result);
  133.     }
  134.  
  135.     /**
  136.      * Subtract all elements of {@code other} matrix from corresponding elements
  137.      * of this matrix and creates new {@code Matrix} with resulting two-dimensional array
  138.      *
  139.      * @param other another {@code Matrix} object
  140.      * @return new matrix
  141.      * @throws MatrixException if matrices have different size
  142.      */
  143.     public Matrix subtract(Matrix other) throws MatrixException {
  144.         if (this.rows != other.rows || this.columns != other.columns) {
  145.             throw new MatrixException("Matrices must have the same dimensions");
  146.         }
  147.         double[][] result = new double[rows][columns];
  148.         for (int i = 0; i < rows; i++) {
  149.             for (int j = 0; j < columns; j++) {
  150.                 result[i][j] = this.matrix[i][j] - other.matrix[i][j];
  151.             }
  152.         }
  153.         return new Matrix(result);
  154.     }
  155.  
  156.     /**
  157.      * Multiply this matrix to {@code other} matrix.<br/>
  158.      * See
  159.      * <a href="https://en.wikipedia.org/wiki/Matrix_multiplication">Matrix multiplication</a>
  160.      * <a href="https://en.wikipedia.org/wiki/Matrix_multiplication#Definition">Matrix multiplication (definition)</a>
  161.      *
  162.      * @param other another {@code Matrix} object
  163.      * @return new matrix
  164.      * @throws MatrixException if columns number of {@code this} matrix is not equal to rows num ber of {@code other} matrix
  165.      */
  166.     public Matrix multiply(Matrix other) throws MatrixException {
  167.         if (this.columns != other.rows) {
  168.             throw new MatrixException("Number of columns in the first matrix must be equal to the number of rows in the second matrix");
  169.         }
  170.         double[][] result = new double[this.rows][other.columns];
  171.         for (int i = 0; i < this.rows; i++) {
  172.             for (int j = 0; j < other.columns; j++) {
  173.                 double sum = 0;
  174.                 for (int k = 0; k < this.columns; k++) {
  175.                     sum += this.matrix[i][k] * other.matrix[k][j];
  176.                 }
  177.                 result[i][j] = sum;
  178.             }
  179.         }
  180.         return new Matrix(result);
  181.     }
  182.  
  183.     /**
  184.      * Exception class for Matrix-related issues.
  185.      */
  186.     public static class MatrixException extends Exception {
  187.         public MatrixException(String message) {
  188.             super(message);
  189.         }
  190.     }
  191. }
  192.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement