如何用C#将两个矩阵相乘
本文关键字:两个 何用 | 更新日期: 2023-09-27 17:58:49
如标题所述,Microsoft框架中是否有允许将两个矩阵相乘的库,或者我必须编写自己的方法才能做到这一点?//我现在已经有了答案
第二个问题:我用MultiplyMatrix方法写了这个多类,但它不能像我想的那样工作。有人能帮忙告诉我哪里出错了吗?
class multi
{
public void MultiplyMatrix(double[,] _A, double[,] _B, int _n, int _m, int _r)
{
int n, m, r;
double si;
n = _n;
m = _m;
r = _r;
double[,] A = new double[n, m];
double[,] B = new double[m, r];
double[,] C = new double[n, r];
A = _A;
B = _B;
try
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < r; j++)
{
si = 0;
for (int k = 0; k < m; k++)
{
si += A[i, m + k] + B[k, r + j];
}
C[i, r + j] = si;
}
}
for (int i = 0; i < C.Length; i++)
{
for (int j = 0; j < C.Length; j++)
{
Console.Write(C[i, j]+" ");
if (j % 3 == 0)
Console.WriteLine();
}
}
}
catch (IndexOutOfRangeException) { } // I always get this exception
}
}
我忘了告诉你:我想做一个网络服务来倍增。
感谢:)
2个矩阵的乘积:
public double[,] MultiplyMatrix(double[,] A, double[,] B)
{
int rA = A.GetLength(0);
int cA = A.GetLength(1);
int rB = B.GetLength(0);
int cB = B.GetLength(1);
if (cA != rB)
{
Console.WriteLine("Matrixes can't be multiplied!!");
}
else
{
double temp = 0;
double[,] kHasil = new double[rA, cB];
for (int i = 0; i < rA; i++)
{
for (int j = 0; j < cB; j++)
{
temp = 0;
for (int k = 0; k < cA; k++)
{
temp += A[i, k] * B[k, j];
}
kHasil[i, j] = temp;
}
}
return kHasil;
}
}
虽然没有内置的数学框架可以做到这一点。NET(可以使用XNA的数学库),系统中有一个Matrix
。Windows。媒体命名空间。矩阵结构有一个乘法方法,它取另一个矩阵并输出一个矩阵。
Matrix matrix1 = new Matrix(5, 10, 15, 20, 25, 30);
Matrix matrix2 = new Matrix(2, 4, 6, 8, 10, 12);
// matrixResult is equal to (70,100,150,220,240,352)
Matrix matrixResult = Matrix.Multiply(matrix1, matrix2);
// matrixResult2 is also
// equal to (70,100,150,220,240,352)
Matrix matrixResult2 = matrix1 * matrix2;
这主要用于2D转换:
表示3x3仿射变换用于二维变换的矩阵空间
但如果它适合您的需求,那么就不需要任何第三方库。
尽管您可以通过迭代方法(对于循环)乘以矩阵,但使用线性代数执行计算将清理您的代码,并使您的性能提高数倍!
在nuget-MathNet中有一个免费的库。数字。它使矩阵相乘变得非常容易:
Matrix<double> a = DenseMatrix.OfArray(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } });
Matrix<double> b = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 } });
Matrix<double> result = a * b;
它没有依赖关系,可以在.net core 2.0中使用,这使它成为避免迭代矩阵乘法技术并利用线性代数的绝佳选择。
没有内置任何内容。NET。你必须自己写乘法,或者使用一些第三方库。我在博客上写了一种实现这一点的方法,比较了两种不同的实现:一种是标准的天真算法,另一种是使用不安全代码。
CSML-C#矩阵库-是一个用于数值线性代数的紧凑而轻量级的包。实现了许多从Matlab、Scilab和Co.已知的矩阵运算。看看这个!
没有这样的内置库。除非你使用的是XNA,否则它有一个Matrix
类,尽管它是有限的,是为3D游戏设计的。
有许多的矩阵库。NET。
namespace matrix_multiplication
{
class Program
{
static void Main(string[] args)
{
int i, j;
int[,] a = new int[2, 2];
Console.WriteLine("Enter no for 2*2 matrix");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
a[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("First matrix is:");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(a[i,j]+"'t");
}
Console.WriteLine();
}
int[,] b = new int[2, 2];
Console.WriteLine("Enter no for 2*2 matrix");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("second matrix is:");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(b[i, j] + "'t");
}
Console.WriteLine();
}
Console.WriteLine("Matrix multiplication is:");
int[,] c = new int[2, 2];
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
c[i,j]=0;
for (int k = 0; k < 2; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(c[i, j]+"'t");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
输出
输入2*2矩阵的编号
8
7
6
0
第一个矩阵是:
8 7
6 0
输入2*2矩阵的编号
4
3
2
1
第二个矩阵是:
4 3
2 1
矩阵乘法是:
46 31
24 18
下面是将int[3,4]矩阵与int[4,3]矩阵相乘的方法,它的时间复杂度为O(n立方体)或三次时间
班级计划{static void Main(string[]args){
MultiplyMatrix();
}
static void MultiplyMatrix()
{
int[,] metrix1 = new int[3,4] { { 1, 2,3,2 }, { 3, 4,5,6 }, { 5, 6,8,4 } };
int[,] metrix2 = new int[4, 3] { { 2, 5, 3 }, { 4, 5, 1 }, { 8, 7, 9 }, { 3, 7, 2 } };
int[,] metrixMultplied = new int[3, 3];
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
for(int i=0;i<4;i++)
{
metrixMultplied[row, col] = metrixMultplied[row, col] + metrix1[row, i] * metrix2[i, col];
}
Console.Write(metrixMultplied[row, col] + ", ");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
这是我的代码:4*4矩阵
for (int i = 0; i < 4; i++)
{
int column = 0;
while (column < 4)
{
int count = 0;
for (int j = 0; j < 4; j++)
{
matrixResult[i, column] += Convert.ToInt32(matrixR[i, j] * matrixT[count, column]);
count = count + 1;
}
column = column + 1;
}
}
如果您有一个助手来生成、迭代和填充int[,]
矩阵,如下所示:
public class VisitMatrix{
public static int[,] execute(int rows, int columns, Func<int, int, int> fn)
{
int[,] result = new int[rows, columns];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
result[i, j] = fn(i, j);
}
}
return result;
}
}
现在矩阵乘法和做(假设m1.GetLength(1) == m2.GetLength(0)
)一样微不足道:
public class MultiplyMatrices{
public static int[,] execute(int[,] m1, int[,] m2, int modulus = 10)
{
return VisitMatrix.execute(m1.GetLength(0), m2.GetLength(1), (i, j) =>
Enumerable.Range(0, m1.GetLength(1)-1)
.Select(k => m1[i, k] * m2[k, j])
.Aggregate(0, (a, b) => a + b, e => e % modulus)
}
}