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;
        }
    }
}
相关推荐
曾几何时`2 小时前
Go(三)GC垃圾回收
开发语言
并不喜欢吃鱼2 小时前
一.C++11:统一列表初始化 + std::initializer_list 超详细精讲
开发语言·c++
Black蜡笔小新2 小时前
自动化AI算法训练服务器DLTM零代码私有化部署筑牢企业AI落地根基
人工智能·算法·自动化
wWYy.2 小时前
算法:最大子数组和
算法
吃着火锅x唱着歌3 小时前
LeetCode 3829.设计共享出行系统
算法·leetcode·职场和发展
炸薯条!3 小时前
二叉树的链式表示
数据结构·算法
CHHH_HHH3 小时前
【C++】二叉搜索树全面升级,深度剖析AVL树
开发语言·数据结构·c++·算法·stl
Mumu12183 小时前
P3211 [HNOI2011] XOR和路径
算法
高一学习c++会秃头吗3 小时前
页面置换算法实现
算法
奋斗的小方3 小时前
Java基础篇09(2):项目实战之基于swing的石头迷阵
java·开发语言