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


    1 文本格式

    using System;

    namespace Legalsoft.Truffer
    {
        ///


        /// Abstract base class used by all interpolation routines in this chapter.
        /// Only the routine interp is called directly by the user.
        ///

        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; }

            ///


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

            ///
            ///
            ///
            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);
            }

            ///


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

            ///
            ///
            ///
            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)));
            }

            ///


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

            ///
            ///
            ///
            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 代码格式

    1. using System;
    2. namespace Legalsoft.Truffer
    3. {
    4. ///
    5. /// Abstract base class used by all interpolation routines in this chapter.
    6. /// Only the routine interp is called directly by the user.
    7. ///
    8. public abstract class Base_interp
    9. {
    10. public int n { get; set; }
    11. public int mm { get; set; }
    12. public int jsav { get; set; }
    13. public int cor { get; set; }
    14. public int dj { get; set; }
    15. public double[] xx { get; set; }
    16. public double[] yy { get; set; }
    17. ///
    18. /// Set up for interpolating on a table of x's and y's of length m. Normally
    19. /// called by a derived class, not by the user.
    20. ///
    21. ///
    22. ///
    23. ///
    24. public Base_interp(double[] x, double y, int m)
    25. {
    26. this.n = x.Length;
    27. this.mm = m;
    28. this.jsav = 0;
    29. this.cor = 0;
    30. this.xx = x;
    31. this.yy = new double[x.Length];
    32. for (int i = 0; i < yy.Length; i++)
    33. {
    34. yy[i] = y;
    35. }
    36. dj = Math.Max(1, (int)Math.Pow((double)n, 0.25));
    37. }
    38. public double interp(double x)
    39. {
    40. int jlo = (cor != 0) ? hunt(x) : locate(x);
    41. return rawinterp(jlo, x);
    42. }
    43. ///
    44. /// Given a value x, return a value j such that x is (insofar as possible)
    45. /// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.
    46. /// The values in xx must be monotonic, either increasing or decreasing.
    47. /// The returned value is not less than 0, nor greater than n-1.
    48. ///
    49. ///
    50. ///
    51. ///
    52. public int locate(double x)
    53. {
    54. if (n < 2 || mm < 2 || mm > n)
    55. {
    56. throw new Exception("locate size error");
    57. }
    58. bool ascnd = (xx[n - 1] >= xx[0]);
    59. int jl = 0;
    60. int ju = n - 1;
    61. while (ju - jl > 1)
    62. {
    63. int jm = (ju + jl) >> 1;
    64. if (x >= xx[jm] == ascnd)
    65. {
    66. jl = jm;
    67. }
    68. else
    69. {
    70. ju = jm;
    71. }
    72. }
    73. cor = Math.Abs(jl - jsav) > dj ? 0 : 1;
    74. jsav = jl;
    75. return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));
    76. }
    77. ///
    78. /// Given a value x, return a value j such that x is (insofar as possible)
    79. /// centered in the subrange xx[j..j + mm - 1], where xx is the stored pointer.
    80. /// The values in xx must be monotonic, either increasing or decreasing.
    81. /// The returned value is not less than 0, nor greater than n-1.
    82. ///
    83. ///
    84. ///
    85. ///
    86. public int hunt(double x)
    87. {
    88. int jl = jsav;
    89. int inc = 1;
    90. if (n < 2 || mm < 2 || mm > n)
    91. {
    92. throw new Exception("hunt size error");
    93. }
    94. bool ascnd = (xx[n - 1] >= xx[0]);
    95. int ju;
    96. if (jl < 0 || jl > n - 1)
    97. {
    98. jl = 0;
    99. ju = n - 1;
    100. }
    101. else
    102. {
    103. if (x >= xx[jl] == ascnd)
    104. {
    105. for (; ; )
    106. {
    107. ju = jl + inc;
    108. if (ju >= n - 1)
    109. {
    110. ju = n - 1;
    111. break;
    112. }
    113. else if (x < xx[ju] == ascnd)
    114. {
    115. break;
    116. }
    117. else
    118. {
    119. jl = ju;
    120. inc += inc;
    121. }
    122. }
    123. }
    124. else
    125. {
    126. ju = jl;
    127. for (; ; )
    128. {
    129. jl = jl - inc;
    130. if (jl <= 0)
    131. {
    132. jl = 0;
    133. break;
    134. }
    135. else if (x >= xx[jl] == ascnd)
    136. {
    137. break;
    138. }
    139. else
    140. {
    141. ju = jl;
    142. inc += inc;
    143. }
    144. }
    145. }
    146. }
    147. while (ju - jl > 1)
    148. {
    149. int jm = (ju + jl) >> 1;
    150. if (x >= xx[jm] == ascnd)
    151. {
    152. jl = jm;
    153. }
    154. else
    155. {
    156. ju = jm;
    157. }
    158. }
    159. cor = Math.Abs(jl - jsav) > dj ? 0 : 1;
    160. jsav = jl;
    161. return Math.Max(0, Math.Min(n - mm, jl - ((mm - 2) >> 1)));
    162. }
    163. public abstract double rawinterp(int jlo, double x);
    164. }
    165. }

  • 相关阅读:
    串(KMP算法)
    AtomicReference实现单例模式
    spring容器
    Vue与TypeScript的配合:如何在Vue项目中使用TypeScript,利用静态类型提高代码的可维护性
    CUDA安装
    python txt 读取 写入
    Python数学基础-识图一、平面直角坐标系
    CoT 的方式使用 LLM 设计测试用例实践
    【Linux C | 网络编程】入门知识:UDP协议、一个最简单的UDP客户端、一个最简单的UDP服务端
    Scala语言用Selenium库写一个爬虫模版
  • 原文地址:https://blog.csdn.net/beijinghorn/article/details/134347571