Class DenseMatrix
- java.lang.Object
-
- dev.nm.algebra.linear.matrix.doubles.matrixtype.dense.DenseMatrix
-
- All Implemented Interfaces:
Matrix,MatrixAccess,MatrixRing,MatrixTable,Densifiable,AbelianGroup<Matrix>,Monoid<Matrix>,Ring<Matrix>,Table,DeepCopyable
- Direct Known Subclasses:
CongruentMatrix,CorrelationMatrix,GoldfeldQuandtTrotter,Inverse,Jacobian,KroneckerProduct,MAT,MatrixRootByDiagonalization,MatthewsDavies,OuterProduct,PositiveDefiniteMatrixByPositiveDiagonal,Pow,PseudoInverse,ReturnsMatrix,SampleCovariance,SimilarMatrix,SymmetricKronecker
public class DenseMatrix extends Object implements Matrix, Densifiable
This class implements the standard, dense,doublebased matrix representation. There are two additional methods to change the matrix content:
-
-
Constructor Summary
Constructors Modifier Constructor Description DenseMatrix(double[][] data)Constructs a matrix from a 2Ddouble[][]array.DenseMatrix(double[] data, int nRows, int nCols)Constructs a matrix from a 1Ddouble[].DenseMatrix(int nRows, int nCols)Constructs a 0 matrix of dimension nRows * nCols.DenseMatrix(Matrix A)Converts any matrix to the standard matrix representation.DenseMatrix(DenseMatrix A)Copy constructor performing a deep copy.protectedDenseMatrix(DenseMatrix A, boolean copy)This constructor is useful for subclass to pass in computed value.DenseMatrix(Vector v)Constructs a column matrix from a vector.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Matrixadd(Matrix that)this + thatDenseMatrixdeepCopy()The implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.booleanequals(Object obj)doubleget(int i, int j)Get the matrix entry at [i,j].VectorgetColumn(int j)Get the specified column in the matrix as a vector.VectorgetColumn(int j, int beginRow, int endRow)Gets a sub-column of the j-th column, frombeginRowrow toendRowrow, inclusively.VectorgetRow(int i)Get the specified row in the matrix as a vector.VectorgetRow(int i, int beginCol, int endCol)Gets a sub-row of the i-th row, frombeginColcolumn toendColcolumn, inclusively.inthashCode()Matrixminus(Matrix that)this - thatMatrixmultiply(Matrix that)this * thatVectormultiply(Vector v)Right multiply this matrix, A, by a vector.intnCols()Gets the number of columns.intnRows()Gets the number of rows.DenseMatrixONE()Get an identity matrix that has the same dimension as this matrix.Matrixopposite()Get the opposite of this matrix.DenseMatrixscaled(double c)Scale this matrix, A, by a constant.voidset(int i, int j, double value)Set the matrix entry at [i,j] to a value.voidsetColumn(int j, Vector v)Changes the matrix column values to a vector value.voidsetRow(int i, Vector v)Changes the matrix row values to a vector value.DenseMatrixt()Get the transpose of this matrix.DenseMatrixtoDense()Densify a matrix, i.e., convert a matrix implementation to the standard dense matrix,DenseMatrix.StringtoString()DenseMatrixZERO()Get a zero matrix that has the same dimension as this matrix.
-
-
-
Constructor Detail
-
DenseMatrix
public DenseMatrix(int nRows, int nCols)Constructs a 0 matrix of dimension nRows * nCols.- Parameters:
nRows- the number of rowsnCols- the number of columns
-
DenseMatrix
public DenseMatrix(double[][] data)
Constructs a matrix from a 2Ddouble[][]array.- Parameters:
data- a 2D array input- Throws:
IllegalArgumentException- whendatais a jagged array
-
DenseMatrix
public DenseMatrix(double[] data, int nRows, int nCols)Constructs a matrix from a 1Ddouble[]. The array is a concatenation of the matrix rows. A sample usage is to convert a vector to a matrix. For example, to construct a column vector, we do
To construct a row vector, we doDenseMatrix V = new DenseMatrix(v.toArray(), v.length, 1);DenseMatrix V = new DenseMatrix(v.toArray(), 1, v.length);- Parameters:
data- the 1D array inputnRows- the number or rowsnCols- the number of columns- Throws:
IllegalArgumentException- when the length ofdatadoes not equal to nRows * nCols
-
DenseMatrix
public DenseMatrix(Vector v)
Constructs a column matrix from a vector.- Parameters:
v- a vector
-
DenseMatrix
public DenseMatrix(Matrix A)
Converts any matrix to the standard matrix representation. This method is the same astoDense()ifAisDensifiable.- Parameters:
A- a matrix
-
DenseMatrix
public DenseMatrix(DenseMatrix A)
Copy constructor performing a deep copy.- Parameters:
A- aDenseMatrix
-
DenseMatrix
protected DenseMatrix(DenseMatrix A, boolean copy)
This constructor is useful for subclass to pass in computed value.- Parameters:
A- aDenseMatrixcopy-trueif a deep copy ofAis needed
-
-
Method Detail
-
deepCopy
public DenseMatrix deepCopy()
Description copied from interface:DeepCopyableThe implementation returns an instance created fromthisby the copy constructor of the class, or justthisif the instance itself is immutable.- Specified by:
deepCopyin interfaceDeepCopyable- Specified by:
deepCopyin interfaceMatrix- Returns:
- an independent (deep) copy of the instance
-
toDense
public DenseMatrix toDense()
Description copied from interface:DensifiableDensify a matrix, i.e., convert a matrix implementation to the standard dense matrix,DenseMatrix.- Specified by:
toDensein interfaceDensifiable- Returns:
- a matrix representation in
DenseMatrix
-
nRows
public int nRows()
Description copied from interface:TableGets the number of rows. Rows count from 1.
-
nCols
public int nCols()
Description copied from interface:TableGets the number of columns. Columns count from 1.
-
set
public void set(int i, int j, double value) throws MatrixAccessExceptionDescription copied from interface:MatrixAccessSet the matrix entry at [i,j] to a value. This is the only method that may change a matrix.- Specified by:
setin interfaceMatrixAccess- Parameters:
i- the row indexj- the column indexvalue- the value to set A[i,j] to- Throws:
MatrixAccessException- if i or j is out of range
-
setRow
public void setRow(int i, Vector v)Changes the matrix row values to a vector value.- Parameters:
i- the i-th row to changev- the values to change the row to
-
setColumn
public void setColumn(int j, Vector v)Changes the matrix column values to a vector value.- Parameters:
j- the j-th column to changev- the values to change the column to
-
get
public double get(int i, int j) throws MatrixAccessExceptionDescription copied from interface:MatrixAccessGet the matrix entry at [i,j].- Specified by:
getin interfaceMatrixAccess- Parameters:
i- the row indexj- the column index- Returns:
- A[i,j]
- Throws:
MatrixAccessException- if i or j is out of range
-
getRow
public Vector getRow(int i)
Description copied from interface:MatrixGet the specified row in the matrix as a vector.
-
getRow
public Vector getRow(int i, int beginCol, int endCol)
Gets a sub-row of the i-th row, frombeginColcolumn toendColcolumn, inclusively.- Parameters:
i- the row to extractbeginCol- the beginning column of the sub-rowendCol- the ending column of the sub-row- Returns:
- a sub-row
-
getColumn
public Vector getColumn(int j)
Description copied from interface:MatrixGet the specified column in the matrix as a vector.
-
getColumn
public Vector getColumn(int j, int beginRow, int endRow)
Gets a sub-column of the j-th column, frombeginRowrow toendRowrow, inclusively.- Parameters:
j- the column to extractbeginRow- the beginning row of the sub-columnendRow- the ending row of the sub-column- Returns:
- a sub-column
-
add
public Matrix add(Matrix that)
Description copied from interface:MatrixRingthis + that- Specified by:
addin interfaceAbelianGroup<Matrix>- Specified by:
addin interfaceMatrixRing- Parameters:
that- a matrix- Returns:
- the sum of
thisandthat
-
minus
public Matrix minus(Matrix that)
Description copied from interface:MatrixRingthis - that- Specified by:
minusin interfaceAbelianGroup<Matrix>- Specified by:
minusin interfaceMatrixRing- Parameters:
that- a matrix- Returns:
- the difference between
thisandthat
-
multiply
public Matrix multiply(Matrix that)
Description copied from interface:MatrixRingthis * that- Specified by:
multiplyin interfaceMatrixRing- Specified by:
multiplyin interfaceMonoid<Matrix>- Parameters:
that- a matrix- Returns:
- the product of
thisandthat
-
ZERO
public DenseMatrix ZERO()
Description copied from interface:MatrixRingGet a zero matrix that has the same dimension as this matrix.- Specified by:
ZEROin interfaceAbelianGroup<Matrix>- Specified by:
ZEROin interfaceMatrixRing- Returns:
- the 0 matrix
-
ONE
public DenseMatrix ONE()
Description copied from interface:MatrixRingGet an identity matrix that has the same dimension as this matrix. For a non-square matrix, it zeros out the rows (columns) with index > nCols (nRows).- Specified by:
ONEin interfaceMatrixRing- Specified by:
ONEin interfaceMonoid<Matrix>- Returns:
- an identity matrix
-
t
public DenseMatrix t()
Description copied from interface:MatrixRingGet the transpose of this matrix. This is the involution on the matrix ring.- Specified by:
tin interfaceMatrixRing- Returns:
- the transpose of this matrix
-
multiply
public Vector multiply(Vector v)
Description copied from interface:MatrixRight multiply this matrix, A, by a vector.
-
scaled
public DenseMatrix scaled(double c)
Description copied from interface:MatrixScale this matrix, A, by a constant.
-
opposite
public Matrix opposite()
Description copied from interface:MatrixRingGet the opposite of this matrix.- Specified by:
oppositein interfaceAbelianGroup<Matrix>- Specified by:
oppositein interfaceMatrixRing- Returns:
- -this
- See Also:
- Wikipedia: Additive inverse
-
-