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

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

Introduction

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

Prototype

public double[] getColumn(final int column) throws OutOfRangeException 

Source Link

Usage

From source file:com.clust4j.data.TestDataSet.java

@Test
public void testConst4() {
    Array2DRowRealMatrix d = IRIS.getData();
    int[] labs = IRIS.getLabels();
    DataSet dat = new DataSet(d, labs, new String[] { "a", "b", "c", "d" });
    assertTrue(dat.numCols() == 4);//from  w  w  w.j  a v  a  2  s  . com
    assertTrue(VecUtils.equalsExactly(dat.getColumn("a"), d.getColumn(0)));
    assertTrue(VecUtils.equalsExactly(dat.getColumn(0), d.getColumn(0)));
}

From source file:com.clust4j.data.TestDataSet.java

@Test
public void testConst6() {
    Array2DRowRealMatrix d = IRIS.getData();
    int[] labs = IRIS.getLabels();
    DataSet dat = new DataSet(d, labs, new String[] { "a", "b", "c", "d" }, new MatrixFormatter());
    assertTrue(dat.numCols() == 4);//from www.j  av a  2  s .c o m
    assertTrue(VecUtils.equalsExactly(dat.getColumn("a"), d.getColumn(0)));
    assertTrue(VecUtils.equalsExactly(dat.getColumn(0), d.getColumn(0)));
}

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

/**
 * Fhrt die Stress-Majorization durch. siehe: Gansner, Koren, North: Graph
 * Drawing by Stress Majorization, 2004//from  w w w .ja  v  a  2  s  . co m
 * 
 * @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:outlineDescriptor.Transform.java

private Array2DRowRealMatrix createSVDmatrix(Array2DRowRealMatrix angleOffset, int aSteps, double step) {

    Array2DRowRealMatrix output = new Array2DRowRealMatrix(2 * aSteps, 2);

    for (int i = 0; i < aSteps; i++) {

        double angle = Math.PI - i * step;

        for (int row = 0; row < angleOffset.getRowDimension(); row++) {

            angleOffset.multiplyEntry(row, i, angleOffset.getEntry(row, i));
        }//from www . j a  v  a 2s  . com

        double stdDev = getDeviance(angleOffset.getColumn(i));
        // new StandardDeviation().evaluate(angleOffset.getColumn(i));

        double outX = Math.cos(angle) * stdDev;
        double outY = Math.sin(angle) * stdDev;

        output.setEntry(i, 0, outX);
        output.setEntry(i, 1, outY);

        output.setEntry(i + aSteps, 0, -outX);
        output.setEntry(i + aSteps, 1, -outY);

    }

    return output;

}

From source file:outlineDescriptor.Transform.java

private Array2DRowRealMatrix simpleHoughTransform(Array2DRowRealMatrix inputMatrix, int aSteps, double step) {

    double xMax = StatUtils.max(inputMatrix.getColumn(0));
    double yMax = StatUtils.max(inputMatrix.getColumn(1));

    int maxDst = (int) Math.sqrt(xMax * xMax + yMax * yMax);

    Array2DRowRealMatrix angleOffset = new Array2DRowRealMatrix(2 * maxDst + 1, aSteps);

    //calculating all possible line/distance pairs for each point
    for (int i = 0; i < aSteps; i++) {
        double angle = i * step;

        double xc = Math.cos(Math.PI / 2 - angle);
        double yc = Math.sin(Math.PI / 2 - angle);

        for (int row = 0; row < inputMatrix.getRowDimension(); row++) {

            if (inputMatrix.getEntry(row, 2) == 0)
                continue;

            int distance = (int) (xc * inputMatrix.getEntry(row, 0) + yc * inputMatrix.getEntry(row, 1));

            distance += maxDst;//  w w w  .java 2 s  .  c  om
            angleOffset.addToEntry(distance, i, inputMatrix.getEntry(row, 2));
        }
    }

    return angleOffset;
}

From source file:outlineDescriptor.Transform.java

private Array2DRowRealMatrix transform(Array2DRowRealMatrix rm, int aSteps) {
    double meanX = StatUtils.mean(rm.getColumn(0));
    double meanY = StatUtils.mean(rm.getColumn(1));
    double step = Math.PI / aSteps;
    Array2DRowRealMatrix output;/*from w w  w  .  j  a  v a  2s  .  c  om*/

    for (int i = 0; i < rm.getRowDimension(); i++) {
        rm.setEntry(i, 0, rm.getEntry(i, 0) - meanX);
        rm.setEntry(i, 1, rm.getEntry(i, 1) - meanY);
    }

    Array2DRowRealMatrix angleOffset = simpleHoughTransform(rm, aSteps, step);

    output = createSVDmatrix(angleOffset, aSteps, step);

    return output;
}