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

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

Introduction

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

Prototype

public HarmonicOscillator(double amplitude, double omega, double phase) 

Source Link

Document

Harmonic oscillator function.

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   ww  w  .  java  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;
}