Example usage for org.apache.commons.math3.analysis.polynomials PolynomialFunction PolynomialFunction

List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunction PolynomialFunction

Introduction

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

Prototype

public PolynomialFunction(double c[]) throws NullArgumentException, NoDataException 

Source Link

Document

Construct a polynomial with the given coefficients.

Usage

From source file:net.liuxuan.temp.mathtest.java

public static void main(String[] args) {

    final CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer());
    fitter.addObservedPoint(-1.00, 2.021170021833143);
    fitter.addObservedPoint(-0.99, 2.221135431136975);
    fitter.addObservedPoint(-0.98, 2.09985277659314);
    fitter.addObservedPoint(-0.97, 2.0211192647627025);
    // ... Lots of lines omitted ...
    fitter.addObservedPoint(0.99, -2.4345814727089854);

    // The degree of the polynomial is deduced from the length of the array containing
    // the initial guess for the coefficients of the polynomial.
    final double[] init = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2

    // Compute optimal coefficients.
    final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init);

    // Construct the polynomial that best fits the data.
    final PolynomialFunction fitted = new PolynomialFunction(best);
    System.out.println(fitted.value(-0.995));
    ;//from   w  ww  .j  a va2s. c om
    System.out.println(fitted.value(0.995));
    ;
    System.out.println(fitted.toString());
    ;
    System.out.println("=============================================================");
    PolynomialCurveFitter pcf = PolynomialCurveFitter.create(3);
    WeightedObservedPoints s;
    List<WeightedObservedPoint> points = new ArrayList<WeightedObservedPoint>();
    points.add(new WeightedObservedPoint(1, -1.00, 2.021170021833143));
    points.add(new WeightedObservedPoint(1, -0.99, 2.221135431136975));
    points.add(new WeightedObservedPoint(1, -0.98, 2.09985277659314));
    points.add(new WeightedObservedPoint(1, -0.97, 2.0211192647627025));
    points.add(new WeightedObservedPoint(1, 0.99, 2.4345814727089854));
    double a[] = pcf.fit(points);
    for (int i = 0; i < a.length; i++) {
        double d = a[i];
        System.out.println(d);
    }
    System.out.println(compute(a, -0.995));
    System.out.println(compute(a, 0.99));
    System.out.println(compute(a, 0.995));

}

From source file:heizung.Heizkurve.java

public static PolynomialFunction getPolynomialFit(List<Point> pList) {
    PolynomialFunction result = null;//from w  w  w.j a v a 2 s .co  m
    if (pList == null) {
        return result;
    }
    try {

        final WeightedObservedPoints obs = new WeightedObservedPoints();
        for (Point p : pList) {
            obs.add(p.getX(), p.getY());
        }

        final ParametricUnivariateFunction function = new PolynomialFunction.Parametric();
        // Start fit from initial guesses that are far from the optimal
        // values.
        // final SimpleCurveFitter fitter =
        // SimpleCurveFitter.create(function,
        // new double[] { -1e20, 3e15, -5e25 });
        final SimpleCurveFitter fitter = SimpleCurveFitter.create(function,
                new double[] { -2e20, 1e15, -1e25 });
        // 2e2 ist 2*10^2 = 2*100
        final double[] best = fitter.fit(obs.toList());
        // System.out.println("Parameters: " + best.length);
        // funktion ausgeben
        result = new PolynomialFunction(best);
    } catch (Exception e) {
        // e.printStackTrace();
        System.out.println("PolynomialFunction: " + e);
    }
    return result;
}

From source file:dom.rootlocus.utils.Utils.java

public PolynomialFunction getEC(PolynomialFunction pNum, PolynomialFunction pDen, PolynomialFunction sNum,
        PolynomialFunction sDen, double k) {
    double[] gain = { k };
    return pNum.multiply(sNum).multiply(new PolynomialFunction(gain)).add(pDen.multiply(sDen));
}

From source file:dom.rootlocus.utils.Utils.java

public PolynomialFunction getEC(PolynomialFunction num, PolynomialFunction den, double k) {
    double[] gain = { k };
    return num.multiply(new PolynomialFunction(gain)).add(den);
}

From source file:dom.rootlocus.utils.Utils.java

