Java Code Examples for org.apache.commons.math3.analysis.function.HarmonicOscillator#value()

The following examples show how to use org.apache.commons.math3.analysis.function.HarmonicOscillator#value() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OscillateEvaluator.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length != 3) {
    throw new IOException("The oscillate function takes 3 arguments.");
  }

  double amp = ((Number)objects[0]).doubleValue();
  double om = ((Number)objects[1]).doubleValue();
  double phase = ((Number)objects[2]).doubleValue();


  HarmonicOscillator pf = new HarmonicOscillator(amp, om, phase);
  double[] x = new double[128];

  @SuppressWarnings({"rawtypes"})
  List list = new ArrayList();
  for(int i=0; i<128; i++) {
    double yvalue= pf.value(i);
    list.add(yvalue);
    x[i] = i;
  }

  VectorFunction func = new VectorFunction(pf, list);
  func.addToContext("x", x);
  return func;
}
 
Example 2
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 3
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 4
Source File: HarmonicCurveFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252L);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    final HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    // Build a regularly spaced array of measurements.
    final int size = 100;
    final double[] xTab = new double[size];
    final double[] yTab = new double[size];
    for (int i = 0; i < size; i++) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; i++) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // Pass it to the fitter.
    final WeightedObservedPoints points = new WeightedObservedPoints();        
    for (int i = 0; i < size; ++i) {
        points.add(1, xTab[i], yTab[i]);
    }

    final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
    final double[] fitted = fitter.fit(points.toList());
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 5
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 6
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 7
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 8
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 9
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 10
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 11
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 12
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 13
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 14
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 15
Source File: HarmonicCurveFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252L);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    final HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    // Build a regularly spaced array of measurements.
    final int size = 100;
    final double[] xTab = new double[size];
    final double[] yTab = new double[size];
    for (int i = 0; i < size; i++) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; i++) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // Pass it to the fitter.
    final WeightedObservedPoints points = new WeightedObservedPoints();        
    for (int i = 0; i < size; ++i) {
        points.add(1, xTab[i], yTab[i]);
    }

    final HarmonicCurveFitter fitter = HarmonicCurveFitter.create();
    final double[] fitted = fitter.fit(points.toList());
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 16
Source File: HarmonicFitterTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testUnsorted() {
    Random randomizer = new Random(64925784252l);
    final double a = 0.2;
    final double w = 3.4;
    final double p = 4.1;
    HarmonicOscillator f = new HarmonicOscillator(a, w, p);

    HarmonicFitter fitter =
        new HarmonicFitter(new LevenbergMarquardtOptimizer());

    // build a regularly spaced array of measurements
    int size = 100;
    double[] xTab = new double[size];
    double[] yTab = new double[size];
    for (int i = 0; i < size; ++i) {
        xTab[i] = 0.1 * i;
        yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
    }

    // shake it
    for (int i = 0; i < size; ++i) {
        int i1 = randomizer.nextInt(size);
        int i2 = randomizer.nextInt(size);
        double xTmp = xTab[i1];
        double yTmp = yTab[i1];
        xTab[i1] = xTab[i2];
        yTab[i1] = yTab[i2];
        xTab[i2] = xTmp;
        yTab[i2] = yTmp;
    }

    // pass it to the fitter
    for (int i = 0; i < size; ++i) {
        fitter.addObservedPoint(1, xTab[i], yTab[i]);
    }

    final double[] fitted = fitter.fit();
    Assert.assertEquals(a, fitted[0], 7.6e-4);
    Assert.assertEquals(w, fitted[1], 3.5e-3);
    Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
}
 
Example 17
Source File: HarmonicFitEvaluator.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public Object doWork(Object... objects) throws IOException{

  if(objects.length > 3) {
    throw new IOException("harmonicFit function takes a maximum of 2 arguments.");
  }

  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 -> ((Number) 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 -> ((Number) value).doubleValue()).toArray();
      y = ((List) second).stream().mapToDouble(value -> ((Number) 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[] guess = new HarmonicCurveFitter.ParameterGuesser(points.toList()).guess();
  curveFitter = curveFitter.withStartPoint(guess);

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

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

  @SuppressWarnings({"unchecked"})
  VectorFunction vectorFunction =  new VectorFunction(pf, list);
  vectorFunction.addToContext("amplitude", coef[0]);
  vectorFunction.addToContext("angularFrequency", coef[1]);
  vectorFunction.addToContext("phase", coef[2]);

  return vectorFunction;

}