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

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

Introduction

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

Prototype

void setColumnVector(int column, RealVector vector)
        throws OutOfRangeException, MatrixDimensionMismatchException;

Source Link

Document

Sets the specified column of this matrix to the entries of the specified vector .

Usage

From source file:edu.stanford.cfuller.colocalization3d.correction.PositionCorrector.java

/**
* Creates a correction from a set of objects whose positions should be the same in each channel.
* 
* @param imageObjects                  A Vector containing all the ImageObjects to be used for the correction
*                                      or in the order it appears in a multiwavelength image file.
* @return                              A Correction object that can be used to correct the positions of other objects based upon the standards provided.
*///from  w ww.  j  av a 2 s  .c  o  m
public Correction getCorrection(java.util.List<ImageObject> imageObjects) {

    int referenceChannel = this.parameters.getIntValueForKey(REF_CH_PARAM);

    int channelToCorrect = this.parameters.getIntValueForKey(CORR_CH_PARAM);

    if (!this.parameters.hasKeyAndTrue(DET_CORR_PARAM)) {
        try {
            return Correction.readFromDisk(FileUtils.getCorrectionFilename(this.parameters));
        } catch (java.io.IOException e) {

            java.util.logging.Logger
                    .getLogger(edu.stanford.cfuller.colocalization3d.Colocalization3DMain.LOGGER_NAME)
                    .severe("Exception encountered while reading correction from disk: ");
            e.printStackTrace();

        } catch (ClassNotFoundException e) {

            java.util.logging.Logger
                    .getLogger(edu.stanford.cfuller.colocalization3d.Colocalization3DMain.LOGGER_NAME)
                    .severe("Exception encountered while reading correction from disk: ");
            e.printStackTrace();

        }

        return null;
    }

    int numberOfPointsToFit = this.parameters.getIntValueForKey(NUM_POINT_PARAM);

    RealMatrix correctionX = new Array2DRowRealMatrix(imageObjects.size(), numberOfCorrectionParameters);
    RealMatrix correctionY = new Array2DRowRealMatrix(imageObjects.size(), numberOfCorrectionParameters);
    RealMatrix correctionZ = new Array2DRowRealMatrix(imageObjects.size(), numberOfCorrectionParameters);

    RealVector distanceCutoffs = new ArrayRealVector(imageObjects.size(), 0.0);

    RealVector ones = new ArrayRealVector(numberOfPointsToFit, 1.0);

    RealVector distancesToObjects = new ArrayRealVector(imageObjects.size(), 0.0);

    RealMatrix allCorrectionParametersMatrix = new Array2DRowRealMatrix(numberOfPointsToFit,
            numberOfCorrectionParameters);

    for (int i = 0; i < imageObjects.size(); i++) {

        RealVector ithPos = imageObjects.get(i).getPositionForChannel(referenceChannel);

        for (int j = 0; j < imageObjects.size(); j++) {

            double d = imageObjects.get(j).getPositionForChannel(referenceChannel).subtract(ithPos).getNorm();

            distancesToObjects.setEntry(j, d);

        }

        //the sorting becomes a bottleneck once the number of points gets large

        //reverse comparator so we can use the priority queue and get the max element at the head

        Comparator<Double> cdReverse = new Comparator<Double>() {

            public int compare(Double o1, Double o2) {

                if (o1.equals(o2))
                    return 0;
                if (o1 > o2)
                    return -1;
                return 1;
            }

        };

        PriorityQueue<Double> pq = new PriorityQueue<Double>(numberOfPointsToFit + 2, cdReverse);

        double maxElement = Double.MAX_VALUE;

        for (int p = 0; p < numberOfPointsToFit + 1; p++) {

            pq.add(distancesToObjects.getEntry(p));

        }

        maxElement = pq.peek();

        for (int p = numberOfPointsToFit + 1; p < distancesToObjects.getDimension(); p++) {

            double value = distancesToObjects.getEntry(p);

            if (value < maxElement) {

                pq.poll();

                pq.add(value);

                maxElement = pq.peek();

            }

        }

        double firstExclude = pq.poll();
        double lastDist = pq.poll();

        double distanceCutoff = (lastDist + firstExclude) / 2.0;

        distanceCutoffs.setEntry(i, distanceCutoff);

        RealVector xPositionsToFit = new ArrayRealVector(numberOfPointsToFit, 0.0);
        RealVector yPositionsToFit = new ArrayRealVector(numberOfPointsToFit, 0.0);
        RealVector zPositionsToFit = new ArrayRealVector(numberOfPointsToFit, 0.0);

        RealMatrix differencesToFit = new Array2DRowRealMatrix(numberOfPointsToFit,
                imageObjects.get(0).getPositionForChannel(referenceChannel).getDimension());

        int toFitCounter = 0;

        for (int j = 0; j < imageObjects.size(); j++) {
            if (distancesToObjects.getEntry(j) < distanceCutoff) {
                xPositionsToFit.setEntry(toFitCounter,
                        imageObjects.get(j).getPositionForChannel(referenceChannel).getEntry(0));
                yPositionsToFit.setEntry(toFitCounter,
                        imageObjects.get(j).getPositionForChannel(referenceChannel).getEntry(1));
                zPositionsToFit.setEntry(toFitCounter,
                        imageObjects.get(j).getPositionForChannel(referenceChannel).getEntry(2));

                differencesToFit.setRowVector(toFitCounter, imageObjects.get(j)
                        .getVectorDifferenceBetweenChannels(referenceChannel, channelToCorrect));

                toFitCounter++;
            }
        }

        RealVector x = xPositionsToFit.mapSubtractToSelf(ithPos.getEntry(0));
        RealVector y = yPositionsToFit.mapSubtractToSelf(ithPos.getEntry(1));

        allCorrectionParametersMatrix.setColumnVector(0, ones);
        allCorrectionParametersMatrix.setColumnVector(1, x);
        allCorrectionParametersMatrix.setColumnVector(2, y);
        allCorrectionParametersMatrix.setColumnVector(3, x.map(new Power(2)));
        allCorrectionParametersMatrix.setColumnVector(4, y.map(new Power(2)));
        allCorrectionParametersMatrix.setColumnVector(5, x.ebeMultiply(y));

        DecompositionSolver solver = (new QRDecomposition(allCorrectionParametersMatrix)).getSolver();

        RealVector cX = solver.solve(differencesToFit.getColumnVector(0));
        RealVector cY = solver.solve(differencesToFit.getColumnVector(1));
        RealVector cZ = solver.solve(differencesToFit.getColumnVector(2));

        correctionX.setRowVector(i, cX);
        correctionY.setRowVector(i, cY);
        correctionZ.setRowVector(i, cZ);

    }

    Correction c = new Correction(correctionX, correctionY, correctionZ, distanceCutoffs, imageObjects,
            referenceChannel, channelToCorrect);

    return c;

}

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)");
    }//  w w  w.  j  ava2 s  . co  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.knime.al.util.noveltydetection.knfst.KNFST.java

