C#,数值计算——插值和外推,曲线插值(Curve_interp)的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Object for interpolating a curve specified by n points in dim dimensions.
    /// </summary>
    public class Curve_interp
    {
        private int dim { get; set; }
        private int n { get; set; }
        private int xin { get; set; }
        private bool cls { get; set; }
        private double[,] pts { get; set; }
        private double[] s { get; set; }
        private double[] ans { get; set; }
        private Spline_interp[] srp { get; set; }// = new Spline_interp[];

        /// <summary>
        /// The n x dim matrix ptsin inputs the data points.Input close as 0 for an
        /// open curve, 1 for a closed curve. (For a closed curve, the last data point
        /// should not duplicate the first 鈥?the algorithm will connect them.)
        /// </summary>
        /// <param name="ptsin"></param>
        /// <param name="close"></param>
        /// <exception cref="Exception"></exception>
        public Curve_interp(double[,] ptsin, bool close = false)
        {
            this.n = ptsin.GetLength(0);
            this.dim = ptsin.GetLength(1);
            this.xin = close ? 2 * n : n;
            this.cls = close;
            this.pts = new double[dim, xin];
            this.s = new double[xin];
            this.ans = new double[dim];
            this.srp = new Spline_interp[dim];

            //int i;
            //int ii;
            //int im;
            //int j;
            //int ofs;
            //double ss;
            //double soff;
            //double db;
            //double de;
            int ofs = close ? n / 2 : 0;
            s[0] = 0.0;
            for (int i = 0; i < xin; i++)
            {
                int ii = (i - ofs + n) % n;
                int im = (ii - 1 + n) % n;
                for (int j = 0; j < dim; j++)
                {
                    pts[j, i] = ptsin[ii, j];
                }
                if (i > 0)
                {
                    //s[i] = s[i - 1] + rad(ptsin.GetRow(ii).ToArray(), ptsin.GetRow(im).ToArray());
                    s[i] = s[i - 1] + Globals.dist(Globals.CopyFrom(ii, ptsin), Globals.CopyFrom(im, ptsin));
                    if (s[i] == s[i - 1])
                    {
                        throw new Exception("error in Curve_interp");
                    }
                }
            }
            double ss = close ? s[ofs + n] - s[ofs] : s[n - 1] - s[0];
            double soff = s[ofs];
            for (int i = 0; i < xin; i++)
            {
                s[i] = (s[i] - soff) / ss;
            }
            for (int j = 0; j < dim; j++)
            {
                double db = xin < 4 ? 1.0e99 : fprime(s, 0, Globals.CopyFrom(j, pts), 0, 1);
                double de = xin < 4 ? 1.0e99 : fprime(s, xin - 1, Globals.CopyFrom(j, pts), xin - 1, -1);
                srp[j] = new Spline_interp(s, pts[j, 0], db, de);
            }
        }

        /// <summary>
        /// Interpolate a point on the stored curve.The point is parameterized by t,
        /// in the range[0, 1]. For open curves, values of t outside this range will
        /// return extrapolations(dangerous!). For closed curves, t is periodic with
        /// period 1.
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public double[] interp(double t)
        {
            if (cls)
            {
                t = t - Math.Floor(t);
            }
            for (int j = 0; j < dim; j++)
            {
                ans[j] = (srp[j]).interp(t);
            }
            return ans;
        }

        /// <summary>
        /// Utility for estimating the derivatives at the endpoints.x and y point to
        /// the abscissa and ordinate of the endpoint.If pm is C1, points to the right
        /// will be used (left endpoint); if it is 1, points to the left will be used
        /// (right endpoint).
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="pm"></param>
        /// <returns></returns>
        public double fprime(double[] x, double[] y, int pm)
        {
            double s1 = x[0] - x[pm * 1];
            double s2 = x[0] - x[pm * 2];
            double s3 = x[0] - x[pm * 3];
            double s12 = s1 - s2;
            double s13 = s1 - s3;
            double s23 = s2 - s3;
            return -(s1 * s2 / (s13 * s23 * s3)) * y[pm * 3] + (s1 * s3 / (s12 * s2 * s23)) * y[pm * 2] - (s2 * s3 / (s1 * s12 * s13)) * y[pm * 1] + (1.0 / s1 + 1.0 / s2 + 1.0 / s3) * y[0];
        }

        private double fprime(double[] x, int off_x, double[] y, int off_y, int pm)
        {
            double s1 = x[off_x + 0] - x[off_x + pm * 1];
            double s2 = x[off_x + 0] - x[off_x + pm * 2];
            double s3 = x[off_x + 0] - x[off_x + pm * 3];
            double s12 = s1 - s2;
            double s13 = s1 - s3;
            double s23 = s2 - s3;
            return -(s1 * s2 / (s13 * s23 * s3)) * y[off_y + pm * 3] + (s1 * s3 / (s12 * s2 * s23)) * y[off_y + pm * 2] - (s2 * s3 / (s1 * s12 * s13)) * y[off_y + pm * 1] + (1.0 / s1 + 1.0 / s2 + 1.0 / s3) * y[off_y + 0];
        }
        /*
        public double rad(double[] p1, double[] p2)
        {
            double sum = 0.0;
            for (int i = 0; i < dim; i++)
            {
                sum += Globals.SQR(p1[i] - p2[i]);
            }
            if (sum <= float.Epsilon)
            {
                return 0.0;
            }
            return Math.Sqrt(sum);
        }
        */
    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// Object for interpolating a curve specified by n points in dim dimensions./// </summary>public class Curve_interp{private int dim { get; set; }private int n { get; set; }private int xin { get; set; }private bool cls { get; set; }private double[,] pts { get; set; }private double[] s { get; set; }private double[] ans { get; set; }private Spline_interp[] srp { get; set; }// = new Spline_interp[];/// <summary>/// The n x dim matrix ptsin inputs the data points.Input close as 0 for an/// open curve, 1 for a closed curve. (For a closed curve, the last data point/// should not duplicate the first 鈥?the algorithm will connect them.)/// </summary>/// <param name="ptsin"></param>/// <param name="close"></param>/// <exception cref="Exception"></exception>public Curve_interp(double[,] ptsin, bool close = false){this.n = ptsin.GetLength(0);this.dim = ptsin.GetLength(1);this.xin = close ? 2 * n : n;this.cls = close;this.pts = new double[dim, xin];this.s = new double[xin];this.ans = new double[dim];this.srp = new Spline_interp[dim];//int i;//int ii;//int im;//int j;//int ofs;//double ss;//double soff;//double db;//double de;int ofs = close ? n / 2 : 0;s[0] = 0.0;for (int i = 0; i < xin; i++){int ii = (i - ofs + n) % n;int im = (ii - 1 + n) % n;for (int j = 0; j < dim; j++){pts[j, i] = ptsin[ii, j];}if (i > 0){//s[i] = s[i - 1] + rad(ptsin.GetRow(ii).ToArray(), ptsin.GetRow(im).ToArray());s[i] = s[i - 1] + Globals.dist(Globals.CopyFrom(ii, ptsin), Globals.CopyFrom(im, ptsin));if (s[i] == s[i - 1]){throw new Exception("error in Curve_interp");}}}double ss = close ? s[ofs + n] - s[ofs] : s[n - 1] - s[0];double soff = s[ofs];for (int i = 0; i < xin; i++){s[i] = (s[i] - soff) / ss;}for (int j = 0; j < dim; j++){double db = xin < 4 ? 1.0e99 : fprime(s, 0, Globals.CopyFrom(j, pts), 0, 1);double de = xin < 4 ? 1.0e99 : fprime(s, xin - 1, Globals.CopyFrom(j, pts), xin - 1, -1);srp[j] = new Spline_interp(s, pts[j, 0], db, de);}}/// <summary>/// Interpolate a point on the stored curve.The point is parameterized by t,/// in the range[0, 1]. For open curves, values of t outside this range will/// return extrapolations(dangerous!). For closed curves, t is periodic with/// period 1./// </summary>/// <param name="t"></param>/// <returns></returns>public double[] interp(double t){if (cls){t = t - Math.Floor(t);}for (int j = 0; j < dim; j++){ans[j] = (srp[j]).interp(t);}return ans;}/// <summary>/// Utility for estimating the derivatives at the endpoints.x and y point to/// the abscissa and ordinate of the endpoint.If pm is C1, points to the right/// will be used (left endpoint); if it is 1, points to the left will be used/// (right endpoint)./// </summary>/// <param name="x"></param>/// <param name="y"></param>/// <param name="pm"></param>/// <returns></returns>public double fprime(double[] x, double[] y, int pm){double s1 = x[0] - x[pm * 1];double s2 = x[0] - x[pm * 2];double s3 = x[0] - x[pm * 3];double s12 = s1 - s2;double s13 = s1 - s3;double s23 = s2 - s3;return -(s1 * s2 / (s13 * s23 * s3)) * y[pm * 3] + (s1 * s3 / (s12 * s2 * s23)) * y[pm * 2] - (s2 * s3 / (s1 * s12 * s13)) * y[pm * 1] + (1.0 / s1 + 1.0 / s2 + 1.0 / s3) * y[0];}private double fprime(double[] x, int off_x, double[] y, int off_y, int pm){double s1 = x[off_x + 0] - x[off_x + pm * 1];double s2 = x[off_x + 0] - x[off_x + pm * 2];double s3 = x[off_x + 0] - x[off_x + pm * 3];double s12 = s1 - s2;double s13 = s1 - s3;double s23 = s2 - s3;return -(s1 * s2 / (s13 * s23 * s3)) * y[off_y + pm * 3] + (s1 * s3 / (s12 * s2 * s23)) * y[off_y + pm * 2] - (s2 * s3 / (s1 * s12 * s13)) * y[off_y + pm * 1] + (1.0 / s1 + 1.0 / s2 + 1.0 / s3) * y[off_y + 0];}/*public double rad(double[] p1, double[] p2){double sum = 0.0;for (int i = 0; i < dim; i++){sum += Globals.SQR(p1[i] - p2[i]);}if (sum <= float.Epsilon){return 0.0;}return Math.Sqrt(sum);}*/}
}

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

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qt-C++笔记之treeWidget初次使用

qt-C笔记之treeWidget初次使用 code review! 文章目录 qt-C笔记之treeWidget初次使用1.运行2.文件结构3.main.cpp4.widget.h5.widget.cpp6.widget.ui7.main.qrc8.qt_widget_test.pro9.options.png 1.运行 2.文件结构 3.main.cpp 代码 #include "widget.h"#include…

生成式AI模型量化简明教程

在不断发展的人工智能领域&#xff0c;生成式AI无疑已成为创新的基石。 这些先进的模型&#xff0c;无论是用于创作艺术、生成文本还是增强医学成像&#xff0c;都以产生非常逼真和创造性的输出而闻名。 然而&#xff0c;生成式AI的力量是有代价的—模型大小和计算要求。 随着生…

计算机视觉基础(9)——相机标定与对极几何

前言 本节我们将学习相机标定和对极几何两部分的内容。 在相机标定部分&#xff0c;我们将学习直接线性变换&#xff08;Direct Linear Transform, DL&#xff09;,张正友标定法&#xff08;Zhang’s Method&#xff09;和 Perspective-n-Point (PnP) 这三种方法。 在对极几何部…

动手学深度学习——循环神经网络的简洁实现(代码详解)

文章目录 循环神经网络的简洁实现1. 定义模型2. 训练与预测 循环神经网络的简洁实现 # 使用深度学习框架的高级API提供的函数更有效地实现相同的语言模型 import torch from torch import nn from torch.nn import functional as F from d2l import torch as d2lbatch_size, …

C#开发的OpenRA游戏之属性BodyOrientation(6)

C#开发的OpenRA游戏之属性BodyOrientation(6) 在顶层定义里会发现这个属性: ^SpriteActor: BodyOrientation: QuantizeFacingsFromSequence: RenderSprites: SpriteActor是用来定义角色的基本属性,它的第一个属性就是BodyOrientation,这个属性主要用来描述角色的身体的…

SVG的viewBox、width和height释义, 示例及代码

svg的是没有边界的&#xff0c;svg画布只是用于展示svg世界中某一个范围的内容&#xff0c;而对于超过了svg画布范围的内容&#xff0c;则会被遮挡。默认svg画布默认显示世界坐标下原点坐标的width*height面积的矩形视野。 ​ 我们可以通过viewBox来修改默认的显示配置&#…

Linux shell编程学习笔记27:tputs

除了stty命令&#xff0c;我们还可以使用tput命令来更改终端的参数和功能。 1 tput 命令的功能 tput 命令的主要功能有&#xff1a;移动更改光标、更改文本显示属性&#xff08;如颜色、下划线、粗体&#xff09;&#xff0c;清除屏幕特定区域等。 2 tput 命令格式 tput [选…

macOS下如何使用Flask进行开发

&#x1f468;&#x1f3fb;‍&#x1f4bb; 热爱摄影的程序员 &#x1f468;&#x1f3fb;‍&#x1f3a8; 喜欢编码的设计师 &#x1f9d5;&#x1f3fb; 擅长设计的剪辑师 &#x1f9d1;&#x1f3fb;‍&#x1f3eb; 一位高冷无情的编码爱好者 大家好&#xff0c;我是全栈工…

想要精通算法和SQL的成长之路 - 摩尔投票法的运用

想要精通算法和SQL的成长之路 - 摩尔投票法的运用 前言一. 多数元素1.1 摩尔投票法 二. 多数元素II2.1 分析 前言 想要精通算法和SQL的成长之路 - 系列导航 一. 多数元素 原题链接 1.1 摩尔投票法 简单来说&#xff0c;假设数组 num 的众数是 x&#xff0c;数组长度为n。 有…

CAS源码工程搭建记录

CAS源码工程搭建 1.下载2.gradle下载源改为阿里云&#xff0c;解决下载慢的问题3.解决保存 1.下载 git clone -b 5.3.x https://gitee.com/mirrors/CAS.git如果下载的是压缩包&#xff0c;导入工程会保存&#xff0c;因为builder.gradle的第20行开始有取git信息&#xff0c;如…

JavaWeb Day10 案例-部门管理

目录 一、查询部门 &#xff08;一&#xff09;需求 &#xff08;二&#xff09;思路 &#xff08;三&#xff09;查询部门 &#xff08;四&#xff09;、前后端联调 二、删除 &#xff08;一&#xff09;需求 &#xff08;二&#xff09;思路 &#xff08;三&#xf…

复杂数据统计与R语言程序设计实验二

1、创建一个对象&#xff0c;并进行数据类型的转换、判别等操作&#xff0c;步骤如下。 ①使用命令清空工作空间&#xff0c;创建一个对象x&#xff0c;内含元素为序列&#xff1a;1&#xff0c;3&#xff0c;5&#xff0c;6&#xff0c;8。 ②判断对象x是否为数值型数据。 ③…

本地开发环境和服务器传输数据的几种方法

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️ &#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…

Vulkan渲染引擎开发教程 一、开发环境搭建

一 安装 Vulkan SDK Vulkan SDK 就是我们要搞的图形接口 首先到官网下载SDK并安装 https://vulkan.lunarg.com/sdk/home 二 安装 GLFW 窗口库 GLFW是个跨平台的小型窗口库&#xff0c;也就是显示窗口&#xff0c;图形的载体 去主页下载并安装&#xff0c;https://www.glfw.…

C语言的由来与发展历程

C语言的起源可以追溯到上世纪70年代&#xff0c;由Dennis Ritchie在贝尔实验室开发出来。C语言的设计目标是提供一种简洁、高效、可移植的编程语言&#xff0c;以便于开发底层的系统软件。在那个时代&#xff0c;计算机技术正在迅速发展&#xff0c;出现了多种高级编程语言&…

html使用天地图写一个地图列表

一、效果图&#xff1a; 点击左侧地址列表&#xff0c;右侧地图跟着改变。 二、代码实现&#xff1a; 一进入页面时&#xff0c;通过body调用onLoad"onLoad()"函数&#xff0c;确保地图正常显示。 <body onLoad"onLoad()"><!--左侧代码-->…

QCheckBox样式表

1、QCheckBox选择器和指示器类型 选择器类型描述QCheckBoxQCheckBox 的默认选择器。QCheckBox::indicatorQCheckBox 的指示器,即复选框的标记部分。QCheckBox::indicator:checkedQCheckBox 选中状态下的指示器。QCheckBox::indicator:uncheckedQCheckBox 未选中状态下的指示器…

MyBatis逆向工程

新建Maven工程 <build><plugins><plugin><!--mybatis代码自动生成插件--><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-maven-plugin</artifactId><version>1.3.6</version><confi…

2023年中职“网络安全“—Web 渗透测试②

2023年中职“网络安全“—Web 渗透测试② Web 渗透测试任务环境说明&#xff1a;1.访问http://靶机IP/web1/,获取flag值&#xff0c;Flag格式为flag{xxx}&#xff1b;2.访问http://靶机IP/web2/,获取flag值&#xff0c;Flag格式为flag{xxx}&#xff1b;3.访问http://靶机IP/web…