对于两个N阶矩阵的乘法,可以使用蛮力法,三层循环即可实现,也可以使用分治法实现。
原数据结构:
public class Matrix
{
public Matrix(int N)
{
this.n = N;
m = new int[n, n];
sx = 0;
sy = 0;
ex = N - 1;
ey = N - 1;
}
public int[,] m;
public int n;
//position.start and end coordinate
public int sx;
public int sy;
public int ex;
public int ey;
public void init()
{
Random r = new Random();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
m[i, j] = r.Next(n);
}
}
}
}
三层循环:
public static Matrix SquareMatrixMultiply(Matrix a, Matrix b)
{
int tmp = 0, n;
n = a.n;
Matrix c = new Matrix(n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{