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);
        }
    }
}
相关推荐
浮生如梦_29 分钟前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
△曉風殘月〆31 分钟前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
励志成为嵌入式工程师2 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
逐·風2 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
捕鲸叉3 小时前
创建线程时传递参数给线程
开发语言·c++·算法
A charmer3 小时前
【C++】vector 类深度解析:探索动态数组的奥秘
开发语言·c++·算法
Peter_chq3 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端
wheeldown3 小时前
【数据结构】选择排序
数据结构·算法·排序算法
记录成长java4 小时前
ServletContext,Cookie,HttpSession的使用
java·开发语言·servlet