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

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

Introduction

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

Prototype

public double[] toArray() 

Source Link

Document

Convert the vector to an array of double s.

Usage

From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheOptimizer.java

/**
 * create a multivariateJacobianFunction from MVF and MMF (using builder?)
 * /*w  w w . j a va  2s  .c om*/
 */
private void internalLeastSquaresOptimize() {
    LeastSquaresOptimizer opt = createLeastSquaresOptimizer();

    try {

        LeastSquaresBuilder builder = new LeastSquaresBuilder().model(createJacobianFunction())
                .target(data.getData()).start(getParameterValues()).lazyEvaluation(false)
                .maxEvaluations(MAX_EVAL).maxIterations(MAX_ITER);

        builder.checker(new EvaluationRmsChecker(REL_TOL, ABS_TOL));

        if (weight != null) {
            builder.weight(MatrixUtils.createRealDiagonalMatrix(weight.getData()));
        }

        // TODO add checker, validator
        LeastSquaresProblem problem = builder.build();

        Optimum result = opt.optimize(problem);

        RealVector res = result.getPoint();
        setParameterValues(
                res instanceof ArrayRealVector ? ((ArrayRealVector) res).getDataRef() : res.toArray());
        try {
            RealVector err = result.getSigma(1e-14);

            //            sqrt(S / (n - m) * C[i][i]);
            double c = result.getCost();
            int n = data.getSize();
            int m = getParameterValues().length;

            double[] s = err instanceof ArrayRealVector ? ((ArrayRealVector) err).getDataRef() : err.toArray();

            errors = new double[s.length];

            for (int i = 0; i < errors.length; i++)
                errors[i] = Math.sqrt(((c * c) / ((n - m)) * (s[i] * s[i])));

        } catch (SingularMatrixException e) {
            logger.warn("Could not find errors as covariance matrix was singular");
        }

        logger.trace("Residual: {} from {}", result.getRMS(), Math.sqrt(calculateResidual()));
    } catch (Exception e) {
        logger.error("Problem with least squares optimizer", e);
        throw new IllegalArgumentException("Problem with least squares optimizer");
    }
}

From source file:USVProsjekt.ThrustAllocator.java

public double[] calculateOutput(double[] tau) throws Exception {
    //        long time1 = System.currentTimeMillis();
    // Sett opp p-vektoren med nskede verdier for tau (kraftvektor)
    p.setEntry(0, tau[0]);//from www  . j a  v  a  2  s  . c o  m
    p.setEntry(1, tau[1]);
    p.setEntry(2, tau[2]);

    // Oppsett for ulikheten A2*z <= C2*p
    RealVector v2 = C2.operate(p);

    for (int i = 0; i < 8; i++) {
        // Hver ulikhet settes opp som et LinearMultivariateRealFunction
        inequalities[i] = new LinearMultivariateRealFunction(A2.getRow(i), -v2.toArray()[i]);
    }
    // Optimaliseringsproblemet
    OptimizationRequest or = new OptimizationRequest();
    // Sett objektivfunksjonen
    or.setF0(objectiveFunction);

    // Sett ulikheten
    or.setFi(inequalities);

    // Sett likheten
    or.setA(A1);
    or.setB(p.getSubVector(0, 3).toArray());

    // Sett toleransen p resultatet. Lavere tall = strre nyaktighet
    or.setTolerance(1.E-1);

    // Optimalisering

    opt.setOptimizationRequest(or);
    int returnCode = opt.optimize();

    if (returnCode == OptimizationResponse.FAILED) {
        System.out.println("Optimization FAIL");
    }
    //        long time2 = System.currentTimeMillis()-time1;
    //System.out.println("Tidsbruk = " + time2);
    return opt.getOptimizationResponse().getSolution();

}