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

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

Introduction

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

Prototype

int getRowDimension();

Source Link

Document

Returns the number of rows in the matrix.

Usage

From source file:eagle.security.userprofile.model.eigen.UserProfileEigenModeler.java

@Override
public List<UserProfileEigenModel> generate(String site, String user, RealMatrix matrix) {
    LOG.info(String.format("Receive aggregated user activity matrix: %s size: %s x %s", user,
            matrix.getRowDimension(), matrix.getColumnDimension()));
    computeStats(matrix);//w  w w. j  a  v a  2s  . com
    RealMatrix normalizedInputMatrix = normalizeData(matrix);
    int lowVariantColumnCount = 0;
    for (int j = 0; j < normalizedInputMatrix.getColumnDimension(); j++) {
        if (statistics[j].isLowVariant()) {
            lowVariantColumnCount++;
        }
    }

    if (normalizedInputMatrix.getColumnDimension() == lowVariantColumnCount) {
        LOG.info("found user: " + user + " with all features being low variant. Nothing to do...");
        UserProfileEigenModel noopModel = UserProfileEigenModel$.MODULE$.apply(System.currentTimeMillis(), site,
                user, null, null, 0, null, null, null, null, null, statistics);
        return Arrays.asList(noopModel);
    } else {
        computeCovarianceAndSVD(normalizedInputMatrix, lowVariantColumnCount);
        computeDimensionWithMaxVariance();
        computePrincipalComponents();
        maximumL2Norm = new ArrayRealVector(principalComponents.length);
        minimumL2Norm = new ArrayRealVector(principalComponents.length);

        for (int i = 0; i < principalComponents.length; i++) {
            RealMatrix trainingDataTranspose = computeMaxDistanceOnPCs(i);
        }

        UserProfileEigenModel userprofileEigenModel = UserProfileEigenModel$.MODULE$.apply(
                System.currentTimeMillis(), site, user, uMatrix, diagonalMatrix, dimension, minVector,
                maxVector, principalComponents, maximumL2Norm, minimumL2Norm, statistics);
        return Arrays.asList(userprofileEigenModel);
    }
}

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

/**
 * Write a matrix in latex/*www . ja va  2 s. 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:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java

/**
 * For debugging/*from w w  w . j  a v a 2 s .  co  m*/
 *
 * @param x a matrix to print
 * @param title title for output
 */
private void printMatrix(RealMatrix x, String title) {
    System.out.println("PRINTING MATRIX: " + title);
    for (int i = 0; i < x.getRowDimension(); i++) {
        for (int j = 0; j < x.getColumnDimension(); j++) {
            System.out.print(x.getEntry(i, j) + "  ");
        }
        System.out.println();
    }
}

From source file:com.clust4j.algo.preprocess.PCA.java

@Override
public PCA fit(RealMatrix X) {
    synchronized (fitLock) {
        this.centerer = new MeanCenterer().fit(X);
        this.m = X.getRowDimension();
        this.n = X.getColumnDimension();

        // ensure n_components not too large
        if (this.n_components > n)
            this.n_components = n;

        final RealMatrix data = this.centerer.transform(X);
        SingularValueDecomposition svd = new SingularValueDecomposition(data);
        RealMatrix U = svd.getU(), S = svd.getS(), V = svd.getV().transpose();

        // flip Eigenvectors' sign to enforce deterministic output
        EntryPair<RealMatrix, RealMatrix> uv_sign_swap = eigenSignFlip(U, V);

        U = uv_sign_swap.getKey();/*from   w w w.j  a  va2s. c o  m*/
        V = uv_sign_swap.getValue();
        RealMatrix components_ = V;

        // get variance explained by singular value
        final double[] s = MatUtils.diagFromSquare(S.getData());
        this.variabilities = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variabilities[i] = (s[i] * s[i]) / (double) m;
            total_var += variabilities[i];
        }

        // get variability ratio
        this.variability_ratio = new double[s.length];
        for (int i = 0; i < s.length; i++) {
            variability_ratio[i] = variabilities[i] / total_var;
        }

        // post-process number of components if in var_mode
        double[] ratio_cumsum = VecUtils.cumsum(variability_ratio);
        if (this.var_mode) {
            for (int i = 0; i < ratio_cumsum.length; i++) {
                if (ratio_cumsum[i] >= this.variability) {
                    this.n_components = i + 1;
                    break;
                }

                // if it never hits the if block, the n_components is
                // equal to the number of columns in its entirety
            }
        }

        // get noise variance
        if (n_components < FastMath.min(n, m)) {
            this.noise_variance = VecUtils.mean(VecUtils.slice(variabilities, n_components, s.length));
        } else {
            this.noise_variance = 0.0;
        }

        // Set the components and other sliced variables
        this.components = new Array2DRowRealMatrix(MatUtils.slice(components_.getData(), 0, n_components),
                false);
        this.variabilities = VecUtils.slice(variabilities, 0, n_components);
        this.variability_ratio = VecUtils.slice(variability_ratio, 0, n_components);

        if (retain) {
            this.U = new Array2DRowRealMatrix(MatUtils.slice(U.getData(), 0, n_components), false);
            ;
            this.S = new Array2DRowRealMatrix(MatUtils.slice(S.getData(), 0, n_components), false);
            ;
        }

        return this;
    }
}

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

