C#,数值计算——插值和外推,谢别德(Shep)插值方法的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// 谢别德插值方法
    /// Object for Shepard interpolation using n points in dim dimensions. Call
    /// constructor once, then interp as many times as desired.
    /// </summary>
    public class Shep_interp
    {
        private int dim { get; set; }
        private int n { get; set; }
        private double[,] pts { get; set; }
        private double[] vals { get; set; }
        private double pneg { get; set; }

        /// <summary>
        /// The n dim matrix ptss inputs the data points, the vector valss the function
        /// values.Set p to the desired exponent.The default value is typical.
        /// </summary>
        /// <param name="ptss"></param>
        /// <param name="valss"></param>
        /// <param name="p"></param>
        public Shep_interp(double[,] ptss, double[] valss, double p = 2.0)
        {
            this.dim = ptss.GetLength(1);
            this.n = ptss.GetLength(0);
            this.pts = ptss;
            this.vals = valss;
            this.pneg = -p;
        }

        /// <summary>
        /// Return the interpolated function value at a dim-dimensional point pt.
        /// </summary>
        /// <param name="pt"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public double interp(double[] pt)
        {
            double r;
            double w;
            double sum = 0.0;
            double sumw = 0.0;
            if (pt.Length != dim)
            {
                throw new Exception("RBF_interp bad pt size");
            }
            for (int i = 0; i < n; i++)
            {
                //if ((r = rad(pt.ToArray(), pts.GetRow(i).ToArray())) == 0.0)
                r = Globals.dist(pt, Globals.CopyFrom(i, pts));
                if (Math.Abs(r) <= float.Epsilon)
                {
                    return vals[i];
                }
                sum += (w = Math.Pow(r, pneg));
                sumw += w * vals[i];
            }
            return sumw / sum;
        }
        /*
        public double rad(double[] p1, double[] p2)
        {
            double sum = 0.0;
            for (int i = 0; i < dim; i++)
            {
                sum += Globals.SQR(p1[i] - p2[i]);
            }
            return Math.Sqrt(sum);
        }
        */
    }
}
 

2 代码格式

using System;namespace Legalsoft.Truffer
{/// <summary>/// 谢别德插值方法/// Object for Shepard interpolation using n points in dim dimensions. Call/// constructor once, then interp as many times as desired./// </summary>public class Shep_interp{private int dim { get; set; }private int n { get; set; }private double[,] pts { get; set; }private double[] vals { get; set; }private double pneg { get; set; }/// <summary>/// The n dim matrix ptss inputs the data points, the vector valss the function/// values.Set p to the desired exponent.The default value is typical./// </summary>/// <param name="ptss"></param>/// <param name="valss"></param>/// <param name="p"></param>public Shep_interp(double[,] ptss, double[] valss, double p = 2.0){this.dim = ptss.GetLength(1);this.n = ptss.GetLength(0);this.pts = ptss;this.vals = valss;this.pneg = -p;}/// <summary>/// Return the interpolated function value at a dim-dimensional point pt./// </summary>/// <param name="pt"></param>/// <returns></returns>/// <exception cref="Exception"></exception>public double interp(double[] pt){double r;double w;double sum = 0.0;double sumw = 0.0;if (pt.Length != dim){throw new Exception("RBF_interp bad pt size");}for (int i = 0; i < n; i++){//if ((r = rad(pt.ToArray(), pts.GetRow(i).ToArray())) == 0.0)r = Globals.dist(pt, Globals.CopyFrom(i, pts));if (Math.Abs(r) <= float.Epsilon){return vals[i];}sum += (w = Math.Pow(r, pneg));sumw += w * vals[i];}return sumw / sum;}/*public double rad(double[] p1, double[] p2){double sum = 0.0;for (int i = 0; i < dim; i++){sum += Globals.SQR(p1[i] - p2[i]);}return Math.Sqrt(sum);}*/}
}