|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object mpv2.AllMatrices
public abstract class AllMatrices
mpv2 = Matrix package version 2
The abstract class AllMatrices contains common stuff for several kinds of matrices.
There is a quite large number of methods available for matrices in general and in this class specially. An overview may be helpful:
copy()
and clone()
are not in
this abstract class and may be defined in the implementing class. get(i,j)
and set(i,j,s)
,
they must be defined in the implementing class as effective as possible,
without control of the indices.
Actually these are the only methods an implementing class need to define.
All other methods here access data by using these two methods. getColumn(k,y)
, setColumn(k,y)
, and addColumn(k,s,y)
,
and times(x,y)
which set y=Ax, and transposeTimes(y,x)
which set x=A'y. getN()
or the equivalent method getRowDimension()
. getK()
or the equivalent method getColumnDimension()
. getValue(i,j)
check that arguments are in valid range and if not just return zero. getColumn(k)
return an array containing the column,
and getColumn(k,x)
put the column into array x
. getRow(n)
return an array containing the row,
and getRow(n,x)
put the row into array x
. getAll()
return an array containing all matrix elements (by columns),
and setAll(vals)
put all matrix elements into array vals
. getSubMatrix(...)
return an array containing elements (by columns)
from a part of the matrix as given by the arguments. setValue(i,j,s)
check that arguments are in valid range and if not just return. setColumn(k,y)
put values from array y
into the matrix. setRow(n,x)
put values from array x
into the matrix. setAll(vals)
put values from array vals
into the matrix. setSubMatrix(...,vals)
put values from array vals
into
a part of the matrix as given by the arguments. addColumn(k,s,y)
add a column to y, i.e. y = y + s*A(:,k)
. times(x)
return A*x
and
times(x,y)
set y = A*x
(return void). transposeTimes(y)
return A'*y
and
transposeTimes(y,x)
set x = A'*y
(return void). solve(y)
solve equation Ax=y, return pinv(A)*y
and
solve(y,x)
set x = pinv(A)*y
(return void). A
.
The eq
-prefix is meant to mimic '=', an (assign) equal sign.
All methods are void, and do not change any of the arguments, it only update this (A).
The arguments can be B
and C
which are matrices of any kind,
D
is a diagonal matrix and P
is a permutation matrix,
and s
is a double.
Note that this (A) may be used as an argument instead of another matrix,
for example instead of B. Then this argument is of course changed.
The methods included here are: A = I, eqIdentity(), identity, ones on main diagonal zero elsewhere. A = 0, eqZeros(), all values to zero. A = 1, eqOnes(), all values to 1. A = s, eqConstant(s), all values to s. A = rand, eqRandom(), all values to random values between 0 and 1. A = B, eqCopy(B), copy values, note A and B may be matrices of two different classes. A = B(..), eqCopy(B,...), copy selected rows and columns of B. A = -B, eqNegate(B). A = B*s, eqProduct(B,s). A = B+C, eqSum(B,C). A = B-C, eqDifference(B,C). A = B.*C, eqEProduct(B,C). Elementwise multipliccation. A = B./C, eqEQuotient(B,C). Elementwise (right) division. Left division by changing arguments. A = B', eqTranspose(B). A = inv(B), eqInverse(B), if inverse not exist, the Moore-Penrose pseudoinverse. A = B*C, eqProduct(B,C). A = B'*C, eqTProduct(B,C). A = inv(B)*C, eqIProduct(B,C). Perhaps also variants: eqNTProduct(B,C), eqNIProduct(B,C), eqTTProduct(B,C), eqIIProduct(B,C). A = P*B, eqPermuteRows(P,B). A = B*P, eqPermuteColumns(B,P). A = D*B, eqScaleRows(D,B). A = B*D, eqScaleColumns(B,D).A class that implements AllMatrices may define methods that returns a matrix (of the implementing class) instead of a void,
eqName(...) -> Name(...)
.
This implementation is quite simple, example with class SimpleMatrix is public static SimpleMatrix sum(AllMatrices B, AllMatrices C){ SimpleMatrix A = new SimpleMatrix(B.getN(), B.getK()); A.eqSum(B,C); // use the general method return A; }
A = s1*A + s2*(u*v'), pluseqOuterProduct(s1,s2,u,v).
norm1()
, normInf()
,
normF()
, trace()
,
det()
, rank()
,
cond()
and norm2()
. isOrthogonal()
,
isSquare()
, isNormal()
, isInvertible()
and isSymmetric()
. <\LI>
chol()
.
lu()
.
qr()
.
svd()
.
eig()
.
print(...)
. innerProduct(k1,k2)
The inner product of two column vectors.
Field Summary | |
---|---|
protected int |
K
Row and column dimensions. |
protected int |
N
Row and column dimensions. |
Constructor Summary | |
---|---|
AllMatrices()
|
Method Summary | |
---|---|
void |
addColumn(int k,
double s,
double[] y)
Add a column of the matrix multiplied by a factor to y, set y[n] = y[n] + s*A[n][k]. |
CholeskyDecomposition |
chol()
Cholesky Decomposition |
int |
columnNorm0(int k)
The pseudo-zero-norm for a given column |
double |
columnNorm1(int k)
One columnNorm for a given column |
double |
columnNorm2(int k)
Two columnNorm for a given column |
double |
columnNormInf(int k)
Inf columnNorm for a given column |
double |
cond()
Matrix condition (2 norm) |
double |
det()
Matrix determinant |
EigenvalueDecomposition |
eig()
Eigenvalue Decomposition |
void |
eqConstant(double s)
Set the matrix (this = A) to a constant s, A = s. |
void |
eqCopy(AllMatrices B)
Set the matrix (this = A) to B, A = B. |
void |
eqCopy(AllMatrices B,
int[] r,
int[] c)
Set the matrix (this = A) to a submatrix of B, A = B(r[], c[]). |
void |
eqCopy(AllMatrices B,
int[] r,
int j0,
int j1)
Set the matrix (this = A) to a submatrix of B, A = B(r[], j0:j1). |
void |
eqCopy(AllMatrices B,
int i0,
int i1,
int[] c)
Set the matrix (this = A) to a submatrix of B, A = B(i0:i1, c[]). |
void |
eqCopy(AllMatrices B,
int i0,
int i1,
int j0,
int j1)
Set the matrix (this = A) to a submatrix of B, A = B(i0:i1, j0:j1). |
void |
eqDifference(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to difference of B and C, A = B - C. |
void |
eqEProduct(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to elementwise multiplication of B and C, A = B. |
void |
eqEQuotient(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to elementwise division of B and C, A = B. |
void |
eqIdentity()
Set the matrix (this = A) to identity, A = I. |
void |
eqInverse(AllMatrices B)
Set the matrix (this = A) to the inverse of B, A = inv(B) Matrix dimension must agree, if A is N-by-K then B is K-by-N If the (normal) inverse does not exist, then the Moore-Penrose pseudoivnverse should be returned |
void |
eqIProduct(AllMatrices A,
AllMatrices B)
Set the matrix (this = X) to product of A inverse and B, X = inv(A) * B. |
void |
eqNegate(AllMatrices B)
Set the matrix (this = A) to the negative of B, A = -B. |
void |
eqOnes()
Set the matrix (this = A) to ones, A = 1. |
void |
eqPermuteColumns(AllMatrices B,
PermutationMatrix P)
Permute the columns of matrix B and put result in this, A = B * P. |
void |
eqPermuteRows(PermutationMatrix P,
AllMatrices B)
Permute the rows of matrix B and put result in this, A = P * B. |
void |
eqProduct(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to product of B and C, A = B * C. |
void |
eqProduct(AllMatrices B,
double s)
Set the matrix (this = A) to B*s, A = B*s. |
void |
eqRandom()
Set the matrix (this = A) to random elements, A = rand. |
void |
eqScaleColumns(AllMatrices B,
DiagonalMatrix D)
Scale the columns of matrix B and put result in this, A = B * D. |
void |
eqScaleRows(DiagonalMatrix D,
AllMatrices B)
Scale the rows of matrix B and put result in this, A = D * B. |
void |
eqSum(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to sum of B and C, A = B + C. |
void |
eqTProduct(AllMatrices B,
AllMatrices C)
Set the matrix (this = A) to product of B transposed and C, A = B' * C. |
void |
eqTranspose(AllMatrices B)
Set the matrix (this = A) to the transposed of B, A = B' Matrix dimension must agree. |
void |
eqZeros()
Set the matrix (this = A) to zeros, A = 0. |
abstract double |
get(int i,
int j)
Get a single element. |
double[] |
getAll()
Get all entries of the matrix, as a one-dimensional column-packed array. |
void |
getAll(double[] vals)
Get all entries of the matrix into argument vals in a column-packed way. |
double[] |
getColumn(int k)
Get a column of the matrix. |
void |
getColumn(int k,
double[] y)
Get a column of the matrix into argument y. |
int |
getColumnDimension()
Get column dimension. |
int |
getK()
Get number of columns in the matrix, i.e. length of each row vector. |
int |
getN()
Get number of rows in the matrix, i.e. length of each column vector. |
double[] |
getRow(int n)
Get a row of the matrix. |
void |
getRow(int n,
double[] x)
Get a row of the matrix into argument x. |
int |
getRowDimension()
Get row dimension. |
double[] |
getSubMatrix(int[] r,
int[] c)
Get a one-dimensional column packed copy of a submatrix. |
double[] |
getSubMatrix(int[] r,
int j0,
int j1)
Get a one-dimensional column packed copy of a submatrix. |
double[] |
getSubMatrix(int i0,
int i1,
int[] c)
Get a one-dimensional column packed copy of a submatrix. |
double[] |
getSubMatrix(int i0,
int i1,
int j0,
int j1)
Get a one-dimensional column packed copy of a submatrix. |
double |
getValue(int n,
int k)
Get a single element, i.e. the value of an entry of the matrix. |
double |
innerProduct(int k1,
int k2)
Returns the inner product of two matrix column vectors. |
LUDecomposition |
lu()
LU Decomposition |
LUPDecomposition |
lup()
LUP Decomposition |
double |
norm1()
One norm |
double |
norm2()
Two norm |
double |
normF()
Frobenius norm |
double |
normInf()
Infinity norm |
void |
pluseqOuterProduct(double s1,
double s2,
double[] u,
double[] v)
Add outer product of two vectors to this, A = s1*A + s2*(u*v'). |
void |
print(int w,
int d)
Print the matrix to stdout. |
void |
print(java.text.NumberFormat format,
int width)
Print the matrix to stdout. |
void |
print(java.io.PrintWriter output,
int w,
int d)
Print the matrix to the output stream. |
void |
print(java.io.PrintWriter output,
java.text.NumberFormat format,
int width)
Print the matrix to the output stream. |
QRDecomposition |
qr()
QR Decomposition |
int |
rank()
Matrix rank |
abstract void |
set(int i,
int j,
double s)
Set a single element. |
void |
setAll(double[] vals)
Set all entries of the matrix to the supplied new values. |
void |
setColumn(int k,
double[] y)
Copy an array (y) into a column of the matrix. |
void |
setRow(int n,
double[] x)
Copy an array (x) into a row of the matrix. |
void |
setValue(int n,
int k,
double s)
Set a single element, i.e. an entry (a value) in the matrix. |
double |
sumAll()
Sum of all elements in matrix |
SingularValueDecomposition |
svd()
Singular Value Decomposition |
double[] |
times(double[] x)
Multiplies the matrix by an array (vector), return y = A*x. |
void |
times(double[] x,
double[] y)
Multiplies the matrix by an array (vector), set y = A*x. |
double[] |
times(SparseVector x)
Multiplies the matrix by a SparseVector x, return y = A*x. |
void |
times(SparseVector x,
double[] y)
Multiplies the matrix by a SparseVector x, set y = A*x. |
double |
trace()
Matrix trace. |
double[] |
transposeTimes(double[] y)
Multiplies the transposed matrix by an array (vector), return x = A'*y. |
void |
transposeTimes(double[] y,
double[] x)
Multiplies the transposed matrix by an array (vector), set x = A'*y. |
double[] |
transposeTimes(SparseVector y)
Multiplies the transposed matrix by a SparseVector y, return x = A'*y. |
void |
transposeTimes(SparseVector y,
double[] x)
Multiplies the transposed matrix by a SparseVector y, set x = A'*y. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected int N
protected int K
Constructor Detail |
---|
public AllMatrices()
Method Detail |
---|
public abstract double get(int i, int j)
i
- Row index.j
- Column index.
java.lang.ArrayIndexOutOfBoundsException
public abstract void set(int i, int j, double s)
i
- Row index.j
- Column index.s
- the value to be set into A(i,j).
java.lang.ArrayIndexOutOfBoundsException
public int getN()
public int getK()
public int getRowDimension()
public int getColumnDimension()
public double getValue(int n, int k)
n
- Row index for the returned entry value.k
- Column index for the returned entry value.
public double[] getColumn(int k)
N
array of zeros should be returned.
k
- number of the column in the matrix
public void getColumn(int k, double[] y)
0 <= k < K
.
k
- number of the column in the matrixy
- is set to the given column of the matrix
java.lang.IllegalArgumentException
public double[] getRow(int n)
0 <= n < N
.
If argument is out of range a length K
array of zeros should be returned.
n
- number of the row in the matrix A.
public void getRow(int n, double[] x)
0 <= n < N
.
n
- number of the row in the matrix.x
- is set to the given row of matrix.
java.lang.IllegalArgumentException
public double[] getAll()
public void getAll(double[] vals)
vals
- an one-dimensional array where the values are copied into
java.lang.IllegalArgumentException
public double[] getSubMatrix(int i0, int i1, int j0, int j1)
i0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column index
public double[] getSubMatrix(int[] r, int[] c)
r
- Array of row indices.c
- Array of column indices.
public double[] getSubMatrix(int i0, int i1, int[] c)
i0
- Initial row indexi1
- Final row indexc
- Array of column indices.
public double[] getSubMatrix(int[] r, int j0, int j1)
r
- Array of row indices.j0
- Initial column indexj1
- Final column index
public void setValue(int n, int k, double s)
0 <= n < N
and 0 <= k < K
.
If any argument is outside legal range nothing is done.
n
- Row index for the entry value to be changed.k
- Column index for the entry value to be changed.s
- Value to be put into the given entry of the matrix.public void setColumn(int k, double[] y)
0 <= k < K
.
k
- number of the column in the matrixy
- the values that are copied into the given column of the matrix
java.lang.IllegalArgumentException
public void setRow(int n, double[] x)
0 <= n < N
.
n
- number of the row in the matrix A
.x
- the given row of matrix A
.
java.lang.IllegalArgumentException
public void setAll(double[] vals)
vals
- One-dimensional array of doubles, packed by columns (ala Fortran).
java.lang.IllegalArgumentException
public void addColumn(int k, double s, double[] y)
0 <= k < K
.
k
- number of the column the matrix A.s
- a scale factor to multiply the column vector by.y
- an array of length N.
java.lang.IllegalArgumentException
public double[] times(double[] x)
x
- the input array
public double[] times(SparseVector x)
x
- the input sparse vector of class SparseVector
public void times(double[] x, double[] y)
x
- the input arrayy
- the results as an array of length N.
java.lang.IllegalArgumentException
public void times(SparseVector x, double[] y)
x
- the input sparse vectory
- the results as an array of length N.
java.lang.IllegalArgumentException
public double[] transposeTimes(double[] y)
y
- the input array
public double[] transposeTimes(SparseVector y)
y
- the input sparse vector
public void transposeTimes(double[] y, double[] x)
y
- the input arrayx
- the results as an array of length K.
java.lang.IllegalArgumentException
public void transposeTimes(SparseVector y, double[] x)
y
- the input sparse vectorx
- the results as an array of length K.
java.lang.IllegalArgumentException
public void eqIdentity()
public void eqZeros()
public void eqOnes()
public void eqConstant(double s)
s
- A single valuepublic void eqRandom()
public void eqCopy(AllMatrices B)
B
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqCopy(AllMatrices B, int i0, int i1, int j0, int j1)
B
- Matrixi0
- Initial row indexi1
- Final row indexj0
- Initial column indexj1
- Final column index
java.lang.IllegalArgumentException
public void eqCopy(AllMatrices B, int[] r, int[] c)
B
- Matrixr
- Array of row indices.c
- Array of column indices.
java.lang.IllegalArgumentException
public void eqCopy(AllMatrices B, int i0, int i1, int[] c)
B
- Matrixi0
- Initial row indexi1
- Final row indexc
- Array of column indices.
java.lang.IllegalArgumentException
public void eqCopy(AllMatrices B, int[] r, int j0, int j1)
B
- Matrixr
- Array of row indices.j0
- Initial column indexj1
- Final column index
java.lang.IllegalArgumentException
public void eqNegate(AllMatrices B)
B
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqProduct(AllMatrices B, double s)
B
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqSum(AllMatrices B, AllMatrices C)
B
- Matrix of same size as AC
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqDifference(AllMatrices B, AllMatrices C)
B
- Matrix of same size as AC
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqEProduct(AllMatrices B, AllMatrices C)
B
- Matrix of same size as AC
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqEQuotient(AllMatrices B, AllMatrices C)
B
- Matrix of same size as AC
- Matrix of same size as A
java.lang.IllegalArgumentException
public void eqTranspose(AllMatrices B)
B
- Matrix of 'opposite' size as A
java.lang.IllegalArgumentException
public void eqInverse(AllMatrices B)
B
- Matrix of 'opposite' size as A
java.lang.IllegalArgumentException
public void eqProduct(AllMatrices B, AllMatrices C)
B
- Matrix of size N-by-LC
- Matrix of size L-by-K, size of result is N-by-K
java.lang.IllegalArgumentException
public void eqTProduct(AllMatrices B, AllMatrices C)
B
- Matrix of size L-by-N (the transposed is N-by-L)C
- Matrix of size L-by-K, size of result is N-by-K
java.lang.IllegalArgumentException
public void eqIProduct(AllMatrices A, AllMatrices B)
A
- Matrix of size L-by-N (the (pseudo)inverse is N-by-L)B
- Matrix of size L-by-K, size of result is N-by-K
java.lang.IllegalArgumentException
public void eqPermuteRows(PermutationMatrix P, AllMatrices B)
P
- Permutation matrix of size N-by-NB
- Matrix of size N-by-K, size of result (this) is also N-by-K
java.lang.IllegalArgumentException
public void eqPermuteColumns(AllMatrices B, PermutationMatrix P)
B
- Matrix of size N-by-K, size of result (this) is also N-by-KP
- Permutation matrix of size K-by-K
java.lang.IllegalArgumentException
public void eqScaleRows(DiagonalMatrix D, AllMatrices B)
D
- Diagonal matrix of size N-by-N, class DiagonalMatrixB
- Matrix of size N-by-K, size of result (this) is also N-by-K
java.lang.IllegalArgumentException
public void eqScaleColumns(AllMatrices B, DiagonalMatrix D)
B
- Matrix of size N-by-K, size of result (this) is also N-by-KD
- Diagonal matrix of size N-by-N, class DiagonalMatrix
java.lang.IllegalArgumentException
public void pluseqOuterProduct(double s1, double s2, double[] u, double[] v)
s1
- A single values2
- A single valueu
- A vector with N elementsv
- A vector with K elements
java.lang.IllegalArgumentException
public double sumAll()
public double norm1()
public double norm2()
public double normInf()
public double normF()
public double trace()
public double det()
public int rank()
public double cond()
public int columnNorm0(int k)
k
- number of the column in the matrix
public double columnNorm1(int k)
k
- number of the column in the matrix
public double columnNorm2(int k)
k
- number of the column in the matrix
public double columnNormInf(int k)
k
- number of the column in the matrix
public LUDecomposition lu()
LUDecomposition
public LUPDecomposition lup()
LUPDecomposition
public QRDecomposition qr()
QRDecomposition
public CholeskyDecomposition chol()
CholeskyDecomposition
public SingularValueDecomposition svd()
SingularValueDecomposition
public EigenvalueDecomposition eig()
EigenvalueDecomposition
public void print(int w, int d)
w
- Column width.d
- Number of digits after the decimal.public void print(java.io.PrintWriter output, int w, int d)
output
- Output stream.w
- Column width.d
- Number of digits after the decimal.public void print(java.text.NumberFormat format, int width)
format
- A Formatting object for individual elements.width
- Field width for each column.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public void print(java.io.PrintWriter output, java.text.NumberFormat format, int width)
output
- the output stream.format
- A formatting object to format the matrix elementswidth
- Column width.DecimalFormat.setDecimalFormatSymbols(java.text.DecimalFormatSymbols)
public double innerProduct(int k1, int k2)
0 <= k1 < K
and 0 <= k2 < K
.
If k1
or k2
are out of range, 0.0 should be returned.
k1
- number for the first dictionary element.k2
- number for the second dictionary element.
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |