javascript multiply a square matrix

Solutions on MaxInterview for javascript multiply a square matrix by the best coders in the world

showing results for - "javascript multiply a square matrix"
Tim
21 Jan 2019
1// Multiply a square matrix
2
3const squareMatrix = (a, b) => a.map(x => x.map((_, i) => x.reduce((s, e, j) => s + e * b[j][i], 0)));
4
5console.log(squareMatrix([[1, 2], [3, 2]], [[3, 2], [1, 1]]) ); 
6//  Answer: [[5, 4], [11, 8]]
7
8console.log(squareMatrix([[4,5,6], [7,8,9], [10,11,12]], [[3, 2, 1], [1, 1, 9], [12,13,14]]) );
9//  Answer: [ [ 89, 91, 133 ], [ 137, 139, 205 ], [ 185, 187, 277 ] ]
10
11console.log(squareMatrix([[1,2],[3,4]], [[5,6],[7,8]]) );
12// Answer: [ [ 19, 22 ], [ 43, 50 ] ]