Class Matrix
- java.lang.Object
-
- org.jquantlib.math.matrixutilities.Cells<Address.MatrixAddress>
-
- org.jquantlib.math.matrixutilities.Matrix
-
- All Implemented Interfaces:
- java.lang.Cloneable
- Direct Known Subclasses:
- Identity
public class Matrix extends Cells<Address.MatrixAddress> implements java.lang.Cloneable
Bidimensional matrix operationsPerformance of multidimensional arrays is a big concern in Java. This is because multidimensional arrays are stored as arrays of arrays, spanning this concept to as many depths as necessary. In C++, a multidimensional array is stored internally as a unidimensional array where depths are stacked together one after another. A very simple calculation is needed in order to map multiple dimensional indexes to an unidimensional index.
This class provides a C/C++ like approach of internal unidimensional array backing a conceptual bidimensional matrix. This mapping is provided by method
op(int row, int col)which is responsible for returning the physical address of the desired tuple, given a certain access method.
The mentioned access method is provided by a concrete implementation of
Addresswhich is passed to constructors. Doing so, it is possible to access the same underlying unidimensional data storage in various different ways, which allows us obtain another Matrix (see methodand alike) from an existing Matrix without any need of copying the underlying data. Certain operations benefit a lot from such approach, liketranspose()which presents constant execution time.The price we have to pay for such flexibility and benefits is that an access method is necessary, which means that a the bytecode may potentially need a dereference to a class which contain the concrete implementation of method
op(int row, int col). In order to keep this cost at minimum, implementation of access method keep indexes at hand whenever possible, in order to being able to calculate the actual unidimensional index as fast as possible. This additional dereference impacts performance more or less in the same ways dereference impacts performance when a bidimensional array (double[][]) is employed. Contrary to the bidimensional implementation, our implementation can potentially benefit from bytecode inlining, which means that calculation of the unidimensional index can be performed potentially faster than calculation of address location when a bidimensional array (double[][]) is used as underlying storage.Assignment operations
opr method this right result --- ---------- ------- -------- ------ = assign Matrix Matrix (1) += addAssign Matrix Matrix this -= subAssign Matrix Matrix this *= mulAssign Matrix scalar this /= divAssign Matrix scalar this
Algebraic products
opr method this right result --- ---------- ------- -------- ------ + add Matrix Matrix Matrix - sub Matrix Matrix Matrix - negative Matrix this * mul Matrix scalar Matrix / div Matrix scalar Matrix
Vectorial products
method this right result -------------- ------- -------- ------ mul Matrix Array Array mul Matrix Matrix Matrix
Decompositions
method this right result -------------- ------- -------- ------ lu Matrix LUDecomposition qr Matrix QRDecomposition cholensky Matrix CholeskyDecomposition svd Matrix SingularValueDecomposition eigenvalue Matrix EigenvalueDecomposition
Element iterators
method this right result ------------ ------- -------- ------ rowIterator Matrix RowIterator columnIterator Matrix ColumnIterator
Miscellaneous
method this right result -------------- ------- -------- ------ transpose Matrix Matrix diagonal Matrix Array determinant Matrix double inverse Matrix Matrix solve Matrix Matrix swap Matrix Matrix this range Matrix Matrix Matrix
(1): clone()
(2): Unary + is equivalent to: array.clone()
(3): Unary ? is equivalent to: array.clone().mulAssign(-1)
-
-
Constructor Summary
Constructors Constructor and Description Matrix()Default constructorMatrix(double[][] data)Creates a Matrix given a double[][] arrayMatrix(double[][] data, java.util.Set<Address.Flags> flags)Creates a Matrix given a double[][] arrayMatrix(int rows, int cols)Builds a Matrix ofrowsbycolsMatrix(int rows, int cols, double[] data, Address.MatrixAddress addr)Matrix(int rows, int cols, java.util.Set<Address.Flags> flags)Builds a Matrix ofrowsbycolsMatrix(Matrix m)copy constructorMatrix(java.util.Set<Address.Flags> flags)Default constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method and Description int_(int row, int col)Deprecated.Matrixadd(Matrix another)Returns the result of addition ofthisMatrix andanotherMatrixMatrixaddAssign(Matrix another)Returns the result of an addition ofthisMatrix andanotherMatrixCholeskyDecompositioncholesky()Cholesky DecompositionMatrixclone()Returns a copy ofthisMatrixMatrixconstRange(int[] ridx, int[] cidx)MatrixconstRange(int[] ridx, int col0, int col1)MatrixconstRange(int row0, int row1, int[] cidx)MatrixconstRange(int row0, int row1, int col0, int col1)ArrayconstRangeCol(int col)ArrayconstRangeCol(int col, int row0)ArrayconstRangeCol(int col, int row0, int row1)ArrayconstRangeRow(int row)ArrayconstRangeRow(int row, int col0)ArrayconstRangeRow(int row, int col0, int col1)doubledeterminant()DeterminantArraydiagonal()Returns a diagonal fromthisMatrix, if it is squareMatrixdiv(double scalar)Returns the result of a division ofthisMatrix by ascalarMatrixdivAssign(double scalar)Returns the result of a division ofthisMatrix by ascalarEigenvalueDecompositioneigenvalue()Eigenvalue Decompositionbooleanequals(java.lang.Object obj)Matrixfill(double scalar)Matrixfill(Matrix another)voidfillCol(int col, Array array)Overwrites contents of a certain columnvoidfillRow(int row, Array array)Overwrites contents of a certain rowdoubleget(int row, int col)Retrieves an element ofthisMatrix which identified by (row, col)inthashCode()Matrixinverse()Returns an inverse Matrix fromthisMatrixLUDecompositionlu()LU DecompositionArraymul(Array array)Returns an Array which represents the multiplication ofthisMatrix by an ArrayMatrixmul(double scalar)Returns the result of a multiplication ofthisMatrix by ascalarMatrixmul(Matrix another)Returns a Matrix which represents the multiplication ofthisMatrix andanotherMatrixMatrixmulAssign(double scalar)Returns the result of a multiplication ofthisMatrix by ascalarMatrixnegative()Returns the negative ofthisMatrixintoffset()QRDecompositionqr()QR DecompositionQRDecompositionqr(boolean pivot)QR DecompositionMatrixrange(int[] ridx, int[] cidx)Matrixrange(int[] ridx, int col0, int col1)Matrixrange(int row0, int row1, int[] cidx)Matrixrange(int row0, int row1, int col0, int col1)ArrayrangeCol(int col)ArrayrangeCol(int col, int row0)ArrayrangeCol(int col, int row0, int row1)ArrayrangeRow(int row)ArrayrangeRow(int row, int col0)ArrayrangeRow(int row, int col0, int col1)SymmetricSchurDecompositionschur()Symmetric Schur Decompositionvoidset(int row, int col, double value)Stores a value into an element ofthisMatrix which is identified by (row, col)Matrixsort()Matrixsub(Matrix another)Returns the result of a subtraction ofthisMatrix andanotherMatrixMatrixsubAssign(Matrix another)Returns the result of a subtraction ofthisMatrix andanotherMatrixSVDsvd()Singular Value DecompositionMatrixswap(Matrix another)MatrixtoFortran()MatrixtoJava()java.lang.StringtoString()Matrixtranspose()Returns the transpose ofthisMatrix
-
-
-
Constructor Detail
-
Matrix
public Matrix()
Default constructorBuilds a Matrix with dimensions 1x1
-
Matrix
public Matrix(java.util.Set<Address.Flags> flags)
Default constructorBuilds a Matrix with dimensions 1x1
- Parameters:
flags- is aSet<Address.Flags>- See Also:
Address.Flags
-
Matrix
public Matrix(int rows, int cols)Builds a Matrix ofrowsbycols- Parameters:
rows- is the number of rowscols- is the number of columns- Throws:
java.lang.IllegalArgumentException- if parameters are less than zero
-
Matrix
public Matrix(int rows, int cols, java.util.Set<Address.Flags> flags)Builds a Matrix ofrowsbycols- Parameters:
rows- is the number of rowscols- is the number of columnsflags- is aSet<Address.Flags>- Throws:
java.lang.IllegalArgumentException- if parameters are less than zero- See Also:
Address.Flags
-
Matrix
public Matrix(double[][] data)
Creates a Matrix given a double[][] array- Parameters:
data-
-
Matrix
public Matrix(double[][] data, java.util.Set<Address.Flags> flags)Creates a Matrix given a double[][] array- Parameters:
data-
-
Matrix
public Matrix(Matrix m)
copy constructor- Parameters:
$-
-
Matrix
public Matrix(int rows, int cols, double[] data, Address.MatrixAddress addr)
-
-
Method Detail
-
_
@Deprecated public int _(int row, int col)Deprecated.This is a convenience method intended to return the physical address of an element.The use of this method is highly discouraged
- Parameters:
index- is a logical address of an element- Returns:
- the physical address to an element
- See Also:
Cells.$
-
get
public double get(int row, int col)Retrieves an element ofthisMatrix which identified by (row, col)- Parameters:
row- coordinatecol- coordinate- Returns:
- the contents of a given cell
-
set
public void set(int row, int col, double value)Stores a value into an element ofthisMatrix which is identified by (row, col)- Parameters:
row- coordinatecol- coordinate
-
addAssign
public Matrix addAssign(Matrix another)
Returns the result of an addition ofthisMatrix andanotherMatrix- Parameters:
another-- Returns:
- this
-
subAssign
public Matrix subAssign(Matrix another)
Returns the result of a subtraction ofthisMatrix andanotherMatrix- Parameters:
another-- Returns:
- this
-
mulAssign
public Matrix mulAssign(double scalar)
Returns the result of a multiplication ofthisMatrix by ascalar- Parameters:
scalar-- Returns:
- this
-
divAssign
public Matrix divAssign(double scalar)
Returns the result of a division ofthisMatrix by ascalar- Parameters:
scalar-- Returns:
- this
-
add
public Matrix add(Matrix another)
Returns the result of addition ofthisMatrix andanotherMatrix- Parameters:
another-- Returns:
- a new instance
-
sub
public Matrix sub(Matrix another)
Returns the result of a subtraction ofthisMatrix andanotherMatrix- Parameters:
another-- Returns:
- a new instance
-
negative
public Matrix negative()
Returns the negative ofthisMatrix- Returns:
- this
-
mul
public Matrix mul(double scalar)
Returns the result of a multiplication ofthisMatrix by ascalar- Parameters:
scalar-- Returns:
- a new instance
-
div
public Matrix div(double scalar)
Returns the result of a division ofthisMatrix by ascalar- Parameters:
scalar-- Returns:
- a new instance
-
mul
public Array mul(Array array)
Returns an Array which represents the multiplication ofthisMatrix by an Array- Parameters:
array- is the input Array which participates in the operation- Returns:
- a new Array which contains the result
-
mul
public Matrix mul(Matrix another)
Returns a Matrix which represents the multiplication ofthisMatrix andanotherMatrix- Parameters:
another-- Returns:
- a new Matrix which contains the result
-
lu
public LUDecomposition lu()
LU Decomposition- Parameters:
moreGreeks- is a rectangular Matrix- Returns:
- Structure to access L, U and piv.
-
qr
public QRDecomposition qr()
QR Decomposition- Returns:
- QRDecomposition
- See Also:
QRDecomposition
-
qr
public QRDecomposition qr(boolean pivot)
QR Decomposition- Returns:
- QRDecomposition
- See Also:
QRDecomposition
-
cholesky
public CholeskyDecomposition cholesky()
Cholesky Decomposition- Returns:
- CholeskyDecomposition
- See Also:
CholeskyDecomposition
-
schur
public SymmetricSchurDecomposition schur()
Symmetric Schur Decomposition- Returns:
- SymmetricSchurDecomposition
- See Also:
SymmetricSchurDecomposition
-
eigenvalue
public EigenvalueDecomposition eigenvalue()
Eigenvalue Decomposition- Returns:
- EigenvalueDecomposition
- See Also:
EigenvalueDecomposition
-
transpose
public Matrix transpose()
Returns the transpose ofthisMatrix- Returns:
- a new instance which contains the result of this operation
-
diagonal
public Array diagonal()
Returns a diagonal fromthisMatrix, if it is square- Returns:
- a new instance which contains the result of this operation
-
determinant
public double determinant()
Determinant- Returns:
- determinant of matrix
- Throws:
java.lang.IllegalArgumentException- Matrix must be square
-
inverse
public Matrix inverse()
Returns an inverse Matrix fromthisMatrix- Returns:
- a new instance which contains the result of this operation
-
rangeRow
public Array rangeRow(int row)
-
rangeRow
public Array rangeRow(int row, int col0)
-
rangeRow
public Array rangeRow(int row, int col0, int col1)
-
rangeCol
public Array rangeCol(int col)
-
rangeCol
public Array rangeCol(int col, int row0)
-
rangeCol
public Array rangeCol(int col, int row0, int row1)
-
range
public Matrix range(int row0, int row1, int col0, int col1)
-
range
public Matrix range(int[] ridx, int col0, int col1)
-
range
public Matrix range(int row0, int row1, int[] cidx)
-
range
public Matrix range(int[] ridx, int[] cidx)
-
constRangeRow
public Array constRangeRow(int row)
-
constRangeRow
public Array constRangeRow(int row, int col0)
-
constRangeRow
public Array constRangeRow(int row, int col0, int col1)
-
constRangeCol
public Array constRangeCol(int col)
-
constRangeCol
public Array constRangeCol(int col, int row0)
-
constRangeCol
public Array constRangeCol(int col, int row0, int row1)
-
constRange
public Matrix constRange(int row0, int row1, int col0, int col1)
-
constRange
public Matrix constRange(int[] ridx, int col0, int col1)
-
constRange
public Matrix constRange(int row0, int row1, int[] cidx)
-
constRange
public Matrix constRange(int[] ridx, int[] cidx)
-
toFortran
public Matrix toFortran()
-
toJava
public Matrix toJava()
-
fill
public Matrix fill(double scalar)
-
fillRow
public void fillRow(int row, Array array)Overwrites contents of a certain row- Parameters:
row- is the requested row to be overwrittenarray- contains the elements to be copied
-
fillCol
public void fillCol(int col, Array array)Overwrites contents of a certain column- Parameters:
col- is the requested column to be overwrittenarray- contains the elements to be copied
-
sort
public Matrix sort()
-
clone
public Matrix clone()
Returns a copy ofthisMatrix- Overrides:
clonein classCells<Address.MatrixAddress>
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
offset
public int offset()
-
-
DataMelt 3.0 © DataMelt by jWork.ORG