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");
        }

    }
}
相关推荐
安忘37 分钟前
LeetCode-274.H 指数
算法·leetcode·职场和发展
xxxmmc39 分钟前
Leetcode 160 Intersection of Two Linked Lists
算法·leetcode·双指针
xcLeigh40 分钟前
WPF跨平台开发探讨:借助相关技术实现多平台应用
c#·wpf
练习两年半的工程师41 分钟前
使用React和google gemini api 打造一个google gemini应用
javascript·人工智能·react.js
王的备忘录2 小时前
结合使用 OpenCV 和 TensorFlow进行图像识别处理
人工智能·opencv·tensorflow
VincentStory2 小时前
分享一个项目中遇到的一个算法题
android·算法
钢铁男儿3 小时前
Python 生成数据(随机漫步)
开发语言·python·信息可视化
赛卡3 小时前
自动驾驶背后的数学:特征提取中的线性变换与非线性激活
人工智能·python·机器学习·自动驾驶·numpy
丶21363 小时前
【AI】深度学习与人工智能应用案例详解
人工智能·深度学习
正经教主3 小时前
【菜鸟飞】在vsCode中安装python的ollama包出错的问题
开发语言·人工智能·vscode·python·ai·编辑器