mpv2
Class DiagonalMatrix

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

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

This is a diagonal matrix class made by implementing AllMatrices.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class mpv2.AllMatrices
K, N
 
Constructor Summary
DiagonalMatrix(AllMatrices B)
          Construct a diagonal matrix from another matrix (of any kind) by extracting the elements on the diagonal.
DiagonalMatrix(double[] vals)
          Construct a diagonal matrix from a one-dimensional array
DiagonalMatrix(double[][] A)
          Construct a diagonal matrix from a 2-D array, only values in the diagonal are copied.
DiagonalMatrix(int n)
          Construct an n-by-n diagonal matrix of zeros.
DiagonalMatrix(int n, double s)
          Construct an n-by-n diagonal matrix with constant value in each element
 
Method Summary
 void addColumn(int k, double factor, double[] x)
          Add a column of the matrix multiplied by a factor to x, x = x + A[][k] Legal range of the integer argument is 0 <= k < K.
 java.lang.Object clone()
          Clone the Matrix object.
 double cond()
          Matrix condition (2 norm)
 DiagonalMatrix copy()
          Make a deep copy of a matrix
 double det()
          Matrix determinant
 double get(int n)
          Get a single element, A(n,n).
 double get(int n, int k)
          Get a single element, A(n,k).
 double innerProduct(int k1, int k2)
          Returns the inner product of two matrix column vectors, which for a diagonal matrix is zero.
 double norm2()
          Two norm
 double normF()
          Frobenius norm
 int rank()
          Matrix rank
 void set(int n, double s)
          Set a single element, A(n,n)=s.
 void set(int n, int k, double s)
          Set a single element, A(n,k)=s, but only if (n==k).
 void setAll(double[] vals)
          Set all entries of to matrix to the supplied new values If argument is wrong length an IllegalArgumentException is thrown.
 void times(double[] y, double[] x)
          Multiplies the matrix by an array, x = A*y.
 void transposeTimes(double[] x, double[] y)
          Multiplies the transposed matrix by an array, y = A'*x.
 
Methods inherited from class mpv2.AllMatrices
chol, columnNorm0, columnNorm1, columnNorm2, columnNormInf, eig, eqConstant, eqCopy, eqCopy, eqCopy, eqCopy, eqCopy, eqDifference, eqEProduct, eqEQuotient, eqIdentity, eqInverse, eqIProduct, eqNegate, eqOnes, eqPermuteColumns, eqPermuteRows, eqProduct, eqProduct, eqRandom, eqScaleColumns, eqScaleRows, eqSum, eqTProduct, eqTranspose, eqZeros, getAll, getAll, getColumn, getColumn, getColumnDimension, getK, getN, getRow, getRow, getRowDimension, getSubMatrix, getSubMatrix, getSubMatrix, getSubMatrix, getValue, lu, lup, norm1, normInf, pluseqOuterProduct, print, print, print, print, qr, setColumn, setRow, setValue, sumAll, svd, times, times, times, trace, transposeTimes, transposeTimes, transposeTimes
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DiagonalMatrix

public DiagonalMatrix(int n)
Construct an n-by-n diagonal matrix of zeros.

Parameters:
n - Number of rows and columns

DiagonalMatrix

public DiagonalMatrix(int n,
                      double s)
Construct an n-by-n diagonal matrix with constant value in each element

Parameters:
n - Number of rows and columns
s - Fill the matrix with this scalar value.

DiagonalMatrix

public DiagonalMatrix(double[][] A)
Construct a diagonal matrix from a 2-D array, only values in the diagonal are copied.

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

DiagonalMatrix

public DiagonalMatrix(double[] vals)
Construct a diagonal matrix from a one-dimensional array

Parameters:
vals - One-dimensional array

DiagonalMatrix

public DiagonalMatrix(AllMatrices B)
Construct a diagonal matrix from another matrix (of any kind) by extracting the elements on the diagonal.

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

copy

public DiagonalMatrix 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)
Get a single element, A(n,n).

Parameters:
n - Row and column index.
Returns:
A(n,n)
Throws:
java.lang.ArrayIndexOutOfBoundsException

get

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

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,
                double s)
Set a single element, A(n,n)=s.

Parameters:
n - Row and column index.
s - A(n,n).
Throws:
java.lang.ArrayIndexOutOfBoundsException

set

public void set(int n,
                int k,
                double s)
Set a single element, A(n,k)=s, but only if (n==k).

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

addColumn

public void addColumn(int k,
                      double factor,
                      double[] x)
Add a column of the matrix multiplied by a factor to x, x = x + A[][k] Legal range of the integer argument is 0 <= k < K.
The corresponding Matlab expression would be: x = x+f*A(:,k+1). Note that this function can not be used from Matlab, use getColumn(k) to get A(:,k+1) and calculate the new x in Matlab.

Overrides:
addColumn in class AllMatrices
Parameters:
k - number of the column the matrix A.
factor - a factor to multiply the column vector by.
x - an array of length N.

setAll

public void setAll(double[] vals)
Set all entries of to matrix to the supplied new values 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).

normF

public double normF()
Frobenius norm

Overrides:
normF in class AllMatrices
Returns:
sqrt of sum of squares of all elements.

det

public double det()
Matrix determinant

Overrides:
det in class AllMatrices
Returns:
determinant

rank

public int rank()
Matrix rank

Overrides:
rank in class AllMatrices
Returns:
effective numerical rank, obtained from SVD.

cond

public double cond()
Matrix condition (2 norm)

Overrides:
cond in class AllMatrices
Returns:
ratio of largest to smallest singular value.

norm2

public double norm2()
Two norm

Overrides:
norm2 in class AllMatrices
Returns:
maximum singular value.

innerProduct

public double innerProduct(int k1,
                           int k2)
Returns the inner product of two matrix column vectors, which for a diagonal matrix is zero.

Overrides:
innerProduct in class AllMatrices
Parameters:
k1 - number for the first dictionary element.
k2 - number for the second dictionary element.

times

public void times(double[] y,
                  double[] x)
Multiplies the matrix by an array, x = A*y.

Overrides:
times in class AllMatrices
Parameters:
y - the input array
x - the results as an array of length N.

transposeTimes

public void transposeTimes(double[] x,
                           double[] y)
Multiplies the transposed matrix by an array, y = A'*x. Since a diagonal matrix is equal to its transposed y = A*x

Overrides:
transposeTimes in class AllMatrices
Parameters:
x - the input array
y - the results as an array of length K.