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

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

Introduction

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

Prototype

double getEntry(int row, int column) throws OutOfRangeException;

Source Link

Document

Get the entry in the specified row and column.

Usage

From source file:edu.oregonstate.eecs.mcplan.ml.SequentialProjectionHashLearner.java

/**
 * @param X Each column is an unlabeled data point. Will be modified to have 0 mean.
 * @param XL Each column is a labeled data point. Will be modified to have 0 mean.
 * @param S S_ij = 1 if XL.get(i) similar to XL.get(j), -1 if not similar, 0 if unknown
 * @param K Number of hash bits/* ww  w .j a  v  a 2  s.c o  m*/
 * @param eta Regularization parameter
 * @param alpha Learning rate (it's more like a boosting weight)
 */
public SequentialProjectionHashLearner(final RealMatrix X, final RealMatrix XL, final RealMatrix S, final int K,
        final double eta, final double alpha) {
    assert (K <= 64); // Because we're going to hash into a long

    this.X = X;
    this.XL = XL;
    this.S = S;
    this.K = K;
    this.eta = eta;
    this.alpha = alpha;

    Xi_ = X;
    Si_ = S;

    final VectorMeanVarianceAccumulator mu = new VectorMeanVarianceAccumulator(X.getRowDimension());
    for (int j = 0; j < X.getColumnDimension(); ++j) {
        mu.add(X.getColumn(j));
    }
    b = new ArrayRealVector(mu.mean(), false);

    for (int i = 0; i < X.getRowDimension(); ++i) {
        for (int j = 0; j < X.getColumnDimension(); ++j) {
            final double x = X.getEntry(i, j);
            X.setEntry(i, j, x / b.getEntry(i));
        }
    }

    for (int i = 0; i < XL.getRowDimension(); ++i) {
        for (int j = 0; j < XL.getColumnDimension(); ++j) {
            final double x = XL.getEntry(i, j);
            XL.setEntry(i, j, x / b.getEntry(i));
        }
    }

    XLt = XL.transpose();
}

From source file:jopensurf.FastHessian.java

private double[] interpolateStep(int r, int c, ResponseLayer t, ResponseLayer m, ResponseLayer b) {
    double[] values = new double[3];

    RealMatrix partialDerivs = getPartialDerivativeMatrix(r, c, t, m, b);
    RealMatrix hessian3D = getHessian3DMatrix(r, c, t, m, b);

    DecompositionSolver solver = new LUDecomposition(hessian3D).getSolver();
    RealMatrix X = solver.getInverse().multiply(partialDerivs);

    //      System.out.println("X = " + X.getColumnDimension() + " col x " + X.getRowDimension() + " rows");
    //      for ( int i = 0; i < X.getRowDimension(); i++ ){
    //         for ( int j = 0; j < X.getColumnDimension(); j++ ){
    //            System.out.print(X.getEntry(i,j) + (j != X.getColumnDimension()-1 ? " - " : ""));
    //         }/* w w  w.  j a va 2s  . c om*/
    //         System.out.println();
    //      }
    //      System.out.println();
    //      
    //values of them are used
    //xi
    values[0] = -X.getEntry(2, 0);
    //xr
    values[1] = -X.getEntry(1, 0);
    //xc
    values[2] = -X.getEntry(0, 0);

    return values;
}

From source file:edu.cudenver.bios.power.test.paper.TestConditionalOrthogonalPolynomial3Factor.java

/**
 * Write a matrix in latex//  w  w  w. j a  v  a2s. c  o m
 * @param section
 * @param name
 * @param matrix
 */
private void appendMatrix(String name, RealMatrix matrix) {
    matrixAltStringBuffer.append("\\begin{eqnarray*}\n");
    // add name label
    matrixAltStringBuffer.append("\\underset{\\left(" + matrix.getRowDimension() + "\\times"
            + matrix.getColumnDimension() + "\\right)}{" + name + "} & = & \\begin{bmatrix}");
    for (int r = 0; r < matrix.getRowDimension(); r++) {
        boolean first = true;
        for (int c = 0; c < matrix.getColumnDimension(); c++) {
            if (!first) {
                matrixAltStringBuffer.append(" & ");
            }
            matrixAltStringBuffer.append(ShortNumber.format(matrix.getEntry(r, c)));
            if (first) {
                first = false;
            }
        }
        matrixAltStringBuffer.append("\\protect\\\\\n");
    }
    matrixAltStringBuffer.append("\\end{bmatrix}\n");
    matrixAltStringBuffer.append("\\end{eqnarray*}\n");
    if (matrix.getColumnDimension() > 10) {
        matrixAltStringBuffer.append("\\normalsize\n");
    }
}

From source file:hulo.localization.models.obs.GaussianProcess.java

public double looPredLogLikelihood() {

    double[][] Y = getY();
    double[][] dY = getdY();

    int ns = X.length;
    int ny = Y[0].length;

    RealMatrix Ky = MatrixUtils.createRealMatrix(this.Ky);
    RealMatrix invKy = new LUDecomposition(Ky).getSolver().getInverse();

    RealMatrix dYmat = MatrixUtils.createRealMatrix(dY);

    double[] LOOPredLL = new double[ny];
    for (int j = 0; j < ny; j++) {
        RealMatrix dy = MatrixUtils.createColumnRealMatrix(dYmat.getColumn(j));
        RealMatrix invKdy = invKy.multiply(dy);
        double sum = 0;
        for (int i = 0; i < ns; i++) {
            double mu_i = dYmat.getEntry(i, j) - invKdy.getEntry(i, 0) / invKy.getEntry(i, i);
            double sigma_i2 = 1 / invKy.getEntry(i, i);
            double logLL = StatUtils.logProbaNormal(dYmat.getEntry(i, j), mu_i, Math.sqrt(sigma_i2));
            sum += logLL;//from   www.j ava  2  s. c om
        }
        LOOPredLL[j] = sum;
    }

    double sumLOOPredLL = 0;
    for (int j = 0; j < ny; j++) {
        sumLOOPredLL += LOOPredLL[j];
    }

    return sumLOOPredLL;
}

From source file:com.itemanalysis.psychometrics.factoranalysis.PrincipalComponentsMethod.java

public double estimateParameters() {

    EigenDecomposition eigen = new EigenDecomposition(R);
    RealMatrix eigenVectors = eigen.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);

    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(eigen.getRealEigenvalue(i));
    }//from  www. jav a2  s  . c om
    DiagonalMatrix evMatrix = new DiagonalMatrix(ev);//USE Apache version of Diagonal matrix when upgrade to version 3.2
    RealMatrix LOAD = eigenVectors.multiply(evMatrix);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];
    communality = new double[nVariables];
    uniqueness = new double[nVariables];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
        uniqueness[i] = 1.0 - communality[i];
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

    return 0.0;

}

