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

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

Introduction

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

Prototype

@Override
public ArrayRealVector add(RealVector v) throws DimensionMismatchException 

Source Link

Usage

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

@Deprecated
private static ArrayRealVector conjugateGradientsMethod(Array2DRowRealMatrix A, ArrayRealVector b,
        ArrayRealVector werte) {/*from ww w .  j a va 2s .  c  o m*/

    Array2DRowRealMatrix preJacobi = new Array2DRowRealMatrix(A.getRowDimension(), A.getColumnDimension());

    // Predconditioner berechnen
    preJacobi.walkInRowOrder(new DefaultRealMatrixChangingVisitor() {
        @Override
        public double visit(int row, int column, double value) {
            if (row == column) {
                return 1 / A.getEntry(row, column);
            } else {
                return 0.0;
            }
        }
    });

    // x_k beliebig whlen
    ArrayRealVector x_k = new ArrayRealVector(werte);

    // r_k berechnen
    ArrayRealVector r_k = b.subtract(A.operate(x_k));

    // h_k berechnen
    ArrayRealVector h_k = (ArrayRealVector) preJacobi.operate(r_k);

    // d_k = r_k
    ArrayRealVector d_k = h_k;

    // x_k+1 und r_k+1 und d_k+1, sowie alpha und beta und z erzeugen
    ArrayRealVector x_k1;
    ArrayRealVector r_k1;
    ArrayRealVector d_k1;
    ArrayRealVector h_k1;
    double alpha;
    double beta;
    ArrayRealVector z;

    do {
        // Speichere Matrix-Vektor-Produkt, um es nur einmal auszurechnen
        z = (ArrayRealVector) A.operate(d_k);

        // Finde von x_k in Richtung d_k den Ort x_k1 des Minimums der
        // Funktion E
        // und aktualisere den Gradienten bzw. das Residuum
        alpha = r_k.dotProduct(h_k) / d_k.dotProduct(z);
        x_k1 = x_k.add(d_k.mapMultiply(alpha));
        r_k1 = r_k.subtract(z.mapMultiply(alpha));
        h_k1 = (ArrayRealVector) preJacobi.operate(r_k1);

        // Korrigiere die Suchrichtung d_k1
        beta = r_k1.dotProduct(h_k1) / r_k.dotProduct(h_k);
        d_k1 = h_k1.add(d_k.mapMultiply(beta));

        // Werte "eins" weitersetzen
        x_k = x_k1;
        r_k = r_k1;
        d_k = d_k1;
        h_k = h_k1;

    } while (r_k1.getNorm() > TOL);

    return x_k1;
}

From source file:edu.utexas.cs.tactex.servercustomers.factoredcustomer.DefaultCapacityBundle.java

/**
 * sum all originator's (scaled) energies (since I believe the broker sees the
 * sum of them only). /*from   w  w  w  .  j  a va  2s.c o  m*/
 * @param currentTimeslot 
 * @throws Exception 
 * @throws DimensionMismatchException 
 */
@Override
public ArrayRealVector getPredictedEnergy(TariffSubscription subscription, int recordLength,
        int currentTimeslot) throws DimensionMismatchException, Exception {
    ArrayRealVector result = new ArrayRealVector(recordLength);

    // sum all originator's energies 
    for (CapacityOriginator originator : capacityOriginators) {
        ArrayRealVector originatorPredictedEnergy = originator.getPredictedEnergy(subscription, recordLength,
                currentTimeslot);
        //log.info("originatorPredictedEnergy " + Arrays.toString(originatorPredictedEnergy.toArray()));
        result = result.add(originatorPredictedEnergy);
        //log.info("bundleresult " + Arrays.toString(result.toArray()));
    }

    // normalize to 1 population member
    result.mapDivideToSelf(customerInfo.getPopulation());
    //log.info("bundleresultnormalized " + Arrays.toString(result.toArray()));

    // all predictions are positive and are adjusted in the server in
    // DefaultUtilityOptimizer.usePower() therefore we adjust the sign here,
    // the last point before returning to our shifting predictor
    double usageSign = getPowerType().isConsumption() ? +1 : -1;
    result.mapMultiplyToSelf(usageSign);

    return result;
}

From source file:edu.utexas.cs.tactex.tariffoptimization.TariffOptimizierTOUFixedMargin.java

