Example usage for org.apache.commons.math3.fitting HarmonicCurveFitter create

List of usage examples for org.apache.commons.math3.fitting HarmonicCurveFitter create

Introduction

In this page you can find the example usage for org.apache.commons.math3.fitting HarmonicCurveFitter create.

Prototype

public static HarmonicCurveFitter create() 

Source Link

Document

Creates a default curve fitter.

Usage

From source file:org.apache.solr.client.solrj.io.eval.HarmonicFitEvaluator.java

@Override
public Object doWork(Object... objects) throws IOException {

    if (objects.length > 3) {
        throw new IOException("harmonicFit function takes a maximum of 2 arguments.");
    }/*from w w  w .  j av a  2  s .co m*/

    Object first = objects[0];

    double[] x = null;
    double[] y = null;

    if (objects.length == 1) {
        //Only the y values passed

        y = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        x = new double[y.length];
        for (int i = 0; i < y.length; i++) {
            x[i] = i;
        }

    } else if (objects.length == 2) {
        // x and y passed
        Object second = objects[1];
        x = ((List) first).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();
        y = ((List) second).stream().mapToDouble(value -> ((BigDecimal) value).doubleValue()).toArray();

    }

    HarmonicCurveFitter curveFitter = HarmonicCurveFitter.create();

    WeightedObservedPoints points = new WeightedObservedPoints();
    for (int i = 0; i < x.length; i++) {
        points.add(x[i], y[i]);
    }

    double[] coef = curveFitter.fit(points.toList());
    HarmonicOscillator pf = new HarmonicOscillator(coef[0], coef[1], coef[2]);

    List list = new ArrayList();
    for (double xvalue : x) {
        double yvalue = pf.value(xvalue);
        list.add(yvalue);
    }

    return list;
}