public PolynomialFunction toPolynomialFunction(String[] coef) {

    double[] cf = new double[coef.length];
    for (int i = 0; i < coef.length; i++) {
        cf[i] = Double.parseDouble(coef[i]);
    }/*ww w .  j  ava  2 s  . com*/
    return new PolynomialFunction(cf);
}

From source file:jurbano.melodyshape.comparison.bspline.BSplinePitchNGramComparer.java

/**
 * {@inheritDoc}/*from   w  w  w .  ja v  a2s  .  c om*/
 * 
 * @return 0 if both {@link NGram}s are equivalent or the area between the
 *         first derivatives of their corresponding Uniform B-Splines.
 */
@Override
public double compare(NGram g1, NGram g2) {
    if (g1 == null)
        g1 = g2.getNullSpan();
    if (g2 == null)
        g2 = g1.getNullSpan();
    if (g1.size() < 2 || g1.size() > UniformBSpline.BASIS_FUNCTIONS.length)
        throw new IllegalArgumentException(this.getName() + " only supports n-grams with 2 to "
                + UniformBSpline.BASIS_FUNCTIONS.length + " notes.");
    if (this.getNGramId(g1).equals(this.getNGramId(g2)))
        return 0;

    PolynomialFunction p1p = new PolynomialFunction(new double[] { 0 });
    PolynomialFunction p2p = new PolynomialFunction(new double[] { 0 });
    for (int i = 1; i < g1.size(); i++) {
        PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i - 1];
        p1p = p1p.add(basis.multiply(new PolynomialFunction(
                new double[] { g1.get(g1.size() - i).getPitch() - g1.get(0).getPitch() })));
        p2p = p2p.add(basis.multiply(new PolynomialFunction(
                new double[] { g2.get(g2.size() - i).getPitch() - g2.get(0).getPitch() })));
    }
    PolynomialFunction pp = p1p.polynomialDerivative().subtract(p2p.polynomialDerivative());

    Laguerre laguerre = new Laguerre();
    ArrayList<Double> realRoots = laguerre.findRoots(pp);
    return -laguerre.computeAreaBetweenDerivatives(p1p, p2p, realRoots);
}

From source file:jurbano.melodyshape.comparison.bspline.BSplineTimeNGramComparer.java

/**
 * {@inheritDoc}//from  w ww  . j av a 2  s .  c o m
 * 
 * @return 0 if both {@link NGram}s are equivalent or the area between the
 *         first derivatives of their corresponding Uniform B-Splines.
 */
@Override
public double compare(NGram g1, NGram g2) {
    if (g1 == null)
        g1 = g2.getNullSpan();
    if (g2 == null)
        g2 = g1.getNullSpan();
    if (g1.size() < 2 || g1.size() > UniformBSpline.BASIS_FUNCTIONS.length)
        throw new IllegalArgumentException(this.getName() + " only supports n-grams with 2 to "
                + UniformBSpline.BASIS_FUNCTIONS.length + " notes.");
    if (this.getNGramId(g1).equals(this.getNGramId(g2)))
        return 0;

    PolynomialFunction p1t = new PolynomialFunction(new double[] { 0 });
    PolynomialFunction p2t = new PolynomialFunction(new double[] { 0 });
    for (int i = 0; i < g1.size(); i++) {
        PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i];
        p1t = p1t.add(basis.multiply(new PolynomialFunction(
                new double[] { g1.get(g1.size() - i - 1).getDuration() / (double) g1.get(0).getDuration() })));
        p2t = p2t.add(basis.multiply(new PolynomialFunction(
                new double[] { g2.get(g2.size() - i - 1).getDuration() / (double) g2.get(0).getDuration() })));
    }
    PolynomialFunction pt = p1t.polynomialDerivative().subtract(p2t.polynomialDerivative());

    Laguerre laguerre = new Laguerre();
    ArrayList<Double> realRoots = laguerre.findRoots(pt);
    return -laguerre.computeAreaBetweenDerivatives(p1t, p2t, realRoots);
}

From source file:curveavg.SolverTest.java

