3 ms·
Ask HN: Is it possible to extract the formula for a spline function in Java
Is there anyway to describe the PolynomialSplineFunction[1], or any other spline function in java, by a single formula [y = f(x)]?
[1] http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.html
- logn 12y agoHere's code I have for a quadratic spline. It loops through each x-axis value and stores y-values in an array. There are three inputs assumed to be given, points: (x0,y0) (x1,y1) (x2,y2). I license this snippet as Public Domain. double a = (x0 * (y1 - y2) + y0 * (x2 - x1) + (x1 * y2) - (x2 * y1)) / ((x1 - x0) * (x2 - x0) * (x2 - x1)); double b = ((y2 - y0) / (x2 - x0)) - (x2 + x0) * a; double c = y0 - (a * (x0 * x0)) - b * x0; for (int xCount = (int) Math.rint(x0); xCount < (int) Math.rint(x2); xCount++) { wave[xCount] = (a * (xCount * xCount) + b * xCount + c); }