Example usage for org.apache.commons.math3.linear RealVector append

List of usage examples for org.apache.commons.math3.linear RealVector append

Introduction

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

Prototype

public abstract RealVector append(double d);

Source Link

Document

Construct a new vector by appending a double to this vector.

Usage

From source file:ImageProc.java

public static float[][] colormapJet(int nClass) {
    int n;/*  w w  w .j  a v a 2s .  co m*/
    n = (int) Math.ceil((float) nClass / 4);
    ArrayRealVector u, r, g, b;
    RealVector R, G, B;

    u = new ArrayRealVector(3 * n - 1);
    for (int i = 0; i < n; i++) {
        u.setEntry(i, (i + 1.0) / n);
        u.setEntry(u.getDimension() - i - 1, (i + 1.0) / n);
    }
    u.setSubVector(n, new ArrayRealVector(n, 1));

    g = new ArrayRealVector(u.getDimension());

    float m;
    m = (float) Math.ceil((float) n / 2);
    if (nClass % 4 == 1)
        m = m - 1;

    for (int i = 0; i < g.getDimension(); i++) {
        g.setEntry(i, (i + 1 + m));
    }

    r = g.add(new ArrayRealVector(g.getDimension(), n));
    b = g.subtract(new ArrayRealVector(g.getDimension(), n));

    R = new ArrayRealVector();
    G = new ArrayRealVector();
    B = new ArrayRealVector();

    for (int i = 0; i < r.getDimension(); i++) {
        if (r.getEntry(i) <= nClass)
            R = R.append(r.getEntry(i));
    }

    for (int i = 0; i < g.getDimension(); i++) {
        if (g.getEntry(i) <= nClass)
            G = G.append(g.getEntry(i));
    }

    for (int i = 0; i < b.getDimension(); i++) {
        if (b.getEntry(i) >= 1)
            B = B.append(b.getEntry(i));
    }

    float[][] J = new float[nClass][3];
    int index;
    for (int i = 0; i < R.getDimension(); i++) {
        index = (int) R.getEntry(i);
        J[index - 1][0] = (float) u.getEntry(i);
    }

    for (int i = 0; i < G.getDimension(); i++) {
        index = (int) G.getEntry(i);
        J[index - 1][1] = (float) u.getEntry(i);
    }

    for (int i = u.getDimension() - B.getDimension(), j = 0; i < u.getDimension(); i++, j++) {
        index = (int) B.getEntry(j);
        J[index - 1][2] = (float) u.getEntry(i);
    }

    /*
    System.out.println("\nRGB Matrix:");
    for(int i=0;i<nClass;i++){
    for(int j=0;j<3;j++){
        System.out.print(J[i][j]+"\t");
    }
    System.out.println();
    }
    */
    return J;
}

From source file:com.joptimizer.solvers.KKTSolver.java

protected boolean checkKKTSolutionAccuracy(RealVector v, RealVector w) {
    // build the full KKT matrix
    double norm;/*from   w w  w  . j a  v a2s.c  o m*/

    DoubleMatrix2D M = F2.make(this.H.getData());
    if (this.A != null) {
        if (h != null) {
            DoubleMatrix2D[][] parts = { { F2.make(this.H.getData()), F2.make(this.AT.getData()) },
                    { F2.make(this.A.getData()), null } };
            M = F2.compose(parts);
            RealMatrix KKT = new Array2DRowRealMatrix(M.toArray());
            RealVector X = v.append(w);
            RealVector Y = g.append(h);
            // check ||KKT.X+Y||<tolerance
            norm = KKT.operate(X).add(Y).getNorm();
        } else {
            //H.v + [A]T.w = -g
            norm = H.operate(v).add(AT.operate(w)).add(g).getNorm();
        }
    } else {
        // check ||H.X+h||<tolerance
        norm = H.operate(v).add(g).getNorm();
    }
    Log.d(MainActivity.JOPTIMIZER_LOGTAG, "KKT solution error: " + norm);
    return norm < toleranceKKT;
}

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

/**
 * Standardization of a problem on the form:
 * min(c) s.t.//from  w  w  w  .  ja v a2 s  .  c  om
 * G.x < h
 * A.x = b
 */
