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

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

Introduction

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

Prototype

public double[] fit(Collection<WeightedObservedPoint> points) 

Source Link

Document

Fits a curve.

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.");
    }// w w  w.  j  a va  2  s. com

    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;
}