forked from v.moiseev/distributed-computing
48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
namespace MatrixCalculator;
|
|
|
|
internal class DefAlg
|
|
{
|
|
internal static int[,] MultiplyMatrices(int[,] matrix1, int[,] matrix2)
|
|
{
|
|
int rows1 = matrix1.GetLength(0);
|
|
int cols1 = matrix1.GetLength(1);
|
|
int rows2 = matrix2.GetLength(0);
|
|
int cols2 = matrix2.GetLength(1);
|
|
|
|
if (cols1 != rows2)
|
|
{
|
|
throw new Exception("Матрицы не могут быть перемножены: количество столбцов первой матрицы не равно количеству строк второй матрицы.");
|
|
}
|
|
|
|
int[,] result = new int[rows1, cols2];
|
|
|
|
for (int i = 0; i < rows1; i++)
|
|
{
|
|
for (int j = 0; j < cols2; j++)
|
|
{
|
|
for (int k = 0; k < cols1; k++)
|
|
{
|
|
result[i, j] += matrix1[i, k] * matrix2[k, j];
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
internal static void PrintMatrix(int[,] matrix)
|
|
{
|
|
int rows = matrix.GetLength(0);
|
|
int cols = matrix.GetLength(1);
|
|
|
|
for (int i = 0; i < rows; i++)
|
|
{
|
|
for (int j = 0; j < cols; j++)
|
|
{
|
|
Console.Write(matrix[i, j] + " ");
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
}
|