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(fj, 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 = aj, l * uj + 1, l + bj, l * uj - 1, l + cj, l * uj, l + 1 + dj, l * uj, l - 1 + ej, l * uj, l - fj, l;

anorm += Math.Abs(resid);

uj, l -= omega * resid / ej, 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");
        }

    }
}
相关推荐
冬奇Lab32 分钟前
Workflow 系列(03):状态管理——持久化、幂等性与版本绑定
人工智能·工作流引擎
冬奇Lab41 分钟前
每日一个开源项目(第146篇):openpilot - 开源自动驾驶辅助系统,曾在 Consumer Reports 评测中超过特斯拉 Autopilot
人工智能·开源·自动驾驶
吴佳浩2 小时前
AI 工程师知识地图:模型格式、框架、部署工具一次讲明白
人工智能·aigc·ai编程
IT_陈寒2 小时前
Java的Date类又坑了我一次,改用时间戳真香
前端·人工智能·后端
码农胖大海3 小时前
AI额度不够用的解决方案
人工智能
后端小肥肠3 小时前
小红书虚拟商品怎么做?我先用 Skill 跑通了壁纸品类
人工智能·aigc·agent
feiyu_gao3 小时前
从零搭建个人 AI 工作台:一个管理者的 3 个月实验
人工智能·aigc·团队管理
先吃饱再说3 小时前
判断回文字符串,从一行代码到双指针优化
算法
程序员cxuan4 小时前
一句话,让你用上 GPT-5.6
人工智能·后端·程序员