/**
 * Fill a fixed column in the design matrix
 *
 * @param fixedColumn column index in fixed submatrix
 * @param fullColumn column index in full design matrix
 * @param fullDesign full design matrix//from  w w w.  ja  va  2 s .c  o  m
 */
private void fillFixedColumn(int fixedColumn, int fullColumn, RealMatrix fullDesign) {
    int essenceRow = 0;
    int reps = groupSampleSize * rowMetaData[essenceRow].getRatio();
    for (int row = 0; row < fullDesign.getRowDimension(); row++) {
        // check if we need to move on to the next row in the essence matrix
        if (reps <= 0) {
            essenceRow++;
            reps = groupSampleSize * rowMetaData[essenceRow].getRatio();
        }

        // fill in the data
        fullDesign.setEntry(row, fullColumn, fixedMatrix.getEntry(essenceRow, fixedColumn));
        // decrement the number of reps remain for this row
        reps--;
    }
}

From source file:com.clust4j.algo.preprocess.BoxCoxTransformer.java

@Override
public BoxCoxTransformer fit(RealMatrix X) {
    synchronized (fitLock) {
        final int n = X.getColumnDimension(), m = X.getRowDimension();

        // If m < 2, we can't effectively measure std-dev and thus can't estimate
        if (m < 2) {
            throw new IllegalArgumentException("need at least two observations");
        }//  w  w  w.jav a  2 s  .  c  om

        // Transpose so we can use VecUtils more efficiently,
        // and then chunk the data for parallel operation
        double[][] x = X.transpose().getData();
        this.shift = estimateShifts(x);

        // add the shifts to the data
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                x[j][i] += shift[j];
            }
        }

        // Estimate the lambdas in parallel...
        try {
            this.lambdas = ParallelLambdaEstimator.doAll(this, x);
        } catch (NotStrictlyPositiveException nspe) {
            throw new IllegalArgumentException("is one of your columns a constant?", nspe);
        } catch (RejectedExecutionException r) {
            // if parallelism fails
            this.lambdas = new double[n];
            for (int i = 0; i < n; i++) {
                lambdas[i] = estimateLambdaSingle(x[i], this, this.lambda_min, this.lambda_max);
            }
        }

        return this;
    }
}

From source file:ellipsoidFit.FitPoints.java

/**
 * Solve the polynomial expression Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz +
 * 2Gx + 2Hy + 2Iz from the provided points.
 * //ww  w .ja  v a 2s .co  m
 * @param points
 *            the points that will be fit to the polynomial expression.
 * @return the solution vector to the polynomial expression.
 */
