SuanShu, a Java numerical and statistical library

com.numericalmethod.suanshu.matrix.doubles.factorization.qr
Class GramSchmidt

java.lang.Object
  extended by com.numericalmethod.suanshu.matrix.doubles.factorization.qr.GramSchmidt
All Implemented Interfaces:
QRDecomposition

public class GramSchmidt
extends java.lang.Object
implements QRDecomposition

The Gram–Schmidt process is a method for orthogonalizing a set of vectors in an inner product space. It does so by iteratively computing the vector orthogonal to the subspace spanned by previously found orthogonal vectors.

The orthogonal vector is the difference between a column vector and its projection on the subspace.

There is the problem of "loss of orthogonality" during the process, which gives arise to the precision error or rounding error. In general, the bigger the matrix is, e.g., dimension = 3500x3500, the less precise the results are. Also, the vectors in Q may not be as orthogonal.

A numerically stable Gram–Schmidt process with twice re-orthogonalization is implemented to alleviate the problem of rounding errors.

Numerical determination of rank requires a criterion to decide when a value should be treated as zero. This is a practical choice which depends on both the matrix and the application. For instance, for a matrix with a big first eigenvector, we should accordingly decrease the precision to compute the rank.

While the results for the orthogonal basis may match those of the Householder Reflection HouseholderReflection, the results for the orthogonal complement may differ because the kernel basis is not unique.

See Also:

Field Summary
 double epsilon
          a precision parameter: when a number |x| ≤ ε, it is considered 0
 int ncols
          number of columns
 int nrows
          number of rows
 
Constructor Summary
GramSchmidt(Matrix A)
          Construct an instance of the Gram-Schmidt process to orthogonalize a matrix.
GramSchmidt(Matrix A, boolean pad0Cols, double epsilon)
          Construct an instance of the Gram-Schmidt process to orthogonalize a matrix.
 
Method Summary
 PermutationMatrix P()
          Get a copy of P, the pivoting matrix in the QR decomposition.
 Matrix Q()
          Get a copy of the orthogonal Q matrix in the QR decomposition.
 UpperTriangularMatrix R()
          Get a copy of the upper triangular matrix R in the QR decomposition.
 int rank()
           Get the numerical rank of the matrix A as computed by the QR decomposition.
 Matrix squareQ()
          Get a copy of the square Q matrix.
 Matrix tallR()
          Get a copy of the tall R matrix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nrows

public final int nrows
number of rows


ncols

public final int ncols
number of columns


epsilon

public final double epsilon
a precision parameter: when a number |x| ≤ ε, it is considered 0

Constructor Detail

GramSchmidt

public GramSchmidt(Matrix A,
                   boolean pad0Cols,
                   double epsilon)
Construct an instance of the Gram-Schmidt process to orthogonalize a matrix.

Parameters:
A - the matrix to orthogonalize
pad0Cols - when a column is linearly dependent on the previous columns, there is no orthogonal vector. We pad the basis with a 0-vector.
epsilon - a precision parameter: when a number |x| ≤ ε, it is considered 0

GramSchmidt

public GramSchmidt(Matrix A)
Construct an instance of the Gram-Schmidt process to orthogonalize a matrix.

Parameters:
A - a matrix
Method Detail

Q

public Matrix Q()
Description copied from interface: QRDecomposition
Get a copy of the orthogonal Q matrix in the QR decomposition.
 A = QR
 

Dimension of Q is nrows x ncols, same as A, the matrix to orthogonalize.

Specified by:
Q in interface QRDecomposition
Returns:
a copy of the Q matrix in the QR decomposition

R

public UpperTriangularMatrix R()
Description copied from interface: QRDecomposition
Get a copy of the upper triangular matrix R in the QR decomposition.
 A = QR
 

Dimension of R is ncols x ncols, a square matrix.

Specified by:
R in interface QRDecomposition
Returns:
a copy of the upper triangular matrix R in the QR decomposition

P

public PermutationMatrix P()
Description copied from interface: QRDecomposition
Get a copy of P, the pivoting matrix in the QR decomposition.

Specified by:
P in interface QRDecomposition
Returns:
a copy of the P pivoting matrix in the QR decomposition

rank

public int rank()
Description copied from interface: QRDecomposition

Get the numerical rank of the matrix A as computed by the QR decomposition.

Numerical determination of rank requires a criterion to decide when a value should be treated as zero.

This is a practical choice which depends on both the matrix and the application. For instance, for a matrix with a big first eigenvector, we should accordingly decrease the precision to compute the rank.

You may need to change the precision parameter to accurately compute the rank. See the test cases for example.

Specified by:
rank in interface QRDecomposition
Returns:
the rank of A

squareQ

public Matrix squareQ()
Get a copy of the square Q matrix. This is an arbitrary orthogonal completion of the Q matrix in the QR decomposition. Dimension is nrows x nrows (square).
 A = square_Q %*% tall_R
 
In this implementation, we extend Q by adding A's orthogonal complement at the end. Suppose Q has the orthogonal basis for a subspace A. To compute the orthogonal complement of A, we can apply the Gram-Schmidt procedure to either
  1. the columns of I - P = I - Q %*% Q.t(), or
  2. the spanning set {u1, u2, ... uk, e1, e2, ... en} and keeping only the first n elements of the resulting basis of Rn. The last n-k elements are the basis for the orthogonal complement. k is the rank.

We implemented the second option.

Specified by:
squareQ in interface QRDecomposition
Returns:
the spanning set of both the orthogonal basis of A and the orthogonal complement

tallR

public Matrix tallR()
Description copied from interface: QRDecomposition
Get a copy of the tall R matrix. This is completed by binding zero rows beneath the square upper triangular matrix R in the QR decomposition. Dimension is nrows x ncols. Note that this may no longer be square.
 A = square_Q %*% tall_R
 

Specified by:
tallR in interface QRDecomposition
Returns:
a copy of the tall R matrix

SuanShu, a Java numerical and statistical library

Copyright © 2011 Numerical Method Inc. Ltd. All Rights Reserved.