C#,数值计算——偏微分方程,Relaxation的计算方法与源程序

1 文本格式

using System;

namespace Legalsoft.Truffer

{

public class Relaxation

{

private Relaxation() { }

public static void sor(double[,] a, double[,] b, double[,] c, double[,] d, double[,] e, double[,] f, double[,] u, double rjac)

{

const int MAXITS = 1000;

const double EPS = 1.0e-13;

double anormf = 0.0;

double omega = 1.0;

int jmax = a.GetLength(0);

for (int j = 1; j < jmax - 1; j++)

{

for (int l = 1; l < jmax - 1; l++)

{

anormf += Math.Abs(f[j, l]);

}

}

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

{

double anorm = 0.0;

int jsw = 1;

for (int ipass = 0; ipass < 2; ipass++)

{

int lsw = jsw;

for (int j = 1; j < jmax - 1; j++)

{

for (int l = lsw; l < jmax - 1; l += 2)

{

double resid = a[j, l] * u[j + 1, l] + b[j, l] * u[j - 1, l] + c[j, l] * u[j, l + 1] + d[j, l] * u[j, l - 1] + e[j, l] * u[j, l] - f[j, l];

anorm += Math.Abs(resid);

u[j, l] -= omega * resid / e[j, l];

}

lsw = 3 - lsw;

}

jsw = 3 - jsw;

omega = (n == 0 && ipass == 0 ? 1.0 / (1.0 - 0.5 * rjac * rjac) : 1.0 / (1.0 - 0.25 * rjac * rjac * omega));

}

if (anorm < EPS * anormf)

{

return;

}

}

throw new Exception("MAXITS exceeded");

}

}

}

2 代码格式

cs 复制代码
using System;

namespace Legalsoft.Truffer
{
    public class Relaxation
    {
        private Relaxation() { }

        public static void sor(double[,] a, double[,] b, double[,] c, double[,] d, double[,] e, double[,] f, double[,] u, double rjac)
        {
            const int MAXITS = 1000;
            const double EPS = 1.0e-13;
            double anormf = 0.0;
            double omega = 1.0;
            int jmax = a.GetLength(0);
            for (int j = 1; j < jmax - 1; j++)
            {
                for (int l = 1; l < jmax - 1; l++)
                {
                    anormf += Math.Abs(f[j, l]);
                }
            }
            for (int n = 0; n < MAXITS; n++)
            {
                double anorm = 0.0;
                int jsw = 1;
                for (int ipass = 0; ipass < 2; ipass++)
                {
                    int lsw = jsw;
                    for (int j = 1; j < jmax - 1; j++)
                    {
                        for (int l = lsw; l < jmax - 1; l += 2)
                        {
                            double resid = a[j, l] * u[j + 1, l] + b[j, l] * u[j - 1, l] + c[j, l] * u[j, l + 1] + d[j, l] * u[j, l - 1] + e[j, l] * u[j, l] - f[j, l];
                            anorm += Math.Abs(resid);
                            u[j, l] -= omega * resid / e[j, l];
                        }
                        lsw = 3 - lsw;
                    }
                    jsw = 3 - jsw;
                    omega = (n == 0 && ipass == 0 ? 1.0 / (1.0 - 0.5 * rjac * rjac) : 1.0 / (1.0 - 0.25 * rjac * rjac * omega));
                }
                if (anorm < EPS * anormf)
                {
                    return;
                }
            }
            throw new Exception("MAXITS exceeded");
        }

    }
}
相关推荐
山上三树1 分钟前
详细介绍读写锁
开发语言·c++·spring
乾元1 分钟前
绕过艺术:使用 GANs 对抗 Web 防火墙(WAF)
前端·网络·人工智能·深度学习·安全·架构
jghhh013 分钟前
基于MATLAB的协同过滤推荐算法实现
开发语言·matlab·推荐算法
蝈蝈tju4 分钟前
Vibe Coding 正确姿势: 先会指挥, 再让AI干
人工智能·经验分享·ai
比特森林探险记5 分钟前
后端开发者快速入门react
开发语言·前端·javascript
重生之我是Java开发战士7 分钟前
【优选算法】滑动窗口:长度最小的子数组,无重复字符的最长子串,最大连续1的个数,将x减到0的最小操作数,水果成篮,异位词,串联所有单词的子串,最小覆盖子串
算法
想你依然心痛10 分钟前
AI 换脸新纪元:Facefusion 人脸融合实战探索
人工智能·换脸·facefusion·人脸融合
马士兵教育13 分钟前
计算机专业学生入行IT行业,编程语言如何选择?
java·开发语言·c++·人工智能·python
CoderJia程序员甲14 分钟前
GitHub 热榜项目 - 日榜(2026-01-28)
人工智能·ai·大模型·github·ai教程
码界奇点18 分钟前
基于eBPF技术的高性能网络防火墙系统设计与实现
开发语言·网络·毕业设计·php·wpf·go语言·源代码管理