C#,数值计算——函数计算,Epsalg的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer

{

/// <summary>

/// Convergence acceleration of a sequence by the algorithm.Initialize by

/// calling the constructor with arguments nmax, an upper bound on the

/// number of terms to be summed, and epss, the desired accuracy.Then make

/// successive calls to the function next, with argument the next partial sum

/// of the sequence. The current estimate of the limit of the sequence is

/// returned by next.The flag cnvgd is set when convergence is detected.

/// </summary>

public class Epsalg

{

private double[] e { get; set; }

private int n { get; set; }

private int ncv { get; set; }

public bool cnvgd { get; set; }

/// <summary>

/// Numbers near machine underflow and overflow limits.

/// </summary>

private double eps { get; set; }

private double small { get; set; }

private double big { get; set; }

private double lastval { get; set; }

private double lasteps { get; set; }

public Epsalg(int nmax, double epss)

{

this.e = new double[nmax];

this.n = 0;

this.ncv = 0;

this.cnvgd = false;

this.eps = epss;

this.lastval = 0.0;

small = float.MinValue * 10.0;

big = double.MaxValue;

}

public double next(double sum)

{

e[n] = sum;

double temp2 = 0.0;

for (int j = n; j > 0; j--)

{

double temp1 = temp2;

temp2 = e[j - 1];

double diff = e[j] - temp2;

if (Math.Abs(diff) <= small)

{

e[j - 1] = big;

}

else

{

e[j - 1] = temp1 + 1.0 / diff;

}

}

n++;

double val = (n & 1) != 0 ? e[0] : e[1];

if (Math.Abs(val) > 0.01 * big)

{

val = lastval;

}

lasteps = Math.Abs(val - lastval);

if (lasteps > eps)

{

ncv = 0;

}

else

{

ncv++;

}

if (ncv >= 3)

{

cnvgd = true;

}

return (lastval = val);

}

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// Convergence acceleration of a sequence by the algorithm.Initialize by
    /// calling the constructor with arguments nmax, an upper bound on the
    /// number of terms to be summed, and epss, the desired accuracy.Then make
    /// successive calls to the function next, with argument the next partial sum
    /// of the sequence. The current estimate of the limit of the sequence is 
    /// returned by next.The flag cnvgd is set when convergence is detected.
    /// </summary>
    public class Epsalg
    {
        private double[] e { get; set; }
        private int n { get; set; }
        private int ncv { get; set; }
        public bool cnvgd { get; set; }
        /// <summary>
        /// Numbers near machine underflow and overflow limits.
        /// </summary>
        private double eps { get; set; }
        private double small { get; set; }
        private double big { get; set; }
        private double lastval { get; set; }
        private double lasteps { get; set; }

        public Epsalg(int nmax, double epss)
        {
            this.e = new double[nmax];
            this.n = 0;
            this.ncv = 0;
            this.cnvgd = false;
            this.eps = epss;
            this.lastval = 0.0;
            small = float.MinValue * 10.0;
            big = double.MaxValue;
        }

        public double next(double sum)
        {
            e[n] = sum;
            double temp2 = 0.0;
            for (int j = n; j > 0; j--)
            {
                double temp1 = temp2;
                temp2 = e[j - 1];
                double diff = e[j] - temp2;
                if (Math.Abs(diff) <= small)
                {
                    e[j - 1] = big;
                }
                else
                {
                    e[j - 1] = temp1 + 1.0 / diff;
                }
            }
            n++;
            double val = (n & 1) != 0 ? e[0] : e[1];
            if (Math.Abs(val) > 0.01 * big)
            {
                val = lastval;
            }
            lasteps = Math.Abs(val - lastval);
            if (lasteps > eps)
            {
                ncv = 0;
            }
            else
            {
                ncv++;
            }
            if (ncv >= 3)
            {
                cnvgd = true;
            }
            return (lastval = val);
        }

    }
}
相关推荐
<但凡.11 分钟前
题海拾贝:力扣 138.随机链表的复制
数据结构·算法·leetcode
장숙혜11 分钟前
JavaScript正则表达式解析:模式、方法与实战案例
开发语言·javascript·正则表达式
安大小万28 分钟前
C++ 学习:深入理解 Linux 系统中的冯诺依曼架构
linux·开发语言·c++
随心Coding32 分钟前
【零基础入门Go语言】错误处理:如何更优雅地处理程序异常和错误
开发语言·后端·golang
T.Ree.36 分钟前
C语言_自定义类型(结构体,枚举,联合)
c语言·开发语言
Channing Lewis38 分钟前
python生成随机字符串
服务器·开发语言·python
田梓燊42 分钟前
图论 八字码
c++·算法·图论
pchmi1 小时前
C# OpenCV机器视觉:红外体温检测
人工智能·数码相机·opencv·计算机视觉·c#·机器视觉·opencvsharp
小熊科研路(同名GZH)1 小时前
【Matlab高端绘图SCI绘图模板】第002期 绘制面积图
开发语言·matlab