private static RealMatrix centerKernelMatrix(final RealMatrix kernelMatrix) {
    // get size of kernelMatrix
    final int n = kernelMatrix.getRowDimension();

    // get mean values for each row/column
    final RealVector columnMeans = MatrixFunctions.columnMeans(kernelMatrix);
    final double matrixMean = MatrixFunctions.mean(kernelMatrix);

    RealMatrix centeredKernelMatrix = kernelMatrix.copy();

    for (int k = 0; k < n; k++) {
        centeredKernelMatrix.setRowVector(k, centeredKernelMatrix.getRowVector(k).subtract(columnMeans));
        centeredKernelMatrix.setColumnVector(k, centeredKernelMatrix.getColumnVector(k).subtract(columnMeans));
    }/*from   w ww . j  ava  2 s  .  c o  m*/

    centeredKernelMatrix = centeredKernelMatrix.scalarAdd(matrixMean);

    return centeredKernelMatrix;
}

From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java

@Override
public FunkSVDModel get() {
    int userCount = snapshot.getUserIds().size();
    RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount);

    int itemCount = snapshot.getItemIds().size();
    RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount);

    logger.debug("Learning rate is {}", rule.getLearningRate());
    logger.debug("Regularization term is {}", rule.getTrainingRegularization());

    logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size());

    TrainingEstimator estimates = rule.makeEstimator(snapshot);

    List<FeatureInfo> featureInfo = new ArrayList<>(featureCount);

    // Use scratch vectors for each feature for better cache locality
    // Per-feature vectors are strided in the output matrices
    RealVector uvec = MatrixUtils.createRealVector(new double[userCount]);
    RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]);

    for (int f = 0; f < featureCount; f++) {
        logger.debug("Training feature {}", f);
        StopWatch timer = new StopWatch();
        timer.start();//from  w  w w.  j a v  a2s  .c  o  m

        uvec.set(initialValue);
        ivec.set(initialValue);

        FeatureInfo.Builder fib = new FeatureInfo.Builder(f);
        trainFeature(f, estimates, uvec, ivec, fib);
        summarizeFeature(uvec, ivec, fib);
        featureInfo.add(fib.build());

        // Update each rating's cached value to accommodate the feature values.
        estimates.update(uvec, ivec);

        // And store the data into the matrix
        userFeatures.setColumnVector(f, uvec);
        assert Math.abs(userFeatures.getColumnVector(f).getL1Norm()
                - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches";
        itemFeatures.setColumnVector(f, ivec);
        assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm()
                - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches";

        timer.stop();
        logger.info("Finished feature {} in {}", f, timer);
    }

    // Wrap the user/item matrices because we won't use or modify them again
    return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(),
            featureInfo);
}