public void testCGhAb3() throws Exception {
    log.debug("testCGhAb3");

    String problemId = "3";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0];
    double expectedTolerance = MatrixUtils.createRealMatrix(A)
            .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b))
            .getNorm();

    //standard form conversion
    LPStandardConverter lpConverter = new LPStandardConverter();
    lpConverter.toStandardForm(c, G, h, A, b, null, null);

    int n = lpConverter.getStandardN();
    int s = lpConverter.getStandardS();
    c = lpConverter.getStandardC().toArray();
    A = lpConverter.getStandardA().toArray();
    b = lpConverter.getStandardB().toArray();
    double[] lb = lpConverter.getStandardLB().toArray();
    double[] ub = lpConverter.getStandardUB().toArray();
    log.debug("n : " + n);
    log.debug("s : " + s);

    //check consistency
    assertEquals(G.length, s);
    assertEquals(A[0].length, n);
    assertEquals(s + lpConverter.getOriginalN(), n);
    assertEquals(lb.length, n);
    assertEquals(ub.length, n);

    //check constraints
    RealMatrix GOrig = new Array2DRowRealMatrix(G);
    RealVector hOrig = new ArrayRealVector(h);
    RealMatrix AStandard = new Array2DRowRealMatrix(A);
    RealVector bStandard = new ArrayRealVector(b);
    RealVector expectedSolVector = new ArrayRealVector(expectedSol);
    RealVector Gxh = GOrig.operate(expectedSolVector).subtract(hOrig);//G.x - h
    RealVector slackVariables = new ArrayRealVector(s);
    for (int i = 0; i < s; i++) {
        slackVariables.setEntry(i, 0. - Gxh.getEntry(i));//the difference from 0
        assertTrue(slackVariables.getEntry(i) >= 0.);
    }
    RealVector sol = slackVariables.append(expectedSolVector);
    RealVector Axmb = AStandard.operate(sol).subtract(bStandard);
    assertEquals(0., Axmb.getNorm(), expectedTolerance);

    //      Utils.writeDoubleArrayToFile(new double[]{s}, "target" + File.separator   + "standardS"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(c, "target" + File.separator   + "standardC"+problemId+".txt");
    //      Utils.writeDoubleMatrixToFile(A, "target" + File.separator   + "standardA"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(b, "target" + File.separator   + "standardB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(lb, "target" + File.separator   + "standardLB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(ub, "target" + File.separator   + "standardUB"+problemId+".txt");
}

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

/**
 * Standardization of a problem on the form:
 * min(c) s.t./*from w w w  . j  a  va 2 s  .com*/
 * G.x < h
 * A.x = b
 */
public void testCGhAb2() throws Exception {
    log.debug("testCGhAb2");

    String problemId = "2";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0];
    double expectedTolerance = MatrixUtils.createRealMatrix(A)
            .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b))
            .getNorm();

    //standard form conversion
    double unboundedLBValue = Double.NEGATIVE_INFINITY;
    double unboundedUBValue = Double.POSITIVE_INFINITY;
    LPStandardConverter lpConverter = new LPStandardConverter(unboundedLBValue, unboundedUBValue);
    lpConverter.toStandardForm(c, G, h, A, b, null, null);

    int n = lpConverter.getStandardN();
    int s = lpConverter.getStandardS();
    c = lpConverter.getStandardC().toArray();
    A = lpConverter.getStandardA().toArray();
    b = lpConverter.getStandardB().toArray();
    double[] lb = lpConverter.getStandardLB().toArray();
    double[] ub = lpConverter.getStandardUB().toArray();
    log.debug("n : " + n);
    log.debug("s : " + s);
    log.debug("c : " + ArrayUtils.toString(c));
    log.debug("A : " + ArrayUtils.toString(A));
    log.debug("b : " + ArrayUtils.toString(b));
    log.debug("lb : " + ArrayUtils.toString(lb));
    log.debug("ub : " + ArrayUtils.toString(ub));

    //check consistency
    assertEquals(G.length, s);
    assertEquals(A[0].length, n);
    assertEquals(s + lpConverter.getOriginalN(), n);
    assertEquals(lb.length, n);
    assertEquals(ub.length, n);

    //check constraints
    RealMatrix GOrig = new Array2DRowRealMatrix(G);
    RealVector hOrig = new ArrayRealVector(h);
    RealMatrix AStandard = new Array2DRowRealMatrix(A);
    RealVector bStandard = new ArrayRealVector(b);
    RealVector expectedSolVector = new ArrayRealVector(expectedSol);
    RealVector Gxh = GOrig.operate(expectedSolVector).subtract(hOrig);//G.x - h
    RealVector slackVariables = new ArrayRealVector(s);
    for (int i = 0; i < s; i++) {
        slackVariables.setEntry(i, 0. - Gxh.getEntry(i));//the difference from 0
        assertTrue(slackVariables.getEntry(i) >= 0.);
    }
    RealVector sol = slackVariables.append(expectedSolVector);
    RealVector Axmb = AStandard.operate(sol).subtract(bStandard);
    assertEquals(0., Axmb.getNorm(), expectedTolerance);

    //      Utils.writeDoubleArrayToFile(new double[]{s}, "target" + File.separator   + "standardS"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(c, "target" + File.separator   + "standardC"+problemId+".txt");
    //      Utils.writeDoubleMatrixToFile(A, "target" + File.separator   + "standardA"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(b, "target" + File.separator   + "standardB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(lb, "target" + File.separator   + "standardLB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(ub, "target" + File.separator   + "standardUB"+problemId+".txt");
}

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

