C#,数值计算——有理函数插值和外推(Rational_interp)的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer

{

/// <summary>

/// 有理函数插值和外推

/// Rational Function Interpolation and Extrapolation

/// Given a value x, and using pointers to data xx and yy, this routine returns

/// an interpolated value y, and stores an error estimate dy. The returned value

/// is obtained by mm-point polynomial interpolation on the subrange

/// xxjl..jl + mm - 1.

/// </summary>

public class Rational_interp : Base_interp

{

private double dy { get; set; }

public Rational_interp(double\[\] xv, double\[\] yv, int m) : base(xv, yv0, m)

{

this.dy = 0.0;

}

/// <summary>

/// Given a value x, and using pointers to data xx and yy, this routine returns

/// an interpolated value y, and stores an error estimate dy. The returned

/// value is obtained by mm-point diagonal rational function interpolation on

/// the subrange xxjl..jl + mm - 1.

/// </summary>

/// <param name="jl"></param>

/// <param name="x"></param>

/// <returns></returns>

/// <exception cref="Exception"></exception>

public override double rawinterp(int jl, double x)

{

const double TINY = 1.0e-99;

int ns = 0;

double\[\] c = new doublemm;

double\[\] d = new doublemm;

double hh = Math.Abs(x - xxjl + 0);

for (int i = 0; i < mm; i++)

{

double h = Math.Abs(x - xxjl + i);

//if (h == 0.0)

if (Math.Abs(h) <= float.Epsilon)

{

dy = 0.0;

return yyjl + i;

}

else if (h < hh)

{

ns = i;

hh = h;

}

ci = yyjl + i;

di = yyjl + i + TINY;

}

double y = yyjl + ns--;

for (int m = 1; m < mm; m++)

{

for (int i = 0; i < mm - m; i++)

{

double w = ci + 1 - di;

double h = xxjl + i + m - x;

double t = (xxjl + i - x) * di / h;

double dd = t - ci + 1;

//if (dd == 0.0)

if (Math.Abs(dd) <= float.Epsilon)

{

throw new Exception("Error in routine ratint");

}

dd = w / dd;

di = ci + 1 * dd;

ci = t * dd;

}

y += (dy = (2 * (ns + 1) < (mm - m) ? cns + 1 : dns--));

}

return y;

}

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// 有理函数插值和外推
    /// Rational Function Interpolation and Extrapolation
    /// Given a value x, and using pointers to data xx and yy, this routine returns
    /// an interpolated value y, and stores an error estimate dy. The returned value
    /// is obtained by mm-point polynomial interpolation on the subrange
    /// xx[jl..jl + mm - 1].
    /// </summary>
    public class Rational_interp : Base_interp
    {
        private double dy { get; set; }

        public Rational_interp(double[] xv, double[] yv, int m) : base(xv, yv[0], m)
        {
            this.dy = 0.0;
        }

        /// <summary>
        /// Given a value x, and using pointers to data xx and yy, this routine returns
        /// an interpolated value y, and stores an error estimate dy. The returned
        /// value is obtained by mm-point diagonal rational function interpolation on
        /// the subrange xx[jl..jl + mm - 1].
        /// </summary>
        /// <param name="jl"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public override double rawinterp(int jl, double x)
        {
            const double TINY = 1.0e-99;
            int ns = 0;
            double[] c = new double[mm];
            double[] d = new double[mm];
            double hh = Math.Abs(x - xx[jl + 0]);
            for (int i = 0; i < mm; i++)
            {
                double h = Math.Abs(x - xx[jl + i]);
                //if (h == 0.0)
                if (Math.Abs(h) <= float.Epsilon)
                {
                    dy = 0.0;
                    return yy[jl + i];
                }
                else if (h < hh)
                {
                    ns = i;
                    hh = h;
                }
                c[i] = yy[jl + i];
                d[i] = yy[jl + i] + TINY;
            }
            double y = yy[jl + ns--];
            for (int m = 1; m < mm; m++)
            {
                for (int i = 0; i < mm - m; i++)
                {
                    double w = c[i + 1] - d[i];
                    double h = xx[jl + i + m] - x;
                    double t = (xx[jl + i] - x) * d[i] / h;
                    double dd = t - c[i + 1];
                    //if (dd == 0.0)
                    if (Math.Abs(dd) <= float.Epsilon)
                    {
                        throw new Exception("Error in routine ratint");
                    }
                    dd = w / dd;
                    d[i] = c[i + 1] * dd;
                    c[i] = t * dd;
                }
                y += (dy = (2 * (ns + 1) < (mm - m) ? c[ns + 1] : d[ns--]));
            }
            return y;
        }
    }
}
相关推荐
稚南城才子,乌衣巷风流1 小时前
函数:编程中的核心概念
开发语言·前端·javascript
阿米亚波1 小时前
【C++】流式数据输入处理(不完全整理)
开发语言·c++·笔记
Mr.HeBoYan1 小时前
一次持续三天才出现的丢包故障——深入解析 DPDK Memory Ordering、rte_ring 与 CPU Memory Barrier (下)
linux·网络·算法·架构·dpdk
大模型码小白1 小时前
JAVA 集合框架进阶:List 与 Set 的深度解析与实战
java·开发语言·人工智能·windows·语言模型·list·ai编程
wabs6662 小时前
关于图论【深度优先搜索的理论基础】
算法·深度优先·图论
皓月斯语2 小时前
【C++基础】三目运算符
开发语言·数据结构·c++
先吃饱再说3 小时前
LeetCode 101. 对称二叉树
算法
初願致夕霞3 小时前
C++懒汉单例设计详解
服务器·开发语言·c++
脱胎换骨-军哥3 小时前
C++分布式系统设计:从通信引擎到分布式共识
开发语言·c++·分布式
是Dream呀3 小时前
人眼看不出的伪造,AI如何识别?实测合合信息跨模态鉴伪与去反光技术
算法