From source file:org.lenskit.mf.funksvd.FunkSVDModelProvider.java

@Override
public FunkSVDModel get() {
    int userCount = snapshot.getUserIds().size();
    RealMatrix userFeatures = MatrixUtils.createRealMatrix(userCount, featureCount);

    int itemCount = snapshot.getItemIds().size();
    RealMatrix itemFeatures = MatrixUtils.createRealMatrix(itemCount, featureCount);

    logger.debug("Learning rate is {}", rule.getLearningRate());
    logger.debug("Regularization term is {}", rule.getTrainingRegularization());

    logger.info("Building SVD with {} features for {} ratings", featureCount, snapshot.getRatings().size());

    TrainingEstimator estimates = rule.makeEstimator(snapshot);

    List<FeatureInfo> featureInfo = new ArrayList<>(featureCount);

    // Use scratch vectors for each feature for better cache locality
    // Per-feature vectors are strided in the output matrices
    RealVector uvec = MatrixUtils.createRealVector(new double[userCount]);
    RealVector ivec = MatrixUtils.createRealVector(new double[itemCount]);

    for (int f = 0; f < featureCount; f++) {
        logger.debug("Training feature {}", f);
        StopWatch timer = new StopWatch();
        timer.start();//  ww w  .j a va 2 s  . c o  m

        uvec.set(initialValue);
        ivec.set(initialValue);

        FeatureInfo.Builder fib = new FeatureInfo.Builder(f);
        double rmse = trainFeature(f, estimates, uvec, ivec, fib);
        summarizeFeature(uvec, ivec, fib);
        featureInfo.add(fib.build());

        // Update each rating's cached value to accommodate the feature values.
        estimates.update(uvec, ivec);

        // And store the data into the matrix
        userFeatures.setColumnVector(f, uvec);
        assert Math.abs(userFeatures.getColumnVector(f).getL1Norm()
                - uvec.getL1Norm()) < 1.0e-4 : "user column sum matches";
        itemFeatures.setColumnVector(f, ivec);
        assert Math.abs(itemFeatures.getColumnVector(f).getL1Norm()
                - ivec.getL1Norm()) < 1.0e-4 : "item column sum matches";

        timer.stop();
        logger.info("Finished feature {} in {} (RMSE={})", f, timer, rmse);
    }

    // Wrap the user/item matrices because we won't use or modify them again
    return new FunkSVDModel(userFeatures, itemFeatures, snapshot.userIndex(), snapshot.itemIndex(),
            featureInfo);
}