private TotalEnergyRecords sumTotalEnergy(HashMap<CustomerInfo, ArrayRealVector> customer2estimatedEnergy,
        HashMap<TariffSpecification, HashMap<CustomerInfo, Integer>> tariffSubscriptions, int currentTimeslot) {

    List<TariffSpecification> allSpecs = new ArrayList<TariffSpecification>();
    allSpecs.addAll(tariffSubscriptions.keySet());
    HashMap<CustomerInfo, HashMap<TariffSpecification, ShiftedEnergyData>> customer2ShiftedEnergy = estimateShiftedPredictions(
            customer2estimatedEnergy, allSpecs, currentTimeslot);

    int predictionRecordLength = customer2estimatedEnergy.values().iterator().next().getDimension();
    ArrayRealVector predictedMyCustomersEnergy = new ArrayRealVector(predictionRecordLength);

    // we currently predict cost by total amount of energy
    ////from   ww w .  j  av a2s .  co  m
    for (Entry<TariffSpecification, HashMap<CustomerInfo, Integer>> entry : tariffSubscriptions.entrySet()) {
        TariffSpecification spec = entry.getKey();
        for (Entry<CustomerInfo, Integer> e : entry.getValue().entrySet()) {
            CustomerInfo customer = e.getKey();
            int subs = e.getValue();
            RealVector customerEnergy = customer2ShiftedEnergy.get(customer).get(spec).getShiftedEnergy()
                    .mapMultiply(subs);
            predictedMyCustomersEnergy = predictedMyCustomersEnergy.add(customerEnergy);
        }
    }
    // competitor energy prediction
    ArrayRealVector predictedCompetitorsEnergyRecord = new ArrayRealVector(predictionRecordLength);
    HashMap<CustomerInfo, HashMap<TariffSpecification, Integer>> predictedCustomerSubscriptions = BrokerUtils
            .revertKeyMapping(tariffSubscriptions);
    for (CustomerInfo cust : predictedCustomerSubscriptions.keySet()) {
        double subsToOthers = cust.getPopulation()
                - BrokerUtils.sumMapValues(predictedCustomerSubscriptions.get(cust));
        RealVector customerNonShiftedEnergy = customer2estimatedEnergy.get(cust).mapMultiply(subsToOthers);
        predictedCompetitorsEnergyRecord = predictedCompetitorsEnergyRecord.add(customerNonShiftedEnergy);
    }

    log.debug("predictedMyCustomersEnergy =" + predictedMyCustomersEnergy.toString());
    log.debug("predictedCompetitorsEnergyRecord =" + predictedCompetitorsEnergyRecord.toString());
    return new TotalEnergyRecords(predictedMyCustomersEnergy, predictedCompetitorsEnergyRecord);
}

From source file:automenta.vivisect.dimensionalize.HyperassociativeMap.java

private ArrayRealVector waitAndProcessFutures(List<Future<ArrayRealVector>> futures)
        throws InterruptedException {
    // wait for all nodes to finish aligning and calculate the new center point
    ArrayRealVector pointSum = newVector();
    try {//  w w  w. ja  v a 2 s  .  c om
        for (Future<ArrayRealVector> future : futures) {
            ArrayRealVector newPoint = future.get();
            //TODO use direct array
            pointSum = pointSum.add(newPoint);

        }
    } catch (ExecutionException caught) {
        //LOGGER.error("Align had an unexpected problem executing.", caught);
        throw new RuntimeException("Unexpected execution exception. Get should block indefinitely", caught);
    }
    if (learningRate * LEARNING_RATE_PROCESSING_ADJUSTMENT < DEFAULT_LEARNING_RATE) {
        if (getAverageMovement() < (equilibriumDistance * acceptableMaxDistanceFactor
                * acceptableDistanceAdjustment)) {
            acceptableMaxDistanceFactor = maxMovement * 2.0;
        }
        learningRate *= LEARNING_RATE_PROCESSING_ADJUSTMENT;
        //LOGGER.debug("learning rate: " + learningRate + ", acceptableDistanceFactor: " + acceptableDistanceFactor);
    }
    return pointSum;
}

From source file:org.rhwlab.dispim.datasource.ClusteredDataSource.java

public RealVector getDataMean() {
    ArrayRealVector ret = new ArrayRealVector(getD());
    for (int k = 0; k < centers.length; ++k) {
        ret = ret.add(centers[k].mapMultiply(gaussians.get(k).getN()));
    }//w  w w . j  av  a 2  s .c o  m
    return ret.mapDivide(X.length);
}

From source file:org.rhwlab.dispim.datasource.MicroClusterDataSource.java

public RealVector getDataMean() {
    ArrayRealVector ret = new ArrayRealVector(getD());
    for (int k = 0; k < micros.length; ++k) {
        ret = ret.add(micros[k].asRealVector());
    }/* w ww . j a v a2s.c  om*/
    return ret.mapDivide(micros.length);
}

From source file:org.rhwlab.variationalbayesian.MicroClusterDataSource.java

public RealVector getDataMean() {
    ArrayRealVector ret = new ArrayRealVector(getD());
    for (int k = 0; k < centers.length; ++k) {
        ret = ret.add(centers[k]);
    }/*from  w ww .j a  va2 s.c  om*/
    return ret.mapDivide(centers.length);
}