矩阵的各种运算(加、减、乘、逆、行列式、转置)的源码(C#)

本文详细介绍了矩阵的基本运算,包括矩阵的乘法、加法、减法、求行列式、转置及求逆等操作,并提供了具体的C#实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/// 矩阵的乘
public bool MatrixMultiply(double[,] a, double[,] b, ref double[,] c)
{
if (a.GetLength(1) != b.GetLength(0))
return false;
if (a.GetLength(0) != c.GetLength(0) || b.GetLength(1) != c.GetLength(1))
return false;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < b.GetLength(1); j++)
{
c[i, j] = 0;
for (int k = 0; k < b.GetLength(0); k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}

return true;
}

/// 矩阵的加
public bool MatrixAdd(double[,] a, double[,] b, ref double[,] c)
{
if (a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1)
|| a.GetLength(0) != c.GetLength(0) || a.GetLength(1) != c.GetLength(1))
return false;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
c[i, j] = a[i, j] + b[i, j];
}
}

return true;
}

/// 矩阵的减
public bool MatrixSubtration(double[,] a, double[,] b, ref double[,] c)
{
if (a.GetLength(0) != b.GetLength(0) || a.GetLength(1) != b.GetLength(1)
|| a.GetLength(0) != c.GetLength(0) || a.GetLength(1) != c.GetLength(1))
return false;
for (int i = 0; i < a.GetLength(0); i++)
{
for (int j = 0; j < a.GetLength(1); j++)
{
c[i, j] = a[i, j] - b[i, j];
}
}

return true;
}

/// 矩阵的行列式的值
public double MatrixSurplus(double[,] a)
{
int i, j, k, p, r, m, n;
m = a.GetLength(0);
n = a.GetLength(1);
double X, temp = 1, temp1 = 1, s = 0,s1 = 0;

if(n == 2)
{
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if ((i + j) % 2 > 0) temp1 *= a[i, j];
else temp *= a[i, j];
X=temp-temp1;
}
else
{
for (k = 0; k < n; k++)
{
for (i = 0, j = k; i < m && j < n; i++, j++)
temp *= a[i, j];
if (m - i > 0)
{
for (p = m - i, r = m - 1; p > 0; p--, r--)
temp *= a[r, p - 1];
}
s += temp;
temp = 1;
}

for (k = n - 1; k >= 0; k--)
{
for (i = 0, j = k; i < m && j >= 0; i++, j--)
temp1 *= a[i, j];
if (m - i > 0)
{
for (p = m - 1, r = i; r < m; p--, r++)
temp1 *= a[r, p];
}
s1 += temp1;
temp1 = 1;
}

X = s - s1;
}
return X;
}

/// 矩阵的转置
public bool MatrixInver(double[,] a, ref double[,] b)
{
if (a.GetLength(0) != b.GetLength(1) || a.GetLength(1) != b.GetLength(0))
return false;
for (int i = 0; i < a.GetLength(1); i++)
for (int j = 0; j < a.GetLength(0); j++)
b[i, j] = a[j, i];

return true;
}

/// 矩阵的逆
public bool MatrixOpp(double[,] a, ref double[,] b)
{
double X = MatrixSurplus(a);
if (X == 0) return false;
X = 1 / X;

double[,] B = new double[a.GetLength(0), a.GetLength(1)];
double[,] SP = new double[a.GetLength(0), a.GetLength(1)];
double[,] AB = new double[a.GetLength(0), a.GetLength(1)];

for (int i = 0; i < a.GetLength(0); i++)
for (int j = 0; j < a.GetLength(1); j++)
{
for (int m = 0; m < a.GetLength(0); m++)
for (int n = 0; n < a.GetLength(1); n++)
B[m, n] = a[m, n];
{
for (int x = 0; x < a.GetLength(1); x++)
B[i, x] = 0;
for (int y = 0; y < a.GetLength(0); y++)
B[y, j] = 0;
B[i, j] = 1;
SP[i, j] = MatrixSurplus(B);
AB[i, j] = X * SP[i, j];
}
}
MatrixInver(AB, ref b);

return true;
}


转自:http://young.xh.blog.163.com/blog/static/956253200761310247675/
该博客值得学习
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值