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;
        }
    }
}
相关推荐
泡海椒5 分钟前
jquick-pdf 表格行列尺寸控制实战:单元格合并的可行边界
java·开发语言·pdf
我不会起名字32213 分钟前
一天一道力扣Hot100(36):深度优先算法---组合总和
数据结构·c++·后端·python·算法·go
纪念 22917 分钟前
c++类和对象(四)
开发语言·c++
Lost of 程序猿26 分钟前
用 Quartz.NET 优雅地处理 .NET 定时任务:从选型到 Docker 部署全记录
后端·c#·asp.net
虚无的纽扣39 分钟前
【力扣刷题】第三天:有效三角形的个数、快乐数
算法·排序算法
tiger86543 分钟前
大语言模型面试和题解,Context Engineering 怎么做?长 Prompt、动态上下文与 Memory 管理
人工智能·深度学习·算法·语言模型·自然语言处理·面试·nlp
辛苦才能1 小时前
C++多态原理:虚函数表的内存布局与动态绑定的汇编真相
开发语言·c++
铲灰1 小时前
做个总结而已
开发语言
不会就选b1 小时前
算法日常・每日刷题--<贪心>17
算法
IvanCodes1 小时前
Python 文件操作(十二):文件与目录的读写
开发语言·python