/**
 * Standardization of a problem on the form:
 * min(c) s.t./*  w  w  w.j  a  v  a 2s .c  o m*/
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 */
public void testCGhAbLbUb1() throws Exception {
    log.debug("testCGhAbLbUb1");

    String problemId = "1";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0];
    //double expectedTolerance = Utils.loadDoubleArrayFromFile("lp"+File.separator+"standardization"+File.separator+"tolerance"+problemId+".txt")[0];
    double expectedTolerance = MatrixUtils.createRealMatrix(A)
            .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b))
            .getNorm();

    //standard form conversion
    double unboundedLBValue = Double.NEGATIVE_INFINITY;//this is because in the file the unbounded lb are -Infinity values (not the default value) 
    double unboundedUBValue = Double.POSITIVE_INFINITY;//this is because in the file the unbounded ub are +Infinity values
    LPStandardConverter lpConverter = new LPStandardConverter(unboundedLBValue, unboundedUBValue);
    lpConverter.toStandardForm(c, G, h, A, b, lb, ub);

    int n = lpConverter.getStandardN();
    int s = lpConverter.getStandardS();
    c = lpConverter.getStandardC().toArray();
    A = lpConverter.getStandardA().toArray();
    b = lpConverter.getStandardB().toArray();
    lb = lpConverter.getStandardLB().toArray();
    ub = lpConverter.getStandardUB().toArray();
    log.debug("n : " + n);
    log.debug("s : " + s);
    log.debug("c : " + ArrayUtils.toString(c));
    log.debug("A : " + ArrayUtils.toString(A));
    log.debug("b : " + ArrayUtils.toString(b));
    log.debug("lb : " + ArrayUtils.toString(lb));
    log.debug("ub : " + ArrayUtils.toString(ub));

    //check consistency
    assertEquals(G.length, s);
    assertEquals(s + lpConverter.getOriginalN(), n);
    assertEquals(lb.length, n);
    assertEquals(ub.length, n);

    //check constraints
    RealMatrix GOrig = new Array2DRowRealMatrix(G);
    RealVector hOrig = new ArrayRealVector(h);
    RealMatrix AStandard = new Array2DRowRealMatrix(A);
    RealVector bStandard = new ArrayRealVector(b);
    RealVector expectedSolVector = new ArrayRealVector(expectedSol);
    RealVector Gxh = GOrig.operate(expectedSolVector).subtract(hOrig);//G.x - h
    RealVector slackVariables = new ArrayRealVector(s);
    for (int i = 0; i < s; i++) {
        slackVariables.setEntry(i, 0. - Gxh.getEntry(i));//the difference from 0
        assertTrue(slackVariables.getEntry(i) >= 0.);
    }
    RealVector sol = slackVariables.append(expectedSolVector);
    RealVector Axmb = AStandard.operate(sol).subtract(bStandard);
    assertEquals(0., Axmb.getNorm(), expectedTolerance);

    //      Utils.writeDoubleArrayToFile(new double[]{s}, "target" + File.separator   + "standardS"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(c, "target" + File.separator   + "standardC"+problemId+".txt");
    //      Utils.writeDoubleMatrixToFile(A, "target" + File.separator   + "standardA"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(b, "target" + File.separator   + "standardB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(lb, "target" + File.separator   + "standardLB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(ub, "target" + File.separator   + "standardUB"+problemId+".txt");
}

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

