埃尔米特插值(hermite 插值) C++

news2024/9/23 15:25:17

埃尔米特插值 原理

在这里插入图片描述
在这里插入图片描述

#pragma once
#include <vector>
#include <functional>
/*

埃尔米特插值

*/
struct InterpolationPoint {
    double x; // 插值点的横坐标
    double y; // 插值点的纵坐标
    double derivative; // 插值点的导数值
     // 默认构造函数
    InterpolationPoint() : x(0.0), y(0.0), derivative(0.0) {}

    // 带参数的构造函数
    InterpolationPoint(double x_val, double y_val, double derivative_val) : x(x_val), y(y_val), derivative(derivative_val) {}

    // 拷贝构造函数
    InterpolationPoint(const InterpolationPoint& other) : x(other.x), y(other.y), derivative(other.derivative) {}

    // 移动构造函数
    InterpolationPoint(InterpolationPoint&& other) noexcept : x(other.x), y(other.y), derivative(other.derivative) {
        other.x = 0.0;
        other.y = 0.0;
        other.derivative = 0.0;
    }
    // Copy assignment operator
    InterpolationPoint& operator=(const InterpolationPoint& other) {
        if (this != &other) {
            x = other.x;
            y = other.y;
            derivative = other.derivative;
        }
        return *this;
    }

    // 设置插值点的值
    void set(double x_val, double y_val, double derivative_val) {
        x = x_val;
        y = y_val;
        derivative = derivative_val;
    }

    // 获取插值点的横坐标
    double get_x() const {
        return x;
    }

    // 获取插值点的纵坐标
    double get_y() const {
        return y;
    }

    // 获取插值点的导数值
    double get_derivative() const {
        return derivative;
    }
};

class HermiteInterpolator {
public:
    HermiteInterpolator(const std::vector<InterpolationPoint>& points);
    HermiteInterpolator(int width, std::vector<int> &adjPoints);
    void setPoints(const std::vector<InterpolationPoint>& points);
    double interpolate(double x) ;

private:
    // 返回连接两点的线段函数
    std::function<double(double)> getLineFunction( InterpolationPoint& p1,  InterpolationPoint& p2);

private:
    std::vector<InterpolationPoint> points_;
};
#include "pch.h"
#include "HermiteInterpolator.h"
#include <fstream>
HermiteInterpolator::HermiteInterpolator(const std::vector<InterpolationPoint>& points) 
	: points_(points)
{
}
HermiteInterpolator::HermiteInterpolator(int width, std::vector<int>& adjPoints)
{
	float step = width / adjPoints.size();
	for (int i = 0; i < adjPoints.size(); i++)
	{
		InterpolationPoint point(step*i, adjPoints[i] , 0);
		points_.push_back(point);
	}
}
void HermiteInterpolator::setPoints(const std::vector<InterpolationPoint>& points)
{
	points_ = points;
}

// 返回连接两点的线段函数
std::function<double(double)> HermiteInterpolator::getLineFunction( InterpolationPoint& p1,  InterpolationPoint& p2) {
    // 计算线段的斜率和截距
    double slope = (p2.y - p1.y) / (p2.x - p1.x);
    double intercept = p1.y - slope * p1.x;

    // 返回线段的lambda表达式
    return [slope, intercept](double x) {
        return slope * x + intercept;
    };
}
// 计算三次分段Hermite插值函数的值
double HermiteInterpolator::interpolate(double x)  {
    int y = 0;
    
    int n = points_.size();
    if (n < 3)
    {
        // 获取线段函数
        std::function<double(double)> lineFunction = getLineFunction(points_[0], points_[1]);
        y= lineFunction(x);
    }
    else
    {
        for (int i = 0; i < n - 1; i++) {
            if (x >= points_[i].x && x <= points_[i + 1].x) {
                double h = points_[i + 1].x - points_[i].x;
                double t = (x - points_[i].x) / h;// (x-x_k)/(x_{k+1} - x_k)
                double tk = (x - points_[i + 1].x) / (-h); // (x - x_{ k + 1 }) / (x_k - x_{ k + 1 }) 
                double y0 = (1 + 2 * t) * tk * tk;
                double y1 = (1 + 2 * tk) * t * t;
                double y2 = (x - points_[i].x) * tk * tk;
                double y3 = (x - points_[i + 1].x) * t * t;

                y= points_[i].y * y0 + points_[i + 1].y * y1 + points_[i].derivative * y2 + points_[i + 1].derivative * y3;
            }
        }
    }
    
    //ofstream  f;
    //f.open("D:\\work\\documentation\\HermiteInterpolator.txt", ios::app);
    //f <<x<<"," << y << endl;
    //f.close();
    return y; // 如果找不到对应的插值段,返回默认值
}

为了可视化效果可以把结果写到HermiteInterpolator.txt
画图python代码:

import matplotlib.pyplot as plt

# 打开文本文件进行读取
with open('D:\\work\\documentation\\HermiteInterpolator.txt') as f:
    data = f.readlines()

# 定义两个列表分别存储横坐标和纵坐标的数据    
x = []
y = [] 

# 遍历每一行
for i, line in enumerate(data):
    # 去除换行符
    if line:
        user_pwd_list = line.strip().split(',')
    
        # 横坐标是行号
        x.append(float(user_pwd_list[0]))
        
        # 纵坐标是数值数据
        y.append(float(user_pwd_list[1]))
    
# 创建散点图    
plt.scatter(x, y)

# 添加标题和轴标签
plt.title('Scatter Plot')  
plt.xlabel('Line')
plt.ylabel('Value')

# 显示并保存图像
#plt.savefig('plot.png')
plt.show()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1237385.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

一个测试驱动的Spring Boot应用程序开发

文章目录 系统任务用户故事搭建开发环境Web应用的框架Spring Boot 自动配置三层架构领域建模域定义与领域驱动设计领域类 业务逻辑功能随机的Challenge验证 表示层RESTSpring Boot和REST API设计API第一个控制器序列化的工作方式使用Spring Boot测试控制器 小结 这里采用面向需…

str转wstr的三种方法和从网站获取json数据到数据随机提取,返回拼接字符串和动态数组

库的设置 hv库 外部包含目录&#xff1a;…\include\libhv_new\hv; 库目录&#xff1a;…\include\libhv_new\lib\x86\Release; 附加依赖项&#xff1a;hv.lib; //Get请求 获取json数据&#xff0c;然后提取符合 条件的&#xff0c;time值大于自定义变量的值&#xff0c;然后取…

老知识复盘-SQL从提交到执行到底经历了什么 | 京东云技术团队

一、什么是SQL sql(Structured Query Language: 结构化查询语言)是高级的费过程化编程语言,允许用户在高层数据结构上工作, 是一种数据查询和程序设计语言, 也是(ANSI)的一项标准的计算机语言. but… 目前仍然存在着许多不同版本的sql语言,为了与ANSI标准相兼容, 它们必须以相…

webpack 创建typescript项目

【视频链接】尚硅谷TypeScript教程&#xff08;李立超老师TS新课&#xff09; 创建webpack 项目 IDE&#xff1a;webstorm 新建一个空的项目运行npm init初始化项目目录结构 1. 安装 webpack&#xff1a;构建工具webpack-cli&#xff1a; webpack的命令行工具typescript&am…

处理无线debug问题

无限debug的产生 条件说明 开发者工具是打开状态 js代码中有debugger js有定时处理 setInterval(() > {(function (a) {return (function (a) {return (Function(Function(arguments[0]" a ")()))})(a)})(bugger)(de, 0, 0, (0, 0)); }, 1000); ​ #这里就…

【论文阅读】An Experimental Survey of Missing Data Imputation Algorithms

论文地址&#xff1a;An Experimental Survey of Missing Data Imputation Algorithms | IEEE Journals & Magazine | IEEE Xplore 处理缺失数据最简单的方法就是是丢弃缺失值的样本&#xff0c;但这会使得数据更加不完整并且导致偏差或影响结果的代表性。因此&#xff0c;…

wpf使用CefSharp.OffScreen模拟网页登录,并获取身份cookie

目录 框架信息&#xff1a;MainWindow.xamlMainWindow.xaml.cs爬取逻辑模拟登录拦截请求Cookie获取 CookieVisitorHandle 框架信息&#xff1a; CefSharp.OffScreen.NETCore 119.1.20 MainWindow.xaml <Window x:Class"Wpf_CHZC_Img_Identy_ApiDataGet.MainWindow&qu…

虚函数可不可以重载为内联 —— 在开启最大优化时gcc、clang和msvc的表现

下面是对该问题的一种常见回答&#xff1a; 首先&#xff0c;内联是程序员对编译器的一种建议&#xff0c;因此可以在在重载虚函数时在声明处加上inline关键字来修饰&#xff0c; 但是因为虚函数在运行时通过虚函数表&#xff0c;而内联函数在编译时进行代码嵌入&#xff0c;因…

【Spring】之IoC与对象存取

未来的几周时间&#xff0c;大概率我会更新一下Spring家族的一些简单知识。而什么是Spring家族&#xff0c;好多同学还不是很清楚&#xff0c;我先来简单介绍一下吧&#xff1a; 所谓Spring家族&#xff0c;它其实就是一个框架&#xff0c;是基于Servlet再次进行封装的内容。为…

SpringBoot启动后自动打开浏览器访问项目

更简单的一个方法 Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " url); Springboot项目启动后自动打开浏览器访问(超实用)_浏览器访问springboot项目-CSDN博客 Springboot项目启动后自动打开浏览器访问 1、在Springboot项目中每次启动完项…

DeepWalk: Online Learning of Social Representations(2014 ACM SIGKDD)

DeepWalk: Online Learning of Social Representations----《DeepWalk&#xff1a;用于图节点嵌入的在线机器学习算法》 DeepWalk 是将 word2vector 用到 GNN 上 DeepWalk&#xff1a; 将 Graph 的每个节点编码为一个 D 维向量&#xff08;无监督学习&#xff09;&#xff0c;E…

云HIS系统源码,医院管理系信息统源码,融合B/S版四级电子病历系统

医院管理信息系统是以推进公共卫生、医疗、医保、药品、财务监管信息化建设为着力点&#xff0c;整合资源&#xff0c;加强信息标准化和公共服务信息平台建设&#xff0c;逐步实现统一高效、互联互通的管理系统。 SaaS模式Java版云HIS系统&#xff0c;在公立二甲医院应用三年…

企业该怎么选择IP证书

IP证书是一种数字证书&#xff0c;它由权威的数字证书颁发机构&#xff08;CA&#xff09;颁发&#xff0c;部署在只有公网IP地址的站点上&#xff0c;用于在网络中验证身份和保护信息安全。IP证书可以在各种场景下保护网站的信息安全&#xff0c;比如网站vip登录&#xff0c;线…

赛氪荣幸受邀参与中国联合国采购促进会第五次会员代表大会

11 月21 日 &#xff08;星期二&#xff09; 下午14:00&#xff0c;在北京市朝阳区定福庄东街1号中国传媒大学&#xff0c;赛氪荣幸参与中国联合国采购促进会第五次会员代表大会。 2022年以来&#xff0c;联合国采购杯全国大学生英语大赛已经走上了国际舞台&#xff0c;共有来自…

Windows安装Linux双系统教程

&#x1f4bb;Windows安装Linux双系统教程 &#x1f959;资源准备&#x1f354;启动盘制作&#x1f373;分区&#x1f32d;重启电脑&#x1f371;安装Ubuntu &#x1f959;资源准备 &#x1f4a1;下载ubuntu系统、refus启动盘制作程序&#x1f448; &#x1f4a1;一个U盘 &am…

【数据分享】全国1-5级流域、河流矢量数据与水体分布、五级水系数据、八级水系边界范围矢量数据

全国3级流域及各级河流数据:今天给大家分享的数据主要为五个&#xff0c;分别为3级流域、1级河流数据、3级以上河流数据以及4级和5级的河流数据。其中1级河流和3级以上河流数据中存在线状矢量以及面状的湖泊数据&#xff1b;4级和5级的河流数据仅为线状的河流矢量数据。数据中大…

单链表相关面试题--5.合并有序链表

5.合并有序链表 21. 合并两个有序链表 - 力扣&#xff08;LeetCode&#xff09; /* 解题思路&#xff1a; 此题可以先创建一个空链表&#xff0c;然后依次从两个有序链表中选取最小的进行尾插操作进行合并。 */ typedef struct ListNode Node; struct ListNode* mergeTwoList…

Docker上部署mysql(超简单!!!)

拉取mysql镜像 运行如下命令 docker pull mysql:5.7 拉取成功 查看镜像 运行容器 此处部署最新版本的mysql docker run -d --name mysql -p 3307:3306 -e TZAsia/Shanghai -e MYSQL_ROOT_PASSWORD111 mysql --name mysql&#xff1a;给容器起个名字&#xff08;唯一&#xff…

振南技术干货集:制冷设备大型IoT监测项目研发纪实(3)

注解目录 1.制冷设备的监测迫在眉睫 1.1 冷食的利润贡献 1.2 冷设监测系统的困难 &#xff08;制冷设备对于便利店为何如何重要&#xff1f;了解一下你所不知道的便利店和新零售行业。关 于电力线载波通信的论战。&#xff09; 2、电路设计 2.1 防护电路 2.1.1 强电防护…

大模型创业“风投”正劲,AGI Foundathon 大模型创业松活动精彩看点

这是一场万众瞩目的大模型领域盛会。当来自世界各地的顶尖大模型开发者、创业者、投资人汇聚一堂&#xff0c;他们对大模型应用层的思考碰撞出了哪些火花&#xff1f;应运而生了哪些令人眼前一亮的AI-Native产品&#xff1f; 让我们一起来回顾吧&#xff5e;