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

/// 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;

}

}

}

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;
        }
    }
}
相关推荐
军训猫猫头23 分钟前
1.如何对多个控件进行高效的绑定 C#例子 WPF例子
开发语言·算法·c#·.net
success37 分钟前
【爆刷力扣-数组】二分查找 及 衍生题型
算法
真的想上岸啊37 分钟前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
明天好,会的44 分钟前
跨平台ZeroMQ:在Rust中使用zmq库的完整指南
开发语言·后端·rust
Orlando cron1 小时前
数据结构入门:链表
数据结构·算法·链表
丁劲犇1 小时前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
旷世奇才李先生2 小时前
Next.js 安装使用教程
开发语言·javascript·ecmascript
牛客企业服务2 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
charlie1145141912 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
糖葫芦君3 小时前
Policy Gradient【强化学习的数学原理】
算法