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

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

Introduction

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

Prototype

public Array2DRowRealMatrix(final double[] v) 

Source Link

Document

Create a new (column) RealMatrix using v as the data for the unique column of the created matrix.

Usage

From source file:edu.ucdenver.bios.powersvc.resource.PowerResourceHelper.java

/**
 * This method takes Named Matrix and converts it into a RealMatrix and
 * returns a Real Matrix.//w w w. j a va  2s  . c o m
 * 
 * @param namedMatrix
 *            The matrix is a input matrix of type NamedMatrix which is to
 *            be converted to type RealMatrix.
 * @return RealMatrix Returns a RealMatrix which is obtained by converting
 *         the input matrix to a RealMatrix.
 */
public static RealMatrix toRealMatrix(final NamedMatrix namedMatrix) {
    RealMatrix realMatrix = null;
    if (namedMatrix != null) {
        Blob2DArray blob = namedMatrix.getData();
        if (blob != null) {
            realMatrix = new Array2DRowRealMatrix(blob.getData());
        }
    }
    return realMatrix;
}

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

License:asdf

@Test
public void testQuerySmall() {
    KDTree kd = new KDTree(new Array2DRowRealMatrix(a));
    assertTrue(VecUtils.equalsExactly(kd.idx_array, new int[] { 0, 1, 2 }));
    assertTrue(kd.node_bounds.length == 2);
    assertTrue(kd.node_bounds[0].length == 1);
    assertTrue(kd.node_bounds[1].length == 1);
    assertTrue(VecUtils.equalsExactly(kd.node_bounds[0][0], new double[] { 0.0, 0.0, 0.0, 2.0 }));
    assertTrue(VecUtils.equalsExactly(kd.node_bounds[1][0], new double[] { 5.0, 6.0, 7.0, 4.0 }));

    double[][] expectedDists = new double[][] { new double[] { 0.0 }, new double[] { 0.0 } };
    int[][] expectedIndices = new int[][] { new int[] { 0 }, new int[] { 1 } };

    Neighborhood neighb;/*from   ww w.  j a  va  2  s  .  co  m*/
    boolean[] trueFalse = new boolean[] { true, false };
    for (boolean dualTree : trueFalse) {
        for (boolean sort : trueFalse) {
            neighb = new Neighborhood(
                    kd.query(new double[][] { new double[] { 0, 1, 0, 2 }, new double[] { 0, 0, 1, 2 } }, 1,
                            dualTree, sort));

            assertTrue(MatUtils.equalsExactly(expectedDists, neighb.getDistances()));
            assertTrue(MatUtils.equalsExactly(expectedIndices, neighb.getIndices()));
        }
    }
}

From source file:gdsc.core.clustering.optics.OPTICSManagerTest.java

/**
 * Test the results of LoOP using the ELKI framework
 */// w  w w. jav a2 s  .  co  m
@Test
public void canComputeLoOP() {
    TrackProgress tracker = null; //new SimpleTrackProgress();
    for (int n : new int[] { 100, 500 }) {
        OPTICSManager om = createOPTICSManager(size, n);
        om.setTracker(tracker);
        om.setNumberOfThreads(1);

        // Use ELKI to provide the expected results
        double[][] data = new Array2DRowRealMatrix(om.getDoubleData()).transpose().getData();

        for (int minPts : new int[] { 5, 10 }) {
            // Reset starting Id to 1
            DatabaseConnection dbc = new ArrayAdapterDatabaseConnection(data, null, 0);
            ListParameterization params = new ListParameterization();
            params.addParameter(AbstractDatabase.Parameterizer.DATABASE_CONNECTION_ID, dbc);
            Database db = ClassGenericsUtil.parameterizeOrAbort(StaticArrayDatabase.class, params);
            db.initialize();
            Relation<?> rel = db.getRelation(TypeUtil.ANY);
            Assert.assertEquals("Database size does not match.", n, rel.size());

            double lambda = 1;

            // Use max range
            long t1 = System.nanoTime();
            float[] r1 = om.loop(minPts, lambda, false);
            t1 = System.nanoTime() - t1;

            // Test verses the ELKI frame work
            params = new ListParameterization();
            params.addParameter(de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP.Parameterizer.KCOMP_ID, minPts);
            params.addParameter(de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP.Parameterizer.KREACH_ID, minPts);
            params.addParameter(de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP.Parameterizer.LAMBDA_ID, lambda);
            params.addParameter(
                    de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP.Parameterizer.COMPARISON_DISTANCE_FUNCTION_ID,
                    EuclideanDistanceFunction.STATIC);
            Class<de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP<DoubleVector>> clz = ClassGenericsUtil
                    .uglyCastIntoSubclass(de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP.class);
            de.lmu.ifi.dbs.elki.algorithm.outlier.lof.LoOP<DoubleVector> loop = params.tryInstantiate(clz);
            long t2 = System.nanoTime();
            OutlierResult or = loop.run(db);
            t2 = System.nanoTime() - t2;

            // Check 
            //System.out.printf("LoOP %d vs %d (ELKI) %f\n", t1, t2, (double)t2 / t1);
            int i = 0;
            DoubleRelation scores = or.getScores();
            for (DBIDIter it = scores.iterDBIDs(); it.valid(); it.advance(), i++) {
                String prefix = "[" + i + "] ";

                int expId = asInteger(it);
                int obsId = i;

                double expL = scores.doubleValue(it);
                double obsL = r1[i];

                //System.out.printf("%s %d %d : %f = %f\n", prefix, expId, obsId, expL, obsL);

                Assert.assertEquals(prefix + "Id", expId, obsId);
                Assert.assertEquals(prefix + "LoOP", expL, obsL, expL * 1e-3);
            }
        }
    }
}

