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


    1 文本格式

    using System;

    namespace Legalsoft.Truffer
    {
        ///


        /// 重心有理插值对象
        /// Barycentric rational interpolation object. 
        /// After constructing the object, 
        /// call interp for interpolated values.
        /// Note that no error estimate dy is calculated.
        ///

        public class BaryRat_interp : Base_interp
        {
            private double[] w { get; set; }
            private int d { get; set; }

            ///


            /// Constructor arguments are x and y vectors of length n, 
            /// and order d of desired approximation.
            ///

            ///
            ///
            ///
            ///
            public BaryRat_interp(double[] xv, double[] yv, int dd) : base(xv, yv[0], xv.Length)
            {
                this.w = new double[n];
                this.d = dd;
                if (n <= d)
                {
                    throw new Exception("d too large for number of points in BaryRat_interp");
                }
                for (int k = 0; k < n; k++)
                {
                    int imin = Math.Max(k - d, 0);
                    int imax = k >= n - d ? n - d - 1 : k;
                    double temp = (imin & 1) != 0 ? -1.0 : 1.0;
                    double sum = 0.0;
                    for (int i = imin; i <= imax; i++)
                    {
                        int jmax = Math.Min(i + d, n - 1);
                        double term = 1.0;
                        for (int j = i; j <= jmax; j++)
                        {
                            if (j == k)
                            {
                                continue;
                            }
                            term *= (xx[k] - xx[j]);
                        }
                        term = temp / term;
                        temp = -temp;
                        sum += term;
                    }
                    w[k] = sum;
                }
            }

            ///


            /// Use equation(3.4.9) to compute the 
            /// barycentric rational interpolant.
            /// Note that jl is not used since 
            /// the approximation is global; 
            /// it is included only
            /// for compatibility with Base_interp.
            ///

            ///
            ///
            ///
            public override double rawinterp(int jl, double x)
            {
                double num = 0;
                double den = 0;
                for (int i = 0; i < n; i++)
                {
                    double h = x - xx[i];
                    //if (h == 0.0)
                    if (Math.Abs(h) <= float.Epsilon)
                    {
                        return yy[i];
                    }
                    else
                    {
                        double temp = w[i] / h;
                        num += temp * yy[i];
                        den += temp;
                    }
                }
                return num / den;
            }

            ///


            /// No need to invoke hunt or locate since 
            /// the interpolation is global, so
            /// override interp to simply call rawinterp 
            /// directly with a dummy value of jl.
            ///

            ///
            ///
            public new double interp(double x)
            {
                return rawinterp(1, x);
            }
        }
    }
     

    2 代码格式

    1. using System;
    2. namespace Legalsoft.Truffer
    3. {
    4. ///
    5. /// 重心有理插值对象
    6. /// Barycentric rational interpolation object.
    7. /// After constructing the object,
    8. /// call interp for interpolated values.
    9. /// Note that no error estimate dy is calculated.
    10. ///
    11. public class BaryRat_interp : Base_interp
    12. {
    13. private double[] w { get; set; }
    14. private int d { get; set; }
    15. ///
    16. /// Constructor arguments are x and y vectors of length n,
    17. /// and order d of desired approximation.
    18. ///
    19. ///
    20. ///
    21. ///
    22. ///
    23. public BaryRat_interp(double[] xv, double[] yv, int dd) : base(xv, yv[0], xv.Length)
    24. {
    25. this.w = new double[n];
    26. this.d = dd;
    27. if (n <= d)
    28. {
    29. throw new Exception("d too large for number of points in BaryRat_interp");
    30. }
    31. for (int k = 0; k < n; k++)
    32. {
    33. int imin = Math.Max(k - d, 0);
    34. int imax = k >= n - d ? n - d - 1 : k;
    35. double temp = (imin & 1) != 0 ? -1.0 : 1.0;
    36. double sum = 0.0;
    37. for (int i = imin; i <= imax; i++)
    38. {
    39. int jmax = Math.Min(i + d, n - 1);
    40. double term = 1.0;
    41. for (int j = i; j <= jmax; j++)
    42. {
    43. if (j == k)
    44. {
    45. continue;
    46. }
    47. term *= (xx[k] - xx[j]);
    48. }
    49. term = temp / term;
    50. temp = -temp;
    51. sum += term;
    52. }
    53. w[k] = sum;
    54. }
    55. }
    56. ///
    57. /// Use equation(3.4.9) to compute the
    58. /// barycentric rational interpolant.
    59. /// Note that jl is not used since
    60. /// the approximation is global;
    61. /// it is included only
    62. /// for compatibility with Base_interp.
    63. ///
    64. ///
    65. ///
    66. ///
    67. public override double rawinterp(int jl, double x)
    68. {
    69. double num = 0;
    70. double den = 0;
    71. for (int i = 0; i < n; i++)
    72. {
    73. double h = x - xx[i];
    74. //if (h == 0.0)
    75. if (Math.Abs(h) <= float.Epsilon)
    76. {
    77. return yy[i];
    78. }
    79. else
    80. {
    81. double temp = w[i] / h;
    82. num += temp * yy[i];
    83. den += temp;
    84. }
    85. }
    86. return num / den;
    87. }
    88. ///
    89. /// No need to invoke hunt or locate since
    90. /// the interpolation is global, so
    91. /// override interp to simply call rawinterp
    92. /// directly with a dummy value of jl.
    93. ///
    94. ///
    95. ///
    96. public new double interp(double x)
    97. {
    98. return rawinterp(1, x);
    99. }
    100. }
    101. }

  • 相关阅读:
    2023医药微信公众号排名榜top100汇总合集
    MongoDB数据库
    【vue后台管理系统】基于Vue+Element-UI+ECharts开发通用管理后台(中)
    Learning From Documents in the Wild to Improve Document Unwarping论文学习笔记
    聊聊 K8S:K8S集群搭建实战
    线性代数基础-矩阵
    【历史上的今天】9 月 16 日:乔布斯的归来;苹果崛起;易语言发布
    微信小程序设计之主体文件app-wxss/less
    2021最新中高级Java面试题目,Java面试题汇总
    第二次线上面试总结(2022.9.14)
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/134347554