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);
        }
    }
}
相关推荐
Dream_Snowar1 小时前
速通Python 第三节
开发语言·python
XH华1 小时前
初识C语言之二维数组(下)
c语言·算法
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
不想当程序猿_2 小时前
【蓝桥杯每日一题】求和——前缀和
算法·前缀和·蓝桥杯
高山我梦口香糖2 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
落魄君子2 小时前
GA-BP分类-遗传算法(Genetic Algorithm)和反向传播算法(Backpropagation)
算法·分类·数据挖掘
菜鸡中的奋斗鸡→挣扎鸡2 小时前
滑动窗口 + 算法复习
数据结构·算法
信号处理学渣2 小时前
matlab画图,选择性显示legend标签
开发语言·matlab
红龙创客2 小时前
某狐畅游24校招-C++开发岗笔试(单选题)
开发语言·c++
Lenyiin2 小时前
第146场双周赛:统计符合条件长度为3的子数组数目、统计异或值为给定值的路径数目、判断网格图能否被切割成块、唯一中间众数子序列 Ⅰ
c++·算法·leetcode·周赛·lenyiin