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;
        }
    }
}
相关推荐
坚持编程的菜鸟6 小时前
模拟实现memmove
c语言·算法·模拟实现memmove
BUG研究员_6 小时前
Runnable与LCEL
开发语言·人工智能·python
wabs6667 小时前
关于图论【最短路径之Dijkstra算法(堆优化版)|卡码网47.参加科学大会的思考】
数据结构·算法·图论·优先级队列·邻接表·小顶堆·卡码网
坚持编程的菜鸟7 小时前
编写判断大小端程序
c语言·算法·判断大小端
papaofdoudou7 小时前
判断排列逆序奇偶性的乘积判别法(范德蒙德符号法)
人工智能·算法
牛艺翔7 小时前
C++基础
开发语言·c++
键盘会跳舞8 小时前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
linux-hzh8 小时前
百日算法修炼 · Day 03
java·算法
美味蛋炒饭.8 小时前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发
yanaiding8 小时前
C# WPF 独立开发看图工具 PixSight 经验介绍(二)—— 水印模块开发实录
图像处理·c#·wpf·个人开发