mpv2
Class SymmetricMatrix

java.lang.Object
  extended by mpv2.AllMatrices
      extended by mpv2.SymmetricMatrix
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class SymmetricMatrix
extends AllMatrices
implements java.lang.Cloneable, java.io.Serializable

This is a symmetric matrix class made by implementing AllMatrices. Entries are stored columnwise in a one-dimensional array, for each column only elements on or below the main diagonal are stored.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class mpv2.AllMatrices
K, N
 
Constructor Summary
SymmetricMatrix(AllMatrices B)
          Construct a new matrix from another matrix (of any kind), only lower-left-values are used.
SymmetricMatrix(double[][] A)
          Construct a matrix from a 2-D array, only lower-left-values are used.
SymmetricMatrix(int n)
          Construct an n-by-n matrix of zeros.
SymmetricMatrix(int n, int k)
          Construct an n-by-n matrix of zeros.
SymmetricMatrix(int n, int k, double s)
          Construct an m-by-n constant matrix.
 
Method Summary
 java.lang.Object clone()
          Clone the Matrix object.
 SymmetricMatrix copy()
          Make a deep copy of a matrix
 void eqInnerProductMatrix(AllMatrices B)
          Set inner-products of matrix B into this, i.e.
 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, double s)
          Set the matrix (this = A) to B*s, A = B*s.
 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 eqScaleRowsAndColumns(DiagonalMatrix D, SymmetricMatrix B)
          Scale the rows and columns of symmtric matrix B and put result in this, A = D * B * D.
 void eqScaleRowsAndColumns(SymmetricMatrix B, DiagonalMatrix D)
          Scale the rows and columns of symmtric matrix B and put result in this, A = D * B * D.
 double get(int n, int k)
          Get a single element.
 void getColumn(int k, double[] d)
          Copy a column of the matrix into argument d Legal range of the integer argument is 0 <= k < K.
 void getRow(int n, double[] d)
          Copy a row of the matrix into argument d
static SymmetricMatrix identity(int n)
          Generate identity matrix
