-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareMatrixMultiplication.cpp
More file actions
66 lines (57 loc) · 2.11 KB
/
SquareMatrixMultiplication.cpp
File metadata and controls
66 lines (57 loc) · 2.11 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <string_view>
using namespace std;
vector<vector<int>> matrix_multiplication(vector<vector<int>>& a, vector<vector<int>>& b, size_t n) {
vector<vector<int>> c(n, vector<int>(n, 0));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
c[i][j] += a[i][j] * b[j][i];
}
}
return c;
}
int main()
{
vector<vector<int>> A = { {1, 2}, {3, 2} };
vector<vector<int>> B = { {3, 2}, {1, 1} };
size_t n = 2;
matrix_multiplication(A, B, n);
}
/*Description:
Write a function that accepts two square (NxN) matrices (two dimensional arrays), and returns the product of the two.
Only square matrices will be given.
How to multiply two square matrices:
We are given two matrices, A and B, of size 2x2 (note: tests are not limited to 2x2). Matrix C, the solution,
will be equal to the product of A and B. To fill in cell [0][0] of matrix C,
you need to compute: A[0][0] * B[0][0] + A[0][1] * B[1][0].
More general: To fill in cell [n][m] of matrix C, you need to first multiply the elements in the nth row of matrix A by the elements in the mth column of matrix B,
then take the sum of all those products. This will give you the value for cell [m][n] in matrix C.
Example
A B C
|1 2| x |3 2| = | 5 4|
|3 2| |1 1| |11 8|
Detailed calculation:
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0] = 1*3 + 2*1 = 5
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1] = 1*2 + 2*1 = 4
C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0] = 3*3 + 2*1 = 11
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1] = 3*2 + 2*1 = 8
Link to Wikipedia explaining matrix multiplication (look at the square matrix example): http://en.wikipedia.org/wiki/Matrix_multiplication
A more visual explanation of matrix multiplication: http://matrixmultiplication.xyz
Matrix
Linear Algebra
Algorithms*/
// /> フ
// | n n 彡
// /`ミ_xノ
// / |
// / ヽ ノ
// │ | | |
// / ̄| | | |
// | ( ̄ヽ__ヽ_)__)
// \二つ
// ITS CAT FOR YOU