Class MatrixAlgebra
- java.lang.Object
-
- com.opengamma.strata.math.impl.matrix.MatrixAlgebra
-
- Direct Known Subclasses:
CommonsMatrixAlgebra
,OGMatrixAlgebra
public abstract class MatrixAlgebra extends Object
Parent class for matrix algebra operations. Basic operations (add, subtract, scale) are implemented in this class.
-
-
Constructor Summary
Constructors Constructor Description MatrixAlgebra()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Matrix
add(Matrix m1, Matrix m2)
Adds two matrices.Matrix
divide(Matrix m1, Matrix m2)
Returns the quotient of two matrices $C = \frac{A}{B} = AB^{-1}$, where $B^{-1}$ is the pseudo-inverse of $B$ i.e.abstract double
getCondition(Matrix m)
Returns the condition number of the matrix.abstract double
getDeterminant(Matrix m)
Returns the determinant of the matrix.abstract double
getInnerProduct(Matrix m1, Matrix m2)
Returns the inner (or dot) product.abstract DoubleMatrix
getInverse(Matrix m)
Returns the inverse (or pseudo-inverse) of the matrix.abstract double
getNorm1(Matrix m)
For a vector, returns the $L_1$ norm (also known as the Taxicab norm or Manhattan norm), i.e.abstract double
getNorm2(Matrix m)
For a vector, returns $L_2$ norm (also known as the Euclidean norm).abstract double
getNormInfinity(Matrix m)
For a vector, returns the $L_\infty$ norm.abstract DoubleMatrix
getOuterProduct(Matrix m1, Matrix m2)
Returns the outer product.abstract DoubleMatrix
getPower(Matrix m, double p)
Returns a matrix raised to a power, $\mathbf{A}^3 = \mathbf{A}\mathbf{A}\mathbf{A}$.abstract DoubleMatrix
getPower(Matrix m, int p)
Returns a matrix raised to an integer power, e.g.abstract double
getTrace(Matrix m)
Returns the trace (i.e.abstract DoubleMatrix
getTranspose(Matrix m)
Returns the transpose of a matrix.Matrix
kroneckerProduct(Matrix m1, Matrix m2)
Returns the Kronecker product of two matrices.DoubleMatrix
matrixTransposeMultiplyMatrix(DoubleMatrix a)
Compute $A^T A$, where A is a matrix.abstract Matrix
multiply(Matrix m1, Matrix m2)
Multiplies two matrices.Matrix
scale(Matrix m, double scale)
Scale a vector or matrix by a given amount, i.e.Matrix
subtract(Matrix m1, Matrix m2)
Subtracts two matrices.
-
-
-
Method Detail
-
add
public Matrix add(Matrix m1, Matrix m2)
Adds two matrices. This operation can only be performed if the matrices are of the same type and dimensions.- Parameters:
m1
- The first matrix, not nullm2
- The second matrix, not null- Returns:
- The sum of the two matrices
- Throws:
IllegalArgumentException
- If the matrices are not of the same type, if the matrices are not the same shape.
-
divide
public Matrix divide(Matrix m1, Matrix m2)
Returns the quotient of two matrices $C = \frac{A}{B} = AB^{-1}$, where $B^{-1}$ is the pseudo-inverse of $B$ i.e. $BB^{-1} = \mathbb{1}$.- Parameters:
m1
- The numerator matrix, not null. This matrix must be aDoubleMatrix
.m2
- The denominator, not null. This matrix must be aDoubleMatrix
.- Returns:
- The result
-
kroneckerProduct
public Matrix kroneckerProduct(Matrix m1, Matrix m2)
Returns the Kronecker product of two matrices. If $\mathbf{A}$ is an $m \times n$ matrix and $\mathbf{B}$ is a $p \times q$ matrix, then the Kronecker product $A \otimes B$ is an $mp \times nq$ matrix with elements $$ \begin{align*} A \otimes B &= \begin{pmatrix} a_{11}\mathbf{B} & \cdots & a_{1n}\mathbf{B} \\ \vdots & \ddots & \vdots \\ a_{m1}\mathbf{B} & \cdots & a_{mn}\mathbf{B} \end{pmatrix}\\ &= \begin{pmatrix} a_{11}b_{11} & a_{11}b_{12} & \cdots & a_{11}b_{1q} & \cdots & a_{1n}b_{11} & a_{1n}b_{12} & \cdots & a_{1n}b_{1q}\\ a_{11}b_{21} & a_{11}b_{22} & \cdots & a_{11}b_{2q} & \cdots & a_{1n}b_{21} & a_{1n}b_{22} & \cdots & a_{1n}b_{2q} \\ \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \ddots & \cdots \\ a_{11}b_{p1} & a_{11}b_{p2} & \cdots & a_{11}b_{pq} & \cdots & a_{1n}b_{p1} & a_{1n}b_{p2} & \cdots & a_{1n}b_{pq} \\ \vdots & \vdots & & \vdots & \ddots & \vdots & \vdots & & \cdots \\ a_{m1}b_{11} & a_{m1}b_{12} & \cdots & a_{m1}b_{1q} & \cdots & a_{mn}b_{11} & a_{mn}b_{12} & \cdots & a_{mn}b_{1q} \\ a_{m1}b_{21} & a_{m1}b_{22} & \cdots & a_{m1}b_{2q} & \cdots & a_{mn}b_{21} & a_{mn}b_{22} & \cdots & a_{mn}b_{2q} \\ \vdots & \vdots & \ddots & \vdots & \cdots & \vdots & \vdots & \ddots & \cdots \\ a_{m1}b_{p1} & a_{m1}b_{p2} & \cdots & a_{m1}b_{pq} & \cdots & a_{mn}b_{p1} & a_{mn}b_{p2} & \cdots & a_{mn}b_{pq} \end{pmatrix} \end{align*} $$- Parameters:
m1
- The first matrix, not null. This matrix must be aDoubleMatrix
.m2
- The second matrix, not null. This matrix must be aDoubleMatrix
.- Returns:
- The Kronecker product
-
multiply
public abstract Matrix multiply(Matrix m1, Matrix m2)
Multiplies two matrices.- Parameters:
m1
- The first matrix, not null.m2
- The second matrix, not null.- Returns:
- The product of the two matrices.
-
scale
public Matrix scale(Matrix m, double scale)
Scale a vector or matrix by a given amount, i.e. each element is multiplied by the scale.- Parameters:
m
- A vector or matrix, not nullscale
- The scale- Returns:
- the scaled vector or matrix
-
subtract
public Matrix subtract(Matrix m1, Matrix m2)
Subtracts two matrices. This operation can only be performed if the matrices are of the same type and dimensions.- Parameters:
m1
- The first matrix, not nullm2
- The second matrix, not null- Returns:
- The second matrix subtracted from the first
- Throws:
IllegalArgumentException
- If the matrices are not of the same type, if the matrices are not the same shape.
-
getCondition
public abstract double getCondition(Matrix m)
Returns the condition number of the matrix.- Parameters:
m
- A matrix, not null- Returns:
- The condition number of the matrix
-
getDeterminant
public abstract double getDeterminant(Matrix m)
Returns the determinant of the matrix.- Parameters:
m
- A matrix, not null- Returns:
- The determinant of the matrix
-
getInverse
public abstract DoubleMatrix getInverse(Matrix m)
Returns the inverse (or pseudo-inverse) of the matrix.- Parameters:
m
- A matrix, not null- Returns:
- The inverse matrix
-
getInnerProduct
public abstract double getInnerProduct(Matrix m1, Matrix m2)
Returns the inner (or dot) product.- Parameters:
m1
- A vector, not nullm2
- A vector, not null- Returns:
- The scalar dot product
- Throws:
IllegalArgumentException
- If the vectors are not the same size
-
getOuterProduct
public abstract DoubleMatrix getOuterProduct(Matrix m1, Matrix m2)
Returns the outer product.- Parameters:
m1
- A vector, not nullm2
- A vector, not null- Returns:
- The outer product
- Throws:
IllegalArgumentException
- If the vectors are not the same size
-
getNorm1
public abstract double getNorm1(Matrix m)
For a vector, returns the $L_1$ norm (also known as the Taxicab norm or Manhattan norm), i.e. $\Sigma |x_i|$.For a matrix, returns the maximum absolute column sum norm of the matrix.
- Parameters:
m
- A vector or matrix, not null- Returns:
- The $L_1$ norm
-
getNorm2
public abstract double getNorm2(Matrix m)
For a vector, returns $L_2$ norm (also known as the Euclidean norm).For a matrix, returns the spectral norm
- Parameters:
m
- A vector or matrix, not null- Returns:
- the norm
-
getNormInfinity
public abstract double getNormInfinity(Matrix m)
For a vector, returns the $L_\infty$ norm. $L_\infty$ norm is the maximum of the absolute values of the elements.For a matrix, returns the maximum absolute row sum norm
- Parameters:
m
- a vector or a matrix, not null- Returns:
- the norm
-
getPower
public abstract DoubleMatrix getPower(Matrix m, int p)
Returns a matrix raised to an integer power, e.g. $\mathbf{A}^3 = \mathbf{A}\mathbf{A}\mathbf{A}$.- Parameters:
m
- A square matrix, not nullp
- An integer power- Returns:
- The result
-
getPower
public abstract DoubleMatrix getPower(Matrix m, double p)
Returns a matrix raised to a power, $\mathbf{A}^3 = \mathbf{A}\mathbf{A}\mathbf{A}$.- Parameters:
m
- A square matrix, not nullp
- The power- Returns:
- The result
-
getTrace
public abstract double getTrace(Matrix m)
Returns the trace (i.e. sum of diagonal elements) of a matrix.- Parameters:
m
- A matrix, not null. The matrix must be square.- Returns:
- The trace
-
getTranspose
public abstract DoubleMatrix getTranspose(Matrix m)
Returns the transpose of a matrix.- Parameters:
m
- A matrix, not null- Returns:
- The transpose matrix
-
matrixTransposeMultiplyMatrix
public DoubleMatrix matrixTransposeMultiplyMatrix(DoubleMatrix a)
Compute $A^T A$, where A is a matrix.- Parameters:
a
- The matrix- Returns:
- The result of $A^T A$
-
-