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;
        }
    }
}
相关推荐
Guofu_Liao2 分钟前
大语言模型---LoRA简介;LoRA的优势;LoRA训练步骤;总结
人工智能·语言模型·自然语言处理·矩阵·llama
----云烟----26 分钟前
QT中QString类的各种使用
开发语言·qt
lsx20240631 分钟前
SQL SELECT 语句:基础与进阶应用
开发语言
开心工作室_kaic1 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
向宇it1 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
武子康1 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
九鼎科技-Leo1 小时前
什么是 WPF 中的依赖属性?有什么作用?
windows·c#·.net·wpf
转世成为计算机大神2 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
宅小海2 小时前
scala String
大数据·开发语言·scala
qq_327342732 小时前
Java实现离线身份证号码OCR识别
java·开发语言