Created
October 15, 2020 16:06
-
-
Save JoshLmao/b95912026d81d59558d87caac1fe58e7 to your computer and use it in GitHub Desktop.
Matrix Multiplication within C# with comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MatrixMultiplication | |
{ | |
/// <summary> | |
/// Multiplies matrices together using 2d arrays | |
/// Uses a method to easily send a "thread index" which represents the position in the Z matrix. | |
/// </summary> | |
class MatrixMultiplication | |
{ | |
/// <summary> | |
/// Matrix A to multiply | |
/// </summary> | |
private static int[,] MatrixA; | |
/// <summary> | |
/// Matrix B to multiply | |
/// </summary> | |
private static int[,] MatrixB; | |
/// <summary> | |
/// Resulting Matrix after multiplying A and B | |
/// </summary> | |
private static int[,] MatrixX; | |
static void Main(string[] args) | |
{ | |
/// Declare Matrix a and it's size | |
MatrixA = new int[4, 3] | |
{ | |
{ 0, 1, 2 }, | |
{ 3, 4, 5 }, | |
{ 6, 7, 8 }, | |
{ 9, 0, 1 } | |
}; | |
/// Declare size and values of Matrix B | |
MatrixB = new int[3, 4] | |
{ | |
{ 0, 1, 2, 3 }, | |
{ 4, 5, 6, 7 }, | |
{ 8, 9, 0, 1 } | |
}; | |
/// Print out A and B | |
Console.WriteLine("Matrix A:"); | |
PrintMatrix(MatrixA); | |
Console.WriteLine("Matrix B:"); | |
PrintMatrix(MatrixB); | |
/// Check if we can multiply matrices by checking if Matrix A columns equals Matrix B rows | |
int xRows, xCols; | |
if (MatrixA.GetLength(1) == MatrixB.GetLength(0)) | |
{ | |
/// https://www.khanacademy.org/math/precalculus/x9e81a4f98389efdf:matrices/x9e81a4f98389efdf:multiplying-matrices-by-matrices/v/multiplying-a-matrix-by-a-matrix#:~:text=Matrix%20multiplication%20is%20only%20valid,columns%20of%20the%20second%20matrix. | |
/// x will have the number of rows of the first matrix and the number of columns of the second matrix. | |
/// | |
// Then set X rows to A rows | |
xRows = MatrixA.GetLength(0); | |
// Set X cols to B cols | |
xCols = MatrixB.GetLength(1); | |
} | |
else | |
{ | |
Console.WriteLine("Unable to multiply matrices. Soz"); | |
return; | |
} | |
/// Init final Matrix X to with its final X and Y | |
MatrixX = new int[xRows, xCols]; | |
/// Count of how many multiplications to work out the dot product of one cell | |
/// Is equal to Matrix A columns or Matrix B rows | |
int dotProductCount = MatrixA.GetLength(1); | |
/// Create index and send off to calculate position | |
int threadIndex = 0; | |
for (int i = 0; i < xRows; i++) | |
{ | |
for (int j = 0; j < xCols; j++) | |
{ | |
MatrixX[i, j] = CalculatePosition(threadIndex, dotProductCount); | |
threadIndex++; | |
} | |
} | |
/// Print out final X matrix | |
Console.WriteLine("Matrix X:"); | |
PrintMatrix(MatrixX); | |
/// Await key input before exiting | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Calculates the value for multiplying matrices together from it's "thread index" | |
/// </summary> | |
/// <param name="index">the "thread index"</param> | |
/// <param name="dotProductCount">Amount of multiplications neeeded to have the dot product</param> | |
/// <returns></returns> | |
static int CalculatePosition(int index, int dotProductCount) | |
{ | |
int xRows = MatrixX.GetLength(0); | |
int xCols = MatrixX.GetLength(1); | |
int col = index % xRows; | |
int row = (index - col) / xCols; | |
Console.WriteLine($"MatrixZ[{row}, {col}] ="); | |
int total = 0; | |
for (int i = 0; i < dotProductCount; i++) | |
{ | |
int a = MatrixA[row, i]; | |
int b = MatrixB[i, col]; | |
total += a * b; | |
Console.WriteLine($"MatrixA[{row}, {i}] {a} * MatrixB[{i}, {col}] {b}"); | |
} | |
Console.WriteLine($"= {total}"); | |
return total; | |
} | |
/// <summary> | |
/// Prints out a matrix (2D array) in the correct formatting with it's values | |
/// </summary> | |
/// <param name="matrix"></param> | |
static void PrintMatrix(int[,] matrix) | |
{ | |
int xCount = matrix.GetLength(0); | |
int yCount = matrix.GetLength(1); | |
for (int i = 0; i < xCount; i++) | |
{ | |
for (int j = 0; j < yCount; j++) | |
{ | |
Console.Write($" {matrix[i, j]} "); | |
} | |
Console.Write(Environment.NewLine); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment