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

    }
}
相关推荐
星星法术嗲人13 分钟前
【Java】—— 集合框架:Collections工具类的使用
java·开发语言
Eric.Lee202126 分钟前
数据集-目标检测系列- 螃蟹 检测数据集 crab >> DataBall
python·深度学习·算法·目标检测·计算机视觉·数据集·螃蟹检测
黑不溜秋的27 分钟前
C++ 语言特性29 - 协程介绍
开发语言·c++
一丝晨光32 分钟前
C++、Ruby和JavaScript
java·开发语言·javascript·c++·python·c·ruby
天上掉下来个程小白34 分钟前
Stream流的中间方法
java·开发语言·windows
林辞忧35 分钟前
算法修炼之路之滑动窗口
算法
xujinwei_gingko1 小时前
JAVA基础面试题汇总(持续更新)
java·开发语言
￴ㅤ￴￴ㅤ9527超级帅1 小时前
LeetCode hot100---二叉树专题(C++语言)
c++·算法·leetcode
liuyang-neu1 小时前
力扣 简单 110.平衡二叉树
java·算法·leetcode·深度优先
penguin_bark1 小时前
LCR 068. 搜索插入位置
算法·leetcode·职场和发展