Example usage for org.apache.commons.math3.linear ArrayRealVector setSubVector

List of usage examples for org.apache.commons.math3.linear ArrayRealVector setSubVector

Introduction

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

Prototype

public void setSubVector(int index, double[] v) throws OutOfRangeException 

Source Link

Document

Set a set of consecutive elements.

Usage

From source file:ImageProc.java

public static float[][] colormapJet(int nClass) {
    int n;//from  w  w  w. j  a va 2  s .  c  o  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:edu.utexas.cs.tactex.utils.BrokerUtils.java

/**
 * Rotates a weekly record to start from the next timeslot
 * and append the last day to continue until the end of the day
 * //from w  ww.  ja  va 2s . c om
 * @param energy
 * @param currentTimeslot
 * @return
 */
public static ArrayRealVector rotateWeeklyRecordAndAppendTillEndOfDay(RealVector record, int currentTimeslot) {
    // sanity check
    if (record.getDimension() != 7 * 24) {
        log.error("record dimension is not 7*24, not sure if code robust to that?");
    }
    int predictionStartWeeklyHour = (currentTimeslot + 1) % (record.getDimension());
    ArrayRealVector copy = new ArrayRealVector(record.getDimension());
    // rotate to start from current moment
    RealVector slice1 = record.getSubVector(predictionStartWeeklyHour,
            record.getDimension() - predictionStartWeeklyHour);
    RealVector slice2 = record.getSubVector(0, predictionStartWeeklyHour);
    copy.setSubVector(0, slice1);
    copy.setSubVector(slice1.getDimension(), slice2);
    return copy;
}

From source file:edu.utexas.cs.tactex.BrokerUtilsTest.java

@Test
public void test_rotateWeeklyRecordAndAppendTillEndOfDay() {

    // this test is very similar and is based on 
    // EnergyPredictionTest.testEnergyPredictionOfAboutOneWeek()
    // we moved it here after refactoring a method to BrokersUtils
    ////from  w w  w.j  a  v a  2  s .  c  o m
    // initialize an record vector where the value of an 
    // entry is its index
    ArrayRealVector record = new ArrayRealVector(7 * 24);
    for (int i = 0; i < record.getDimension(); ++i) {
        record.setEntry(i, i);
    }

    int currentTimeslot;
    ArrayRealVector expected;
    ArrayRealVector actual;

    currentTimeslot = 7 * 24 - 1;
    expected = new ArrayRealVector(record);
    actual = BrokerUtils.rotateWeeklyRecordAndAppendTillEndOfDay(record, currentTimeslot);
    assertArrayEquals("no rotation at the beginning of week", expected.toArray(), actual.toArray(), 1e-6);

    currentTimeslot = 1 * 24 - 1;
    // actual
    actual = BrokerUtils.rotateWeeklyRecordAndAppendTillEndOfDay(record, currentTimeslot);
    // prepare expected
    RealVector slice1 = record.getSubVector(1 * 24, 7 * 24 - 24);
    expected.setSubVector(0, slice1);
    expected.setSubVector(slice1.getDimension(), record.getSubVector(0, 24));
    assertArrayEquals("end of first day results in days 2,...,7,1", expected.toArray(), actual.toArray(), 1e-6);

    currentTimeslot = 6 * 24 - 1;
    // actual
    actual = BrokerUtils.rotateWeeklyRecordAndAppendTillEndOfDay(record, currentTimeslot);
    // prepare expected
    slice1 = record.getSubVector(6 * 24, 7 * 24 - 6 * 24);
    expected.setSubVector(0, slice1);
    expected.setSubVector(slice1.getDimension(), record.getSubVector(0, 6 * 24));
    assertArrayEquals("end of 6th day results in days 7,1,...,6", expected.toArray(), actual.toArray(), 1e-6);

    currentTimeslot = 0;
    // predict
    actual = BrokerUtils.rotateWeeklyRecordAndAppendTillEndOfDay(record, currentTimeslot);
    // prepare expected
    slice1 = record.getSubVector(1, 7 * 24 - 1);
    expected.setSubVector(0, slice1);
    expected.setSubVector(slice1.getDimension(), record.getSubVector(0, 1));
    expected.append(record.getSubVector(1, 24 - 1));
    assertArrayEquals("if not at day start, appending until the end of day", expected.toArray(),
            actual.toArray(), 1e-6);
}

From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java

public ArrayRealVector formA() {
    int numOfGen = opfNet.getNoOfGen(), numOfBus = opfNet.getNoActiveBus();
    ArrayRealVector A = new ArrayRealVector(numOfGen + numOfBus - 1);
    A.setSubVector(0, getGenCoeffAVector());
    return A;//www.  j a  va 2 s  .  c o m
}

From source file:org.interpss.opf.dc.impl.EqIneqMatrixBuilder.java

public ArrayRealVector formBiq() {
    int numOfBranch = opfNet.getNoActiveBranch();
    int numOfGen = opfNet.getNoOfGen();

    ArrayRealVector bt = formBranchMvaConstraint();
    ArrayRealVector b_Pmax = formGenInequConstraintPmax();
    ArrayRealVector b_Pmin = formGenInequConstraintPmin();

    ArrayRealVector biq = new ArrayRealVector(numOfBranch * 2 + numOfGen * 2);
    biq.setSubVector(0, bt);
    biq.setSubVector(numOfBranch, bt);//w w  w .j a v a2  s  . c om
    biq.setSubVector(numOfBranch * 2, b_Pmin);
    biq.setSubVector(numOfGen + numOfBranch * 2, b_Pmax);
    return biq;
}