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

    }
}
相关推荐
新加坡内哥谈技术几秒前
微软发布Majorana 1芯片,开启量子计算新路径
人工智能·深度学习·语言模型·自然语言处理
C#Thread11 分钟前
C#上位机--流程控制(IF语句)
开发语言·javascript·ecmascript
真智AI25 分钟前
使用 DistilBERT 进行资源高效的自然语言处理
人工智能·自然语言处理
OpenBuild.xyz30 分钟前
我是如何从 0 到 1 找到 Web3 工作的?
人工智能·web3·去中心化·区块链·智能合约
Sui_Network31 分钟前
Sui 如何支持各种类型的 Web3 游戏
大数据·数据库·人工智能·游戏·web3·区块链
万兴丶36 分钟前
Unity 适用于单机游戏的红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含源码)
数据结构·unity·设计模式·c#
牵牛老人1 小时前
Qt开发中出现中文乱码问题深度解析与解决方案
开发语言·qt
维齐洛波奇特利(male)1 小时前
(动态规划 完全背包 **)leetcode279完全平方数
算法·动态规划
大脑经常闹风暴@小猿1 小时前
1.1 go环境搭建及基本使用
开发语言·后端·golang
ZKNOW甄知科技1 小时前
IT服务运营管理体系的常用方法论与实践指南(上)
大数据·数据库·人工智能