Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.kul.gaweda;
- import java.util.ArrayList;
- import java.util.Random;
- public class Matrix {
- protected double[] _matrix = null;
- private int _rowCount = 0;
- private int _colCount = 0;
- private ArrayList _matrixList = null;
- private Random _rng = new Random();
- /**
- *Initialises the matrix based on the specified array.
- * The matrix created will contain a single row.
- * @param array
- */
- public Matrix(double[] array) {
- int rows = 1;
- int cols = array.length;
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- _matrix = array;
- }
- /**
- * Initialises the matrix based on the specified array.
- * The matrix created will contain a single row.
- * @param array
- */
- public Matrix(boolean[] array) {
- int rows = 1;
- int cols = array.length;
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- _matrix = ConvertToDoubleBool(array);
- }
- /**
- * Initialises the matrix based on the specified array.
- * If the columnMatrix parameter is set to true,
- * the matrix created will contain a single column.
- * @param array
- * @param columnMatrix
- *
- */
- public Matrix(double[] array, boolean columnMatrix) {
- int cols = 1;
- int rows = 1;
- if (columnMatrix) {
- rows = array.length;
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- } else {
- cols = array.length;
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- _matrix = array;
- }
- /**
- * Initialises the matrix based on the specified array.
- * If the columnMatrix parameter is set to false, the matrix created will contain a single row.
- * If the coulmnMatrix parameter is set to true, the matrix created will contain a single column.
- * @param array
- * @param columnMatrix
- */
- public Matrix(boolean[] array, boolean columnMatrix) {
- int cols = 1;
- int rows = 1;
- if (columnMatrix) {
- rows = array.length;
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- } else {
- cols = array.length;
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- _matrix = ConvertToDoubleBool(array);
- }
- /**
- * Initialises the matrix based on the specified array. Dimension 0 of the array will
- * be represented as Rows and dimension 1 of the array will be represented as Columns.
- * @param array
- */
- public Matrix(double[][] array) {
- int rows = array.length;
- int cols = array[0].length;
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- //populate the matrix (cant use array copy as the arrays are different dimensions)
- _matrix = new double[rows * cols];
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- _matrix[row * cols + col] = array[row][col];
- }
- }
- }
- /**
- * Initialises the matrix based on the specified array. Dimension 0 of the array will
- * be represented as Rows and dimension 1 of the array will be represented as Columns.
- * @param array
- */
- public Matrix(boolean[][] array) {
- int rows = array.length;
- int cols = array[0].length;
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- //populate the matrix (cant use array copy as the arrays are different dimensions)
- _matrix = new double[rows * cols];
- for (int row = 0; row < rows; row++) {
- for (int col = 0; col < cols; col++) {
- if (array[row][col]) {
- _matrix[row * cols + col] = 1.0;
- } else {
- _matrix[row * cols + col] = 0;
- }
- }
- }
- }
- /**
- * Initialises the matrix based on the specified array, column and row parameters.
- * @param array
- * @param rows
- * @param cols
- */
- public Matrix(double[] array, int rows, int cols) {
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- _matrix = new double[rows * cols];
- if (_matrix.length == array.length) {
- _matrix = array;
- } else {
- throw new IllegalArgumentException("ArgumentVectorNotValid");
- }
- }
- /**
- * Initialises the matrix to the specified size. All elements are set to zero.
- * @param rows
- * @param cols
- */
- public Matrix(int rows, int cols) {
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- //store the whole thing in a single array
- //one row at a time for example the matrix
- //
- // 1 2 3
- // 6 7 8
- //
- //would be stored as follows
- //
- // 1 2 3 6 7 8
- _matrix = new double[rows * cols];
- for (int index = 0; index < _matrix.length; index++) {
- _matrix[index] = 0.0;
- }
- }
- /**
- * Initialises the matrix to the specified size, setting all cells to the
- * specified value.
- * @param rows
- * @param cols
- * @param value
- */
- public Matrix(int rows, int cols, double value) {
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- //store the whole thing in a single array
- //one row at a time for example the matrix
- //
- // 1 2 3
- // 6 7 8
- //
- //would be stored as follows
- //
- // 1 2 3 6 7 8
- _matrix = new double[rows * cols];
- for (int index = 0; index < _matrix.length; index++) {
- _matrix[index] = value;
- }
- }
- /**
- * Initialises the matrix to the specified size. If the addRandomValues parameter
- * is set to true, all elements are set to a random value between 0 and 1.
- * @param rows
- * @param cols
- * @param addRandomValues
- */
- public Matrix(int rows, int cols, boolean addRandomValues) {
- if (rows < 1) {
- throw new ArrayIndexOutOfBoundsException(rows);
- }
- if (cols < 1) {
- throw new ArrayIndexOutOfBoundsException(cols);
- }
- //set the properties
- _rowCount = rows;
- _colCount = cols;
- //store the whole thing in a single array
- //one row at a time for example the matrix
- //
- // 1 2 3
- // 6 7 8
- //
- //would be stored as follows
- //
- // 1 2 3 6 7 8
- _matrix = new double[rows * cols];
- if (addRandomValues) {
- for (int index = 0; index < _matrix.length; index++) {
- _matrix[index] = _rng.nextDouble();
- }
- } else {
- for (int index = 0; index < _matrix.length; index++) {
- _matrix[index] = 0.0;
- }
- }
- }
- /**
- * Create a square diagonal matrix. Cells are initialised to zero with the exception of those
- * on the diagonal (Top Left to Bottom Right), which are set to the value specified.
- * @param diagonalMatrixSize
- * @param diagonalValue
- */
- public Matrix(int diagonalMatrixSize, double diagonalValue) {
- if (diagonalMatrixSize < 1) {
- throw new ArrayIndexOutOfBoundsException("diagonalMatrixSize < 1");
- }
- _matrix = new double[diagonalMatrixSize * diagonalMatrixSize];
- _rowCount = diagonalMatrixSize;
- _colCount = diagonalMatrixSize;
- int row = -1;
- for (int index = 0; index < _matrix.length; index++) {
- if (index % this.ColumnCount() == 0) {
- row++;
- }
- if (index == (row * this.ColumnCount()) + row) {
- _matrix[index] = diagonalValue;
- } else {
- _matrix[index] = 0.0;
- }
- }
- }
- /**
- * Create a square unit matrix. Cells are initialised to zero with the exception of those
- * on the diagonal (Top Left to Bottom Right), which are set to 1.
- * @param unitMatrixSize
- */
- public Matrix(int unitMatrixSize) {
- if (unitMatrixSize < 1) {
- throw new ArrayIndexOutOfBoundsException("unitMatrixSize < 1");
- }
- _matrix = new double[unitMatrixSize * unitMatrixSize];
- _rowCount = unitMatrixSize;
- _colCount = unitMatrixSize;
- int row = -1;
- for (int index = 0; index < _matrix.length; index++) {
- if (index % this.ColumnCount() == 0) {
- row++;
- }
- if (index == (row * this.ColumnCount()) + row) {
- _matrix[index] = 1.0;
- } else {
- _matrix[index] = 0.0;
- }
- }
- }
- /**
- * Returns an integer representing the number of rows in the matrix.
- * @return
- */
- public int RowCount() {
- return _rowCount;
- }
- /**
- * Returns an integer representing the number of columns in the matrix.
- * @return
- */
- public int ColumnCount() {
- return _colCount;
- }
- /**
- * Returns true if the matrix consists of a single row or a single column.
- * @return
- */
- public boolean IsVector() {
- return this.ColumnCount() == 1 || this.RowCount() == 1;
- }
- /**
- * Returns true if the matrix consists of a single row.
- * @return
- */
- public boolean IsRowVector() {
- return this.RowCount() == 1;
- }
- /**
- * Returns true if the matrix consists of a single column.
- * @return
- */
- public boolean IsColumnVector() {
- return this.ColumnCount() == 1;
- }
- /**
- * Returns the number of cells in the matrix.
- * @return
- */
- public int length() {
- return _matrix.length;
- }
- /**
- * Returns the total of the values of all elements of the matrix.
- * @return
- */
- public double Sum() {
- double result = 0.0;
- for (int index = 0; index < _matrix.length; index++) {
- result += _matrix[index];
- }
- return result;
- }
- /**
- * Returns the square root of the sum of the squared elements.
- * @return
- */
- public double Vectorlength() {
- if (!this.IsVector()) {
- throw new IllegalArgumentException("ArgumentMatrixIsNotVector");
- }
- double result = 0.0;
- for (int index = 0; index < _matrix.length; index++) {
- result += Math.pow(_matrix[index], 2);
- }
- return Math.sqrt(result);
- }
- /**
- * Returns a Matrix containing the specified rows.
- * @param startRow
- * @param endRow
- * @return
- */
- public Matrix GetRows(int startRow, int endRow) {
- //validate arguments
- if (startRow >= endRow) {
- throw new ArrayIndexOutOfBoundsException("ArgumentStartRowGreaterThanEndRow");
- }
- if (startRow >= this.RowCount() || endRow >= this.RowCount() || startRow < 0 || endRow < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow");
- }
- //get rows to return and number of columns in a row
- int rowsToReturn = endRow - startRow + 1;
- //create a matrix for the result
- Matrix result = new Matrix(rowsToReturn, this.ColumnCount());
- //populate the result
- System.arraycopy(this._matrix, startRow * this.ColumnCount(), result._matrix, 0, rowsToReturn * this.ColumnCount());
- //create the Matrix to return
- return result;
- }
- /**
- * Returns a RowMatrix containing the specified row.
- * @param row
- * @return
- */
- public Matrix GetRow(int row) {
- //validate arguments
- if (row >= this.RowCount() || row < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow");
- }
- double[] selectedRow = new double[this.ColumnCount()];
- System.arraycopy(_matrix, row * this.ColumnCount(), selectedRow, 0, this.ColumnCount());
- //get number of elements in each Row (i.e. how many columns it has)
- Matrix result = new Matrix(1, this.ColumnCount());
- result._matrix = selectedRow;
- return result;
- }
- /**
- * Returns a Matrix containing the specified columns.
- * @param startColumn
- * @param endColumn
- * @return
- */
- public Matrix GetColumns(int startColumn, int endColumn) {
- //validate arguments
- if (startColumn >= endColumn) {
- throw new ArrayIndexOutOfBoundsException("ArgumentStartColumnGreaterThanEndRow");
- }
- if (startColumn >= this.ColumnCount() || endColumn >= this.ColumnCount() || startColumn < 0 || endColumn < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn");
- }
- //get rows to return and number of columns in a row
- int colsToReturn = endColumn - startColumn + 1;
- //create a matrix for the result
- Matrix result = new Matrix(this.RowCount(), colsToReturn);
- //populate the result
- for (int row = 0; row < this.RowCount(); row++) {
- System.arraycopy(_matrix, (row * this.ColumnCount()) + startColumn, result._matrix, row * colsToReturn, colsToReturn);
- }
- return result;
- }
- /**
- * Returns a ColumnMatrix containing the specified coulmn.
- * @param column
- * @return
- */
- public Matrix GetColumn(int column) {
- //validate arguments
- if (column >= this.ColumnCount() || column < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
- }
- double[] selectedColumn = new double[this.RowCount()];
- for (int index = 0; index < this.RowCount(); index++) {
- selectedColumn[index] = _matrix[(this.ColumnCount() * index) + column];
- }
- //get number of elements in each Row (i.e. how many columns it has)
- Matrix result = new Matrix(this.RowCount(), 1);
- result._matrix = selectedColumn;
- return result;
- }
- /**
- * Returns the contents of the specified matrix element.
- * @param row
- * @param column
- * @return
- */
- public double GetElement(int row, int column) {
- //validate arguments
- if (row >= this.RowCount() || row < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow:" + row);
- }
- if (column >= this.ColumnCount() || column < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
- }
- return _matrix[(row * this.ColumnCount()) + column];
- }
- /**
- * Returns the contents of the specified matrix element.
- * @param row
- * @param column
- * @param value
- */
- public void SetElement(int row, int column, double value) {
- //validate arguments
- if (row >= this.RowCount() || row < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidRow:" + row);
- }
- if (column >= this.ColumnCount() || column < 0) {
- throw new ArrayIndexOutOfBoundsException("ArgumentInvalidColumn:" + column);
- }
- _matrix[(row * this.ColumnCount()) + column] = value;
- }
- /**
- * Returns the matrix as a one dimension generic list.
- * @return
- */
- public ArrayList ToList() {
- _matrixList = new ArrayList();
- for (int i = 0; i < _matrix.length; i++) {
- _matrixList.add(_matrix[i]);
- }
- return _matrixList;
- }
- /**
- * Returns the matrix as a one dimension array. Returns Row 1 followed by Row 2 etc.
- * @return
- */
- public double[] ToArray() {
- return _matrix;
- }
- /**
- * Returns a BiPolar copy of the Matrix as a new Matrix. Matrix returned will contain values or only 1 and -1 and will relflect the values of instance of the Matrix as follows:
- * Values greater than 0 will be represented by -1, values less than or equal to 0 will be represented by values of -1.
- * @return
- */
- public Matrix ToBiPolar() {
- Matrix result = new Matrix(this.RowCount(), this.ColumnCount());
- double[] biPolarMatrix = new double[_matrix.length];
- for (int index = 0; index < biPolarMatrix.length; index++) {
- if (_matrix[index] > 0) {
- biPolarMatrix[index] = 1;
- } else {
- biPolarMatrix[index] = -1;
- }
- }
- result._matrix = biPolarMatrix;
- return result;
- }
- /**
- * Returns a Binary copy of the Matrix as a new Matrix. Matrix returned will contain values or only 1 and 0 and will relflect the values of this instance of the Matrix as follows:
- * Values greater than 0 will be represented by 1, values less than or eqaul to 0 will be represented by values of 0.
- * @return
- */
- public Matrix ToBinary() {
- Matrix result = new Matrix(this.RowCount(), this.ColumnCount());
- double[] binaryMatrix = new double[_matrix.length];
- for (int index = 0; index < binaryMatrix.length; index++) {
- if (_matrix[index] > 0) {
- binaryMatrix[index] = 1;
- } else {
- binaryMatrix[index] = 0;
- }
- }
- result._matrix = binaryMatrix;
- return result;
- }
- /**
- * Returns the matrix as a comma delimited string. Uses the default format "%.2f" and the default row seperator CrLf.
- * @return
- */
- public String ToString() {
- return this.ToString("%.2f", ",", ";");
- }
- /// <summary>
- /// Returns the matrix with format applied to the numeric values. Uses the default format \"F4\" and the default row seperator CrLf.
- /// </summary>
- /// <returns></returns>
- /**
- * Returns the matrix with format applied to the numeric values.
- * @param format
- * @return
- */
- public String ToString(String format) {
- return this.ToString(format, ",", ";");
- }
- /**
- * Returns the matrix with format applied to the numeric values and rowDelimiter
- * @param format
- * @param columnDelimiter
- * @param rowDelimiter
- * @return
- */
- public String ToString(String format, String columnDelimiter, String rowDelimiter) {
- StringBuilder toString = new StringBuilder();
- for (int index = 0; index < _matrix.length; index++) {
- toString.append(String.format(format, _matrix[index]));
- if ((index + 1) % this.ColumnCount() == 0) {
- toString.append(rowDelimiter);
- } else {
- toString.append(columnDelimiter);
- }
- }
- String result = toString.toString();
- //tidy up spaces at the end, any other character is left in place
- if (result.endsWith(" ")) {
- return result.trim();
- } else {
- return result;
- }
- }
- /**
- * Returns true if the specified matrix is equal in value to this instance.
- * @param matrix
- * @return
- */
- public boolean Equals(Matrix matrix) {
- //compare rows columns and matrix values
- boolean result = true;
- if (this.RowCount() == matrix.RowCount() && this.ColumnCount() == matrix.ColumnCount()) {
- //check lengths
- if (this._matrix.length == matrix._matrix.length) {
- //compare values
- for (int index = 0; index < this._matrix.length; index++) {
- if (this._matrix[index] != matrix._matrix[index]) {
- result = false;
- }
- }
- } else {
- result = false;
- }
- } else {
- result = false;
- }
- return result;
- }
- /**
- * Returns a new Matrix that is an exact copy of this instance.
- * @return
- */
- public Matrix Clone() {
- int index = 0;
- Matrix clone = new Matrix(this.RowCount(), this.ColumnCount());
- for (int row = 0; row < this.RowCount(); row++) {
- for (int col = 0; col < this.ColumnCount(); col++) {
- index = (this.ColumnCount() * row) + col;
- clone._matrix[index] = this._matrix[index];
- }
- }
- return clone;
- }
- /**
- * Sets all elements of the matrix to 0.
- */
- public void Clear() {
- for (int index = 0; index < _matrix.length; index++) {
- _matrix[index] = 0.0;
- }
- }
- /**
- * Converts an array of booleanean values to an array of doubles in the range 0 to 1.
- * @param array
- * @return
- */
- private double[] ConvertToDoubleBool(boolean[] array) {
- double[] result = new double[array.length];
- for (int index = 0; index < array.length; index++) {
- if (array[index]) {
- result[index] = 1.0;
- } else {
- result[index] = 0;
- }
- }
- return result;
- }
- /**
- * Converts an array of booleanean values to an array of doubles in the range -1 to 1.
- * @param array
- * @return
- */
- private double[] ConvertToDoubleBiPolar(boolean[] array) {
- double[] result = new double[array.length];
- for (int index = 0; index < array.length; index++) {
- if (array[index]) {
- result[index] = 1.0;
- } else {
- result[index] = -1.0;
- }
- }
- return result;
- }
- /**
- * Multiplies the specified matrix by the scalar value.
- * @param matrix
- * @param scalarValue
- * @return
- */
- public static Matrix MultiplyScalar(Matrix matrix, double scalarValue) {
- int index = 0;
- Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
- for (int row = 0; row < matrix.RowCount(); row++) {
- for (int col = 0; col < matrix.ColumnCount(); col++) {
- index = (matrix.ColumnCount() * row) + col;
- result._matrix[index] = matrix._matrix[index] * scalarValue;
- }
- }
- return result;
- }
- /**
- * Divides the specified matrix by the scalar value.
- * @param matrix
- * @param scalarValue
- * @return
- */
- public static Matrix DivideScalar(Matrix matrix, double scalarValue) {
- int index = 0;
- Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
- for (int row = 0; row < matrix.RowCount(); row++) {
- for (int col = 0; col < matrix.ColumnCount(); col++) {
- index = (matrix.ColumnCount() * row) + col;
- result._matrix[index] = matrix._matrix[index] / scalarValue;
- }
- }
- return result;
- }
- /**
- * Adds the the scalar value to the specified matrix.
- * @param matrix
- * @param scalarValue
- * @return
- */
- public static Matrix AddScalar(Matrix matrix, double scalarValue) {
- int index = 0;
- Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
- for (int row = 0; row < matrix.RowCount(); row++) {
- for (int col = 0; col < matrix.ColumnCount(); col++) {
- index = (matrix.ColumnCount() * row) + col;
- result._matrix[index] = matrix._matrix[index] + scalarValue;
- }
- }
- return result;
- }
- /**
- * Subtracts the the scalar value to the specified matrix.
- * @param matrix
- * @param scalarValue
- * @return
- */
- public static Matrix SubtractScalar(Matrix matrix, double scalarValue) {
- int index = 0;
- Matrix result = new Matrix(matrix.RowCount(), matrix.ColumnCount());
- for (int row = 0; row < matrix.RowCount(); row++) {
- for (int col = 0; col < matrix.ColumnCount(); col++) {
- index = (matrix.ColumnCount() * row) + col;
- result._matrix[index] = matrix._matrix[index] - scalarValue;
- }
- }
- return result;
- }
- /**
- * Adds two matrices and returns the resultant matrix.
- * @param matrix1
- * @param matrix2
- * @return
- */
- public static Matrix Add(Matrix matrix1, Matrix matrix2) {
- if (matrix1.RowCount() != matrix2.RowCount() || matrix1.ColumnCount() != matrix2.ColumnCount()) {
- throw new IllegalArgumentException("ArgumentMatricesIncompatible");
- }
- int index = 0;
- Matrix result = new Matrix(matrix1.RowCount(), matrix1.ColumnCount());
- for (int row = 0; row < matrix1.RowCount(); row++) {
- for (int col = 0; col < matrix1.ColumnCount(); col++) {
- index = (matrix1.ColumnCount() * row) + col;
- result._matrix[index] = matrix1._matrix[index] + matrix2._matrix[index];
- }
- }
- return result;
- }
- /**
- * Subtracts matrix m2 from matrix m1 and returns the result.
- * @param matrix1
- * @param matrix2
- * @return
- */
- public static Matrix Subtract(Matrix matrix1, Matrix matrix2) {
- if (matrix1.RowCount() != matrix2.RowCount() || matrix1.ColumnCount() != matrix2.ColumnCount()) {
- throw new IllegalArgumentException("ArgumentMatricesIncompatible");
- }
- int index = 0;
- Matrix result = new Matrix(matrix1.RowCount(), matrix1.ColumnCount());
- for (int row = 0; row < matrix1.RowCount(); row++) {
- for (int col = 0; col < matrix1.ColumnCount(); col++) {
- index = (matrix1.ColumnCount() * row) + col;
- result._matrix[index] = matrix1._matrix[index] - matrix2._matrix[index];
- }
- }
- return result;
- }
- /**
- * Returns the Dot Product of two vectors. The respective elements of each vector are multiplied together and summed.
- * Note that the vectors must be the same length.
- * @param matrix1
- * @param matrix2
- * @return
- */
- public static double DotProduct(Matrix matrix1, Matrix matrix2) {
- //both must be a vector
- if (!(matrix1.IsVector() && matrix2.IsVector())) {
- throw new IllegalArgumentException("ArgumentMatricesIncompatible");
- }
- if (matrix1.length() != matrix2.length()) {
- throw new IllegalArgumentException("ArgumentMatricesIncompatible");
- }
- double result = 0.0;
- for (int index = 0; index < matrix1.length(); index++) {
- result += matrix1._matrix[index] * matrix2._matrix[index];
- }
- return result;
- }
- /**
- * Multiplies the two specified matrices. Note that matrix1 must have the same number of columns as matrix2 has rows.
- * @param matrix1
- * @param matrix2
- * @return
- */
- public static Matrix Multiply(Matrix matrix1, Matrix matrix2) {
- if (matrix1.ColumnCount() != matrix2.RowCount()) {
- throw new IllegalArgumentException("ArgumentMatricesIncompatible");
- }
- Matrix result = new Matrix(matrix1.RowCount(), matrix2.ColumnCount());
- for (int rowIndex = 0; rowIndex < result.RowCount(); rowIndex++) {
- for (int colIndex = 0; colIndex < result.ColumnCount(); colIndex++) {
- double[] row = matrix1.GetRow(rowIndex).ToArray();
- double[] col = matrix2.GetColumn(colIndex).ToArray();
- //loop here to perform the multiplication
- for (int index = 0; index < row.length; index++) {
- result._matrix[(rowIndex * result.ColumnCount()) + colIndex] += row[index] * col[index];
- }
- }
- }
- return result;
- }
- /**
- * Method to transpose the matrix. All rows become columns and all columns become rows.
- * @param matrix
- * @return
- */
- public static Matrix Transpose(Matrix matrix) {
- //create a result matrix that is the reverse of the passed in matrix
- Matrix result = new Matrix(matrix.ColumnCount(), matrix.RowCount());
- for (int colIndex = 0; colIndex < matrix.ColumnCount(); colIndex++) {
- System.arraycopy(matrix.GetColumn(colIndex).ToArray(), 0, result._matrix, colIndex * matrix.RowCount(), matrix.RowCount());
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement