如何在C#中乘以两个矩阵?

就像在标题中描述的那样,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 } } 

我忘了告诉:我想建立一个网络服务来增加它。

谢谢:)

.NET中没有内置任何东西。 您必须自己编写乘法或使用某些第三方库。 我写了一篇关于两种不同实现的比较方法:标准的朴素算法和使用不安全代码的算法。

虽然在.NET中没有内置的Maths框架(可以使用XNA的Maths库),但System.Windows.Media命名空间中有一个Matrix 。 Matrix结构具有乘法方法 ,该方法接收另一个矩阵并输出矩阵。

 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转换:

表示用于二维空间中的变换的3×3仿射变换矩阵。

但如果它符合您的需求,那么就不需要任何第三方库。

两个矩阵相乘的逻辑如下图所示:

在此处输入图像描述

获取第二个Matrix的第一个Matrix和Column的一行。 相应的术语相乘并相加。 它们存储在由矩阵A的行号和矩阵B的列号指定的位置(矩阵C)。在代码中执行此操作的最简单方法是添加总共3个for循环。 前两个循环模拟行号和列号。 第三个for循环将三个相乘的元素组合在一起并将其结果存储在C Matrix中。 请观看以下代码:

  public void MultiplyMatrix() { if (a.GetLength(1) == b.GetLength(0)) { c = new int[a.GetLength(0), b.GetLength(1)]; for (int i = 0; i < c.GetLength(0); i++) { for (int j = 0; j < c.GetLength(1); j++) { c[i, j] = 0; for (int k = 0; k < a.GetLength(1); k++) // OR k 

来源http://www.Code-Kings.com//

乘以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); double temp = 0; double[,] kHasil = new double[rA, cB]; if (cA != rB) { Console.WriteLine("matrik can't be multiplied !!"); } else { 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; } } 

CSML – C#矩阵库 – 是一种用于数值线性代数的紧凑而轻量级的包。 实现了Matlab,Scilab和Co.公司的许多矩阵运算。 看到了 !

虽然您可以通过迭代方法(for循环)来乘以矩阵,但使用线性代数执行计算将清理代码并为您提供快几倍的性能提升!

nuget提供免费图书馆 – MathNet.Numerics。 它使矩阵的乘法非常容易:

 Matrix a = DenseMatrix.OfArray(new double[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }); Matrix b = DenseMatrix.OfArray(new double[,] { { 1 }, { 2 }, { 3 } }); Matrix result = a * b; 

它没有依赖性,可以在.net core 2.0中使用,使其成为避免迭代矩阵乘法技术并利用线性代数的绝佳选择。

没有这样的内置库。 除非你使用XNA – 它有一个Matrix类,虽然它是有限的并且是为3D游戏设计的。

但是.NET有很多 矩阵 库 。

至少在今天,或者更确切地说从.NET Framework 3.0开始,我猜Matrix.Multiply会做得最好。

 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矩阵输入no

 8 7 6 0 

第一个矩阵是:

 8 7 6 0 

为2 * 2矩阵输入no

 4 3 2 1 

第二个矩阵是:

 4 3 2 1 

矩阵乘法是:

 46 31 24 18 

下面是将int [3,4]矩阵与int [4,3]矩阵相乘的方法,它具有O(n立方)或立方时间的时间复杂度

class Program {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(); } } 

我写了一个小程序来乘以两个3 x 3矩阵,作为我的A-level项目的神经网络的一部分。 希望人们觉得它很有用。

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _3_x_3_Matrix_multiplier { class Program { static void Main(string[] args) { int[,,] matrix = new int[3, 3, 3]; for (int z = 0; z < 2; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { Console.WriteLine("element: {0} , {1}", x, y); matrix[x, y, z] = int.Parse(Console.ReadLine()); } } } for (int xm = 0; xm < 3; xm++) { for (int ym = 0; ym < 3; ym++) { for (int zm = 0; zm < 3; zm++) { matrix[xm, ym, 2] += (matrix[0 + zm, ym, 0] * matrix[xm, 0 + zm, 1]); } } } for (int i = 0; i < 3; i++) { Console.Write("\n"); for (int j = 0; j < 3; j++) { Console.Write(matrix[j, i, 2] + " "); } } 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; } 

}