private RealVector solveSystem(ArrayList<ThreeSpacePoint> points) {
    // determine the number of points
    int numPoints = points.size();

    // the design matrix
    // size: numPoints x 9
    RealMatrix d = new Array2DRowRealMatrix(numPoints, 9);

    // Fit the ellipsoid in the form of
    // Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz + 2Fyz + 2Gx + 2Hy + 2Iz
    for (int i = 0; i < d.getRowDimension(); i++) {
        double xx = Math.pow(points.get(i).x, 2);
        double yy = Math.pow(points.get(i).y, 2);
        double zz = Math.pow(points.get(i).z, 2);
        double xy = 2 * (points.get(i).x * points.get(i).y);
        double xz = 2 * (points.get(i).x * points.get(i).z);
        double yz = 2 * (points.get(i).y * points.get(i).z);
        double x = 2 * points.get(i).x;
        double y = 2 * points.get(i).y;
        double z = 2 * points.get(i).z;

        d.setEntry(i, 0, xx);
        d.setEntry(i, 1, yy);
        d.setEntry(i, 2, zz);
        d.setEntry(i, 3, xy);
        d.setEntry(i, 4, xz);
        d.setEntry(i, 5, yz);
        d.setEntry(i, 6, x);
        d.setEntry(i, 7, y);
        d.setEntry(i, 8, z);
    }

    // solve the normal system of equations
    // v = (( d' * d )^-1) * ( d' * ones.mapAddToSelf(1));

    // Multiply: d' * d
    RealMatrix dtd = d.transpose().multiply(d);

    // Create a vector of ones.
    RealVector ones = new ArrayRealVector(numPoints);
    ones.mapAddToSelf(1);

    // Multiply: d' * ones.mapAddToSelf(1)
    RealVector dtOnes = d.transpose().operate(ones);

    // Find ( d' * d )^-1
    DecompositionSolver solver = new SingularValueDecomposition(dtd).getSolver();
    RealMatrix dtdi = solver.getInverse();

    // v = (( d' * d )^-1) * ( d' * ones.mapAddToSelf(1));
    RealVector v = dtdi.operate(dtOnes);

    return v;
}

From source file:edu.dfci.cccb.mev.hcl.domain.simple.SimpleTwoDimensionalHclBuilder.java

