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

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

Introduction

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

Prototype

@Override
public double[] toArray() 

Source Link

Usage

From source file:info.rmarcus.birkhoffvonneumann.MatrixUtils.java

public static double[] normalize(double[] input) {
    ArrayRealVector arv = new ArrayRealVector(input);
    arv.mapDivideToSelf(arv.getNorm());/* w  ww.j av  a 2s  .c o  m*/
    return NullUtils.orThrow(arv.toArray(), () -> new BVNRuntimeException("Could not normalize array!"));
}

From source file:mr.go.sgfilter.SGFilter.java

/**
 * Computes Savitzky-Golay coefficients for given parameters
 * // w ww.j a  va2s  .  c o  m
 * @param nl
 *            numer of past data points filter will use
 * @param nr
 *            number of future data points filter will use
 * @param degree
 *            order of smoothin polynomial
 * @return Savitzky-Golay coefficients
 * @throws IllegalArgumentException
 *             if {@code nl < 0} or {@code nr < 0} or {@code nl + nr <
 *             degree}
 */
public static double[] computeSGCoefficients(int nl, int nr, int degree) {
    if (nl < 0 || nr < 0 || nl + nr < degree)
        throw new IllegalArgumentException("Bad arguments");
    Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(degree + 1, degree + 1);
    double[][] a = matrix.getDataRef();
    double sum;
    for (int i = 0; i <= degree; i++) {
        for (int j = 0; j <= degree; j++) {
            sum = (i == 0 && j == 0) ? 1 : 0;
            for (int k = 1; k <= nr; k++)
                sum += pow(k, i + j);
            for (int k = 1; k <= nl; k++)
                sum += pow(-k, i + j);
            a[i][j] = sum;
        }
    }
    double[] b = new double[degree + 1];
    b[0] = 1;
    ArrayRealVector b1 = new ArrayRealVector(b);
    b1 = (ArrayRealVector) (new LUDecomposition(matrix)).getSolver().solve(b1);
    b = b1.toArray();
    double[] coeffs = new double[nl + nr + 1];
    for (int n = -nl; n <= nr; n++) {
        sum = b[0];
        for (int m = 1; m <= degree; m++)
            sum += b[m] * pow(n, m);
        coeffs[n + nl] = sum;
    }
    return coeffs;
}

From source file:edu.utexas.cs.tactex.utils.ChargeEstimatorDefault.java

@Override
public double estimateCharge(ArrayRealVector customerEnergy, TariffSpecification spec) {

    TariffEvaluationHelper helper = new TariffEvaluationHelper();
    helper.init(); // init with no parameters since we don't know the customer's parameters(?)

    Tariff tariff = tariffRepoMgr.findTariffById(spec.getId());
    if (null == tariff) {
        log.error("failed to find spec in repo, spec-id: " + spec.getId());
        return -Double.MAX_VALUE;
    }//from   w ww. j  av  a2  s. c  o m

    // evaluate
    double evaluation = helper.estimateCost(tariff, customerEnergy.toArray(), true);

    return evaluation;
}

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
    ///* ww  w  .j ava  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:gamlss.algorithm.AdditiveFit.java

/**
 * Calculates rss = weighted.mean(residuals^2, w).
 * @param resid - residuals//  www.ja v a  2  s .co  m
 * @param weights - vector of weights
 * @return weighted.mean(residuals^2, w)
 */
private double calculateRss(final ArrayRealVector resid, final ArrayRealVector weights) {
    //rss <- weighted.mean(residuals^2, w)
    size = resid.getDimension();
    tempArr = new double[size];
    for (int i = 0; i < size; i++) {
        tempArr[i] = resid.getEntry(i) * resid.getEntry(i);
    }
    size = 0;
    return mean.evaluate(new ArrayRealVector(tempArr, false).getDataRef(), weights.toArray());
}

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

/**
 * Test /*from   ww  w .ja va  2 s .  co  m*/
 * TODO cover a few more cases 
 */
@Test
public void test_MeanMarketPrice() {
    initWithBootData();

    double avgPricePerMWH = marketManagerService.getMeanMarketPricePerMWH();
    assertEquals("correct value for market price/mWh", 15.0, avgPricePerMWH, 1e-6);

    double avgPricePerKWH = marketManagerService.getMeanMarketPricePerKWH();
    assertEquals("correct value for market price/kWh", 0.015, avgPricePerKWH, 1e-6);

    ArrayRealVector avgPriceArray = marketManagerService.getMarketAvgPricesArrayKwh();
    double[] expected = new double[] { 0.015, 0.015, 0.015 };
    assertArrayEquals("correct value for avg-prices array", expected, avgPriceArray.toArray(), 1e-6);

    double expected0 = 0.015;
    double expected1 = 0.015;
    double expected2 = 0.015;
    assertEquals("correct avg price per slot0", expected0, marketManagerService.getMarketAvgPricePerSlotKWH(0),
            1e-6);
    assertEquals("correct avg price per slot1", expected1, marketManagerService.getMarketAvgPricePerSlotKWH(1),
            1e-6);
    assertEquals("correct avg price per slot2", expected2, marketManagerService.getMarketAvgPricePerSlotKWH(2),
            1e-6);
    assertEquals("correct avg price per slot - wrapping index", expected0,
            marketManagerService.getMarketAvgPricePerSlotKWH(4), 1e-6);
}

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

@Override
public double getMarketPricePerKWHRecordStd() {
    ArrayRealVector marketAvgPricePerSlot = getMarketAvgPricesArrayKwh();
    return Math.sqrt(StatUtils.variance(marketAvgPricePerSlot.toArray()));
}