public abstract class BaseMatrix<M extends BaseMatrix<M>> extends java.lang.Object implements IArchive
Class BaseMatrix
. This is a base class for objects representing
real-number matrix objects. Thus it contains basic matrix operations where the interacting
objects are all of type M
, or vectors of the singular type V
.
(If matrix and vectors are not of compatible dimensions the operations fail.)
The template parameter M
is the type of the child class. This
mechanism allows BaseMatrix<M extends BaseMatrix<M>>
to recognize the type of it derived classes in order to create and process
new objects as necessary.
Currently the internal matrix operations are supported by the Jama matrix package. However, the Jama matrix package has been deemed a "proof of principle" for the Java language and scientific computing and is, thus, no longer supported. The objective of this base class is to hide the internal implementation of matrix operations from the child classes and all developers using the matrix packages. If it is determined that the Jama matrix package is to be removed from XAL, the modification will be substantially simplified in the current architecture.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
ATTR_DATA
Attribute marker for data managed by IArchive interface
|
protected Jama.Matrix |
matImpl
internal matrix implementation
|
Modifier | Constructor and Description |
---|---|
protected |
BaseMatrix(int cntRows,
int cntCols)
Creates a new, uninitialized instance of a square matrix with the given
matrix dimensions.
|
protected |
BaseMatrix(int cntRows,
int cntCols,
double[][] arrVals)
Initializing constructor for bases class
SquareMatrix . |
protected |
BaseMatrix(int cntRows,
int cntCols,
java.lang.String strTokens)
Parsing Constructor - creates an instance of the child class and initialize it
according to a token string of element values.
|
protected |
BaseMatrix(M matParent)
Copy constructor for
BaseMatrix . |
Modifier and Type | Method and Description |
---|---|
protected void |
assignMatrix(Jama.Matrix matValue)
Sets the internal matrix value to that given in the argument.
|
void |
assignZero()
Assign this matrix to be the zero matrix, specifically
the matrix containing all 0's.
|
abstract M |
clone()
Base classes must override the clone operation in order to
make deep copies of the current object.
|
double |
conditionNumber()
Ratio of the largest singular value over the smallest singular value.
|
M |
copy()
Create a deep copy of the this matrix object.
|
boolean |
equals(java.lang.Object objTest)
Checks absolute equivalency.
|
double[][] |
getArrayCopy()
Returns a copy of the internal Java array containing
the matrix elements.
|
int |
getColCnt()
Returns the number of columns in this matrix.
|
double |
getElem(IIndex indRow,
IIndex indCol)
Returns the matrix element at the position indicated by the
given row and column index sources.
|
double |
getElem(int i,
int j)
Return matrix element value.
|
protected Jama.Matrix |
getMatrix()
Return the internal matrix representation.
|
int |
getRowCnt()
Returns the number of rows in this matrix.
|
int |
hashCode()
"Borrowed" implementation from AffineTransform, since it is based on
double attribute values.
|
void |
load(DataAdaptor daptArchive)
Restore the value of the this
PhaseMatrix from the
contents of a data archive. |
double |
max()
Return the maximum absolute value of all matrix elements.
|
M |
minus(M matSub)
Non-destructive matrix subtraction.
|
void |
minusEquals(M mat)
In-place matrix subtraction.
|
protected abstract M |
newInstance()
Creates a new, uninitialized instance of this matrix type.
|
protected M |
newInstance(Jama.Matrix impInit)
Creates a new instance of this matrix type initialized to the given
implementation matrix.
|
double |
norm1()
The matrix norm || · ||1 induced from
the l1 vector norm on Rn.
|
double |
norm2()
Returns the l2 induced norm of this matrix,
which is the maximum, which turns out to be the spectral radius
of the matrix.
|
double |
normF()
Return the Frobenius norm ||A||F .
|
double |
normInf()
The matrix norm || · ||∞ induced from
the l∞ vector norm on Rn.
|
M |
plus(M matAddend)
Non-destructive matrix addition.
|
void |
plusEquals(M mat)
In-place matrix addition.
|
void |
print(java.io.PrintWriter os)
Print out the contents of the R2x2 in text format.
|
void |
save(DataAdaptor daptArchive)
Save the value of this
PhaseMatrix to a data sink
represented by the DataAdaptor interface. |
void |
setElem(int i,
int j,
double s)
Element assignment - assigns matrix element to the specified value
|
void |
setMatrix(double[][] arrMatrix)
Sets the entire matrix to the values given in the Java primitive type
double array.
|
void |
setMatrix(java.lang.String strValues)
Parsing assignment - set the
PhaseMatrix value
according to a token string of element values. |
void |
setSubMatrix(int i0,
int i1,
int j0,
int j1,
double[][] arrSub)
Set a block sub-matrix within the current matrix.
|
java.lang.String |
toString()
Convert the contents of the matrix to a string representation.
|
java.lang.String |
toStringMatrix()
Returns a string representation of this matrix.
|
java.lang.String |
toStringMatrix(java.text.NumberFormat fmt)
Returns a string representation of this matrix.
|
java.lang.String |
toStringMatrix(java.text.NumberFormat fmt,
int intColWd)
Returns a string representation of this matrix.
|
public static final java.lang.String ATTR_DATA
protected final Jama.Matrix matImpl
protected BaseMatrix(int cntRows, int cntCols)
cntRows
- the matrix row count of this objectcntCols
- the matrix column countjava.lang.UnsupportedOperationException
- child class has not defined a public, zero-argument constructorprotected BaseMatrix(M matParent)
BaseMatrix
. Creates a deep
copy of the given object. The dimensions are set and the
internal array is cloned.matParent
- the matrix to be clonedjava.lang.UnsupportedOperationException
- base class has not defined a public, zero-argument constructorprotected BaseMatrix(int cntRows, int cntCols, java.lang.String strTokens) throws java.lang.IllegalArgumentException, java.lang.NumberFormatException
cntRows
- the matrix row size of this objectcntCols
- the matrix column size of this objectstrTokens
- token vector of getSize()^2 numeric valuesjava.lang.IllegalArgumentException
- wrong number of token stringsjava.lang.NumberFormatException
- bad number format, unparseableprotected BaseMatrix(int cntRows, int cntCols, double[][] arrVals) throws java.lang.ArrayIndexOutOfBoundsException
Initializing constructor for bases class SquareMatrix
.
Sets the entire matrix to the values given in the Java primitive type
double array. The argument itself remains unchanged.
The dimensions of the given Java double array must be consistent with the size of the matrix. Thus, if the arguments are inconsistent, an exception is thrown.
cntRows
- the matrix row size of this objectcntCols
- the matrix column size of this objectarrMatrix
- Java primitive array containing new matrix valuesjava.lang.ArrayIndexOutOfBoundsException
- the argument must have the same dimensions as this matrixpublic abstract M clone()
clone
in class java.lang.Object
Object.clone()
public void setElem(int i, int j, double s) throws java.lang.ArrayIndexOutOfBoundsException
i
- row indexj
- column indexjava.lang.ArrayIndexOutOfBoundsException
- an index was equal to or larger than the matrix sizepublic void setSubMatrix(int i0, int i1, int j0, int j1, double[][] arrSub) throws java.lang.ArrayIndexOutOfBoundsException
i0
- row index of upper left blocki1
- row index of lower right blockj0
- column index of upper left blockj1
- column index of lower right blockarrSub
- two-dimensional sub element arrayjava.lang.ArrayIndexOutOfBoundsException
- sub-matrix does not fit into base matrixpublic void setMatrix(double[][] arrMatrix) throws java.lang.ArrayIndexOutOfBoundsException
arrMatrix
- Java primitive array containing new matrix valuesjava.lang.ArrayIndexOutOfBoundsException
- the argument must have the same dimensions as this matrixpublic void setMatrix(java.lang.String strValues) throws java.lang.NumberFormatException, java.lang.IllegalArgumentException
PhaseMatrix
value
according to a token string of element values.
The token string argument is assumed to be one-dimensional and packed by
column (aka FORTRAN).strValues
- token vector of SIZE2 numeric valuesjava.lang.IllegalArgumentException
- wrong number of token stringsjava.lang.NumberFormatException
- bad number format, unparseablepublic int getRowCnt()
public int getColCnt()
public double getElem(int i, int j) throws java.lang.ArrayIndexOutOfBoundsException
Diagonal
position.i
- row indexj
- column indexjava.lang.ArrayIndexOutOfBoundsException
- an index was equal to or larger than the matrix sizepublic double getElem(IIndex indRow, IIndex indCol)
Returns the matrix element at the position indicated by the given row and column index sources.
IIndex
interface is an enumeration
class restricting the number of possible index values.
indRow
- source of the row indexindCol
- source of the column indexpublic double[][] getArrayCopy()
#getSize()
.public M copy()
public void assignZero()
public double conditionNumber()
Jama.Matrix
internal implementation). Thus, this computation is not cheap
if the matrix is large.public M plus(M matAddend)
matAddend
- matrix to be added to thisnull
if errorpublic void plusEquals(M mat)
mat
- matrix to be added to this (no new objects are created)public M minus(M matSub)
matSub
- the subtrahendnull
if an error occurredpublic void minusEquals(M mat)
mat
- subtrahendpublic double max()
Return the maximum absolute value of all matrix elements. This can
be considered a norm on matrices, but it is not sub-multiplicative.
That is,
||AB||max is not necessarily bound by ||A||max ||B||max .
public double norm1()
The matrix norm || · ||1 induced from
the l1 vector norm on Rn. That is,
||A||1 ≡ maxx∈Rn ||Ax||1
where, by context, the second occurrence of ||·||1 is the
Lesbeque 1-norm on Rn.
public double norm2()
Returns the l2 induced norm of this matrix,
which is the maximum, which turns out to be the spectral radius
of the matrix. Specifically,
||A||2 ≡ [ max λ(ATA) ]1/2 ,
= max ρ(A) ,
where λ(·) is the eigenvalue operator and ρ(·) is the
singular value operator.
public double normInf()
The matrix norm || · ||∞ induced from
the l∞ vector norm on Rn. That is,
||A||∞ ≡ maxx∈Rn
||Ax||∞
where, by context, the second occurrence of ||·||∞ is the
Lesbeque ∞-norm on Rn.
public double normF()
Return the Frobenius norm ||A||F .
The Frobenius norm has the property that it is
both the element-wise Lebesgue 2-norm the Schatten 2-norm. Thus we have
||A||F = [ Σi Σj Ai,j2 ]1/2
= [ Tr(ATA) ]1/2
= [ Σi σi2 ]1/2
where Tr is the trace operator and σi are the singular values of
matrix A.
public void print(java.io.PrintWriter os)
os
- output stream to receive text dumppublic void save(DataAdaptor daptArchive)
PhaseMatrix
to a data sink
represented by the DataAdaptor
interface.save
in interface IArchive
daptArchive
- interface to data sinkIArchive.save(xal.tools.data.DataAdaptor)
public void load(DataAdaptor daptArchive) throws DataFormatException
PhaseMatrix
from the
contents of a data archive.load
in interface IArchive
daptArchive
- interface to data sourceDataFormatException
- malformed datajava.lang.IllegalArgumentException
- wrong number of string tokensIArchive.load(xal.tools.data.DataAdaptor)
public boolean equals(java.lang.Object objTest)
equals
in class java.lang.Object
objTest
- object under equivalency testtrue
if the argument is this object,
false
otherwisepublic java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toStringMatrix()
public java.lang.String toStringMatrix(java.text.NumberFormat fmt)
fmt
- NumberFormat
object containing output format for matrix entriespublic java.lang.String toStringMatrix(java.text.NumberFormat fmt, int intColWd)
fmt
- NumberFormat
object containing output format for matrix entriesintColWd
- number of characters used for each column (padding is with spaces)public int hashCode()
Object
.hashCode
in class java.lang.Object
protected Jama.Matrix getMatrix()
protected void assignMatrix(Jama.Matrix matValue)
matValue
- internal implementation of matrix valuesprotected abstract M newInstance()
Creates a new, uninitialized instance of this matrix type.
NOTE: · This method was made abstract by Ivo List. Rather than use reflection to instantiate new objects, this function is now delegated to the concrete classes. This architecture is more robust and allows the compiler to do more error checking.
M
protected M newInstance(Jama.Matrix impInit)
impInit
- implementation matrix containing initialization valuesM