C#,数值计算——插值和外推,多项式插值与外推插值(Poly_interp)的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    ///


    /// 多项式插值与外推插值
    /// Polynomial Interpolation and 
    /// Extrapolation interpolation routines for one dimension
    ///

    public class Poly_interp : Base_interp
    {
        public double dy { get; set; }

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

        ///


        /// 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].
        ///

        ///
        ///
        ///
        ///
        public override double rawinterp(int jl, double x)
        {
            int ns = 0;
            //double[] xa = xx.range(jl);
            //double[] ya = yy.range(jl);
            double[] c = new double[mm];
            double[] d = new double[mm];
            //double dif = Math.Abs(x - xa[0]);
            double dif = Math.Abs(x - xx[jl + 0]);
            for (int i = 0; i < mm; i++)
            {
                double dift = Math.Abs(x - xx[jl + i]);
                if ((dift) < dif)
                {
                    ns = i;
                    dif = dift;
                }
                c[i] = yy[jl + i];
                d[i] = yy[jl + i];
            }
            double y = yy[jl + ns--];
            for (int m = 1; m < mm; m++)
            {
                for (int i = 0; i < mm - m; i++)
                {
                    double ho = xx[jl + i] - x;
                    double hp = xx[jl + i + m] - x;
                    double w = c[i + 1] - d[i];
                    double den = ho - hp;
                    //if ((den) == 0.0)
                    if (Math.Abs(den) <= float.Epsilon)
                    {
                        throw new Exception("Poly_interp error");
                    }
                    den = w / den;
                    d[i] = hp * den;
                    c[i] = ho * den;
                }
                y += (dy = (2 * (ns + 1) < (mm - m) ? c[ns + 1] : d[ns--]));
            }
            return y;
        }
    }
}
 

2 代码格式

using System;

namespace Legalsoft.Truffer
{
    /// 
    /// 多项式插值与外推插值
    /// Polynomial Interpolation and 
    /// Extrapolation interpolation routines for one dimension
    /// 
    public class Poly_interp : Base_interp
    {
        public double dy { get; set; }

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

        /// 
        /// 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].
        /// 
        /// 
        /// 
        /// 
        /// 
        public override double rawinterp(int jl, double x)
        {
            int ns = 0;
            //double[] xa = xx.range(jl);
            //double[] ya = yy.range(jl);
            double[] c = new double[mm];
            double[] d = new double[mm];
            //double dif = Math.Abs(x - xa[0]);
            double dif = Math.Abs(x - xx[jl + 0]);
            for (int i = 0; i < mm; i++)
            {
                double dift = Math.Abs(x - xx[jl + i]);
                if ((dift) < dif)
                {
                    ns = i;
                    dif = dift;
                }
                c[i] = yy[jl + i];
                d[i] = yy[jl + i];
            }
            double y = yy[jl + ns--];
            for (int m = 1; m < mm; m++)
            {
                for (int i = 0; i < mm - m; i++)
                {
                    double ho = xx[jl + i] - x;
                    double hp = xx[jl + i + m] - x;
                    double w = c[i + 1] - d[i];
                    double den = ho - hp;
                    //if ((den) == 0.0)
                    if (Math.Abs(den) <= float.Epsilon)
                    {
                        throw new Exception("Poly_interp error");
                    }
                    den = w / den;
                    d[i] = hp * den;
                    c[i] = ho * den;
                }
                y += (dy = (2 * (ns + 1) < (mm - m) ? c[ns + 1] : d[ns--]));
            }
            return y;
        }
    }
}

你可能感兴趣的:(C#数值计算,Numerical,Recipes,c#,开发语言,数值计算,算法)