From source file:edu.cmu.tetrad.util.MatrixUtils.java

public static RealMatrix transposeWithoutCopy(final RealMatrix apacheData) {
    return new AbstractRealMatrix(apacheData.getColumnDimension(), apacheData.getRowDimension()) {
        @Override/*from   w w w .  j ava2  s .c o m*/
        public int getRowDimension() {
            return apacheData.getColumnDimension();
        }

        @Override
        public int getColumnDimension() {
            return apacheData.getRowDimension();
        }

        @Override
        public RealMatrix createMatrix(int rowDimension, int columnDimension)
                throws NotStrictlyPositiveException {
            return apacheData.createMatrix(rowDimension, columnDimension);
        }

        @Override
        public RealMatrix copy() {
            throw new IllegalArgumentException("Can't copy");
        }

        @Override
        public double getEntry(int i, int j) throws OutOfRangeException {
            //                throw new UnsupportedOperationException();
            return apacheData.getEntry(j, i);
        }

        @Override
        public void setEntry(int i, int j, double v) throws OutOfRangeException {
            throw new UnsupportedOperationException();
        }
    };
}

From source file:msi.gama.util.matrix.GamaFloatMatrix.java

public IMatrix fromApacheMatrix(final IScope scope, final RealMatrix rm) {
    if (rm == null) {
        return null;
    }//w w w .  j a  va 2  s .  c  o  m
    final GamaFloatMatrix matrix = new GamaFloatMatrix(rm.getColumnDimension(), rm.getRowDimension());
    for (int i = 0; i < numCols; i++) {
        for (int j = 0; j < numRows; j++) {
            matrix.set(scope, i, j, rm.getEntry(j, i));
        }
    }
    return matrix;

}

From source file:com.itemanalysis.psychometrics.factoranalysis.GeneralizedLeastSquaresMethod.java

private void computeFactorLoadings(double[] x) {
    uniqueness = x;/*from ww  w. j a v a  2 s  .c om*/
    communality = new double[nVariables];

    for (int i = 0; i < nVariables; i++) {
        R.setEntry(i, i, 1.0 - x[i]);
    }

    EigenDecomposition E = new EigenDecomposition(R);
    RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);
    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(E.getRealEigenvalue(i));
    }
    DiagonalMatrix M = new DiagonalMatrix(ev);
    RealMatrix LOAD = L.multiply(M);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

}

From source file:iDynoOptimizer.MOEAFramework26.src.org.moeaframework.algorithm.DBEA.java

/**
 * Updates the ideal point and intercepts given the new solution.
 * //from w  w w . j  a v  a2  s. c om
 * @param solution the new solution
 */
