forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix_multiplication.js
151 lines (139 loc) · 3.78 KB
/
Matrix_multiplication.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
matrix multiplication is a binary operation that produces a product matrix
from two matrices . To multiply two matrices, the number of columns of first
matrix should be equal to the number of rows to second matrix.
This program finds the product of two given matrices
*/
function matrix_multiplication(a_1, n1, m1, a_2, n2, m2) {
//res is the result array
let res = new Array(n1);
for (let i = 0; i < n1; i++) {
res[i] = new Array(m2);
}
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m2; j++) {
res[i][j] = 0;
for (let k = 0; k < m1; k++) {
res[i][j] += a_1[i][k] * a_2[k][j]
}
}
}
return res;
}
const readline = require('readline');
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const getLine = (function () {
const getLineGen = (async function* () {
for await (const line of rl) {
yield line;
}
})();
return async () => ((await getLineGen.next()).value);
})();
const main = async () => {
console.log("Enter the number of rows in the first matrix");
let n1 = Number(await getLine());
console.log("Enter the number of columns in the first matrix");
let m1 = Number(await getLine());
console.log("Enter the elements of the first matrix");
//initializing array
let a_1 = new Array(n1);
for (let i = 0; i < n1; i++) {
a_1[i] = new Array(m1);
}
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m1; j++) {
console.log("Enter the element at position " + i + " , " + j);
a_1[i][j] = Number(await getLine());
}
}
console.log("Enter the number of rows in the second matrix");
let n2 = Number(await getLine());
console.log("Enter the number of columns in the second matrix");
let m2 = Number(await getLine());
console.log("Enter the elements of the second matrix");
//initializing array
let a_2 = new Array(n2);
for (let i = 0; i < n2; i++) {
a_2[i] = new Array(m2);
}
for (let i = 0; i < n2; i++) {
for (let j = 0; j < m2; j++) {
console.log("Enter the element at position " + i + " , " + j);
a_2[i][j] = Number(await getLine());
}
}
let res = matrix_multiplication(a_1, n1, m1, a_2, n2, m2);
let s = '';
for (let i = 0; i < n1; i++) {
for (let j = 0; j < m2; j++) {
s += res[i][j] + " ";
}
s += '\n';
}
console.log("Result of matrix multiplication is");
console.log(s);
process.exit(0);
};
main();
/*
Sample I/O:
Enter the number of rows in the first matrix
3
Enter the number of columns in the first matrix
3
Enter the elements of the first matrix
Enter the element at position 0 , 0
1
Enter the element at position 0 , 1
2
Enter the element at position 0 , 2
3
Enter the element at position 1 , 0
4
Enter the element at position 1 , 1
5
Enter the element at position 1 , 2
6
Enter the element at position 2 , 0
7
Enter the element at position 2 , 1
8
Enter the element at position 2 , 2
9
Enter the number of rows in the second matrix
3
Enter the number of columns in the second matrix
4
Enter the elements of the second matrix
Enter the element at position 0 , 0
1
Enter the element at position 0 , 1
2
Enter the element at position 0 , 2
3
Enter the element at position 0 , 3
4
Enter the element at position 1 , 0
5
Enter the element at position 1 , 1
6
Enter the element at position 1 , 2
7
Enter the element at position 1 , 3
8
Enter the element at position 2 , 0
9
Enter the element at position 2 , 1
10
Enter the element at position 2 , 2
11
Enter the element at position 2 , 3
12
Result of matrix multiplication is
38 44 50 56
83 98 113 128
128 152 176 200
Time complexity : O(n^3)
Space complexity : O(n^2)
*/