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

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

Introduction

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

Prototype

@Override
public int getRowDimension() 

Source Link

Usage

From source file:jurls.core.becca.Daisychain.java

public static double[] ravel(Array2DRowRealMatrix t, double[] x, double min, double max) {
    int size = t.getRowDimension() * t.getColumnDimension();
    if ((x == null) || (x.length != size))
        x = new double[size];

    double[][] xd = t.getDataRef();
    int p = 0;//from  ww w  .jav a 2  s . c o  m

    double factor = min != max ? (1.0 / (max - min)) : 1.0;

    for (double[] row : xd) {
        for (int i = 0; i < row.length; i++)
            row[i] = (row[i] - min) * factor;

        System.arraycopy(row, 0, x, p, row.length);
        p += row.length;
    }

    return x;
}

From source file:jmbench.PackageMatrixConversion.java

public static void convertToEjml(final Array2DRowRealMatrix src, final DenseMatrix64F dst) {
    if ((src.getRowDimension() != dst.getNumRows()) || (src.getColumnDimension() != dst.getNumCols())) {
        throw new IllegalArgumentException("Matrices are not the same shape");
    }/*  www  .j a va 2s. c om*/

    for (int y = 0; y < src.getRowDimension(); y++) {
        for (int x = 0; x < src.getColumnDimension(); x++) {
            dst.set(y, x, src.getEntry(y, x));
        }
    }
}

From source file:dataGen.DataGen.java

/**
 * Saves the Data/*  ww w  .  j a va 2 s .co m*/
 * @param values
 *       The values which should be saved.
 * @param fw
 *       The File-Writer including the File location
 * @param nf
 *       How should the data get formatted?
 * @throws IOException
 *       If Stream to a File couldn't be written/closed 
 */
public static void saveData(Array2DRowRealMatrix values, Writer fw, NumberFormat nf) throws IOException {
    for (int i = 0; i < values.getRowDimension(); i++) {
        String row = i + 1 + " ";
        //String row = "";

        for (int j = 0; j < values.getColumnDimension(); j++) {
            row = row + nf.format(values.getEntry(i, j)) + " ";
        }
        fw.write(row);
        fw.append(System.getProperty("line.separator"));
    }
    fw.close();
}

From source file:com.ibm.bi.dml.runtime.matrix.data.LibCommonsMath.java

/**
 * Function to compute matrix inverse via matrix decomposition.
 * //  w  w  w  .  ja  va 2s  . c  o m
 * @param in
 * @return
 * @throws DMLRuntimeException
 */
private static MatrixBlock computeMatrixInverse(Array2DRowRealMatrix in) throws DMLRuntimeException {

    if (!in.isSquare())
        throw new DMLRuntimeException("Input to inv() must be square matrix -- given: a " + in.getRowDimension()
                + "x" + in.getColumnDimension() + " matrix.");

    QRDecomposition qrdecompose = new QRDecomposition(in);
    DecompositionSolver solver = qrdecompose.getSolver();
    RealMatrix inverseMatrix = solver.getInverse();

    MatrixBlock inverse = DataConverter.convertToMatrixBlock(inverseMatrix.getData());

    return inverse;
}

From source file:fp.overlapr.algorithmen.StressMajorization.java