void updateIdealPointAndIntercepts(Solution solution) {
    if (!solution.violatesConstraints()) {
        // update the ideal point
        for (int j = 0; j < problem.getNumberOfObjectives(); j++) {
            idealPoint[j] = Math.min(idealPoint[j], solution.getObjective(j));
            intercepts[j] = Math.max(intercepts[j], solution.getObjective(j));
        }

        // compute the axis intercepts
        Population feasibleSolutions = getFeasibleSolutions(population);
        feasibleSolutions.add(solution);

        Population nondominatedSolutions = getNondominatedFront(feasibleSolutions);

        if (!nondominatedSolutions.isEmpty()) {
            // find the points with the largest value in each objective
            Population extremePoints = new Population();

            for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
                extremePoints.add(largestObjectiveValue(i, nondominatedSolutions));
            }

            if (numberOfUniqueSolutions(extremePoints) != problem.getNumberOfObjectives()) {
                for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
                    intercepts[i] = extremePoints.get(i).getObjective(i);
                }
            } else {
                try {
                    RealMatrix b = new Array2DRowRealMatrix(problem.getNumberOfObjectives(), 1);
                    RealMatrix A = new Array2DRowRealMatrix(problem.getNumberOfObjectives(),
                            problem.getNumberOfObjectives());

                    for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
                        b.setEntry(i, 0, 1.0);

                        for (int j = 0; j < problem.getNumberOfObjectives(); j++) {
                            A.setEntry(i, j, extremePoints.get(i).getObjective(j));
                        }
                    }

                    double numerator = new LUDecomposition(A).getDeterminant();
                    b.scalarMultiply(numerator);
                    RealMatrix normal = MatrixUtils.inverse(A).multiply(b);

                    for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
                        intercepts[i] = numerator / normal.getEntry(i, 0);

                        if (intercepts[i] <= 0 || Double.isNaN(intercepts[i])
                                || Double.isInfinite(intercepts[i])) {
                            intercepts[i] = extremePoints.get(i).getObjective(i);
                        }
                    }
                } catch (RuntimeException e) {
                    for (int i = 0; i < problem.getNumberOfObjectives(); i++) {
                        intercepts[i] = extremePoints.get(i).getObjective(i);
                    }
                }
            }
        }
    }
}

From source file:com.itemanalysis.psychometrics.factoranalysis.MaximumLikelihoodMethod.java

private void computeFactorLoadings(double[] x) {
    uniqueness = x;/*from   w  w w .ja v  a2s.  co m*/
    communality = new double[nVariables];

    double[] sqrtPsi = new double[nVariables];
    double[] invSqrtPsi = new double[nVariables];
    for (int i = 0; i < nVariables; i++) {
        sqrtPsi[i] = Math.sqrt(x[i]);
        invSqrtPsi[i] = 1.0 / Math.sqrt(x[i]);
    }
    DiagonalMatrix diagPsi = new DiagonalMatrix(x);
    DiagonalMatrix diagSqtPsi = new DiagonalMatrix(sqrtPsi);
    DiagonalMatrix diagInvSqrtPsi = new DiagonalMatrix(invSqrtPsi);

    RealMatrix Sstar = diagInvSqrtPsi.multiply(R).multiply(diagInvSqrtPsi);
    EigenDecomposition E = new EigenDecomposition(Sstar);
    RealMatrix L = E.getV().getSubMatrix(0, nVariables - 1, 0, nFactors - 1);
    double[] ev = new double[nFactors];
    for (int i = 0; i < nFactors; i++) {
        ev[i] = Math.sqrt(Math.max(E.getRealEigenvalue(i) - 1, 0));
    }
    DiagonalMatrix M = new DiagonalMatrix(ev);
    RealMatrix LOAD2 = L.multiply(M);
    RealMatrix LOAD = diagSqtPsi.multiply(LOAD2);

    //rotate factor loadings
    if (rotationMethod != RotationMethod.NONE) {
        GPArotation gpa = new GPArotation();
        RotationResults results = gpa.rotate(LOAD, rotationMethod);
        LOAD = results.getFactorLoadings();
    }

    Sum[] colSums = new Sum[nFactors];
    Sum[] colSumsSquares = new Sum[nFactors];

    for (int j = 0; j < nFactors; j++) {
        colSums[j] = new Sum();
        colSumsSquares[j] = new Sum();
    }

    factorLoading = new double[nVariables][nFactors];

    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            factorLoading[i][j] = LOAD.getEntry(i, j);
            colSums[j].increment(factorLoading[i][j]);
            colSumsSquares[j].increment(Math.pow(factorLoading[i][j], 2));
            communality[i] += Math.pow(factorLoading[i][j], 2);
        }
    }

    //check sign of factor
    double sign = 1.0;
    for (int i = 0; i < nVariables; i++) {
        for (int j = 0; j < nFactors; j++) {
            if (colSums[j].getResult() < 0) {
                sign = -1.0;
            } else {
                sign = 1.0;
            }
            factorLoading[i][j] = factorLoading[i][j] * sign;
        }
    }

    double totSumOfSquares = 0.0;
    sumsOfSquares = new double[nFactors];
    proportionOfExplainedVariance = new double[nFactors];
    proportionOfVariance = new double[nFactors];
    for (int j = 0; j < nFactors; j++) {
        sumsOfSquares[j] = colSumsSquares[j].getResult();
        totSumOfSquares += sumsOfSquares[j];
    }
    for (int j = 0; j < nFactors; j++) {
        proportionOfExplainedVariance[j] = sumsOfSquares[j] / totSumOfSquares;
        proportionOfVariance[j] = sumsOfSquares[j] / nVariables;
    }

}