Package edu.cmu.tetrad.util
Class Matrix
java.lang.Object
edu.cmu.tetrad.util.Matrix
- All Implemented Interfaces:
TetradSerializable
,Serializable
Wraps the Apache math3 linear algebra library for most uses in Tetrad. Specialized uses will still have to use the
library directly. One issue this fixes is that a BlockRealMatrix cannot represent a matrix with zero rows; this uses
an Array2DRowRealMatrix to represent that case.
- Version:
- $Id: $Id
- Author:
- josephramsey
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds the given matrix to this matrix element-wise.void
assign.void
assignColumn
(int col, Vector doubles) assignColumn.void
assignRow.copy()
copy.double
det()
det.diag()
diag.boolean
equals.double
get
(int i, int j) get.org.apache.commons.math3.linear.RealMatrix
Getter for the fieldapacheData
.getColumn
(int j) getColumn.int
getNumColumns.int
getNumRows.getPart
(int i, int j, int k, int l) getPart.getRow
(int i) getRow.getSelection
(int[] rows, int[] cols) getSelection.ginverse()
ginverse.static Matrix
identity
(int rows) identity.inverse()
inverse.boolean
isSquare()
isSquare.boolean
isSymmetric
(double tolerance) isSymmetric.like()
like.minus.double
norm1()
norm1.plus.int
rank()
rank.scalarAdd
(double d) Adds a scalar value to each element of the matrix.scalarMult
(double scalar) scalarMult.static Matrix
Generates a simple exemplar of this class to test serialization.void
set
(int i, int j, double v) set.static Matrix
sparseMatrix
(int m, int n) sparseMatrix.sqrt()
Calculates the square root of the matrix using Singular Value Decomposition.sum
(int direction) sum.symmetricInverse.times.times.double[][]
toArray()
toArray.toString()
toString.double
trace()
trace.transpose.double
zSum()
Calculates the sum of all elements in a matrix.
-
Constructor Details
-
Matrix
public Matrix(double[][] data) Constructor for Matrix.
- Parameters:
data
- an array ofinvalid reference
double
-
Matrix
public Matrix(org.apache.commons.math3.linear.RealMatrix data) Constructor for Matrix.
- Parameters:
data
- aRealMatrix
object
-
Matrix
public Matrix(int m, int n) Constructor for Matrix.
- Parameters:
m
- a intn
- a int
-
Matrix
Constructor for Matrix.
- Parameters:
m
- aMatrix
object
-
-
Method Details
-
identity
identity.
- Parameters:
rows
- a int- Returns:
- a
Matrix
object
-
sparseMatrix
sparseMatrix.
- Parameters:
m
- a intn
- a int- Returns:
- a
Matrix
object
-
serializableInstance
Generates a simple exemplar of this class to test serialization.- Returns:
- a
Matrix
object
-
assign
assign.
- Parameters:
matrix
- aMatrix
object
-
getNumColumns
public int getNumColumns()getNumColumns.
- Returns:
- a int
-
diag
diag.
- Returns:
- a
Vector
object
-
getSelection
getSelection.
- Parameters:
rows
- an array ofinvalid reference
int
cols
- an array ofinvalid reference
int
- Returns:
- a
Matrix
object
-
copy
copy.
- Returns:
- a
Matrix
object
-
getColumn
getColumn.
- Parameters:
j
- a int- Returns:
- a
Vector
object
-
times
times.
-
times
times.
-
toArray
public double[][] toArray()toArray.
- Returns:
- an array of
invalid reference
double
-
getApacheData
public org.apache.commons.math3.linear.RealMatrix getApacheData()Getter for the field
apacheData
.- Returns:
- a
RealMatrix
object
-
get
public double get(int i, int j) get.
- Parameters:
i
- a intj
- a int- Returns:
- a double
-
like
like.
- Returns:
- a
Matrix
object
-
set
public void set(int i, int j, double v) set.
- Parameters:
i
- a intj
- a intv
- a double
-
getRow
getRow.
- Parameters:
i
- a int- Returns:
- a
Vector
object
-
getPart
getPart.
- Parameters:
i
- a intj
- a intk
- a intl
- a int- Returns:
- a
Matrix
object
-
inverse
inverse.
- Returns:
- a
Matrix
object - Throws:
org.apache.commons.math3.linear.SingularMatrixException
- if any.
-
symmetricInverse
symmetricInverse.
- Returns:
- a
Matrix
object
-
ginverse
ginverse.
- Returns:
- a
Matrix
object
-
assignRow
assignRow.
- Parameters:
row
- a intdoubles
- aVector
object
-
assignColumn
assignColumn.
- Parameters:
col
- a intdoubles
- aVector
object
-
trace
public double trace()trace.
- Returns:
- a double
-
det
public double det()det.
- Returns:
- a double
-
transpose
transpose.
- Returns:
- a
Matrix
object
-
equals
equals.
- Parameters:
m
- aMatrix
objecttolerance
- a double- Returns:
- a boolean
-
isSquare
public boolean isSquare()isSquare.
- Returns:
- a boolean
-
isSymmetric
public boolean isSymmetric(double tolerance) isSymmetric.
- Parameters:
tolerance
- a double- 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
Matrix
object
-
scalarAdd
Adds a scalar value to each element of the matrix.- Parameters:
d
- the scalar value to add to each element of the matrix- Returns:
- a new Matrix object with each element increased by the scalar value
-
sqrt
Calculates the square root of the matrix using Singular Value Decomposition.- Returns:
- The square root of the matrix as a new Matrix object.
-
sum
sum.
- Parameters:
direction
- a int- Returns:
- a
Vector
object
-
toString
toString.
-
add
Adds the given matrix to this matrix element-wise.- Parameters:
m
- the matrix to be added to this matrix
-
zSum
public double zSum()Calculates the sum of all elements in a matrix.- Returns:
- the sum of all elements in the matrix
-