From source file:com.joptimizer.optimizers.LPPresolver.java

/**
 * Given R the row scaling factor and T the column scaling factor, if x is 
 * the solution of the problem before scaling and x1 is the solution 
 * of the problem after scaling, we have: 
 * <br>R.A.T.x1 = Rb and x = T.x1/*  www .  ja va 2  s .c o  m*/
 * 
 * Every scaling needs the adjustment of the other data vectors of the LP problem. 
 * After scaling, the vectors c, lb, ub become
 * <br>c -> T.c     
 * <br>lb -> InvT.lb
 * <br>ub -> InvT.ub
 * 
 * The objective value is the same.
 *  
 * @see Xin Huang, "Preprocessing and Postprocessing in Linear Optimization" 2.8
 */
private void scaling() {
    if (presolvedA instanceof SparseDoubleMatrix2D) {
        MatrixRescaler rescaler = new Matrix1NormRescaler();
        DoubleMatrix1D[] UV = rescaler.getMatrixScalingFactors((SparseDoubleMatrix2D) presolvedA);
        this.R = UV[0];
        this.T = UV[1];
        if (log.isDebugEnabled()) {
            boolean checkOK = rescaler.checkScaling(presolvedA, R, T);
            if (!checkOK) {
                log.warn("Scaling failed (checkScaling = false)");
            }
            double[] cn_00_original = ColtUtils
                    .getConditionNumberRange(new Array2DRowRealMatrix(presolvedA.toArray()), Integer.MAX_VALUE);
            log.debug("cn_00_original A before scaling: " + ArrayUtils.toString(cn_00_original));
            double[] cn_2_original = ColtUtils
                    .getConditionNumberRange(new Array2DRowRealMatrix(presolvedA.toArray()), 2);
            log.debug("cn_2_original A before scaling : " + ArrayUtils.toString(cn_2_original));
        }
        //scaling A -> R.A.T
        presolvedA = ColtUtils.diagonalMatrixMult(R, presolvedA, T);

        if (log.isDebugEnabled()) {
            double[] cn_00_scaled = ColtUtils
                    .getConditionNumberRange(new Array2DRowRealMatrix(presolvedA.toArray()), Integer.MAX_VALUE);
            log.debug("cn_00_scaled A after scaling : " + ArrayUtils.toString(cn_00_scaled));
            double[] cn_2_scaled = ColtUtils
                    .getConditionNumberRange(new Array2DRowRealMatrix(presolvedA.toArray()), 2);
            log.debug("cn_2_scaled A after scaling  : " + ArrayUtils.toString(cn_2_scaled));
        }
        for (int i = 0; i < R.size(); i++) {
            double ri = R.getQuick(i);
            presolvedB.setQuick(i, presolvedB.getQuick(i) * ri);
        }

        this.minRescaledLB = Double.MAX_VALUE;
        this.maxRescaledUB = -Double.MAX_VALUE;
        for (int i = 0; i < T.size(); i++) {
            double ti = T.getQuick(i);

            presolvedC.setQuick(i, presolvedC.getQuick(i) * ti);

            double lbi = presolvedLB.getQuick(i) / ti;
            presolvedLB.setQuick(i, lbi);
            this.minRescaledLB = Math.min(this.minRescaledLB, lbi);

            double ubi = presolvedUB.getQuick(i) / ti;
            presolvedUB.setQuick(i, ubi);
            this.maxRescaledUB = Math.max(this.maxRescaledUB, ubi);
        }
    }
}