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

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);}*/}
}

相关文章:

  • 【Oracle 客户端连接数据库过程解析】
  • 若依启动步骤
  • 数据采集与大数据架构分享
  • Spring Boot - filter 的顺序
  • 三十分钟学会zookeeper
  • uniapp app tabbar 页面默认隐藏
  • 【【萌新的SOC学习之 VDMA 彩条显示实验之一】】
  • 配置Nginx服务器用于Web应用代理和SSL{仅配置文件}
  • Eclipse切换中文环境
  • 解决公网下,k8s calico master节点无法访问node节点创建的pod
  • java回调函数
  • 2023.11.14 hivesql的容器,数组与映射
  • WPF中有哪些布局方式和对齐方法
  • GCD:异步同步?串行并发?一文轻松拿捏!
  • leetcoe刷题日志-6N字形变换
  • CentOS6 编译安装 redis-3.2.3
  • gulp 教程
  • JavaScript DOM 10 - 滚动
  • jQuery(一)
  • leetcode-27. Remove Element
  • MySQL数据库运维之数据恢复
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • Puppeteer:浏览器控制器
  • underscore源码剖析之整体架构
  • 从输入URL到页面加载发生了什么
  • 构造函数(constructor)与原型链(prototype)关系
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 关于extract.autodesk.io的一些说明
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 罗辑思维在全链路压测方面的实践和工作笔记
  • 让你的分享飞起来——极光推出社会化分享组件
  • 少走弯路,给Java 1~5 年程序员的建议
  • 手写双向链表LinkedList的几个常用功能
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • MiKTeX could not find the script engine ‘perl.exe‘ which is required to execute ‘latexmk‘.
  • MPAndroidChart 教程:Y轴 YAxis
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • 小白应该如何快速入门阿里云服务器,新手使用ECS的方法 ...
  • # Panda3d 碰撞检测系统介绍
  • #if 1...#endif
  • #pragma once与条件编译
  • $HTTP_POST_VARS['']和$_POST['']的区别
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (175)FPGA门控时钟技术
  • (Matlab)遗传算法优化的BP神经网络实现回归预测
  • (阿里云万网)-域名注册购买实名流程
  • (二)Linux——Linux常用指令
  • (二)PySpark3:SparkSQL编程
  • (十六)一篇文章学会Java的常用API
  • (四)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (学习日记)2024.01.19
  • (转)程序员疫苗:代码注入
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • ***利用Ms05002溢出找“肉鸡