C#,数值计算——积分方程与逆理论,构造n点等间隔求积的权重的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer

{

/// <summary>

/// 构造n点等间隔求积的权重

/// Constructs weights for the n-point equal-interval quadrature

/// from O to(n-1)h of a function f(x) times an arbitrary

/// (possibly singular) weight function w(x). The indefinite-integral

/// moments Fn(y) of w(x) are provided by the user-supplied function

/// kermom in the quad object.

/// </summary>

public class Wwghts

{

private double h { get; set; }

private int n { get; set; }

//private Quad_matrix quad { get; set; }

UniVarRealMultiValueFun quad;

private double[] wghts { get; set; }

public Wwghts(double hh, int nn, UniVarRealMultiValueFun q)

{

this.h = hh;

this.n = nn;

this.quad = q;

this.wghts = new double[n];

}

public double[] weights()

{

double hi = 1.0 / h;

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

{

wghts[j] = 0.0;

}

if (n >= 4)

{

double[] w = new double[4];

double[] wold = quad.funk(0.0);

double b = 0.0;

for (int j = 0; j < n - 3; j++)

{

double c = j;

double a = b;

b = a + h;

if (j == n - 4)

{

b = (n - 1) * h;

}

double[] wnew = quad.funk(b);

double fac = 1.0;

for (int k = 0; k < 4; k++, fac *= hi)

{

w[k] = (wnew[k] - wold[k]) * fac;

}

wghts[j] += (((c + 1.0) * (c + 2.0) * (c + 3.0) * w[0] - (11.0 + c * (12.0 + c * 3.0)) * w[1] + 3.0 * (c + 2.0) * w[2] - w[3]) / 6.0);

wghts[j + 1] += ((-c * (c + 2.0) * (c + 3.0) * w[0] + (6.0 + c * (10.0 + c * 3.0)) * w[1] - (3.0 * c + 5.0) * w[2] + w[3]) * 0.5);

wghts[j + 2] += ((c * (c + 1.0) * (c + 3.0) * w[0] - (3.0 + c * (8.0 + c * 3.0)) * w[1] + (3.0 * c + 4.0) * w[2] - w[3]) * 0.5);

wghts[j + 3] += ((-c * (c + 1.0) * (c + 2.0) * w[0] + (2.0 + c * (6.0 + c * 3.0)) * w[1] - 3.0 * (c + 1.0) * w[2] + w[3]) / 6.0);

for (int k = 0; k < 4; k++)

{

wold[k] = wnew[k];

}

}

}

else if (n == 3)

{

double[] w = new double[3];

double[] wold = quad.funk(0.0);

double[] wnew = quad.funk(h + h);

w[0] = wnew[0] - wold[0];

w[1] = hi * (wnew[1] - wold[1]);

w[2] = hi * hi * (wnew[2] - wold[2]);

wghts[0] = w[0] - 1.5 * w[1] + 0.5 * w[2];

wghts[1] = 2.0 * w[1] - w[2];

wghts[2] = 0.5 * (w[2] - w[1]);

}

else if (n == 2)

{

double[] wold = quad.funk(0.0);

double[] wnew = quad.funk(h);

wghts[0] = wnew[0] - wold[0] - (wghts[1] = hi * (wnew[1] - wold[1]));

}

return wghts;

}

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    /// <summary>
    /// 构造n点等间隔求积的权重
    /// Constructs weights for the n-point equal-interval quadrature
    /// from O to(n-1)h of a function f(x) times an arbitrary
    /// (possibly singular) weight function w(x). The indefinite-integral
    /// moments Fn(y) of w(x) are provided by the user-supplied function
    /// kermom in the quad object.
    /// </summary>
    public class Wwghts
    {
        private double h { get; set; }
        private int n { get; set; }
        //private Quad_matrix quad { get; set; }
        UniVarRealMultiValueFun quad;
        private double[] wghts { get; set; }

        public Wwghts(double hh, int nn, UniVarRealMultiValueFun q)
        {
            this.h = hh;
            this.n = nn;
            this.quad = q;
            this.wghts = new double[n];
        }

        public double[] weights()
        {
            double hi = 1.0 / h;
            for (int j = 0; j < n; j++)
            {
                wghts[j] = 0.0;
            }
            if (n >= 4)
            {
                double[] w = new double[4];
                double[] wold = quad.funk(0.0);
                double b = 0.0;
                for (int j = 0; j < n - 3; j++)
                {
                    double c = j;
                    double a = b;
                    b = a + h;
                    if (j == n - 4)
                    {
                        b = (n - 1) * h;
                    }
                    double[] wnew = quad.funk(b);
                    double fac = 1.0;
                    for (int k = 0; k < 4; k++, fac *= hi)
                    {
                        w[k] = (wnew[k] - wold[k]) * fac;
                    }
                    wghts[j] += (((c + 1.0) * (c + 2.0) * (c + 3.0) * w[0] - (11.0 + c * (12.0 + c * 3.0)) * w[1] + 3.0 * (c + 2.0) * w[2] - w[3]) / 6.0);
                    wghts[j + 1] += ((-c * (c + 2.0) * (c + 3.0) * w[0] + (6.0 + c * (10.0 + c * 3.0)) * w[1] - (3.0 * c + 5.0) * w[2] + w[3]) * 0.5);
                    wghts[j + 2] += ((c * (c + 1.0) * (c + 3.0) * w[0] - (3.0 + c * (8.0 + c * 3.0)) * w[1] + (3.0 * c + 4.0) * w[2] - w[3]) * 0.5);
                    wghts[j + 3] += ((-c * (c + 1.0) * (c + 2.0) * w[0] + (2.0 + c * (6.0 + c * 3.0)) * w[1] - 3.0 * (c + 1.0) * w[2] + w[3]) / 6.0);
                    for (int k = 0; k < 4; k++)
                    {
                        wold[k] = wnew[k];
                    }
                }
            }
            else if (n == 3)
            {
                double[] w = new double[3];
                double[] wold = quad.funk(0.0);
                double[] wnew = quad.funk(h + h);
                w[0] = wnew[0] - wold[0];
                w[1] = hi * (wnew[1] - wold[1]);
                w[2] = hi * hi * (wnew[2] - wold[2]);
                wghts[0] = w[0] - 1.5 * w[1] + 0.5 * w[2];
                wghts[1] = 2.0 * w[1] - w[2];
                wghts[2] = 0.5 * (w[2] - w[1]);
            }
            else if (n == 2)
            {
                double[] wold = quad.funk(0.0);
                double[] wnew = quad.funk(h);
                wghts[0] = wnew[0] - wold[0] - (wghts[1] = hi * (wnew[1] - wold[1]));
            }
            return wghts;
        }
    }
}
相关推荐
秃头佛爷几秒前
Python学习大纲总结及注意事项
开发语言·python·学习
待磨的钝刨1 分钟前
【格式化查看JSON文件】coco的json文件内容都在一行如何按照json格式查看
开发语言·javascript·json
昨日之日20061 小时前
Moonshine - 新型开源ASR(语音识别)模型,体积小,速度快,比OpenAI Whisper快五倍 本地一键整合包下载
人工智能·whisper·语音识别
浮生如梦_1 小时前
Halcon基于laws纹理特征的SVM分类
图像处理·人工智能·算法·支持向量机·计算机视觉·分类·视觉检测
深度学习lover1 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
△曉風殘月〆1 小时前
WPF MVVM入门系列教程(二、依赖属性)
c#·wpf·mvvm
XiaoLeisj2 小时前
【JavaEE初阶 — 多线程】单例模式 & 指令重排序问题
java·开发语言·java-ee
热爱跑步的恒川2 小时前
【论文复现】基于图卷积网络的轻量化推荐模型
网络·人工智能·开源·aigc·ai编程
励志成为嵌入式工程师3 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
逐·風3 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#