Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java曲线拟合库_Java_Curve Fitting - Fatal编程技术网

Java曲线拟合库

Java曲线拟合库,java,curve-fitting,Java,Curve Fitting,我希望找到一个简单的库,可以获取一系列的二维点,然后返回一系列更大的点,对曲线进行建模。基本上,我想获得曲线拟合的效果,如JFreeChart中的示例: JFreeChart的问题在于代码没有提供这种类型的api。我甚至查看了源代码,并且算法与实际图形紧密耦合。我从来没有这样做过,但快速的谷歌搜索显示贝塞尔曲线是在 然后,您可以从这条曲线中获取PathIterator(),根据文档所述,使用它可以得到“形状边界的坐标”,我想这就是您要寻找的 我们从Groovy中调用alpha(x)、beta(

我希望找到一个简单的库,可以获取一系列的二维点,然后返回一系列更大的点,对曲线进行建模。基本上,我想获得曲线拟合的效果,如JFreeChart中的示例:


JFreeChart的问题在于代码没有提供这种类型的api。我甚至查看了源代码,并且算法与实际图形紧密耦合。

我从来没有这样做过,但快速的谷歌搜索显示贝塞尔曲线是在

然后,您可以从这条曲线中获取PathIterator(),根据文档所述,使用它可以得到“形状边界的坐标”,我想这就是您要寻找的

我们从Groovy中调用alpha(x)、beta(x)插值函数的示例:

package example.com

import org.apache.commons.math3.analysis.interpolation.SplineInterpolator
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction

import statec.Extrapolate.Value;

class Interpolate {

    enum Value {
        ALPHA, BETA
    }

    def static xValues     = [
        -284086,
        -94784,
        31446,
        354837,
        667782,
        982191
    ]
    def static alphaValues = [
        71641,
        78245,
        80871,
        94045,
        105780,
        119616
    ]
    def static betaValues = [
        95552,
        103413,
        108667,
        128456,
        144686,
        171953
    ]

    static def getValueByName(Value value, int i) {
        def res
        switch (value) {
            case Value.ALPHA:
                res = alphaValues[i]
                break
            case Value.BETA:
                res = betaValues[i]
                break
            default:
                assert false
        }
        return res
    }

    static PolynomialSplineFunction interpolate(Value value) {
        def yValues = []
        int i = 0
        xValues.each {
            def y = getValueByName(value, i++)
            yValues << (y as Double)
        }
        SplineInterpolator spi = new SplineInterpolator()
        return spi.interpolate(xValues as double[], yValues as double[])
    }

    static void main(def argv) {
        //
        // Create a map mapping a Value instance to its interpolating function
        //
        def interpolations = [:]
        Value.values().each {
            interpolations[it] = interpolate(it)
        }
        //
        // Create an array of new x values to compute display.
        // Make sure the last "original" value is in there!
        // Note that the newxValues MUST stay within the range of the original xValues!
        //
        def newxValues = []
        for (long x = xValues[0] ; x < xValues[-1] ; x+=25000) {
            newxValues << x
        }
        newxValues << xValues[-1]
        //
        // Write interpolated values for ALPHA and BETA, adding the original values in columns 4 and 5
        //
        System.out << "X , ALPHA, BETA, X_orig, ALPHA_orig, BETA_orig" << "\n"
        int origIndex = 0
        newxValues.each { long x ->
            def alpha_ipol = interpolations[Value.ALPHA].value(x)
            def beta_ipol  = interpolations[Value.BETA].value(x)
            String out = "${x} ,  ${alpha_ipol} , ${beta_ipol}"
            if (x >= xValues[origIndex]) {
                out += ", ${xValues[origIndex]}, ${alphaValues[origIndex]}, ${betaValues[origIndex]}"
                origIndex++
            }
            System.out << out << "\n"
        }
    }
}
package example.com
导入org.apache.commons.math3.analysis.interpolator.SplineInterpolator
导入org.apache.commons.math3.analysis.polynomies.PolynomialSplineFunction
进口国c.外推价值;
类插值{
枚举值{
α,β
}
def静态xValues=[
-284086,
-94784,
31446,
354837,
667782,
982191
]
def静态字母值=[
71641,
78245,
80871,
94045,
105780,
119616
]
def静态betaValues=[
95552,
103413,
108667,
128456,
144686,
171953
]
静态def getValueByName(值,int i){
def res
开关(值){
case Value.ALPHA:
res=字母值[i]
打破
case Value.BETA:
res=贝塔瓦卢[i]
打破
违约:
断言错误
}
返回res
}
静态多项式样条函数插值(值){
def yValues=[]
int i=0
xvalue.each{
def y=getValueByName(值,i++)

Y值不,他不想这样做。他需要使用样条插值或一些启发式方法计算曲线的控制点。我建议你最好阅读Bezier曲线,Bezier曲线是建模这些曲线的方法之一。通过拥有曲线上点的坐标,他有效地得到了他想要的。问题是“暂停”:我们可以改写这个问题,用Java给出曲线拟合代码的示例代码(当然,该代码将引入一些库,因此我们可以将其视为一种推荐)。这个问题与JFreeChart无关,JFreeChart只获取点并显示它们,但不生成额外的点。实际上,我很惊讶链接和相关并没有准确显示该问题。
package example.com

import org.apache.commons.math3.analysis.polynomials.PolynomialFunction
import org.apache.commons.math3.fitting.PolynomialFitter
import org.apache.commons.math3.fitting.WeightedObservedPoint
import org.apache.commons.math3.optim.SimpleVectorValueChecker
import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer

class Extrapolate {

    enum Value {
        ALPHA, BETA
    }

    def static xValues     = [
        -284086,
        -94784,
        31446,
        354837,
        667782,
        982191
    ]
    def static alphaValues = [
        71641,
        78245,
        80871,
        94045,
        105780,
        119616
    ]
    def static betaValues = [
        95552,
        103413,
        108667,
        128456,
        144686,
        171953
    ]

    static def getValueByName(Value value, int i) {
        def res
        switch (value) {
            case Value.ALPHA:
                res = alphaValues[i]
                break
            case Value.BETA:
                res = betaValues[i]
                break
            default:
                assert false
        }
        return res
    }

    static PolynomialFunction extrapolate(Value value) {
        //
        // how to check that we converged
        //
        def checker
        A: {
            double relativeThreshold = 0.01
            double absoluteThreshold = 10
            int maxIter = 1000
            checker = new SimpleVectorValueChecker(relativeThreshold, absoluteThreshold, maxIter)
        }
        //
        // how to fit
        //
        def fitter
        B: {
            def useLUdecomposition = true
            def optimizer = new GaussNewtonOptimizer(useLUdecomposition, checker)
            fitter = new PolynomialFitter(optimizer)
            int i = 0
            xValues.each {
                def weight = 1.0
                def y = getValueByName(value, i++)
                fitter.addObservedPoint(new WeightedObservedPoint(weight, it, y))
            }
        }
        //
        // fit using a 2-degree polynomial; guess at a linear function at first
        // "a0 + (a1 * x) + (a2 * x²)"; a linear guess mean a2 == 0
        //
        def params
        C: {
            def mStart = getValueByName(value,0)
            def mEnd   = getValueByName(value,-1)
            def xStart = xValues[0]
            def xEnd   = xValues[-1]
            def a2 = 0
            def a1 = (mEnd - mStart) / (xEnd - xStart) // slope
            def a0 = mStart - (xStart * a1) // 0-intersection
            def guess = [a0 , a1 , a2]
            params = fitter.fit(guess as double[])
        }
        //
        // make polynomial
        //
        return new PolynomialFunction(params)
    }

    static void main(def argv) {
        //
        // Create a map mapping a Value instance to its interpolating function
        //
        def extrapolations = [:]
        Value.values().each {
            extrapolations[it] = extrapolate(it)
        }
        //
        // New x, this times reaching out past the range of the original xValues
        //
        def newxValues = []
        for (long x = xValues[0] - 400000L ; x < xValues[-1] + 400000L ; x += 10000) {
            newxValues << x
        }
        //
        // Write the extrapolated series ALPHA and BETA, adding the original values in columns 4 and 5
        //
        System.out << "X , ALPHA, BETA, X_orig, ALPHA_orig, BETA_orig" << "\n"
        int origIndex = 0
        newxValues.each { long x ->
            def alpha_xpol = extrapolations[Value.ALPHA].value(x)
            def beta_xpol  = extrapolations[Value.BETA].value(x)
            String out = "${x} ,  ${alpha_xpol} , ${beta_xpol}"
            if (origIndex < xValues.size() && x >= xValues[origIndex]) {
                out += ", ${xValues[origIndex]}, ${alphaValues[origIndex]}, ${betaValues[origIndex]}"
                origIndex++
            }
            System.out << out << "\n"
        }
    }
}