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

1 文本格式

using System;

namespace Legalsoft.Truffer

{

public class Eulsum

{

private double[] wksp { get; set; }

private int n { get; set; }

private int ncv { get; set; }

public bool cnvgd { get; set; }

private double sum { get; set; }

private double eps { get; set; }

private double lastval { get; set; }

private double lasteps { get; set; }

public Eulsum(int nmax, double epss)

{

this.wksp = new double[nmax];

this.n = 0;

this.ncv = 0;

this.cnvgd = false;

this.sum = 0.0;

this.eps = epss;

this.lastval = 0.0;

}

public double next(double term)

{

if (n + 1 > wksp.Length)

{

throw new Exception("wksp too small in eulsum");

}

if (n == 0)

{

sum = 0.5 * (wksp[n++] = term);

}

else

{

double tmp = wksp[0];

wksp[0] = term;

for (int j = 1; j < n; j++)

{

double dum = wksp[j];

wksp[j] = 0.5 * (wksp[j - 1] + tmp);

tmp = dum;

}

wksp[n] = 0.5 * (wksp[n - 1] + tmp);

if (Math.Abs(wksp[n]) <= Math.Abs(wksp[n - 1]))

{

sum += (0.5 * wksp[n++]);

}

else

{

sum += wksp[n];

}

}

lasteps = Math.Abs(sum - lastval);

if (lasteps <= eps)

{

ncv++;

}

if (ncv >= 2)

{

cnvgd = true;

}

return (lastval = sum);

}

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    public class Eulsum
    {
        private double[] wksp { get; set; }
        private int n { get; set; }
        private int ncv { get; set; }
        public bool cnvgd { get; set; }
        private double sum { get; set; }
        private double eps { get; set; }
        private double lastval { get; set; }
        private double lasteps { get; set; }

        public Eulsum(int nmax, double epss)
        {
            this.wksp = new double[nmax];
            this.n = 0;
            this.ncv = 0;
            this.cnvgd = false;
            this.sum = 0.0;
            this.eps = epss;
            this.lastval = 0.0;
        }

        public double next(double term)
        {
            if (n + 1 > wksp.Length)
            {
                throw new Exception("wksp too small in eulsum");
            }
            if (n == 0)
            {
                sum = 0.5 * (wksp[n++] = term);
            }
            else
            {
                double tmp = wksp[0];
                wksp[0] = term;
                for (int j = 1; j < n; j++)
                {
                    double dum = wksp[j];
                    wksp[j] = 0.5 * (wksp[j - 1] + tmp);
                    tmp = dum;
                }
                wksp[n] = 0.5 * (wksp[n - 1] + tmp);
                if (Math.Abs(wksp[n]) <= Math.Abs(wksp[n - 1]))
                {
                    sum += (0.5 * wksp[n++]);
                }
                else
                {
                    sum += wksp[n];
                }
            }
            lasteps = Math.Abs(sum - lastval);
            if (lasteps <= eps)
            {
                ncv++;
            }
            if (ncv >= 2)
            {
                cnvgd = true;
            }
            return (lastval = sum);
        }
    }
}
相关推荐
----云烟----1 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024061 小时前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic1 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it1 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康2 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
九鼎科技-Leo2 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
转世成为计算机大神2 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海2 小时前
scala String
大数据·开发语言·scala
qq_327342732 小时前
Java实现离线身份证号码OCR识别
java·开发语言
锅包肉的九珍2 小时前
Scala的Array数组
开发语言·后端·scala