// spline interpolation import java.util.ArrayList; import java.util.List;

class Spline { public enum BdType { FIRST_DERIV(1), SECOND_DERIV(2);

    private final int value;

    BdType(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

private List<Double> m_x, m_y;            // x,y coordinates of points
// interpolation parameters
// f(x) = a*(x-x_i)^3 + b*(x-x_i)^2 + c*(x-x_i) + y_i
private List<Double> m_a, m_b, m_c;        // spline coefficients
private double m_b0, m_c0;                     // for left extrapol
private BdType m_left, m_right;
private double m_left_value, m_right_value;
private boolean m_force_linear_extrapolation;

// set default boundary condition to be zero curvature at both ends
public Spline() {
    m_left = BdType.SECOND_DERIV;
    m_right = BdType.SECOND_DERIV;
    m_left_value = 0.0;
    m_right_value = 0.0;
    m_force_linear_extrapolation = false;
    m_x = new ArrayList<>();
    m_y = new ArrayList<>();
    m_a = new ArrayList<>();
    m_b = new ArrayList<>();
    m_c = new ArrayList<>();
}

// optional, but if called it has to come be before set_points()
public void setBoundary(BdType left, double left_value,
                        BdType right, double right_value,
                        boolean force_linear_extrapolation) {
    m_left = left;
    m_right = right;
    m_left_value = left_value;
    m_right_value = right_value;
    m_force_linear_extrapolation = force_linear_extrapolation;
}

public void setPoints(List<Double> x, List<Double> y, boolean cubic_spline) {
    m_x = x;
    m_y = y;
    if (cubic_spline) {
        calculateSplineCoefficients();
    }
}

public double getValue(double x) {
    return evaluateSpline(x);
}

public double getDerivative(int order, double x) {
    return evaluateDerivative(order, x);
}

private void calculateSplineCoefficients() {
    // implementation for calculating spline coefficients
}

private double evaluateSpline(double x) {
    // implementation for evaluating spline
    return 0.0;
}

private double evaluateDerivative(int order, double x) {
    // implementation for evaluating derivative
    return 0.0;
}

}

C++ to Java Spline Interpolation Conversion: A Detailed Guide

原文地址: https://www.cveoy.top/t/topic/fA8s 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录