Package jisa.maths.matrices
Class AbstractMatrix<T>
- java.lang.Object
-
- jisa.maths.matrices.AbstractMatrix<T>
-
- All Implemented Interfaces:
java.lang.Iterable<T>
,Matrix<T>
- Direct Known Subclasses:
AbstractMatrix.LMatrix
,MMatrix
public abstract class AbstractMatrix<T> extends java.lang.Object implements Matrix<T>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
AbstractMatrix.LMatrix<T>
-
Nested classes/interfaces inherited from interface jisa.maths.matrices.Matrix
Matrix.EntryConsumer<T>, Matrix.EntryMapper<U,T>, Matrix.LinearMapper<T>
-
-
Constructor Summary
Constructors Constructor Description AbstractMatrix(T zero, T unity, int rows, int cols)
AbstractMatrix(T zero, T unity, Matrix<T> matrix)
AbstractMatrix(T zero, T unity, org.apache.commons.math.linear.FieldMatrix<jisa.maths.matrices.AbstractMatrix.FieldElement> matrix)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Matrix<T>
add(Matrix<T> rhs)
Adds the given matrix to this one.Matrix<T>
add(T rhs)
Add the given value to each element in this matrix, returning the result.protected abstract T
add(T a, T b)
void
addToElement(int row, int col, T value)
Replace the value of an element with itself plus a given value.Matrix<T>
appendCols(Matrix<T> cols)
Creates a new matrix consisting of the given columns appended to the right of this matrix.Matrix<T>
appendRows(Matrix<T> rows)
Creates a new matrix consisting of the given rows appended to the bottom of this matrix.int
cols()
Returns the number of columns in the matrix.Matrix<T>
copy()
Returns a deep copy of the matrix.protected abstract T
copy(T a)
AbstractMatrix<T>
create()
AbstractMatrix<T>
create(int rows, int cols)
AbstractMatrix<T>
create(Matrix<T> matrix)
AbstractMatrix<T>
create(org.apache.commons.math.linear.FieldMatrix<jisa.maths.matrices.AbstractMatrix.FieldElement> matrix)
Matrix<T>
divide(T rhs)
Divides all elements in this matrix by the given value.protected abstract T
divide(T a, T b)
void
divideElement(int row, int col, T value)
Replace the value of an element with itself divided by a given value.Matrix<T>
elementDivide(Matrix<T> rhs)
Performs element-wise division of this matrix by the specified matrix (divides each element in this matrix by its corresponding element in the other).Matrix<T>
elementMultiply(Matrix<T> rhs)
Computes the element-wise product of this matrix with another (this .* other).T
get(int row, int col)
Returns the element at the given indices.T[]
getColArray(int col)
Returns the values in a single column as an array.Matrix<T>
getColMatrix(int col)
Returns a single column as a column matrix.Matrix<T>
getColSums()
Returns a row matrix of the sums of each column in this matrix.T[][]
getData()
Returns all elements as a 2-dimensional array.T
getDeterminant()
Returns the determinant of the matrix (only for square matrices).Matrix<T>
getDiagonal()
Returns the diagonal values of the square matrix as a column matrix.T[]
getFlatData()
Returns all elements in a 1-dimensional array.LU<T>
getLU()
Computes and returns the LU decomposition of this matrix (only for square matrices).QR<T>
getQR()
Computes and returns the QR decomposition of this matrix.T[]
getRowArray(int row)
Returns the values in a single row as an array.Matrix<T>
getRowMatrix(int row)
Returns a single row as a row matrix.Matrix<T>
getRowSums()
Returns a column matrix of the sums of each row in this matrix.Matrix<T>
getSubMatrix(int[] rows, int[] cols)
Returns a sub-matrix containing only the specified rows and columns from this matrix.Matrix<T>
getSubMatrix(int startRow, int endRow, int startCol, int endCol)
Returns a sub-matrix containing only the rows and columns specified in the given ranges (inclusive).T
getTrace()
Returns the trace of the matrix (sum of diagonal elements).Matrix<T>
invert()
Inverts the matrix and returns the result (only for square matrices).boolean
isSingular()
Returns whether the matrix is singular (has no inverse).Matrix<T>
leftDivide(Matrix<T> rhs)
Multiplies the supplied matrix by the inverse of this matrix.Matrix<T>
leftDivide(T lhs)
Divides the given value by each element in the matrix returning a matrix of the results.Matrix<T>
leftElementDivide(Matrix<T> lhs)
Performs element-wise division of the specified matrix by this matrix (divides each element in the supplied matrix by its corresponding element in this matrix).Matrix<T>
leftElementMultiply(Matrix<T> lhs)
Computes the element-wise product of another matrix with this matrix (other * this).Matrix<T>
leftMultiply(Matrix<T> lhs)
Multiplies another matrix by this matrix (other * this).Matrix<T>
leftMultiply(T lhs)
Pre-multiplies each element in this matrix by a scalar value.Matrix<T>
map(GFunction<T,T> mapper)
Map all elements of this matrix into a new matrix according to the provided mapping function.Matrix<T>
map(Matrix.EntryMapper<T,T> mapper)
Map all elements of this matrix into a new matrix according to the provided mapping function.void
mapCol(int col, Matrix.LinearMapper<T> mapper)
Maps the values of a specified column onto themselves.void
mapColToCol(int source, int dest, Matrix.LinearMapper<T> mapper)
Maps one column onto another.void
mapElement(int row, int col, GFunction<T,T> mapper)
Replace an element with the value of a function of itself.void
mapRow(int row, Matrix.LinearMapper<T> mapper)
Maps the values of a specified row onto themselves.void
mapRowToRow(int source, int dest, Matrix.LinearMapper<T> mapper)
Maps one row onto another.void
mapSelf(Matrix.EntryMapper<T,T> mapper)
Map all elements of this matrix onto themselves according to the provided mapping function.Matrix<T>
multiply(Matrix<T> rhs)
Multiplies this matrix by another (this * other).Matrix<T>
multiply(T rhs)
Multiplies each element in this matrix by a scalar value.protected abstract T
multiply(T a, T b)
void
multiplyElement(int row, int col, T value)
Replace the value of an element with itself multiplied by a given value.Matrix<T>
reshape(int rows, int cols)
Reshapes the elements in the matrix into a matrix of different dimensions (but same total number of elements), returning the result.int
rows()
Returns the number of rows in the matrix.void
set(int row, int col, T value)
Sets the matrix element at the given (row, col) position.void
setAll(T value)
Sets all elements to a single value.void
setAll(T... values)
Sets all elements in the matrix.void
setDiagonal(T value)
Sets the values along the leading diagonal to the same value.void
setDiagonal(T... values)
Sets the elements on the leading diagonal of the matrix.Matrix<T>
subtract(Matrix<T> rhs)
Subtracts the given matrix from this one, returning the result.Matrix<T>
subtract(T rhs)
Subtracts the given value from each element in this matrix, returning the result.protected abstract T
subtract(T a, T b)
void
subtractFromElement(int row, int col, T value)
Replace the value of an element with itself minus a given value.java.lang.String
toString()
Returns a textual representation of this matrix.Matrix<T>
transpose()
Transposes the matrix and returns the result.-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jisa.maths.matrices.Matrix
checkIndices, div, div, divide, equals, forEach, forEach, forEachCol, forEachRow, isCol, isRow, isSquare, iterator, map, map, mapCol, mapColToCol, mapRow, mapRowToRow, mapSelf, minus, minus, plus, plus, setCol, setCol, setRow, setRow, setSubMatrix, size, stream, times, times
-
-
-
-
Method Detail
-
getDiagonal
public Matrix<T> getDiagonal()
Description copied from interface:Matrix
Returns the diagonal values of the square matrix as a column matrix.- Specified by:
getDiagonal
in interfaceMatrix<T>
- Returns:
- Diagonal values
-
toString
public java.lang.String toString()
Description copied from interface:Matrix
Returns a textual representation of this matrix.
-
create
public AbstractMatrix<T> create(int rows, int cols)
-
create
public AbstractMatrix<T> create()
-
create
public AbstractMatrix<T> create(org.apache.commons.math.linear.FieldMatrix<jisa.maths.matrices.AbstractMatrix.FieldElement> matrix)
-
create
public AbstractMatrix<T> create(Matrix<T> matrix)
-
rows
public int rows()
Description copied from interface:Matrix
Returns the number of rows in the matrix.
-
cols
public int cols()
Description copied from interface:Matrix
Returns the number of columns in the matrix.
-
get
public T get(int row, int col)
Description copied from interface:Matrix
Returns the element at the given indices.
-
getData
public T[][] getData()
Description copied from interface:Matrix
Returns all elements as a 2-dimensional array.
-
getFlatData
public T[] getFlatData()
Description copied from interface:Matrix
Returns all elements in a 1-dimensional array.- Specified by:
getFlatData
in interfaceMatrix<T>
- Returns:
- All elements
-
set
public void set(int row, int col, T value)
Description copied from interface:Matrix
Sets the matrix element at the given (row, col) position.
-
setAll
public void setAll(T... values)
Description copied from interface:Matrix
Sets all elements in the matrix.
-
setDiagonal
public void setDiagonal(T... values)
Description copied from interface:Matrix
Sets the elements on the leading diagonal of the matrix. Matrix must be square.- Specified by:
setDiagonal
in interfaceMatrix<T>
- Parameters:
values
- Diagonal elements
-
setDiagonal
public void setDiagonal(T value)
Description copied from interface:Matrix
Sets the values along the leading diagonal to the same value. Matrix must be square.- Specified by:
setDiagonal
in interfaceMatrix<T>
- Parameters:
value
- Element value
-
setAll
public void setAll(T value)
Description copied from interface:Matrix
Sets all elements to a single value.
-
mapElement
public void mapElement(int row, int col, GFunction<T,T> mapper)
Description copied from interface:Matrix
Replace an element with the value of a function of itself.- Specified by:
mapElement
in interfaceMatrix<T>
- Parameters:
row
- Row indexcol
- Column indexmapper
- Function for mapping
-
multiplyElement
public void multiplyElement(int row, int col, T value)
Description copied from interface:Matrix
Replace the value of an element with itself multiplied by a given value.- Specified by:
multiplyElement
in interfaceMatrix<T>
- Parameters:
row
- Row indexcol
- Column indexvalue
- Factor to multiply by
-
divideElement
public void divideElement(int row, int col, T value)
Description copied from interface:Matrix
Replace the value of an element with itself divided by a given value.- Specified by:
divideElement
in interfaceMatrix<T>
- Parameters:
row
- Row indexcol
- Column indexvalue
- Factor to divide by
-
addToElement
public void addToElement(int row, int col, T value)
Description copied from interface:Matrix
Replace the value of an element with itself plus a given value.- Specified by:
addToElement
in interfaceMatrix<T>
- Parameters:
row
- Row indexcol
- Column indexvalue
- Value to add
-
subtractFromElement
public void subtractFromElement(int row, int col, T value)
Description copied from interface:Matrix
Replace the value of an element with itself minus a given value.- Specified by:
subtractFromElement
in interfaceMatrix<T>
- Parameters:
row
- Row indexcol
- Column indexvalue
- Value to subtract
-
mapRow
public void mapRow(int row, Matrix.LinearMapper<T> mapper)
Description copied from interface:Matrix
Maps the values of a specified row onto themselves.
-
mapCol
public void mapCol(int col, Matrix.LinearMapper<T> mapper)
Description copied from interface:Matrix
Maps the values of a specified column onto themselves.
-
mapRowToRow
public void mapRowToRow(int source, int dest, Matrix.LinearMapper<T> mapper)
Description copied from interface:Matrix
Maps one row onto another.- Specified by:
mapRowToRow
in interfaceMatrix<T>
- Parameters:
source
- Source row indexdest
- Destination row indexmapper
- Function that defines mapping
-
mapColToCol
public void mapColToCol(int source, int dest, Matrix.LinearMapper<T> mapper)
Description copied from interface:Matrix
Maps one column onto another.- Specified by:
mapColToCol
in interfaceMatrix<T>
- Parameters:
source
- Source column indexdest
- Destination column indexmapper
- Function that defines mapping
-
getRowArray
public T[] getRowArray(int row)
Description copied from interface:Matrix
Returns the values in a single row as an array.- Specified by:
getRowArray
in interfaceMatrix<T>
- Parameters:
row
- Row index- Returns:
- Array of values
-
getColArray
public T[] getColArray(int col)
Description copied from interface:Matrix
Returns the values in a single column as an array.- Specified by:
getColArray
in interfaceMatrix<T>
- Parameters:
col
- Column index- Returns:
- Array of values
-
getRowMatrix
public Matrix<T> getRowMatrix(int row)
Description copied from interface:Matrix
Returns a single row as a row matrix.- Specified by:
getRowMatrix
in interfaceMatrix<T>
- Parameters:
row
- Row index- Returns:
- Row matrix
-
getColMatrix
public Matrix<T> getColMatrix(int col)
Description copied from interface:Matrix
Returns a single column as a column matrix.- Specified by:
getColMatrix
in interfaceMatrix<T>
- Parameters:
col
- Column index- Returns:
- Column matrix
-
multiply
public Matrix<T> multiply(Matrix<T> rhs)
Description copied from interface:Matrix
Multiplies this matrix by another (this * other).
-
leftMultiply
public Matrix<T> leftMultiply(Matrix<T> lhs)
Description copied from interface:Matrix
Multiplies another matrix by this matrix (other * this).- Specified by:
leftMultiply
in interfaceMatrix<T>
- Parameters:
lhs
- Matrix to multiply (left-hand side)- Returns:
- Result of multiplication
-
multiply
public Matrix<T> multiply(T rhs)
Description copied from interface:Matrix
Multiplies each element in this matrix by a scalar value.
-
leftMultiply
public Matrix<T> leftMultiply(T lhs)
Description copied from interface:Matrix
Pre-multiplies each element in this matrix by a scalar value.- Specified by:
leftMultiply
in interfaceMatrix<T>
- Parameters:
lhs
- Scalar value.- Returns:
- Result of multiplication
-
elementMultiply
public Matrix<T> elementMultiply(Matrix<T> rhs)
Description copied from interface:Matrix
Computes the element-wise product of this matrix with another (this .* other).- Specified by:
elementMultiply
in interfaceMatrix<T>
- Parameters:
rhs
- Matrix to multiply by (right-hand side)- Returns:
- Result of multiplication
-
leftElementMultiply
public Matrix<T> leftElementMultiply(Matrix<T> lhs)
Description copied from interface:Matrix
Computes the element-wise product of another matrix with this matrix (other * this).- Specified by:
leftElementMultiply
in interfaceMatrix<T>
- Parameters:
lhs
- Matrix to multiply (left-hand side)- Returns:
- Result of multiplication
-
elementDivide
public Matrix<T> elementDivide(Matrix<T> rhs)
Description copied from interface:Matrix
Performs element-wise division of this matrix by the specified matrix (divides each element in this matrix by its corresponding element in the other).- Specified by:
elementDivide
in interfaceMatrix<T>
- Parameters:
rhs
- Matrix to divide by- Returns:
- Result of division
-
leftElementDivide
public Matrix<T> leftElementDivide(Matrix<T> lhs)
Description copied from interface:Matrix
Performs element-wise division of the specified matrix by this matrix (divides each element in the supplied matrix by its corresponding element in this matrix).- Specified by:
leftElementDivide
in interfaceMatrix<T>
- Parameters:
lhs
- Matrix to divide- Returns:
- Result of division
-
divide
public Matrix<T> divide(T rhs)
Description copied from interface:Matrix
Divides all elements in this matrix by the given value.
-
leftDivide
public Matrix<T> leftDivide(T lhs)
Description copied from interface:Matrix
Divides the given value by each element in the matrix returning a matrix of the results.- Specified by:
leftDivide
in interfaceMatrix<T>
- Parameters:
lhs
- Value to be divided- Returns:
- Result of division
-
add
public Matrix<T> add(Matrix<T> rhs)
Description copied from interface:Matrix
Adds the given matrix to this one. (Adds each element with its corresponding element).
-
add
public Matrix<T> add(T rhs)
Description copied from interface:Matrix
Add the given value to each element in this matrix, returning the result.
-
subtract
public Matrix<T> subtract(Matrix<T> rhs)
Description copied from interface:Matrix
Subtracts the given matrix from this one, returning the result.
-
subtract
public Matrix<T> subtract(T rhs)
Description copied from interface:Matrix
Subtracts the given value from each element in this matrix, returning the result.
-
map
public Matrix<T> map(Matrix.EntryMapper<T,T> mapper)
Description copied from interface:Matrix
Map all elements of this matrix into a new matrix according to the provided mapping function.
-
map
public Matrix<T> map(GFunction<T,T> mapper)
Description copied from interface:Matrix
Map all elements of this matrix into a new matrix according to the provided mapping function.
-
mapSelf
public void mapSelf(Matrix.EntryMapper<T,T> mapper)
Description copied from interface:Matrix
Map all elements of this matrix onto themselves according to the provided mapping function.
-
copy
public Matrix<T> copy()
Description copied from interface:Matrix
Returns a deep copy of the matrix.
-
getSubMatrix
public Matrix<T> getSubMatrix(int[] rows, int[] cols)
Description copied from interface:Matrix
Returns a sub-matrix containing only the specified rows and columns from this matrix.- Specified by:
getSubMatrix
in interfaceMatrix<T>
- Parameters:
rows
- Array of row indicescols
- Array of column indices- Returns:
- Sub-matrix
-
getSubMatrix
public Matrix<T> getSubMatrix(int startRow, int endRow, int startCol, int endCol)
Description copied from interface:Matrix
Returns a sub-matrix containing only the rows and columns specified in the given ranges (inclusive).- Specified by:
getSubMatrix
in interfaceMatrix<T>
- Parameters:
startRow
- Start of row rangeendRow
- End of row rangestartCol
- Start of column rangeendCol
- End of column range- Returns:
- Sub-matrix
-
appendRows
public Matrix<T> appendRows(Matrix<T> rows)
Description copied from interface:Matrix
Creates a new matrix consisting of the given rows appended to the bottom of this matrix.- Specified by:
appendRows
in interfaceMatrix<T>
- Parameters:
rows
- Rows to append (must match column dimension)- Returns:
- Combined matrix
-
appendCols
public Matrix<T> appendCols(Matrix<T> cols)
Description copied from interface:Matrix
Creates a new matrix consisting of the given columns appended to the right of this matrix.- Specified by:
appendCols
in interfaceMatrix<T>
- Parameters:
cols
- Columns to append (must match row dimension)- Returns:
- Combined matrix
-
isSingular
public boolean isSingular()
Description copied from interface:Matrix
Returns whether the matrix is singular (has no inverse).- Specified by:
isSingular
in interfaceMatrix<T>
- Returns:
- Singular?
-
getDeterminant
public T getDeterminant()
Description copied from interface:Matrix
Returns the determinant of the matrix (only for square matrices).- Specified by:
getDeterminant
in interfaceMatrix<T>
- Returns:
- Determinant value
-
getTrace
public T getTrace()
Description copied from interface:Matrix
Returns the trace of the matrix (sum of diagonal elements).
-
invert
public Matrix<T> invert()
Description copied from interface:Matrix
Inverts the matrix and returns the result (only for square matrices).
-
transpose
public Matrix<T> transpose()
Description copied from interface:Matrix
Transposes the matrix and returns the result.
-
reshape
public Matrix<T> reshape(int rows, int cols)
Description copied from interface:Matrix
Reshapes the elements in the matrix into a matrix of different dimensions (but same total number of elements), returning the result.rows * cols must equal this.size().
-
leftDivide
public Matrix<T> leftDivide(Matrix<T> rhs)
Description copied from interface:Matrix
Multiplies the supplied matrix by the inverse of this matrix.- Specified by:
leftDivide
in interfaceMatrix<T>
- Parameters:
rhs
- Matrix to be "divided"- Returns:
- Result of division
-
getQR
public QR<T> getQR()
Description copied from interface:Matrix
Computes and returns the QR decomposition of this matrix.
-
getLU
public LU<T> getLU()
Description copied from interface:Matrix
Computes and returns the LU decomposition of this matrix (only for square matrices).
-
getRowSums
public Matrix<T> getRowSums()
Description copied from interface:Matrix
Returns a column matrix of the sums of each row in this matrix.- Specified by:
getRowSums
in interfaceMatrix<T>
- Returns:
- Column matrix of sums
-
-