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

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

Introduction

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

Prototype

void setSubMatrix(double[][] subMatrix, int row, int column)
        throws NoDataException, OutOfRangeException, DimensionMismatchException, NullArgumentException;

Source Link

Document

Replace the submatrix starting at row, column using data in the input subMatrix array.

Usage

From source file:org.knime.al.util.noveltydetection.knfst.KNFST.java

public static RealMatrix projection(final RealMatrix kernelMatrix, final String[] labels)
        throws KNFSTException {

    final ClassWrapper[] classes = ClassWrapper.classes(labels);

    // check labels
    if (classes.length == 1) {
        throw new IllegalArgumentException(
                "not able to calculate a nullspace from data of a single class using KNFST (input variable \"labels\" only contains a single value)");
    }//from  www  . ja v  a  2 s.c o  m

    // check kernel matrix
    if (!kernelMatrix.isSquare()) {
        throw new IllegalArgumentException("The KernelMatrix must be quadratic!");
    }

    // calculate weights of orthonormal basis in kernel space
    final RealMatrix centeredK = centerKernelMatrix(kernelMatrix);

    final EigenDecomposition eig = new EigenDecomposition(centeredK);
    final double[] eigVals = eig.getRealEigenvalues();
    final ArrayList<Integer> nonZeroEigValIndices = new ArrayList<Integer>();
    for (int i = 0; i < eigVals.length; i++) {
        if (eigVals[i] > 1e-12) {
            nonZeroEigValIndices.add(i);
        }
    }

    int eigIterator = 0;
    final RealMatrix eigVecs = eig.getV();
    RealMatrix basisvecs = null;
    try {
        basisvecs = MatrixUtils.createRealMatrix(eigVecs.getRowDimension(), nonZeroEigValIndices.size());
    } catch (final Exception e) {
        throw new KNFSTException("Something went wrong. Try different parameters or a different kernel.");
    }

    for (final Integer index : nonZeroEigValIndices) {
        final double normalizer = 1 / Math.sqrt(eigVals[index]);
        final RealVector basisVec = eigVecs.getColumnVector(eigIterator).mapMultiply(normalizer);
        basisvecs.setColumnVector(eigIterator++, basisVec);
    }

    // calculate transformation T of within class scatter Sw:
    // T= B'*K*(I-L) and L a block matrix
    final RealMatrix L = kernelMatrix.createMatrix(kernelMatrix.getRowDimension(),
            kernelMatrix.getColumnDimension());
    int start = 0;
    for (final ClassWrapper cl : classes) {
        final int count = cl.getCount();
        L.setSubMatrix(MatrixFunctions.ones(count, count).scalarMultiply(1.0 / count).getData(), start, start);
        start += count;
    }

    // need Matrix M with all entries 1/m to modify basisvecs which allows
    // usage of
    // uncentered kernel values (eye(size(M)).M)*basisvecs
    final RealMatrix M = MatrixFunctions
            .ones(kernelMatrix.getColumnDimension(), kernelMatrix.getColumnDimension())
            .scalarMultiply(1.0 / kernelMatrix.getColumnDimension());
    final RealMatrix I = MatrixUtils.createRealIdentityMatrix(M.getColumnDimension());

    // compute helper matrix H
    final RealMatrix H = ((I.subtract(M)).multiply(basisvecs)).transpose().multiply(kernelMatrix)
            .multiply(I.subtract(L));

    // T = H*H' = B'*Sw*B with B=basisvecs
    final RealMatrix T = H.multiply(H.transpose());

    // calculate weights for null space
    RealMatrix eigenvecs = MatrixFunctions.nullspace(T);

    if (eigenvecs == null) {
        final EigenDecomposition eigenComp = new EigenDecomposition(T);
        final double[] eigenvals = eigenComp.getRealEigenvalues();
        eigenvecs = eigenComp.getV();
        final int minId = MatrixFunctions.argmin(MatrixFunctions.abs(eigenvals));
        final double[] eigenvecsData = eigenvecs.getColumn(minId);
        eigenvecs = MatrixUtils.createColumnRealMatrix(eigenvecsData);
    }

    // System.out.println("eigenvecs:");
    // test.printMatrix(eigenvecs);

    // calculate null space projection
    final RealMatrix proj = ((I.subtract(M)).multiply(basisvecs)).multiply(eigenvecs);

    return proj;
}

From source file:org.orekit.frames.TransformTest.java

@Test
public void testLinear() {

    RandomGenerator random = new Well19937a(0x14f6411217b148d8l);
    for (int n = 0; n < 100; ++n) {
        Transform t = randomTransform(random);

        // build an equivalent linear transform by extracting raw translation/rotation
        RealMatrix linearA = MatrixUtils.createRealMatrix(3, 4);
        linearA.setSubMatrix(t.getRotation().getMatrix(), 0, 0);
        Vector3D rt = t.getRotation().applyTo(t.getTranslation());
        linearA.setEntry(0, 3, rt.getX());
        linearA.setEntry(1, 3, rt.getY());
        linearA.setEntry(2, 3, rt.getZ());

        // build an equivalent linear transform by observing transformed points
        RealMatrix linearB = MatrixUtils.createRealMatrix(3, 4);
        Vector3D p0 = t.transformPosition(Vector3D.ZERO);
        Vector3D pI = t.transformPosition(Vector3D.PLUS_I).subtract(p0);
        Vector3D pJ = t.transformPosition(Vector3D.PLUS_J).subtract(p0);
        Vector3D pK = t.transformPosition(Vector3D.PLUS_K).subtract(p0);
        linearB.setColumn(0, new double[] { pI.getX(), pI.getY(), pI.getZ() });
        linearB.setColumn(1, new double[] { pJ.getX(), pJ.getY(), pJ.getZ() });
        linearB.setColumn(2, new double[] { pK.getX(), pK.getY(), pK.getZ() });
        linearB.setColumn(3, new double[] { p0.getX(), p0.getY(), p0.getZ() });

        // both linear transforms should be equal
        Assert.assertEquals(0.0, linearB.subtract(linearA).getNorm(), 1.0e-15 * linearA.getNorm());

        for (int i = 0; i < 100; ++i) {
            Vector3D p = randomVector(1.0e3, random);
            Vector3D q = t.transformPosition(p);

            double[] qA = linearA.operate(new double[] { p.getX(), p.getY(), p.getZ(), 1.0 });
            Assert.assertEquals(q.getX(), qA[0], 1.0e-13 * p.getNorm());
            Assert.assertEquals(q.getY(), qA[1], 1.0e-13 * p.getNorm());
            Assert.assertEquals(q.getZ(), qA[2], 1.0e-13 * p.getNorm());

            double[] qB = linearB.operate(new double[] { p.getX(), p.getY(), p.getZ(), 1.0 });
            Assert.assertEquals(q.getX(), qB[0], 1.0e-10 * p.getNorm());
            Assert.assertEquals(q.getY(), qB[1], 1.0e-10 * p.getNorm());
            Assert.assertEquals(q.getZ(), qB[2], 1.0e-10 * p.getNorm());

        }//  w ww. j a v  a2 s . c  om

    }

}