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;
        }
    }
}
相关推荐
3DVisionary几秒前
3D-DIC与机器学习协同模拟材料应力-应变本构行为研究
人工智能·机器学习·3d·3d-dic技术 机器学习·应力-应变本构行为·卷积神经网络(ecnn)·数字图像相关法(dic)
神经星星3 分钟前
无需预对齐即可消除批次效应,东京大学团队开发深度学习框架STAIG,揭示肿瘤微环境中的详细基因信息
人工智能·深度学习·机器学习
神经星星3 分钟前
【vLLM 学习】调试技巧
人工智能·机器学习·编程语言
Y.O.U..3 分钟前
今日八股——C++
开发语言·c++·面试
weixin_3077791312 分钟前
使用C#实现从Hive的CREATE TABLE语句中提取分区字段名和数据类型
开发语言·数据仓库·hive·c#
Xiaok101820 分钟前
解决 Hugging Face SentenceTransformer 下载失败的完整指南:ProxyError、SSLError与手动下载方案
开发语言·神经网络·php
程序员Linc21 分钟前
写给新人的深度学习扫盲贴:向量与矩阵
人工智能·深度学习·矩阵·向量
绿草在线22 分钟前
Mock.js虚拟接口
开发语言·javascript·ecmascript
xcLeigh29 分钟前
OpenCV从零开始:30天掌握图像处理基础
图像处理·人工智能·python·opencv
果冻人工智能32 分钟前
如何有效应对 RAG 中的复杂查询?
人工智能