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 小时前
基于指针的线程池
开发语言·c++
沐知全栈开发1 小时前
Servlet 点击计数器
开发语言
m0Java门徒1 小时前
Java 递归全解析:从原理到优化的实战指南
java·开发语言
uhakadotcom1 小时前
Apache Airflow入门指南:数据管道的强大工具
算法·面试·github
跳跳糖炒酸奶1 小时前
第四章、Isaacsim在GUI中构建机器人(2):组装一个简单的机器人
人工智能·python·算法·ubuntu·机器人
桃子酱紫君2 小时前
华为配置篇-BGP实验
开发语言·华为·php
绵绵细雨中的乡音2 小时前
动态规划-第六篇
算法·动态规划
程序员黄同学2 小时前
动态规划,如何应用动态规划解决实际问题?
算法·动态规划
QTX187302 小时前
JavaScript 中的原型链与继承
开发语言·javascript·原型模式
shaoing2 小时前
MySQL 错误 报错:Table ‘performance_schema.session_variables’ Doesn’t Exist
java·开发语言·数据库