@Deprecated
private static ArrayRealVector conjugateGradientsMethod(Array2DRowRealMatrix A, ArrayRealVector b,
        ArrayRealVector werte) {/*w  ww.  j a v  a 2  s.c  om*/

    Array2DRowRealMatrix preJacobi = new Array2DRowRealMatrix(A.getRowDimension(), A.getColumnDimension());

    // Predconditioner berechnen
    preJacobi.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == column) {
                return 1 / A.getEntry(row, column);
            } else {
                return 0.0;
            }
        }
    });

    // x_k beliebig whlen
    ArrayRealVector x_k = new ArrayRealVector(werte);

    // r_k berechnen
    ArrayRealVector r_k = b.subtract(A.operate(x_k));

    // h_k berechnen
    ArrayRealVector h_k = (ArrayRealVector) preJacobi.operate(r_k);

    // d_k = r_k
    ArrayRealVector d_k = h_k;

    // x_k+1 und r_k+1 und d_k+1, sowie alpha und beta und z erzeugen
    ArrayRealVector x_k1;
    ArrayRealVector r_k1;
    ArrayRealVector d_k1;
    ArrayRealVector h_k1;
    double alpha;
    double beta;
    ArrayRealVector z;

    do {
        // Speichere Matrix-Vektor-Produkt, um es nur einmal auszurechnen
        z = (ArrayRealVector) A.operate(d_k);

        // Finde von x_k in Richtung d_k den Ort x_k1 des Minimums der
        // Funktion E
        // und aktualisere den Gradienten bzw. das Residuum
        alpha = r_k.dotProduct(h_k) / d_k.dotProduct(z);
        x_k1 = x_k.add(d_k.mapMultiply(alpha));
        r_k1 = r_k.subtract(z.mapMultiply(alpha));
        h_k1 = (ArrayRealVector) preJacobi.operate(r_k1);

        // Korrigiere die Suchrichtung d_k1
        beta = r_k1.dotProduct(h_k1) / r_k.dotProduct(h_k);
        d_k1 = h_k1.add(d_k.mapMultiply(beta));

        // Werte "eins" weitersetzen
        x_k = x_k1;
        r_k = r_k1;
        d_k = d_k1;
        h_k = h_k1;

    } while (r_k1.getNorm() > TOL);

    return x_k1;
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * @param mat//  w w w  .  j  a  va  2  s .  c  o m
 *            A matrix with feature vectors as rows.
 * @return Returns the BICValue of the Gaussian model that approximates the
 *         the feature vectors data samples
 */
public static double getBICValue(Array2DRowRealMatrix mat) {
    double ret = 0;
    EigenDecomposition ed = new EigenDecomposition(new Covariance(mat).getCovarianceMatrix());
    double[] re = ed.getRealEigenvalues();
    for (int i = 0; i < re.length; i++)
        ret += Math.log(re[i]);
    return ret * (mat.getRowDimension() / 2);
}

From source file:com.clust4j.metrics.scoring.TestMetrics.java

@Test
public void testSilhouetteScoreNaN() {
    Array2DRowRealMatrix X = IRIS.getData();
    final int[] labels = VecUtils.repInt(1, X.getRowDimension());

    assertTrue(Double.isNaN(SILHOUETTE.evaluate(X, labels)));
}

From source file:broadwick.math.Matrix.java

/**
 * Create a matrix from the internal representation of the matrix (copy construtor). This constructor
 * is intentionally private to hide the internal representation of the matrix.
 * @param data the data to be copied.//from   w  w  w  .j av a  2  s. c om
 */
private Matrix(final Array2DRowRealMatrix data) {
    this.numRows = data.getRowDimension();
    this.numCols = data.getColumnDimension();
    this.data = (Array2DRowRealMatrix) data.copy();
}

From source file:com.clust4j.algo.MeanShift.java

/**
 * For testing...//from   w w  w . java2 s . co  m
 * @param data
 * @param quantile
 * @param sep
 * @param seed
 * @param parallel
 * @return
 */
final protected static double autoEstimateBW(Array2DRowRealMatrix data, double quantile,
        GeometricallySeparable sep, Random seed, boolean parallel) {

    return autoEstimateBW(
            new NearestNeighbors(data,
                    new NearestNeighborsParameters((int) (data.getRowDimension() * quantile)).setSeed(seed)
                            .setForceParallel(parallel)).fit(),
            data.getDataRef(), quantile, sep, seed, parallel, null);
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

void printMatrix(Array2DRowRealMatrix a) {
    for (int i = 0; i < a.getRowDimension(); i++) {
        for (int j = 0; j < a.getColumnDimension(); j++)
            System.out.print(a.getEntry(i, j) + " ");
        System.out.println();//  w  ww  .  jav  a2s.c  o  m
    }
}