@Test
public void testQuinticZero_NewtonRaphsonFailureCase() {

    // Solve using NewtonRaphson and count the number of results
    float c[] = { -38.764412f, 76.10117f, -56.993206f, 28.603401f, -10.2824955f, 1.0f };
    MedialAxisTransform.SolverResult res = MedialAxisTransform.solveQuinticNewtonRaphson(c[0], c[1], c[2], c[3],
            c[4], c[5]);/*from   w  w w  . jav a  2  s  . com*/
    int rootsNR = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res.im[i]) < 1e-3)
            rootsNR++;
    }
    System.out.println("# NR roots: " + rootsNR);

    // Solve using NewtonRaphson and count the number of results
    MedialAxisTransform.SolverResult res2 = MedialAxisTransform.solveQuinticLaguerre(c[0], c[1], c[2], c[3],
            c[4], c[5]);
    int rootsL = 0;
    for (int i = 0; i < res.im.length; i++) {
        if (Math.abs(res2.im[i]) < 1e-3)
            rootsL++;
    }
    System.out.println("# L roots: " + rootsL);

    // Solve using Laguerre
    double dt = 0.01;
    double coefficients[] = new double[6];
    for (int i = 0; i < 6; i++)
        coefficients[i] = c[5 - i];
    PolynomialFunction f = new PolynomialFunction(coefficients);
    LaguerreSolver solver = new LaguerreSolver();
    for (double t = 0; t <= (1.0 - dt); t += dt) {

        // Check if there is a sign-crossing between the two times
        double t2 = t + dt;
        double f1 = f.value(t);
        double f2 = f.value(t2);
        if (Math.signum(f1) == Math.signum(f2))
            continue;

        // Compute using Laguerre
        double result = solver.solve(100, f, t, t2);
        System.out.println("Result: " + result);
    }

}

From source file:jurbano.melodyshape.comparison.bspline.BSplineShapeNGramComparer.java

/**
 * {@inheritDoc}/*from   ww w.  jav a 2  s . co  m*/
 */
@Override
public double compare(NGram g1, NGram g2) {
    if (g1 == null)
        g1 = g2.getNullSpan();
    if (g2 == null)
        g2 = g1.getNullSpan();
    if (g1.size() != 3)
        throw new IllegalArgumentException(this.getName() + " only supports n-grams with 3 notes.");
    if (this.getNGramId(g1).equals(this.getNGramId(g2)))
        return 0;

    PolynomialFunction p1 = new PolynomialFunction(new double[] { 0 });
    PolynomialFunction p2 = new PolynomialFunction(new double[] { 0 });
    for (int i = 1; i < g1.size(); i++) {
        PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i - 1];
        p1 = p1.add(basis.multiply(new PolynomialFunction(
                new double[] { g1.get(g1.size() - i).getPitch() - g1.get(0).getPitch() })));
        p2 = p2.add(basis.multiply(new PolynomialFunction(
                new double[] { g2.get(g2.size() - i).getPitch() - g2.get(0).getPitch() })));
    }
    PolynomialFunction p1_ = p1.polynomialDerivative();
    PolynomialFunction p2_ = p2.polynomialDerivative();

    /*
    double p1_0 = Math.signum(p1_.value(0));
    double p1_1 = Math.signum(p1_.value(1));
    double p2_0 = Math.signum(p2_.value(0));
    double p2_1 = Math.signum(p2_.value(1));
            
    if(p1_0 == p2_0){ // same at 0
       if(p1_1 == p2_1) // same at 1
    return -this.dMin;
       else if(p1_1*p2_1<0) // opposite at 1
    return -this.dMed;
       else // some flat at 1
    return -this.dMed;
    }else if(p1_0*p2_0 < 0) { // opposite at 0
       if(p1_1 == p2_1) // same at 1
    return -this.dMed;
       else if(p1_1*p2_1<0) // opposite at 1
    return -this.dMax;
       else // some flat at 1
    return -this.dMax;
    }else { // some flat at 0
       if(p1_1 == p2_1) // same at 1
    return -this.dMed;
       else if(p1_1*p2_1<0) // opposite at 1
    return -this.dMax;
       else // some flat at 1
    return -this.dMax;
    }*/

    double p1_0 = p1_.value(0);
    double p1_1 = p1_.value(1);
    double p2_0 = p2_.value(0);
    double p2_1 = p2_.value(1);

    // TODO: this similarity function is not symmetric
    if (p1_0 <= 0 && p1_1 >= 0) { // p1 is \/
        if (p2_0 <= 0 && p2_1 >= 0) // p2 is \/
            return -this.dMin;
        else if (p2_0 >= 0 && p2_1 <= 0) // p2 is /\
            return -this.dMax;
        else
            return -this.dMed;
    }
    if (p1_0 >= 0 && p1_1 <= 0) { // p1 is /\
        if (p2_0 >= 0 && p2_1 <= 0) // p2 is /\
            return -this.dMin;
        else if (p2_0 <= 0 && p2_1 >= 0) // p2 is \/
            return -this.dMax;
        else
            return -this.dMed;
    }
    if (p1_0 >= 0 && p1_1 >= 0) { // p1 is /
        if (p2_0 >= 0 && p2_1 >= 0) // p2 is /
            return -this.dMin;
        else if (p2_0 <= 0 && p2_1 <= 0) // p2 is \
            return -this.dMax;
        else
            return -this.dMed;
    }
    if (p1_0 <= 0 && p1_1 <= 0) { // p1 is \
        if (p2_0 <= 0 && p2_1 <= 0) // p2 is \
            return -this.dMin;
        else if (p2_0 >= 0 && p2_1 >= 0) // p2 is /
            return -this.dMax;
        else
            return -this.dMed;
    }

    return -this.dMin;
}

