插值算法-代码实现

1、

import java.util.HashMap;
import java.util.Map;

public class Interpolation {
    public static void main(String[] args) {
        // 定义给定的 XML 字段值
        Map<String, double[]> xmlValues = new HashMap<>();
        xmlValues.put("faceSize", new double[]{1000, 1500});
        xmlValues.put("section1", new double[]{0.2, 0.3, 0.4});
        xmlValues.put("section2", new double[]{0.25, 0.35, 0.5});

        // 调用插值函数
        double interpolatedSection = interpolate(xmlValues, 1250);

        // 打印插值结果
        System.out.println("Interpolated section: " + interpolatedSection);
    }

    public static double interpolate(Map<String, double[]> xmlValues, double targetFaceSize) {
        // 获取 faceSize 数组
        double[] faceSizes = xmlValues.get("faceSize");
        // 获取 section1、section2 数组
        double[] section1 = xmlValues.get("section1");
        double[] section2 = xmlValues.get("section2");

        // 找到目标 faceSize 对应的索引
        int index1 = 0;
        int index2 = 1;

        // 执行线性插值
        double section1Interpolated = interpolateLinear(faceSizes[index1], section1[index1], faceSizes[index2], section1[index2], targetFaceSize);
        double section2Interpolated = interpolateLinear(faceSizes[index1], section2[index1], faceSizes[index2], section2[index2], targetFaceSize);

        // 计算 section 的平均值
        return (section1Interpolated + section2Interpolated) / 2;
    }

    // 线性插值函数
    public static double interpolateLinear(double x1, double y1, double x2, double y2, double x) {
        return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
    }
}

import java.util.HashMap;
import java.util.Map;

public class Interpolation {
    public static void main(String[] args) {
        // 定义给定的 XML 字段值
        Map<String, double[]> xmlValues = new HashMap<>();
        xmlValues.put("faceSize", new double[]{1000, 1500});
        xmlValues.put("section1", new double[]{0.2, 0.3, 0.4});
        xmlValues.put("section2", new double[]{0.25, 0.35, 0.5});
        xmlValues.put("weight", new double[]{0, 0.5, 1});

        // 调用插值函数
        double[] interpolatedValues = interpolate(xmlValues, 1250);

        // 打印插值结果
        System.out.println("Interpolated section: " + interpolatedValues[0]);
        System.out.println("Interpolated weight: " + interpolatedValues[1]);
    }

    public static double[] interpolate(Map<String, double[]> xmlValues, double targetFaceSize) {
        // 获取 faceSize 数组
        double[] faceSizes = xmlValues.get("faceSize");
        // 获取 section1、section2 和 weight 数组
        double[] section1 = xmlValues.get("section1");
        double[] section2 = xmlValues.get("section2");
        double[] weight = xmlValues.get("weight");

        // 找到目标 faceSize 对应的两个索引
        int index1 = -1;
        int index2 = -1;
        for (int i = 0; i < faceSizes.length - 1; i++) {
            if (targetFaceSize >= faceSizes[i] && targetFaceSize <= faceSizes[i + 1]) {
                index1 = i;
                index2 = i + 1;
                break;
            }
        }

        // 执行线性插值
        double section1Interpolated = interpolateLinear(faceSizes[index1], section1[index1], faceSizes[index2], section1[index2], targetFaceSize);
        double section2Interpolated = interpolateLinear(faceSizes[index1], section2[index1], faceSizes[index2], section2[index2], targetFaceSize);
        double weightInterpolated = interpolateLinear(faceSizes[index1], weight[index1], faceSizes[index2], weight[index2], targetFaceSize);

        return new double[]{(section1Interpolated + section2Interpolated) / 2, weightInterpolated};
    }

    // 线性插值函数
    public static double interpolateLinear(double x1, double y1, double x2, double y2, double x) {
        return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
    }
}

2、

#include <iostream>
#include <map>

// 线性插值函数
double interpolateLinear(double x1, double y1, double x2, double y2, double x) {
    return y1 + (y2 - y1) * (x - x1) / (x2 - x1);
}

// 插值函数
double interpolate(std::map<std::string, double*> xmlValues, double targetFaceSize) {
    // 获取 faceSize 数组
    double* faceSizes = xmlValues["faceSize"];
    // 获取 section1、section2 数组
    double* section1 = xmlValues["section1"];
    double* section2 = xmlValues["section2"];

    // 找到目标 faceSize 对应的索引
    int index1 = 0;
    int index2 = 1;

    // 执行线性插值
    double section1Interpolated = interpolateLinear(faceSizes[index1], section1[index1], faceSizes[index2], section1[index2], targetFaceSize);
    double section2Interpolated = interpolateLinear(faceSizes[index1], section2[index1], faceSizes[index2], section2[index2], targetFaceSize);

    // 计算 section 的平均值
    return (section1Interpolated + section2Interpolated) / 2;
}

int main() {
    // 定义给定的 XML 字段值
    std::map<std::string, double*> xmlValues;
    double faceSizes[] = {1000, 1500};
    double section1[] = {0.2, 0.3, 0.4};
    double section2[] = {0.25, 0.35, 0.5};
    xmlValues["faceSize"] = faceSizes;
    xmlValues["section1"] = section1;
    xmlValues["section2"] = section2;

    // 调用插值函数
    double interpolatedSection = interpolate(xmlValues, 1250);

    // 打印插值结果
    std::cout << "Interpolated section: " << interpolatedSection << std::endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值