org.ojalgo.matrix
Class PrimitiveMatrix

java.lang.Object
  extended by org.ojalgo.matrix.PrimitiveMatrix
All Implemented Interfaces:
Serializable, Access2D<Double>, Basic2D, BasicMatrix

public final class PrimitiveMatrix
extends Object

PrimitiveMatrix

Author:
apete
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.ojalgo.matrix.BasicMatrix
BasicMatrix.Factory<I extends BasicMatrix>
 
Field Summary
static MatrixFactory<Double> FACTORY
           
 
Constructor Summary
PrimitiveMatrix(MatrixStore<Double> aStore)
           
 
Method Summary
 BasicMatrix add(BasicMatrix aMtrx)
          Adds the elements of aMtrx to the elements of this matrix.
 BasicMatrix add(int aRow, int aCol, Number aNmbr)
          Do not use this method to populate large dense matrices! Only use it to change a few (a small number) of elements.
 BasicMatrix add(Number aNmbr)
          Adds aNmbr to the elements of this.
 BasicMatrix conjugate()
          For real matrices conjugate() and transpose() are identical.
 BasicMatrix divide(Number aNmbr)
          Divides the elements of this with aNmbr.
 BasicMatrix divideElements(BasicMatrix aMtrx)
          Divides the elements of this with the elements of aMtrx.
 double doubleValue(int i, int j)
          Extracts one element of this matrix as a double.
 BasicMatrix enforce(NumberContext aContext)
           
 boolean equals(BasicMatrix aMtrx, NumberContext aCntxt)
           
 boolean equals(Object obj)
           
 void flushCache()
          BasicMatrix instances are intended to be immutable.
 N get(int aRow, int aColumn)
           
static MatrixBuilder<Double> getBuilder(int aRowDim, int aColDim)
           
 int getColDim()
          The size of this structure in the column-direction/dimension
 BasicMatrix getColumns(int... someCols)
           
 Scalar<N> getCondition()
           
 Scalar<N> getDeterminant()
           
 List<ComplexNumber> getEigenvalues()
           
 Scalar<N> getFrobeniusNorm()
          The Frobenius norm is the square root of the sum of the squares of each element, or the square root of the sum of the square of the singular values.
 Scalar<?> getInfinityNorm()
           
 Scalar<N> getKyFanNorm(int k)
           
 int getMinDim()
           
 Scalar<?> getOneNorm()
           
 Scalar<N> getOperatorNorm()
          2-norm, max singular value
 int getRank()
          The rank of a matrix is the (maximum) number of linearly independent rows or columns it contains.
 int getRowDim()
          The size of this structure in the row-direction/dimension
 BasicMatrix getRows(int... someRows)
           
 List<Double> getSingularValues()
           
 Scalar<N> getTrace()
          The sum of the diagonal elements.
 Scalar<N> getTraceNorm()
           
 Scalar<N> getVectorNorm(int aDegree)
          Treats [this] as if it is one dimensional (a vector) and calculates the vector norm.
 int hashCode()
           
 BasicMatrix invert()
           About inverting matrices:
 boolean isEmpty()
          Matrices are either square, tall, fat or empty.
 boolean isFat()
          Matrices are either square, tall, fat or empty.
 boolean isFullRank()
           
 boolean isHermitian()
           
 boolean isScalar()
           
 boolean isSquare()
          Matrices are either square, tall, fat or empty.
 boolean isSymmetric()
           
 boolean isTall()
          Matrices are either square, tall, fat or empty.
 boolean isVector()
           
 BasicMatrix mergeColumns(BasicMatrix aMtrx)
          [aMtrx] is appended to the bottom of [this].
 BasicMatrix mergeRows(BasicMatrix aMtrx)
          [aMtrx] is appended to the right side of [this].
 BasicMatrix multiply(Number aNmbr)
          Multiplies the elements of this matrix with aNmbr.
 BasicMatrix multiplyElements(BasicMatrix aMtrx)
          Multiplies the elements of this matrix with the elements of aMtrx.
 BasicMatrix multiplyLeft(BasicMatrix aMtrx)
          Matrix multiplication: [aMtrx][this]
The column dimension of the left matrix must equal the row dimension of the right matrix.
 BasicMatrix multiplyRight(BasicMatrix aMtrx)
          Matrix multiplication: [this][aMtrx]
The column dimension of the left matrix must equal the row dimension of the right matrix.
 Scalar<N> multiplyVectors(BasicMatrix aVctr)
          Assumes that both [this] and [aVctr] have row or column dimension, doesn't matter which, equal to 1.
 BasicMatrix negate()
           
 BasicMatrix replace(int aRow, int aCol, Number aNmbr)
          Do not use this method to populate large dense matrices! Only use it to change a few (a small number) of elements.
 BasicMatrix round(NumberContext aContext)
           
 int size()
           
 BasicMatrix solve(BasicMatrix aRHS)
           This method solves a system of linear equations: [this][X]=[aRHS].
 BasicMatrix subtract(BasicMatrix aMtrx)
          Subtracts the elements of aMtrx from the elements of this matrix.
 BasicMatrix subtract(Number aNmbr)
          Subtracts aNmbr from the elements of this matrix.
 BigDecimal toBigDecimal(int aRow, int aCol)
          Extracts one element of this matrix as a BigDecimal.
 PhysicalStore<BigDecimal> toBigStore()
          Must be a copy that is safe to modify.
 ComplexNumber toComplexNumber(int aRow, int aCol)
          Extracts one element of this matrix as a ComplexNumber.
 PhysicalStore<ComplexNumber> toComplexStore()
          Must be a copy that is safe to modify.
 List<BasicMatrix> toListOfColumns()
           
 List<N> toListOfElements()
          It is also possible to call BasicMatrix.toBigStore(), BasicMatrix.toComplexStore() or BasicMatrix.toPrimitiveStore() and then PhysicalStore.asList().
 List<BasicMatrix> toListOfRows()
           
 PhysicalStore<Double> toPrimitiveStore()
          Must be a copy that is safe to modify.
 Scalar<N> toScalar(int aRow, int aCol)
          Extracts one element of this matrix as a Scalar.
 String toString()
           
 String toString(int aRow, int aCol)
           
 BasicMatrix transpose()
          Transposes this matrix.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

FACTORY

public static final MatrixFactory<Double> FACTORY
Constructor Detail

PrimitiveMatrix

public PrimitiveMatrix(MatrixStore<Double> aStore)
Method Detail

getBuilder

public static MatrixBuilder<Double> getBuilder(int aRowDim,
                                               int aColDim)

enforce

public BasicMatrix enforce(NumberContext aContext)

round

public BasicMatrix round(NumberContext aContext)

toBigDecimal

public BigDecimal toBigDecimal(int aRow,
                               int aCol)
Description copied from interface: BasicMatrix
Extracts one element of this matrix as a BigDecimal.

Parameters:
aRow - A row index.
aCol - A column index.
Returns:
One matrix element

toComplexNumber

public ComplexNumber toComplexNumber(int aRow,
                                     int aCol)
Description copied from interface: BasicMatrix
Extracts one element of this matrix as a ComplexNumber.

Parameters:
aRow - A row index.
aCol - A column index.
Returns:
One matrix element

toPrimitiveStore

public PhysicalStore<Double> toPrimitiveStore()
Description copied from interface: BasicMatrix
Must be a copy that is safe to modify.

Specified by:
toPrimitiveStore in interface BasicMatrix
See Also:
BasicMatrix.toBigStore(), BasicMatrix.toComplexStore()

toString

public String toString(int aRow,
                       int aCol)

add

public BasicMatrix add(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Adds the elements of aMtrx to the elements of this matrix. The matrices must have equal dimensions.

Specified by:
add in interface BasicMatrix
Parameters:
aMtrx - What to add.
Returns:
A new matrix whos elements are the sum of this' and aMtrx'.

add

public BasicMatrix add(int aRow,
                       int aCol,
                       Number aNmbr)
Description copied from interface: BasicMatrix
Do not use this method to populate large dense matrices! Only use it to change a few (a small number) of elements.

Specified by:
add in interface BasicMatrix

add

public BasicMatrix add(Number aNmbr)
Description copied from interface: BasicMatrix
Adds aNmbr to the elements of this.

Specified by:
add in interface BasicMatrix
Parameters:
aNmbr - What to add
Returns:
A new matrix whos elements are the sum of this' elements and aNmbr.

conjugate

public BasicMatrix conjugate()
Description copied from interface: BasicMatrix
For real matrices conjugate() and transpose() are identical. For complex matrices they're not.

Specified by:
conjugate in interface BasicMatrix
Returns:
The conjugate transpose of this.
See Also:
BasicMatrix.transpose()

divide

public BasicMatrix divide(Number aNmbr)
Description copied from interface: BasicMatrix
Divides the elements of this with aNmbr.

Specified by:
divide in interface BasicMatrix
Parameters:
aNmbr - The denominator.
Returns:
A new matrix whos elements are the elements of this divided with aNmbr.

divideElements

public BasicMatrix divideElements(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Divides the elements of this with the elements of aMtrx. The matrices must have equal dimensions.

Specified by:
divideElements in interface BasicMatrix
Parameters:
aMtrx - The denominator elements.
Returns:
A new matrix whos elements are the elements of this divided with the elements of aMtrx.

doubleValue

public double doubleValue(int i,
                          int j)
Extracts one element of this matrix as a double.

Parameters:
i - A row index.
j - A column index.
Returns:
One matrix element

equals

public final boolean equals(BasicMatrix aMtrx,
                            NumberContext aCntxt)
Specified by:
equals in interface BasicMatrix
Returns:
true if the frobenius norm of the difference between [this] and [aStore] is zero within the limits of aCntxt.

equals

public final boolean equals(Object obj)
Overrides:
equals in class Object

flushCache

public void flushCache()
Description copied from interface: BasicMatrix
BasicMatrix instances are intended to be immutable. If they are it is possible to cache (partial) calculation results. Calling this method should flush any cached calculation results.

Specified by:
flushCache in interface BasicMatrix

get

public N get(int aRow,
             int aColumn)

getColDim

public int getColDim()
The size of this structure in the column-direction/dimension

Returns:
The number of columns

getColumns

public BasicMatrix getColumns(int... someCols)
Specified by:
getColumns in interface BasicMatrix
Parameters:
someCols - An ordered array of column indeces.
Returns:
A matrix with a subset of, reordered, columns.

getCondition

public Scalar<N> getCondition()
Specified by:
getCondition in interface BasicMatrix

getDeterminant

public Scalar<N> getDeterminant()
Specified by:
getDeterminant in interface BasicMatrix
Returns:
The matrix' determinant.

getEigenvalues

public List<ComplexNumber> getEigenvalues()
Specified by:
getEigenvalues in interface BasicMatrix

getFrobeniusNorm

public Scalar<N> getFrobeniusNorm()
Description copied from interface: BasicMatrix
The Frobenius norm is the square root of the sum of the squares of each element, or the square root of the sum of the square of the singular values.

Specified by:
getFrobeniusNorm in interface BasicMatrix
Returns:
The matrix' Frobenius norm
See Also:
BasicMatrix.getFrobeniusNorm()

getInfinityNorm

public Scalar<?> getInfinityNorm()
Specified by:
getInfinityNorm in interface BasicMatrix
Returns:
Max row sum
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

getKyFanNorm

public Scalar<N> getKyFanNorm(int k)
Specified by:
getKyFanNorm in interface BasicMatrix
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

getMinDim

public int getMinDim()

getOneNorm

public Scalar<?> getOneNorm()
Specified by:
getOneNorm in interface BasicMatrix
Returns:
Max col sum
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

getOperatorNorm

public Scalar<N> getOperatorNorm()
Description copied from interface: BasicMatrix
2-norm, max singular value

Specified by:
getOperatorNorm in interface BasicMatrix
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

getRank

public int getRank()
Description copied from interface: BasicMatrix
The rank of a matrix is the (maximum) number of linearly independent rows or columns it contains. It is also equal to the number of nonzero singular values of the matrix.

Specified by:
getRank in interface BasicMatrix
Returns:
The matrix' rank.

getRowDim

public int getRowDim()
The size of this structure in the row-direction/dimension

Returns:
The number of rows

getRows

public BasicMatrix getRows(int... someRows)
Specified by:
getRows in interface BasicMatrix
Parameters:
someRows - An ordered array of row indeces.
Returns:
A matrix with a subset of, reordered, rows.

getSingularValues

public List<Double> getSingularValues()
Specified by:
getSingularValues in interface BasicMatrix

getTrace

public Scalar<N> getTrace()
Description copied from interface: BasicMatrix
The sum of the diagonal elements.

Specified by:
getTrace in interface BasicMatrix
Returns:
The matrix' trace.

getTraceNorm

public Scalar<N> getTraceNorm()
Specified by:
getTraceNorm in interface BasicMatrix
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

getVectorNorm

public Scalar<N> getVectorNorm(int aDegree)
Description copied from interface: BasicMatrix
Treats [this] as if it is one dimensional (a vector) and calculates the vector norm. The interface only requires that implementations can handle arguments 0, 1, 2 and Integer.MAX_VALUE.

Specified by:
getVectorNorm in interface BasicMatrix
See Also:
BasicMatrix.getFrobeniusNorm(), BasicMatrix.getInfinityNorm(), BasicMatrix.getKyFanNorm(int), BasicMatrix.getOneNorm(), BasicMatrix.getOperatorNorm(), BasicMatrix.getTraceNorm(), BasicMatrix.getVectorNorm(int)

hashCode

public final int hashCode()
Overrides:
hashCode in class Object

invert

public BasicMatrix invert()
Description copied from interface: BasicMatrix

About inverting matrices:

Specified by:
invert in interface BasicMatrix
Returns:
The "best possible" inverse....

isEmpty

public boolean isEmpty()
Description copied from interface: BasicMatrix
Matrices are either square, tall, fat or empty. m <= 0 or n <= 0

Specified by:
isEmpty in interface BasicMatrix
Returns:
true if matrix is empty

isFat

public boolean isFat()
Description copied from interface: BasicMatrix
Matrices are either square, tall, fat or empty. 1 <= m < n

Specified by:
isFat in interface BasicMatrix
Returns:
true if matrix is fat

isFullRank

public final boolean isFullRank()
Specified by:
isFullRank in interface BasicMatrix
Returns:
true if BasicMatrix.getRank() == min(getRowDim(),getColDim())

isHermitian

public boolean isHermitian()
Specified by:
isHermitian in interface BasicMatrix

isScalar

public boolean isScalar()
Specified by:
isScalar in interface BasicMatrix
Returns:
true if this is a 1x1 matrix

isSquare

public boolean isSquare()
Description copied from interface: BasicMatrix
Matrices are either square, tall, fat or empty. m = n <> 0

Specified by:
isSquare in interface BasicMatrix
Returns:
true if matrix is square

isSymmetric

public boolean isSymmetric()
Specified by:
isSymmetric in interface BasicMatrix

isTall

public boolean isTall()
Description copied from interface: BasicMatrix
Matrices are either square, tall, fat or empty. m > n >= 1

Specified by:
isTall in interface BasicMatrix
Returns:
true if matrix is tall

isVector

public boolean isVector()
Specified by:
isVector in interface BasicMatrix
Returns:
true if the row or column dimensions are equal to 1.

mergeColumns

public BasicMatrix mergeColumns(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
[aMtrx] is appended to the bottom of [this]. The two matrices must have the same number of columns.

Specified by:
mergeColumns in interface BasicMatrix
Parameters:
aMtrx - The matrix to merge.
Returns:
A new matrix with more rows.

mergeRows

public BasicMatrix mergeRows(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
[aMtrx] is appended to the right side of [this]. The two matrices must have the same number of rows.

Specified by:
mergeRows in interface BasicMatrix
Parameters:
aMtrx - The matrix to merge.
Returns:
A new matrix with more columns.

multiply

public BasicMatrix multiply(Number aNmbr)
Description copied from interface: BasicMatrix
Multiplies the elements of this matrix with aNmbr.

Specified by:
multiply in interface BasicMatrix
Parameters:
aNmbr - What to multiply with.
Returns:
A new matrix whos elements are the elements of this multiplied with aNmbr.

multiplyElements

public BasicMatrix multiplyElements(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Multiplies the elements of this matrix with the elements of aMtrx. The matrices must have equal dimensions.

Specified by:
multiplyElements in interface BasicMatrix
Parameters:
aMtrx - The elements to multiply by.
Returns:
A new matrix whos elements are the elements of this multiplied with the elements of aMtrx.

multiplyLeft

public BasicMatrix multiplyLeft(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Matrix multiplication: [aMtrx][this]
The column dimension of the left matrix must equal the row dimension of the right matrix.

Specified by:
multiplyLeft in interface BasicMatrix
Parameters:
aMtrx - The left matrix.
Returns:
The product.
See Also:
BasicMatrix.multiplyRight(BasicMatrix)

multiplyRight

public BasicMatrix multiplyRight(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Matrix multiplication: [this][aMtrx]
The column dimension of the left matrix must equal the row dimension of the right matrix.

Specified by:
multiplyRight in interface BasicMatrix
Parameters:
aMtrx - The right matrix.
Returns:
The product.
See Also:
BasicMatrix.multiplyLeft(BasicMatrix)

multiplyVectors

public Scalar<N> multiplyVectors(BasicMatrix aVctr)
Description copied from interface: BasicMatrix
Assumes that both [this] and [aVctr] have row or column dimension, doesn't matter which, equal to 1. The two vectors must have the same number of elements.

Specified by:
multiplyVectors in interface BasicMatrix

negate

public BasicMatrix negate()
Specified by:
negate in interface BasicMatrix
Returns:
A new matrix with negated elements.

replace

public BasicMatrix replace(int aRow,
                           int aCol,
                           Number aNmbr)
Description copied from interface: BasicMatrix
Do not use this method to populate large dense matrices! Only use it to change a few (a small number) of elements.

Specified by:
replace in interface BasicMatrix

size

public int size()
Returns:
The total number of elements contained in this structure

solve

public BasicMatrix solve(BasicMatrix aRHS)
Description copied from interface: BasicMatrix

This method solves a system of linear equations: [this][X]=[aRHS]. A combination of columns in [this] should produce a column in [aRHS]. It is ok for [aRHS] to have more than 1 column.

Remember that: [X][this]=[aRHS] is equivalent to [this]T[X]T=[aRHS]T

Specified by:
solve in interface BasicMatrix
Parameters:
aRHS - The right hand side of the equation.
Returns:
The solution, [X].

subtract

public BasicMatrix subtract(BasicMatrix aMtrx)
Description copied from interface: BasicMatrix
Subtracts the elements of aMtrx from the elements of this matrix. The matrices must have equal dimensions.

Specified by:
subtract in interface BasicMatrix
Parameters:
aMtrx - What to subtract.
Returns:
A new matrix whos elements are the difference of this' and aMtrx'.

subtract

public BasicMatrix subtract(Number aNmbr)
Description copied from interface: BasicMatrix
Subtracts aNmbr from the elements of this matrix.

Specified by:
subtract in interface BasicMatrix
Parameters:
aNmbr - What to subtract.
Returns:
A new matrix whos elements are the differences between this' elements and aNmbr.

toBigStore

public PhysicalStore<BigDecimal> toBigStore()
Description copied from interface: BasicMatrix
Must be a copy that is safe to modify.

Specified by:
toBigStore in interface BasicMatrix
See Also:
BasicMatrix.toComplexStore(), BasicMatrix.toPrimitiveStore()

toComplexStore

public PhysicalStore<ComplexNumber> toComplexStore()
Description copied from interface: BasicMatrix
Must be a copy that is safe to modify.

Specified by:
toComplexStore in interface BasicMatrix
See Also:
BasicMatrix.toBigStore(), BasicMatrix.toPrimitiveStore()

toListOfColumns

public List<BasicMatrix> toListOfColumns()
Specified by:
toListOfColumns in interface BasicMatrix

toListOfElements

public List<N> toListOfElements()
Description copied from interface: BasicMatrix
It is also possible to call BasicMatrix.toBigStore(), BasicMatrix.toComplexStore() or BasicMatrix.toPrimitiveStore() and then PhysicalStore.asList().

Specified by:
toListOfElements in interface BasicMatrix

toListOfRows

public List<BasicMatrix> toListOfRows()
Specified by:
toListOfRows in interface BasicMatrix

toScalar

public Scalar<N> toScalar(int aRow,
                          int aCol)
Description copied from interface: Basic2D
Extracts one element of this matrix as a Scalar.

Specified by:
toScalar in interface Basic2D
Parameters:
aRow - A row index.
aCol - A column index.
Returns:
One matrix element

toString

public String toString()
Overrides:
toString in class Object

transpose

public BasicMatrix transpose()
Description copied from interface: BasicMatrix
Transposes this matrix. For complex matrices conjugate() and transpose() are NOT EQUAL.

Specified by:
transpose in interface BasicMatrix
Returns:
A matrix that is the transpose of this matrix.
See Also:
BasicMatrix.conjugate()