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

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)

{

en = sum;

double temp2 = 0.0;

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

{

double temp1 = temp2;

temp2 = ej - 1;

double diff = ej - temp2;

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

{

ej - 1 = big;

}

else

{

ej - 1 = temp1 + 1.0 / diff;

}

}

n++;

double val = (n & 1) != 0 ? e0 : e1;

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

    }
}
相关推荐
2401_87241878几秒前
算法入门:数据结构-堆
数据结构·算法
石山代码41 分钟前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
程序大视界1 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
xwz小王子1 小时前
手术机器人登上Science Robotics:2毫米纤细手臂,从3厘米切口完成腰椎神经减压
算法·机器人
枫叶v.2 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
黎阳之光2 小时前
视频孪生智护供水生命线:黎阳之光赋能医疗与园区水务高质量升级
运维·物联网·算法·安全·数字孪生
Black蜡笔小新3 小时前
自动化AI算法训练服务器DLTM制造业AI质检工作站助力制造业实现AI智检
人工智能·算法·自动化
嵌入式小能手3 小时前
飞凌嵌入式ElfBoard-进程间的通信之命名管道
linux·服务器·算法
sleven fung3 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
萤萤七悬3 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python