Example usage for org.apache.commons.math3.linear RealMatrix getFrobeniusNorm

List of usage examples for org.apache.commons.math3.linear RealMatrix getFrobeniusNorm

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear RealMatrix getFrobeniusNorm.

Prototype

double getFrobeniusNorm();

Source Link

Document

Returns the <a href="http://mathworld.wolfram.com/FrobeniusNorm.html"> Frobenius norm</a> of the matrix.

Usage

From source file:com.cloudera.oryx.common.math.RRQRDecomposition.java

/**
 * Return the effective numerical matrix rank.
 * <p>The effective numerical rank is the number of non-negligible
 * singular values.</p>/*from ww  w . ja v a 2 s  . c  om*/
 * <p>This implementation looks at Frobenius norms of the sequence of
 * bottom right submatrices.  When a large fall in norm is seen,
 * the rank is returned. The drop is computed as:</p>
 * {@code (thisNorm/lastNorm) * rNorm < dropThreshold }
 * <p>
 * where thisNorm is the Frobenius norm of the current submatrix,
 * lastNorm is the Frobenius norm of the previous submatrix,
 * rNorm is is the Frobenius norm of the complete matrix
 * </p>
 *
 * @param dropThreshold threshold triggering rank computation
 * @return effective numerical matrix rank
 */
public int getRank(final double dropThreshold) {
    RealMatrix r = getR();
    int rows = r.getRowDimension();
    int columns = r.getColumnDimension();
    int rank = 1;
    double lastNorm = r.getFrobeniusNorm();
    double rNorm = lastNorm;
    while (rank < Math.min(rows, columns)) {
        double thisNorm = r.getSubMatrix(rank, rows - 1, rank, columns - 1).getFrobeniusNorm();
        if (thisNorm == 0 || (thisNorm / lastNorm) * rNorm < dropThreshold) {
            break;
        }
        lastNorm = thisNorm;
        rank++;
    }
    return rank;
}

From source file:edu.cudenver.bios.matrix.GramSchmidtOrthonormalization.java

/**
 * Perform Gram Schmidt Orthonormalization on the specified 
 * matrix. The matrix A (mxn) is decomposed into two matrices 
 * Q (mxn), R (nxn) such that/*from w  w  w .j  a  v a 2  s  .c  om*/
 * <ul>
 * <li>A = QR
 * <li>Q'Q = Identity
 * <li>R is upper triangular
 * </ul> 
 * The resulting Q, R matrices can be retrieved with the getQ()
 * and getR() functions.
 * 
 * @param matrix
 */
public GramSchmidtOrthonormalization(RealMatrix matrix) {
    if (matrix == null)
        throw new IllegalArgumentException("Null matrix");

    // create the Q, R matrices
    int m = matrix.getRowDimension();
    int n = matrix.getColumnDimension();
    Q = MatrixUtils.createRealMatrix(m, n);
    R = MatrixUtils.createRealMatrix(n, n);

    // perform Gram Schmidt process using the following algorithm
    // let w<n> be the resulting orthonormal column vectors
    // let v<n> be the columns of the incoming matrix
    // w1 = (1/norm(v1))*v1
    // ...
    // wj = 1/norm(vj - projectionVj-1Vj)*[vj - projectionVj-1Vj]
    // where projectionVj-1Vj = (w1 * vj) * w1 + (w2 * vj) * w2 + ... + (wj-1 * vj) * wj-1
    //
    for (int i = 0; i < n; i++) {
        RealMatrix v = matrix.getColumnMatrix(i);
        for (int j = 0; j < i; j++) {
            RealMatrix Qj = Q.getColumnMatrix(j);
            double value = Qj.transpose().multiply(v).getEntry(0, 0);
            R.setEntry(j, i, value);
            v = v.subtract(Qj.scalarMultiply(value));
        }
        double norm = v.getFrobeniusNorm();
        R.setEntry(i, i, norm);
        Q.setColumnMatrix(i, v.scalarMultiply(1 / norm));
    }
}

From source file:org.ojalgo.benchmark.lab.library.ACM.java

@Override
protected double norm(final RealMatrix matrix) {
    return matrix.getFrobeniusNorm();
}