-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiply.cs
More file actions
49 lines (42 loc) · 1022 Bytes
/
MatrixMultiply.cs
File metadata and controls
49 lines (42 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
class Program
{
static void Main()
{
var matrixA = new int[,] {{-1,4,3},{5,2,3},{5,2,3}};
var matrixB = new int[,] {{1,2},{3,4},{5,6}};
MatrixMultiply(matrixA, matrixB);
}
static int[,] MatrixMultiply(int[,] matrixA, int[,] matrixB)
{
var rowsA = matrixA.GetLength(0);
var columnsA = matrixA.GetLength(1);
var rowsB = matrixB.GetLength(0);
var columnsB = matrixB.GetLength(1);
if (columnsA != rowsB)
{
throw new Exception("Columns of A don't match rows of B");
}
var matrixR = new int[rowsA,columnsB];
for (int ii = 0; ii < rowsA; ii++)
{
for (int jj = 0; jj < columnsB; jj++)
{
matrixR[ii,jj] = SumProducts(matrixA, matrixB, ii, jj);
Console.Write(matrixR[ii,jj] + "\t");
}
Console.WriteLine();
}
return matrixR;
}
static int SumProducts(int[,] a, int [,] b, int row, int column)
{
var sum = 0;
var length = a.GetLength(1);
for (int ii = 0; ii < length; ii++)
{
sum += a[row,ii] * b[ii,column];
}
return sum;
}
}