Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.epam.rd.qa.basicio;
- /**
- * Encapsulates two-dimensional array-matrix block of real ({@code double}) type.
- * {@code Matrix} is the cover for two-dimensional array of real values, storing matrix
- * values with operations of matrix addition, deduction, and multiplication.
- */
- public class Matrix {
- private final double[][] matrix;
- private final int rows;
- private final int columns;
- /**
- * Creates an empty matrix with predetermined number
- * of rows and columns (all values in matrix equal to 0)
- *
- * @param rows number of rows
- * @param cols number of columns
- * @throws MatrixException if {@code rows} or {@code cols} less than 1
- */
- public Matrix(int rows, int cols) throws MatrixException {
- if (rows < 1 || cols < 1) {
- throw new MatrixException("Rows and columns must be greater than 0");
- }
- this.rows = rows;
- this.columns = cols;
- this.matrix = new double[rows][cols];
- }
- /**
- * Creates a matrix based on existing two-dimensional array
- *
- * @param values two-dimensional array
- * @throws MatrixException if {@code rows} or {@code cols} less than 1
- */
- public Matrix(double[][] values) throws MatrixException {
- if (values.length < 1 || values[0].length < 1) {
- throw new MatrixException("Rows and columns must be greater than 0");
- }
- this.rows = values.length;
- this.columns = values[0].length;
- this.matrix = new double[rows][columns];
- for (int i = 0; i < rows; i++) {
- if (values[i].length != columns) {
- throw new MatrixException("All rows must have the same number of columns");
- }
- System.arraycopy(values[i], 0, this.matrix[i], 0, columns);
- }
- }
- /**
- * Returns count of matrix rows.
- *
- * @return count of rows in the matrix
- */
- public int getRows() {
- return rows;
- }
- /**
- * Returns count of matrix columns
- *
- * @return count of columns in the matrix
- */
- public int getColumns() {
- return columns;
- }
- /**
- * Returns an element via predetermined correct indexes.
- *
- * @param row row index
- * @param col column index
- * @return the element via indexes
- * @throws MatrixException if index out of bounds
- */
- public double get(int row, int col) throws MatrixException {
- if (row < 0 || row >= rows || col < 0 || col >= columns) {
- throw new MatrixException("Index out of bounds");
- }
- return matrix[row][col];
- }
- /**
- * Sets new value via predetermined correct indexes.
- *
- * @param row row index
- * @param col column index
- * @param value value to set
- * @throws MatrixException if index out of bounds
- */
- public void set(int row, int col, double value) throws MatrixException {
- if (row < 0 || row >= rows || col < 0 || col >= columns) {
- throw new MatrixException("Index out of bounds");
- }
- matrix[row][col] = value;
- }
- /**
- * Returns standard two-dimensional array out of matrix.
- * Any changes in the returned array will be reflected to internal array.
- *
- * @return matrix values
- */
- public double[][] toArray() {
- double[][] result = new double[rows][columns];
- for (int i = 0; i < rows; i++) {
- System.arraycopy(matrix[i], 0, result[i], 0, columns);
- }
- return result;
- }
- /**
- * Adds all elements of {@code other} matrix to corresponding elements
- * of this matrix and creates new {@code Matrix} with resulting two-dimensional array
- *
- * @param other another {@code Matrix} object
- * @return new matrix
- * @throws MatrixException if matrices have different size
- */
- public Matrix add(Matrix other) throws MatrixException {
- if (this.rows != other.rows || this.columns != other.columns) {
- throw new MatrixException("Matrices must have the same dimensions");
- }
- double[][] result = new double[rows][columns];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- result[i][j] = this.matrix[i][j] + other.matrix[i][j];
- }
- }
- return new Matrix(result);
- }
- /**
- * Subtract all elements of {@code other} matrix from corresponding elements
- * of this matrix and creates new {@code Matrix} with resulting two-dimensional array
- *
- * @param other another {@code Matrix} object
- * @return new matrix
- * @throws MatrixException if matrices have different size
- */
- public Matrix subtract(Matrix other) throws MatrixException {
- if (this.rows != other.rows || this.columns != other.columns) {
- throw new MatrixException("Matrices must have the same dimensions");
- }
- double[][] result = new double[rows][columns];
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- result[i][j] = this.matrix[i][j] - other.matrix[i][j];
- }
- }
- return new Matrix(result);
- }
- /**
- * Multiply this matrix to {@code other} matrix.<br/>
- * See
- * <a href="https://en.wikipedia.org/wiki/Matrix_multiplication">Matrix multiplication</a>
- * <a href="https://en.wikipedia.org/wiki/Matrix_multiplication#Definition">Matrix multiplication (definition)</a>
- *
- * @param other another {@code Matrix} object
- * @return new matrix
- * @throws MatrixException if columns number of {@code this} matrix is not equal to rows num ber of {@code other} matrix
- */
- public Matrix multiply(Matrix other) throws MatrixException {
- if (this.columns != other.rows) {
- throw new MatrixException("Number of columns in the first matrix must be equal to the number of rows in the second matrix");
- }
- double[][] result = new double[this.rows][other.columns];
- for (int i = 0; i < this.rows; i++) {
- for (int j = 0; j < other.columns; j++) {
- double sum = 0;
- for (int k = 0; k < this.columns; k++) {
- sum += this.matrix[i][k] * other.matrix[k][j];
- }
- result[i][j] = sum;
- }
- }
- return new Matrix(result);
- }
- /**
- * Exception class for Matrix-related issues.
- */
- public static class MatrixException extends Exception {
- public MatrixException(String message) {
- super(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement