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.hqwc.cn/news/193061.html

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

相关文章

上网行为审计软件能审计到什么

上网行为审计软件是一种用于监控和分析员工在工作时间使用互联网行为的软件工具。这种软件可以帮助企业管理员工在工作时间内的互联网使用情况&#xff0c;以确保员工的行为符合企业规定和法律法规。 域之盾软件---上网行为审计软件可以审计到以下内容&#xff1a; 1、网络访问…

初刷leetcode题目(2)——数据结构与算法

&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️Take your time ! &#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️&#x1f636;‍&#x1f32b;️…

openGauss通过VIP实现的故障转移

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

浅谈WPF之控件模板和数据模板

WPF不仅支持传统的Windows Forms编程的用户界面和用户体验设计&#xff0c;同时还推出了以模板为核心的新一代设计理念。在WPF中&#xff0c;通过引入模板&#xff0c;将数据和算法的“内容”和“形式”进行解耦。模板主要分为两大类&#xff1a;数据模板【Data Template】和控…

算法设计与分析复习--递归与分治(二)

文章目录 上一篇归并排序统计逆序对快速排序线性时间选择最接近点对问题一维二维 循环赛日程表下一篇 上一篇 算法设计与分析复习–递归与分治(一) 归并排序 问题特点&#xff1a;局部有序到整体有序 AcWing787.归并排序 #include <iostream> #include <cstring>…

信安.网络安全.UDP协议拥塞

第一部分 如何解决UDP丢包问题 一、UDP 报文格式 每个 UDP 报文分为 UDP 报头和 UDP 数据区两部分。报头由 4 个 16 位长&#xff08;2 字节&#xff09;字段组成&#xff0c;分别说明该报文的源端口、目的端口、报文长度和校验值。UDP 报文格式如图所示。 UDP 报文中每个…

【如何学习Python自动化测试】—— 页面元素定位

接上篇自动化测试环境搭建&#xff0c;现在我们介绍 webdriver 对浏览器操作的 API。 2、 页面元素定位 通过自动化操作 web 页面&#xff0c;首先要解决的问题就是定位到要操作的对象&#xff0c;比如要模拟用户在页面上的输入框中输入一段字符串&#xff0c;那就必须得定位到…

FPGA模块——IIC协议(读写PCF8591)

FPGA模块——IIC协议&#xff08;读取PCF8591&#xff09; PCF8591/AT8591芯片对iic协议的使用 PCF8591/AT8591芯片 低功耗8位CMOS数据采集设备&#xff0c;4路模拟输入&#xff0c;1路模拟输出&#xff0c;分时多路复用&#xff0c;读取数据用串型iic总线接口&#xff0c;最大…

RT-Thread STM32F407 BMI088--SPI

BMI088是一款高性能6轴惯性传感器&#xff0c;由16位数字三轴24g加速度计和16位数字三轴2000/ s陀螺仪组成。 这里用SPI来驱动BMI088进行数据解读 第一步&#xff0c;首先在 RT-Thread Settings中进行配置 第二步&#xff0c;退出RT-Thread Settings&#xff0c;进入board.h…

UiPath Studio 2023.10 Crack

UiPath Studio是一款功能强大且用户友好的集成开发环境 (IDE)&#xff0c;专为机器人流程自动化 (RPA) 设计。它由自动化技术领域的领先公司UiPath开发。 以下是 UiPath Studio 的一些主要功能和组件&#xff1a; 图形用户界面 (GUI)&#xff1a;UiPath Studio 具有直观且用户友…

Kafka(四)消费者消费消息

文章目录 如何确保不重复消费消息&#xff1f;消费者业务逻辑重试消费者提交自定义反序列化类消费者参数配置及其说明重要的参数session.time.ms和heartbeat.interval.ms和group.instance.id增加消费者的吞吐量消费者消费的超时时间和poll()方法的关系 消费者消费逻辑启动消费者…

buildadmin+tp8表格操作(2)----表头上方按钮绑定事件处理,实现功能(全选/全不选)

buildAdmin 表格上方的按钮添加完成之后&#xff0c; 就要对其实现功能了 有了上面的说明&#xff0c; 我就只要得到了 ref 中的表格对象&#xff0c; 就可以象el-table 一样来操作表格的属性和方法了 我们来实现上面的几个按钮的方法 全选/全不选 上面就是添加按钮功能的全过…