private Node cluster(final Dataset dataset, Dimension dimension, Metric metric, Linkage linkage)
        throws DatasetException {
    final Type dimensionType = dimension.type();
    final RealMatrix original = toRealMatrix(dataset);
    final int size = dimensionType == ROW ? original.getRowDimension() : original.getColumnDimension();
    final int other = dimensionType == COLUMN ? original.getRowDimension() : original.getColumnDimension();
    Iterator<Integer> enumerator = new Iterator<Integer>() {

        private int counter = -1;

        @Override// ww  w  . j av a2s.c o m
        public boolean hasNext() {
            return true;
        }

        @Override
        public Integer next() {
            counter--;
            if (counter > 0)
                counter = -1;
            return counter;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
    final double[][] distances = new double[size][size];

    log.debug("Populating node hash");
    final Map<Integer, Node> genehash = new HashMap<Integer, Node>() {
        private static final long serialVersionUID = 1L;

        {
            for (int index = size; --index >= 0; put(index,
                    nodeBuilder().leaf(dataset.dimension(dimensionType).keys().get(index))))
                ;
        }
    };
    TreeMap<Double, int[]> sorted = new TreeMap<>();

    log.debug("Populating distance matrix");
    for (int i = 0; i < size; i++) {
        for (int j = i + 1; j < size; j++) {
            double distance = metric.distance(new AbstractList<Double>() {

                private int i;

                @Override
                public Double get(int index) {
                    return dimensionType == ROW ? original.getEntry(i, index) : original.getEntry(index, i);
                }

                @Override
                public int size() {
                    return other;
                }

                private List<Double> initializeProjection(int i) {
                    this.i = i;
                    return this;
                }
            }.initializeProjection(i), new AbstractList<Double>() {

                private int j;

                @Override
                public Double get(int index) {
                    return dimensionType == ROW ? original.getEntry(j, index) : original.getEntry(index, j);
                }

                @Override
                public int size() {
                    return other;
                }

                private List<Double> initializeProjection(int j) {
                    this.j = j;
                    return this;
                }
            }.initializeProjection(j));

            distances[i][j] = distance;
            distances[j][i] = distance;
            int[] genePair = { i, j };
            // Enter the distance calculated and the genes measured into a
            // treemap. Will be automatically sorted.
            sorted.put(distance, genePair);
        }
    }

    log.debug("Aggregating");
    while (true) {
        // Get the first key of the TreeMap. Will be the shortest distance de
        // facto.
        final double minkey = (Double) sorted.firstKey();
        int[] minValues = (int[]) sorted.firstEntry().getValue();

        final int value1 = minValues[0], value2 = minValues[1];
        // find

        Node cluster = nodeBuilder().branch(minkey, genehash.get(value1), genehash.get(value2));
        int id = enumerator.next();

        genehash.put(id, cluster);
        genehash.remove(value1);
        genehash.remove(value2);

        if (genehash.size() <= 1)
            break;

        // Iterate over all the current clusters to remeasure distance with the
        // previously clustered group.
        for (Entry<Integer, Node> e : genehash.entrySet()) {
            Node c = e.getValue();
            // Skip measuring the new cluster with itself.
            if (c == cluster)
                continue;

            List<Double> aggregation = new ArrayList<>();
            // Get genes from each cluster. Distance is measured from each element
            // to every element.
            for (int current : traverse(dimension.keys(), c))
                for (int created : traverse(dimension.keys(), cluster))
                    aggregation.add(distances[current][created]);

            int[] valuePair = { e.getKey(), id };
            sorted.put(linkage.aggregate(aggregation), valuePair);
        }

        // Get the shortest distance.
        // Check to make sure shortest distance does not include a gene pair
        // that
        // has already had its elements clustered.
        boolean minimized = false;
        while (!minimized) {
            double mk = sorted.firstKey();
            minValues = sorted.firstEntry().getValue();
            // If the gene pair is not present in the current gene set, remove
            // this distance.
            if (!genehash.containsKey(minValues[0]) || !genehash.containsKey(minValues[1]))
                sorted.remove(mk);
            else
                minimized = true;
        }
    }

    Node result = genehash.entrySet().iterator().next().getValue();
    log.debug("Clustered " + result);
    return result;
}

From source file:edu.ucdenver.bios.statisticaltest.HotellingLawleyTraceTest.java

/**
 * Constructor//from w  w  w .j  a v  a  2 s . c o  m
 * @param hypothesisSumOfSquares
 * @param errorSumOfSquares
 * @param rowsBetweenContrast
 * @param columnsWithinContrast
 * @param totalOutcomes
 * @param designRank
 * @param totalSampleSize
 */
public HotellingLawleyTraceTest(RealMatrix hypothesisSumOfSquares, RealMatrix errorSumOfSquares,
        int rowsBetweenContrast, int columnsWithinContrast, int totalOutcomes, int designRank,
        int totalSampleSize) {

    if (!hypothesisSumOfSquares.isSquare() || !errorSumOfSquares.isSquare()
            || hypothesisSumOfSquares.getColumnDimension() != errorSumOfSquares.getRowDimension()) {
        throw new IllegalArgumentException("hypothesis and error matrices must be square and same dimensions");
    }

    this.hypothesisSumOfSquares = hypothesisSumOfSquares;
    this.errorSumOfSquares = errorSumOfSquares;
    this.a = rowsBetweenContrast;
    this.b = columnsWithinContrast;

    // calculate the value of the statistic
    this.HLT = getHotellingLawleyTrace();

    // calculate nuE
    this.nuE = totalSampleSize - designRank;
}

From source file:edu.stanford.cfuller.imageanalysistools.fitting.NelderMeadMinimizer.java

/**
 * Constructs the initial simplex that is the starting point of the optimization given an initial guess at the minimum and a size scale for each parameter.
 * @param initialPoint      The initial guess at the minimum, one component per parameter.
 * @param componentScales   A size scale for each parameter that is used to set how large the initial simplex is.
 * @return                  A matrix containing p + 1 rows, each of which is one set of p parameters, which specify the simplex.
 *///from w w  w  . jav  a2s. co  m
public RealMatrix generateInitialSimplex(RealVector initialPoint, RealVector componentScales) {
    RealMatrix initialSimplex = new Array2DRowRealMatrix(initialPoint.getDimension() + 1,
            initialPoint.getDimension());

    initialSimplex.setRowVector(0, initialPoint);

    for (int i = 1; i < initialSimplex.getRowDimension(); i++) {
        RealVector newVector = initialPoint.copy();
        newVector.setEntry(i - 1, newVector.getEntry(i - 1) + componentScales.getEntry(i - 1));
        initialSimplex.setRowVector(i, newVector);
    }

    return initialSimplex;
}