Example usage for org.apache.commons.math3.analysis.function HarmonicOscillator value

List of usage examples for org.apache.commons.math3.analysis.function HarmonicOscillator value

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.function HarmonicOscillator value.

Prototype

public DerivativeStructure value(final DerivativeStructure t) 

Source Link

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 ww.  j  ava  2s  .c  om

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