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

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

Introduction

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

Prototype

@Override
public int getColumnDimension() 

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;/*www . j  a  va  2  s .co  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");
    }/*from   ww w . jav  a 2 s.co m*/

    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:com.ibm.bi.dml.runtime.matrix.data.LibCommonsMath.java

/**
 * Function to compute matrix inverse via matrix decomposition.
 * //w w  w.j a  va 2  s .  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) {//from w w w.j  a  v a 2s .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:dataGen.DataGen.java

/**
 * Saves the Data/*w w w .  ja  va  2s  .c o 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:fp.overlapr.algorithmen.StressMajorization.java

/**
 * Fhrt die Stress-Majorization durch. siehe: Gansner, Koren, North: Graph
 * Drawing by Stress Majorization, 2004//ww w  .  ja  v a 2s .c om
 * 
 * @param graph
 *            Graph, dessen Knoten-Positionen neu berechnet werden sollen
 * @param d
 *            Matrix, die die idealen Distanzen (d_ij) zwischen den Knoten
 *            enthlt
 * @return Matrix, die die neuen x- und y-Werte der einzelnen Knoten enthlt
 */
public static double[][] doStressMajorization(Graph graph, double[][] d) {

    int iter = 0;

    // X holen
    Array2DRowRealMatrix X = new Array2DRowRealMatrix(graph.getKnotenAnzahl(), 2);
    for (int i = 0; i < graph.getKnotenAnzahl(); i++) {
        X.setEntry(i, 0, graph.getKnoten().get(i).getX());
        X.setEntry(i, 1, graph.getKnoten().get(i).getY());
    }

    // D holen
    Array2DRowRealMatrix D = new Array2DRowRealMatrix(d);

    // W berechnen
    Array2DRowRealMatrix W = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
    W.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (D.getEntry(row, column) == 0) {
                return 0.0;
            } else {
                return 1.0 / (D.getEntry(row, column) * D.getEntry(row, column));
            }
        }
    });

    // LW berechnen
    Array2DRowRealMatrix LW = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
    LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row != column) {
                return (-1) * W.getEntry(row, column);
            } else {

                return value;
            }
        }
    });
    LW.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == column) {

                double sum = 0;

                for (int k = 0; k < LW.getColumnDimension(); k++) {
                    if (k != row) {
                        sum = sum + W.getEntry(row, k);
                    }
                }

                return sum;
            } else {

                return value;
            }
        }
    });

    double[][] x = null;

    while (iter < ITER) {

        iter++;

        // LX berechnen
        Array2DRowRealMatrix LX = new Array2DRowRealMatrix(D.getRowDimension(), D.getColumnDimension());
        LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                if (row != column) {

                    // norm 2
                    double term1 = FastMath.pow((X.getEntry(row, 0) - X.getEntry(column, 0)), 2);
                    double term2 = FastMath.pow((X.getEntry(row, 1) - X.getEntry(column, 1)), 2);

                    double abst = Math.sqrt(term1 + term2);

                    return (-1) * W.getEntry(row, column) * D.getEntry(row, column) * inv(abst);
                } else {
                    return value;
                }
            }
        });
        LX.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
            @Override
            public double visit(int row, int column, double value) {
                if (row == column) {

                    double sum = 0;

                    for (int k = 0; k < LX.getColumnDimension(); k++) {
                        if (k != row) {
                            sum = sum + LX.getEntry(row, k);
                        }
                    }
                    return (-1) * sum;
                } else {
                    return value;
                }
            }
        });

        /*
         * Lineare Gleichungssysteme lsen
         */
        // x-Werte holen
        ArrayRealVector xWerte = new ArrayRealVector(X.getColumn(0));

        // y-Werte holen
        ArrayRealVector yWerte = new ArrayRealVector(X.getColumn(1));

        // b_x berechnen
        ArrayRealVector b_x = (ArrayRealVector) LX.operate(xWerte);

        // b_y berechnen
        ArrayRealVector b_y = (ArrayRealVector) LX.operate(yWerte);

        /*
         * CG-Verfahren anwenden
         */
        // neue x-Werte berechnen mittels PCG
        // xWerte = conjugateGradientsMethod(LW, b_x, xWerte);

        // neue y-Werte berechnen mittels PCG
        // yWerte = conjugateGradientsMethod(LW, b_y, yWerte);

        ConjugateGradient cg = new ConjugateGradient(Integer.MAX_VALUE, TOL, false);
        xWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_x);
        yWerte = (ArrayRealVector) cg.solve(LW, JacobiPreconditioner.create(LW), b_y);

        /*
         * neue Positiones-Werte zurckgeben
         */
        x = new double[X.getRowDimension()][2];
        for (int i = 0; i < x.length; i++) {

            x[i][0] = xWerte.getEntry(i);
            x[i][1] = yWerte.getEntry(i);

            X.setEntry(i, 0, xWerte.getEntry(i));
            X.setEntry(i, 1, yWerte.getEntry(i));

        }
    }

    return x;
}

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   ww w .  ja  v  a 2s  .  co  m
 */
private Matrix(final Array2DRowRealMatrix data) {
    this.numRows = data.getRowDimension();
    this.numCols = data.getColumnDimension();
    this.data = (Array2DRowRealMatrix) data.copy();
}

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

/**
 * @param start/*from   w  ww  .j  a  va  2s. co  m*/
 *            The starting frame
 * @param length
 *            The length of the interval, as numbers of frames
 * @param features
 *            The matrix build with feature vectors as rows
 * @return Returns the changing point in the input represented by features
 * 
 */

private int getPoint(int start, int length, int step, Array2DRowRealMatrix features) {
    double max = Double.NEGATIVE_INFINITY;
    int ncols = features.getColumnDimension(), point = 0;
    Array2DRowRealMatrix sub = (Array2DRowRealMatrix) features.getSubMatrix(start, start + length - 1, 0,
            ncols - 1);
    double bicValue = getBICValue(sub);
    for (int i = Segment.FEATURES_SIZE + 1; i < length - Segment.FEATURES_SIZE; i += step) {
        double aux = getLikelihoodRatio(bicValue, i, sub);
        if (aux > max) {
            max = aux;
            point = i;
        }
    }
    if (max < 0)
        point = Integer.MIN_VALUE;
    return point + start;
}

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();//from   ww w. jav a 2 s.c  o  m
    }
}

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

/**
 * /*  ww w  . jav a 2  s . c  o m*/
 * @param bicValue
 *            The bicValue of the model represented by only one Gaussian.
 *            This parameter it's useful when this function is called
 *            repeatedly for different frame values and the same features
 *            parameter
 * @param frame
 *            the frame which is tested for being a change point
 * @param features
 *            the feature vectors matrix
 * @return the likelihood ratio
 */
double getLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) {
    double bicValue1, bicValue2;
    int d = Segment.FEATURES_SIZE;
    double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(features.getRowDimension()) * 2;
    int nrows = features.getRowDimension(), ncols = features.getColumnDimension();
    Array2DRowRealMatrix sub1, sub2;
    sub1 = (Array2DRowRealMatrix) features.getSubMatrix(0, frame - 1, 0, ncols - 1);
    sub2 = (Array2DRowRealMatrix) features.getSubMatrix(frame, nrows - 1, 0, ncols - 1);
    bicValue1 = getBICValue(sub1);
    bicValue2 = getBICValue(sub2);
    return (bicValue - bicValue1 - bicValue2 - penalty);
}