/**
 * Standardization of a problem on the form:
 * min(c) s.t./*ww  w.  j a  v  a2  s  .  c o  m*/
 * G.x < h
 * A.x = b
 * lb <= x <= ub
 */
public void testCGhAbLbUb4() throws Exception {
    log.debug("testCGhAbLbUb4");

    String problemId = "4";

    double[] c = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "c" + problemId + ".txt");
    double[][] G = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "G" + problemId + ".csv",
            ",".charAt(0));
    double[] h = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "h" + problemId + ".txt");
    ;
    double[][] A = Utils.loadDoubleMatrixFromFile(
            "lp" + File.separator + "standardization" + File.separator + "A" + problemId + ".csv",
            ",".charAt(0));
    double[] b = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "b" + problemId + ".txt");
    double[] lb = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "lb" + problemId + ".txt");
    double[] ub = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "ub" + problemId + ".txt");
    double[] expectedSol = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "sol" + problemId + ".txt");
    double expectedValue = Utils.loadDoubleArrayFromFile(
            "lp" + File.separator + "standardization" + File.separator + "value" + problemId + ".txt")[0];
    double expectedTolerance = MatrixUtils.createRealMatrix(A)
            .operate(MatrixUtils.createRealVector(expectedSol)).subtract(MatrixUtils.createRealVector(b))
            .getNorm();

    int nOsSplittingVariables = 0;
    //      for(int i=0; i<lb.length; i++){
    //         if(Double.compare(lb[i], 0.) != 0){
    //            nOsSplittingVariables++;
    //         }
    //      }

    //standard form conversion
    double unboundedLBValue = Double.NaN;//this is because in the file the unbounded lb are NaN values (and also the default value) 
    double unboundedUBValue = Double.NaN;//this is because in the file the unbounded ub are NaN values
    LPStandardConverter lpConverter = new LPStandardConverter(unboundedLBValue, unboundedUBValue);
    lpConverter.toStandardForm(c, G, h, A, b, lb, ub);

    int n = lpConverter.getStandardN();
    int s = lpConverter.getStandardS();
    c = lpConverter.getStandardC().toArray();
    A = lpConverter.getStandardA().toArray();
    b = lpConverter.getStandardB().toArray();
    lb = lpConverter.getStandardLB().toArray();
    ub = lpConverter.getStandardUB().toArray();
    log.debug("n : " + n);
    log.debug("s : " + s);

    //check consistency
    assertEquals(G.length, s);
    assertEquals(s + lpConverter.getOriginalN() + nOsSplittingVariables, n);
    assertEquals(lb.length, n);
    assertEquals(ub.length, n);

    //check constraints
    RealMatrix GOrig = new Array2DRowRealMatrix(G);
    RealVector hOrig = new ArrayRealVector(h);
    RealMatrix AStandard = new Array2DRowRealMatrix(A);
    RealVector bStandard = new ArrayRealVector(b);
    RealVector expectedSolVector = new ArrayRealVector(expectedSol);
    RealVector Gxh = GOrig.operate(expectedSolVector).subtract(hOrig);//G.x - h
    RealVector slackVariables = new ArrayRealVector(s);
    for (int i = 0; i < s; i++) {
        slackVariables.setEntry(i, 0. - Gxh.getEntry(i));//the difference from 0
        assertTrue(slackVariables.getEntry(i) >= 0.);
    }
    RealVector sol = slackVariables.append(expectedSolVector);
    RealVector Axmb = AStandard.operate(sol).subtract(bStandard);
    assertEquals(0., Axmb.getNorm(), expectedTolerance * 1.001);

    //      Utils.writeDoubleArrayToFile(new double[]{s}, "target" + File.separator   + "standardS"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(c, "target" + File.separator   + "standardC"+problemId+".txt");
    //      Utils.writeDoubleMatrixToFile(A, "target" + File.separator   + "standardA"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(b, "target" + File.separator   + "standardB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(lb, "target" + File.separator   + "standardLB"+problemId+".txt");
    //      Utils.writeDoubleArrayToFile(ub, "target" + File.separator   + "standardUB"+problemId+".txt");
}