From source file:au.gov.ga.conn4d.utils.SplineInterpolator.java

/**
 * Computes an interpolating function for the data set.
 * /*  w  ww . j ava  2s . co m*/
 * @param x
 *            the arguments for the interpolation points
 * @param y
 *            the values for the interpolation points
 * @return a function which interpolates the data set
 * @throws DimensionMismatchException
 *             if {@code x} and {@code y} have different sizes.
 * @throws NonMonotonicSequenceException
 *             if {@code x} is not sorted in strict increasing order.
 * @throws NumberIsTooSmallException
 *             if the size of {@code x} is smaller than 3.
 */
public PolynomialSplineFunction interpolate(double x[], double y[])
        throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException {
    if (x.length != y.length) {
        throw new DimensionMismatchException(x.length, y.length);
    }

    if (x.length < 3) {
        throw new NumberIsTooSmallException(LocalizedFormats.NUMBER_OF_POINTS, x.length, 3, true);
    }

    // Number of intervals. The number of data points is n + 1.
    final int n = x.length - 1;

    MathArrays.checkOrder(x);

    // Differences between knot points
    final double h[] = new double[n];
    for (int i = 0; i < n; i++) {
        h[i] = x[i + 1] - x[i];
    }

    final double mu[] = new double[n];
    final double z[] = new double[n + 1];
    mu[0] = 0d;
    z[0] = 0d;
    double g = 0;
    for (int i = 1; i < n; i++) {
        g = 2d * (x[i + 1] - x[i - 1]) - h[i - 1] * mu[i - 1];
        mu[i] = h[i] / g;
        z[i] = (3d * (y[i + 1] * h[i - 1] - y[i] * (x[i + 1] - x[i - 1]) + y[i - 1] * h[i]) / (h[i - 1] * h[i])
                - h[i - 1] * z[i - 1]) / g;
    }

    // cubic spline coefficients -- b is linear, c quadratic, d is cubic
    // (original y's are constants)
    final double b[] = new double[n];
    final double c[] = new double[n + 1];
    final double d[] = new double[n];

    z[n] = 0d;
    c[n] = 0d;

    for (int j = n - 1; j >= 0; j--) {
        c[j] = z[j] - mu[j] * c[j + 1];
        b[j] = (y[j + 1] - y[j]) / h[j] - h[j] * (c[j + 1] + 2d * c[j]) / 3d;
        d[j] = (c[j + 1] - c[j]) / (3d * h[j]);
    }

    final PolynomialFunction polynomials[] = new PolynomialFunction[n];
    final double coefficients[] = new double[4];
    for (int i = 0; i < n; i++) {
        coefficients[0] = y[i];
        coefficients[1] = b[i];
        coefficients[2] = c[i];
        coefficients[3] = d[i];
        polynomials[i] = new PolynomialFunction(coefficients);
    }

    return new PolynomialSplineFunction(x, polynomials);
}