C#,数值计算——插值和外推,Base_interp的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer

{

/// <summary>

/// Abstract base class used by all interpolation routines in this chapter.

/// Only the routine interp is called directly by the user.

/// </summary>

public abstract class Base_interp

{

public int n { get; set; }

public int mm { get; set; }

public int jsav { get; set; }

public int cor { get; set; }

public int dj { get; set; }

public double[] xx { get; set; }

public double[] yy { get; set; }

/// <summary>

/// Set up for interpolating on a table of x's and y's of length m. Normally

/// called by a derived class, not by the user.

/// </summary>

/// <param name="x"></param>

/// <param name="y"></param>

/// <param name="m"></param>

public Base_interp(double[] x, double y, int m)

{

this.n = x.Length;

this.mm = m;

this.jsav = 0;

this.cor = 0;

this.xx = x;

this.yy = new double[x.Length];

for (int i = 0; i < yy.Length; i++)

{

yy[i] = y;

}

dj = Math.Max(1, (int)Math.Pow((double)n, 0.25));

}

public double interp(double x)

{

int jlo = (cor != 0) ? hunt(x) : locate(x);

return rawinterp(jlo, x);

}

/// <summary>

/// Given a value x, return a value j such that x is (insofar as possible)

/// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.

/// The values in xx must be monotonic, either increasing or decreasing.

/// The returned value is not less than 0, nor greater than n-1.

/// </summary>

/// <param name="x"></param>

/// <returns></returns>

/// <exception cref="Exception"></exception>

public int locate(double x)

{

if (n < 2 || mm < 2 || mm > n)

{

throw new Exception("locate size error");

}

bool ascnd = (xx[n - 1] >= xx[0]);

int jl = 0;

int ju = n - 1;

while (ju - jl > 1)

{

int jm = (ju + jl) >> 1;

if (x >= xx[jm] == ascnd)

{

jl = jm;

}

else

{

ju = jm;

}

}

cor = Math.Abs(jl - jsav) > dj ? 0 : 1;

jsav = jl;

return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));

}

/// <summary>

/// Given a value x, return a value j such that x is (insofar as possible)

/// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.

/// The values in xx must be monotonic, either increasing or decreasing.

/// The returned value is not less than 0, nor greater than n-1.

/// </summary>

/// <param name="x"></param>

/// <returns></returns>

/// <exception cref="Exception"></exception>

public int hunt(double x)

{

int jl = jsav;

int inc = 1;

if (n < 2 || mm < 2 || mm > n)

{

throw new Exception("hunt size error");

}

bool ascnd = (xx[n - 1] >= xx[0]);

int ju;

if (jl < 0 || jl > n - 1)

{

jl = 0;

ju = n - 1;

}

else

{

if (x >= xx[jl] == ascnd)

{

for (; ; )

{

ju = jl + inc;

if (ju >= n - 1)

{

ju = n - 1;

break;

}

else if (x < xx[ju] == ascnd)

{

break;

}

else

{

jl = ju;

inc += inc;

}

}

}

else

{

ju = jl;

for (; ; )

{

jl = jl - inc;

if (jl <= 0)

{

jl = 0;

break;

}

else if (x >= xx[jl] == ascnd)

{

break;

}

else

{

ju = jl;

inc += inc;

}

}

}

}

while (ju - jl > 1)

{

int jm = (ju + jl) >> 1;

if (x >= xx[jm] == ascnd)

{

jl = jm;

}

else

{

ju = jm;

}

}

cor = Math.Abs(jl - jsav) > dj ? 0 : 1;

jsav = jl;

return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));

}

public abstract double rawinterp(int jlo, double x);

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Abstract base class used by all interpolation routines in this chapter.
    /// Only the routine interp is called directly by the user.
    /// </summary>
    public abstract class Base_interp
    {
        public int n { get; set; }
        public int mm { get; set; }
        public int jsav { get; set; }
        public int cor { get; set; }
        public int dj { get; set; }
        public double[] xx { get; set; }
        public double[] yy { get; set; }

        /// <summary>
        /// Set up for interpolating on a table of x's and y's of length m. Normally
        /// called by a derived class, not by the user.
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="m"></param>
        public Base_interp(double[] x, double y, int m)
        {
            this.n = x.Length;
            this.mm = m;
            this.jsav = 0;
            this.cor = 0;
            this.xx = x;
            this.yy = new double[x.Length];
            for (int i = 0; i < yy.Length; i++)
            {
                yy[i] = y;
            }
            dj = Math.Max(1, (int)Math.Pow((double)n, 0.25));
        }

        public double interp(double x)
        {
            int jlo = (cor != 0) ? hunt(x) : locate(x);
            return rawinterp(jlo, x);
        }

        /// <summary>
        /// Given a value x, return a value j such that x is (insofar as possible)
        /// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.
        /// The values in xx must be monotonic, either increasing or decreasing.
        /// The returned value is not less than 0, nor greater than n-1.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int locate(double x)
        {
            if (n < 2 || mm < 2 || mm > n)
            {
                throw new Exception("locate size error");
            }
            bool ascnd = (xx[n - 1] >= xx[0]);
            int jl = 0;
            int ju = n - 1;
            while (ju - jl > 1)
            {
                int jm = (ju + jl) >> 1;
                if (x >= xx[jm] == ascnd)
                {
                    jl = jm;
                }
                else
                {
                    ju = jm;
                }
            }
            cor = Math.Abs(jl - jsav) > dj ? 0 : 1;
            jsav = jl;
            return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));
        }

        /// <summary>
        /// Given a value x, return a value j such that x is (insofar as possible)
        /// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.
        /// The values in xx must be monotonic, either increasing or decreasing.
        /// The returned value is not less than 0, nor greater than n-1.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public int hunt(double x)
        {
            int jl = jsav;
            int inc = 1;
            if (n < 2 || mm < 2 || mm > n)
            {
                throw new Exception("hunt size error");
            }
            bool ascnd = (xx[n - 1] >= xx[0]);
            int ju;
            if (jl < 0 || jl > n - 1)
            {
                jl = 0;
                ju = n - 1;
            }
            else
            {
                if (x >= xx[jl] == ascnd)
                {
                    for (; ; )
                    {
                        ju = jl + inc;
                        if (ju >= n - 1)
                        {
                            ju = n - 1;
                            break;
                        }
                        else if (x < xx[ju] == ascnd)
                        {
                            break;
                        }
                        else
                        {
                            jl = ju;
                            inc += inc;
                        }
                    }
                }
                else
                {
                    ju = jl;
                    for (; ; )
                    {
                        jl = jl - inc;
                        if (jl <= 0)
                        {
                            jl = 0;
                            break;
                        }
                        else if (x >= xx[jl] == ascnd)
                        {
                            break;
                        }
                        else
                        {
                            ju = jl;
                            inc += inc;
                        }
                    }
                }
            }
            while (ju - jl > 1)
            {
                int jm = (ju + jl) >> 1;
                if (x >= xx[jm] == ascnd)
                {
                    jl = jm;
                }
                else
                {
                    ju = jm;
                }
            }
            cor = Math.Abs(jl - jsav) > dj ? 0 : 1;
            jsav = jl;
            return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));
        }

        public abstract double rawinterp(int jlo, double x);

    }
}
相关推荐
Dream_Snowar1 小时前
速通Python 第三节
开发语言·python
XH华1 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_2 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
高山我梦口香糖2 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
落魄君子2 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘
菜鸡中的奋斗鸡→挣扎鸡2 小时前
滑动窗口 + 算法复习
数据结构·算法
信号处理学渣2 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客2 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin2 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin