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;
        }
    }
}
相关推荐
xlsw_5 分钟前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
靴子学长5 分钟前
基于字节大模型的论文翻译(含免费源码)
人工智能·深度学习·nlp
AI_NEW_COME1 小时前
知识库管理系统可扩展性深度测评
人工智能
Dream_Snowar1 小时前
速通Python 第三节
开发语言·python
海棠AI实验室1 小时前
AI的进阶之路:从机器学习到深度学习的演变(一)
人工智能·深度学习·机器学习
hunteritself1 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
高山我梦口香糖2 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
IT古董2 小时前
【机器学习】机器学习的基本分类-强化学习-策略梯度(Policy Gradient,PG)
人工智能·机器学习·分类
centurysee2 小时前
【最佳实践】Anthropic:Agentic系统实践案例
人工智能
mahuifa2 小时前
混合开发环境---使用编程AI辅助开发Qt
人工智能·vscode·qt·qtcreator·编程ai