Package edu.cmu.tetrad.util
Class Matrix
java.lang.Object
edu.cmu.tetrad.util.Matrix
- All Implemented Interfaces:
TetradSerializable,Serializable
Wraps the EJML linear algebra library for most uses in Tetrad. Specialized uses will still have to use the library
directly. One issue is that we need to be able to represent empty matrices gracefully; this case is handled
separately and incorporated into the class.
- Version:
- $Id: $Id
- Author:
- josephramsey
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidassign.voidassignColumn(int col, Vector doubles) assignColumn.voidassignPart(int[] range1, int[] range2, Matrix from) Assigns a part of the given matrix to a specified submatrix while adding the values to the existing data.voidassignRow.chooseInverse(double lambda) Calculates and returns the appropriate inverse of the matrix based on the provided lambda value: If lambda is 0.0, the inverse of the matrix is computed using the standard inversion method. If lambda is greater than 0.0, the matrix is regularized by adding lambda to the diagonal, ensuring numerical stability for near-singular matrices, and then the inverse is computed. If lambda is less than 0.0, the Moore-Penrose pseudoinverse is computed, which is useful for non-square or singular matrices. This method is designed to handle a variety of inverse computations depending on the requirements, such as regularization or handling singular matrices.col(int i) Retrieves the specified column from a data structure as a Vector object.copy()copy.doubledet()det.diag()diag.booleanequals.doubleget(int i, int j) get.getColumn(int j) getColumn.org.ejml.simple.SimpleMatrixgetData()Returns the wrapped SimpleMatrix.intgetNumColumns.intgetNumRows.getPart(int i, int j, int k, int l) Extracts a submatrix from the current matrix based on the specified row and column ranges.org.ejml.simple.SimpleMatrixCreates and returns a copy of the internal data matrix.static Matrixidentity(int rows) identity.inverse()Returns the inverse of the matrix.booleanisSquare()isSquare.like()like.minus.doublenorm1()norm1.plus.Returns the Moore-Penrose pseudoinverse of the matrix.intrank()rank.regularize(double lambda) Regularizes the diagonal of the matrix by adding a scaled identity matrix to it.row(int i) getRow.scalarMult(double scalar) scalarMult.scalarPlus(double scalar) Adds the specified scalar value to each element of the matrix and returns the resulting matrix.static MatrixGenerates a simple exemplar of this class to test serialization.voidset(int i, int j, double v) set.Solves the linear system A * X = B where this matrix is A and the argument is B.sqrt()sqrt.sum(int direction) sum.times.times.double[][]toArray()toArray.toString()toString.doubletrace()trace.transpose.view()Creates a view of the entire matrix.view(int[] range1, int[] range2) Creates a view of a matrix using the specified row and column indices.viewColumn(int column) Creates a view of a specific column from the matrix.viewPart(int fromRow, int toRow, int fromColumn, int toColumn) Creates a view of a specified submatrix defined by the given row and column ranges.viewRow(int row) Creates a sub-view of the matrix representing a specific row.
-
Constructor Details
-
Matrix
public Matrix(double[][] data) Constructor for Matrix.
- Parameters:
data- an array of objects
-
Matrix
public Matrix(org.ejml.simple.SimpleMatrix data) Constructs a Matrix object based on the given SimpleMatrix data.- Parameters:
data- the SimpleMatrix object containing the data to initialize the matrix
-
Matrix
public Matrix(int m, int n) Constructor for Matrix.
- Parameters:
m- a intn- a int
-
Matrix
Constructor for Matrix.
- Parameters:
m- aMatrixobject
-
-
Method Details
-
identity
identity.
- Parameters:
rows- a int- Returns:
- a
Matrixobject
-
serializableInstance
Generates a simple exemplar of this class to test serialization.- Returns:
- a
Matrixobject
-
assignPart
Assigns a part of the given matrix to a specified submatrix while adding the values to the existing data. The values from the 'from' matrix are added to the corresponding elements of the submatrix defined by the ranges.- Parameters:
range1- an array of integers representing the row indices of the submatrix to which the values will be assignedrange2- an array of integers representing the column indices of the submatrix to which the values will be assignedfrom- a Matrix object providing the values to be added to the specified submatrix
-
assign
assign.
- Parameters:
matrix- aMatrixobject
-
getNumColumns
public int getNumColumns()getNumColumns.
- Returns:
- a int
-
diag
diag.
- Returns:
- a
Vectorobject
-
copy
copy.
- Returns:
- a
Matrixobject
-
getColumn
getColumn.
- Parameters:
j- a int- Returns:
- a
Vectorobject
-
times
times.
-
times
times.
-
toArray
public double[][] toArray()toArray.
- Returns:
- an array of objects
-
get
public double get(int i, int j) get.
- Parameters:
i- a intj- a int- Returns:
- a double
-
like
like.
- Returns:
- a
Matrixobject
-
set
public void set(int i, int j, double v) set.
- Parameters:
i- a intj- a intv- a double
-
row
getRow.
- Parameters:
i- a int- Returns:
- a
Vectorobject
-
col
Retrieves the specified column from a data structure as a Vector object.- Parameters:
i- the index of the column to retrieve- Returns:
- a Vector representing the specified column
-
getPart
Extracts a submatrix from the current matrix based on the specified row and column ranges.- Parameters:
i- the starting row index (inclusive) of the submatrixj- the ending row index (inclusive) of the submatrixk- the starting column index (inclusive) of the submatrixl- the ending column index (inclusive) of the submatrix- Returns:
- a new Matrix instance representing the extracted submatrix
-
inverse
Returns the inverse of the matrix. If the matrix is not square, an exception is thrown. If the matrix is singular, an exception is thrown.- Returns:
- a
Matrixobject
-
pseudoinverse
Returns the Moore-Penrose pseudoinverse of the matrix.- Returns:
- a
Matrixobject
-
chooseInverse
Calculates and returns the appropriate inverse of the matrix based on the provided lambda value:- If lambda is 0.0, the inverse of the matrix is computed using the standard inversion method.
- If lambda is greater than 0.0, the matrix is regularized by adding lambda to the diagonal, ensuring numerical stability for near-singular matrices, and then the inverse is computed.
- If lambda is less than 0.0, the Moore-Penrose pseudoinverse is computed, which is useful for non-square or singular matrices.
- Parameters:
lambda- A value controlling the method of inversion:0.0: Standard matrix inversion.> 0.0: Regularized inversion for numerical stabilit. A small value should be chosen.< 0.0: Moore-Penrose pseudoinverse.
- Returns:
- The calculated inverse of the matrix based on the specified lambda value.
- Throws:
IllegalArgumentException- If lambda is neither -1.0, 0.0, nor a positive value.
-
regularize
Regularizes the diagonal of the matrix by adding a scaled identity matrix to it.- Parameters:
lambda- the scalar value used to scale the identity matrix before adding it to the current matrix- Returns:
- a new Matrix object representing the result of adding the scaled identity matrix to the current matrix
-
assignRow
assignRow.
- Parameters:
row- a intdoubles- aVectorobject
-
assignColumn
assignColumn.
- Parameters:
col- a intdoubles- aVectorobject
-
trace
public double trace()trace.
- Returns:
- a double
-
det
public double det()det.
- Returns:
- a double
-
transpose
transpose.
- Returns:
- a
Matrixobject
-
equals
equals.
- Parameters:
m- aMatrixobjecttolerance- a double- Returns:
- a boolean
-
isSquare
public boolean isSquare()isSquare.
- Returns:
- a boolean
-
minus
minus.
-
norm1
public double norm1()norm1.
- Returns:
- a double
-
plus
plus.
-
rank
public int rank()rank.
- Returns:
- a int
-
getNumRows
public int getNumRows()getNumRows.
- Returns:
- a int
-
scalarMult
scalarMult.
- Parameters:
scalar- a double- Returns:
- a
Matrixobject
-
scalarPlus
Adds the specified scalar value to each element of the matrix and returns the resulting matrix.- Parameters:
scalar- the scalar value to be added to each element of the matrix- Returns:
- a new Matrix instance where each element is the sum of the corresponding element in the original matrix and the scalar value
-
sqrt
sqrt.
- Returns:
- a
Matrixobject
-
sum
sum.
- Parameters:
direction- a int- Returns:
- a
Vectorobject
-
view
Creates a view of a matrix using the specified row and column indices.- Parameters:
range1- an array of row indices to include in the view. Each index must be within the range [0, number of rows).range2- an array of column indices to include in the view. Each index must be within the range [0, number of columns).- Returns:
- a MatrixView object representing the specified subset of the matrix.
- Throws:
IllegalArgumentException- if any row index in range1 is out of bounds or any column index in range2 is out of bounds.
-
view
Creates a view of the entire matrix.- Returns:
- a MatrixView object representing the entire matrix.
-
viewRow
Creates a sub-view of the matrix representing a specific row.- Parameters:
row- the index of the row to be viewed- Returns:
- a MatrixView object representing the specified row
-
viewColumn
Creates a view of a specific column from the matrix.- Parameters:
column- the index of the column to be viewed- Returns:
- a MatrixView object representing the specified column
-
viewPart
Creates a view of a specified submatrix defined by the given row and column ranges.- Parameters:
fromRow- the starting row index (inclusive) of the submatrixtoRow- the ending row index (exclusive) of the submatrixfromColumn- the starting column index (inclusive) of the submatrixtoColumn- the ending column index (exclusive) of the submatrix- Returns:
- a MatrixView object representing the specified submatrix view
-
toString
toString.
-
getSimpleMatrix
public org.ejml.simple.SimpleMatrix getSimpleMatrix()Creates and returns a copy of the internal data matrix.- Returns:
- A new SimpleMatrix instance that is a copy of the internal data.
-
getData
public org.ejml.simple.SimpleMatrix getData()Returns the wrapped SimpleMatrix. Be careful with this.- Returns:
- the wrapped SimpleMatrix instance.
-
solve
Solves the linear system A * X = B where this matrix is A and the argument is B.If A is square and nonsingular, uses a direct solver. Otherwise falls back to a least-squares solution via the Moore–Penrose pseudoinverse.
- Parameters:
rhs- The right-hand side matrix B.- Returns:
- The solution matrix X.
- Throws:
IllegalArgumentException- if the row dimensions are incompatible.
-