static SymmetricMatrix innerProductMatrix(AllMatrices B)
          Generate inner-product matrix from input matrix B, i.e.
 void pluseqOuterProduct(double s1, double s2, double[] u)
          Add outer product of a vector to scaled this, A = s1*A + s2*(u*u').
 void pluseqOuterProduct(double s1, double s2, double[] u, double[] v)
          Add outer product of two vectors to this, A = s1*A + s2*(u*v'+v*u').
 void set(int n, int k, double s)
          Set one or two elements in the symmetric matrix A(n,k)=A(k,n)=s.
 void setAll(double[] vals)
          Set all entries of the matrix to the supplied new values.
 void setColumn(int k, double[] d)
          Copy an array (d) into a column, and a row, of the matrix
 void setRow(int n, double[] r)
          Set a row, and a column, of the matrix
 
Methods inherited from class mpv2.AllMatrices
addColumn, chol, columnNorm0, columnNorm1, columnNorm2, columnNormInf, cond, det, eig, eqConstant, eqCopy, eqCopy, eqCopy, eqCopy, eqCopy, eqDifference, eqEProduct, eqEQuotient, eqIdentity, eqInverse, eqIProduct, eqNegate, eqOnes, eqProduct, eqRandom, eqSum, eqTProduct, eqTranspose, eqZeros, getAll, getAll, getColumn, getColumnDimension, getK, getN, getRow, getRowDimension, getSubMatrix, getSubMatrix, getSubMatrix, getSubMatrix, getValue, innerProduct, lu, lup, norm1, norm2, normF, normInf, print, print, print, print, qr, rank, setValue, sumAll, svd, times, times, times, times, trace, transposeTimes, transposeTimes, transposeTimes, transposeTimes
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SymmetricMatrix

public SymmetricMatrix(int n)
Construct an n-by-n matrix of zeros. Note that there exist a constructor that takes a 2D array argument and that a single double number as input is interpreted as an array. Thus to call this one from Matlab use: jA = SymmetricMatrix(int32(5));

Parameters:
n - Number of rows.

SymmetricMatrix

public SymmetricMatrix(int n,
                       int k)
Construct an n-by-n matrix of zeros.

Parameters:
n - Number of rows.
k - Number of colums is also n, so k is ignored!

SymmetricMatrix

public SymmetricMatrix(int n,
                       int k,
                       double s)
Construct an m-by-n constant matrix.

Parameters:
n - Number of rows.
k - Number of colums is also n, so k is ignored!
s - Fill the matrix with this scalar value.

SymmetricMatrix

public SymmetricMatrix(double[][] A)
Construct a matrix from a 2-D array, only lower-left-values are used.

Parameters:
A - Two-dimensional array of doubles.
Throws:
java.lang.IllegalArgumentException - All rows must have the same length

SymmetricMatrix

public SymmetricMatrix(AllMatrices B)
Construct a new matrix from another matrix (of any kind), only lower-left-values are used. If argument B is an object of class SymmetricMatrix this is the same as (deep) copy (or clone). Note that argument B may be another kind of matrix, ex. an object of class SparseMatrix or BandMatrix or any subclass of AllMatrices

Parameters:
B - A matrix of any kind, class is a subclass of AllMatrices
Throws:
java.lang.IllegalArgumentException - Number of rows and Columns must be the same
See Also:
copy()
Method Detail

identity

public static SymmetricMatrix identity(int n)
Generate identity matrix

Parameters:
n - Size is n-by-n

innerProductMatrix

public static SymmetricMatrix innerProductMatrix(AllMatrices B)
Generate inner-product matrix from input matrix B, i.e. A = B'*B,

Parameters:
B - A matrix of any kind, class is a subclass of AllMatrices

copy

public SymmetricMatrix copy()
Make a deep copy of a matrix


clone

public java.lang.Object clone()
Clone the Matrix object.

Overrides:
clone in class java.lang.Object

get

public double get(int n,
                  int k)
Get a single element.

Specified by:
get in class AllMatrices
Parameters:
n - Row index.
k - Column index.
Returns:
A(n,k)
Throws:
java.lang.ArrayIndexOutOfBoundsException

set

public void set(int n,
                int k,
                double s)
Set one or two elements in the symmetric matrix A(n,k)=A(k,n)=s.

Specified by:
set in class AllMatrices
Parameters:
n - Row index.
k - Column index.
s - A(n,k).
Throws:
java.lang.ArrayIndexOutOfBoundsException

getColumn

public void getColumn(int k,
                      double[] d)
Copy a column of the matrix into argument d Legal range of the integer argument is 0 <= k < K. If argument is out of range a length N array of zeros should be returned.
The corresponding Matlab expression would be: A(:,k+1).

Overrides:
getColumn in class AllMatrices
Parameters:
k - number of the column in the matrix
d - the given column of the matrix
Throws:
java.lang.IllegalArgumentException

setColumn

public void setColumn(int k,
                      double[] d)
Copy an array (d) into a column, and a row, of the matrix

Overrides:
setColumn in class AllMatrices
Parameters:
k - number of the column, and row, in the matrix
d - the given column, and row, of the matrix

getRow

public void getRow(int n,
                   double[] d)
Copy a row of the matrix into argument d

Overrides:
getRow in class AllMatrices
Parameters:
n - number of the row in the matrix
d - the given column of the matrix

setRow

public void setRow(int n,
                   double[] r)
Set a row, and a column, of the matrix

Overrides:
setRow in class AllMatrices
Parameters:
n - number of the row in the matrix A.
r - the given row of matrix A.

setAll

public void setAll(double[] vals)
Set all entries of the matrix to the supplied new values. Note that even if vals contains all the N*N values of the symmetric matrix, only those in the lower left part is used, and thus the result is symmetric anyway. If argument is wrong length an IllegalArgumentException is thrown.

Overrides:
setAll in class AllMatrices
Parameters:
vals - One-dimensional array of doubles, packed by columns (ala Fortran).
Throws:
java.lang.IllegalArgumentException

eqProduct

public void eqProduct(AllMatrices B,
                      double s)
Set the matrix (this = A) to B*s, A = B*s. Note that since this matrix (A) is symmetric only lower left part of B is used.

Overrides:
eqProduct in class AllMatrices
Parameters:
B - Matrix of same size as A
Throws:
java.lang.IllegalArgumentException

eqPermuteRows

public void eqPermuteRows(PermutationMatrix P,
                          AllMatrices B)
Permute the rows of matrix B and put result in this, A = P * B. Note that the result usually is not a symmetric matrix, but symmetry in A is kept anyway so result will probably be wrong.

Overrides:
eqPermuteRows in class AllMatrices
Parameters:
P - Permutation matrix of size N-by-N
B - Matrix of size N-by-K, size of result (this) is also N-by-K
Throws:
java.lang.IllegalArgumentException

eqPermuteColumns

public void eqPermuteColumns(AllMatrices B,
                             PermutationMatrix P)
Permute the columns of matrix B and put result in this, A = B * P. Note that the result usually is not a symmetric matrix, but symmetry in A is kept anyway so result will probably be wrong.

Overrides:
eqPermuteColumns in class AllMatrices
Parameters:
B - Matrix of size N-by-K, size of result (this) is also N-by-K
P - Permutation matrix of size K-by-K
Throws:
java.lang.IllegalArgumentException

eqScaleRowsAndColumns

public void eqScaleRowsAndColumns(SymmetricMatrix B,
                                  DiagonalMatrix D)
Scale the rows and columns of symmtric matrix B and put result in this, A = D * B * D.

Parameters:
B - Symmetric matrix of size N-by-N, size of result (this) is also N-by-N
D - Diagonal matrix of size N-by-N, class DiagonalMatrix

eqScaleRowsAndColumns

public void eqScaleRowsAndColumns(DiagonalMatrix D,
                                  SymmetricMatrix B)
Scale the rows and columns of symmtric matrix B and put result in this, A = D * B * D.

Parameters:
D - Diagonal matrix of size N-by-N, class DiagonalMatrix
B - Symmetric matrix of size N-by-N, size of result (this) is also N-by-N

eqScaleRows

public void eqScaleRows(DiagonalMatrix D,
                        AllMatrices B)
Scale the rows of matrix B and put result in this, A = D * B. Note that the result usually is not a symmetric matrix, but symmetry in A is kept anyway so result will probably be wrong.

Overrides:
eqScaleRows in class AllMatrices
Parameters:
D - Diagonal matrix of size N-by-N, class DiagonalMatrix
B - Matrix of size N-by-K, size of result (this) is also N-by-K
Throws:
java.lang.IllegalArgumentException

eqScaleColumns

public void eqScaleColumns(AllMatrices B,
                           DiagonalMatrix D)
Scale the columns of matrix B and put result in this, A = B * D. Note that the result usually is not a symmetric matrix, but symmetry in A is kept anyway so result will probably be wrong.

Overrides:
eqScaleColumns in class AllMatrices
Parameters:
B - Matrix of size N-by-K, size of result (this) is also N-by-K
D - Diagonal matrix of size N-by-N, class DiagonalMatrix
Throws:
java.lang.IllegalArgumentException

eqInnerProductMatrix

public void eqInnerProductMatrix(AllMatrices B)
Set inner-products of matrix B into this, i.e. A = B'*B,

Parameters:
B - A matrix of any kind, class is a subclass of AllMatrices
Throws:
java.lang.IllegalArgumentException

pluseqOuterProduct

public void pluseqOuterProduct(double s1,
                               double s2,
                               double[] u,
                               double[] v)
Add outer product of two vectors to this, A = s1*A + s2*(u*v'+v*u'). Note that this method is different for a SymmetricMatrix than for other matrices, (AllMatrices).

Overrides:
pluseqOuterProduct in class AllMatrices
Parameters:
s1 - A single value
s2 - A single value
u - A vector with N elements
v - A vector with N elements
Throws:
java.lang.IllegalArgumentException

pluseqOuterProduct

public void pluseqOuterProduct(double s1,
                               double s2,
                               double[] u)
Add outer product of a vector to scaled this, A = s1*A + s2*(u*u'). This matrix, A, is of size N-by-N. Each element is updated as: A(n,k) = s1*A(n,k) + s2*u(n)*u(k) Note that this method is like the one with two arguments vectors for other matrices, (AllMatrices).

Parameters:
s1 - A single value
s2 - A single value
u - A vector with N elements
Throws:
java.lang.IllegalArgumentException