当前位置: 首页 > news >正文

插值算法-代码实现

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;
}

http://www.lryc.cn/news/339101.html

相关文章:

  • 113.PyQt5_QtPrintSupport_打印操作
  • 在vue中使用bing map 的小demo
  • 基于uni-app的埋点sdk设计
  • Python学习笔记(三)
  • Python办公自动化之Excel做表自动化:全网最全,看这一篇就够了!
  • 【学习笔记】R语言入门与数据分析1
  • MyBatis-Spring整合
  • 资深亚马逊运营实战技巧:跨境电商6大选品法
  • bugku-web-需要管理员
  • STM32之FreeRTOS移植
  • SpringBoot实用开发(十四)-- 消息(Message)的简单认识
  • 【Spring Boot 源码学习】SpringApplication 的 run 方法核心流程介绍
  • 如何保证消息不丢失?——使用rabbitmq的死信队列!
  • html、css、京东移动端静态页面,资源免费分享,可作为参考,提供InsCode在线运行演示
  • 头歌-机器学习 第13次实验 特征工程——共享单车之租赁需求预估
  • Unity 2D让相机跟随角色移动
  • 【面试题】s += 1 和 s = s + 1的区别
  • ARM的学习
  • Restful API接口规范(以Django为例)
  • AI助力,程序员压力倍增?
  • LoRA微调
  • 45.基于SpringBoot + Vue实现的前后端分离-驾校预约学习系统(项目 + 论文)
  • 系统思考—时间滞延
  • SSM项目转Springboot项目
  • VUE3.0对比VUE2.0
  • 车内AR互动娱乐解决方案,打造沉浸式智能座舱体验
  • OR36 链表的回文结构
  • 【译】微调与人工引导: 语言模型调整中的 SFT 和 RLHF
  • kylin java.io.IOException: error=13, Permission denied
  • 前端面试01总结