MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / matrixMultiply

Function matrixMultiply

Maths/Fibonacci.js:117–136  ·  view source on GitHub ↗
(A, B)

Source from the content-addressed store, hash-verified

115// both matrices must have same-type numeric values
116// either both BigInt or both Number
117const matrixMultiply = (A, B) => {
118 A = copyMatrix(A)
119 B = copyMatrix(B)
120 const isBigInt = typeof A[0][0] === 'bigint'
121 const l = A.length
122 const m = B.length
123 const n = B[0].length // Assuming non-empty matrices
124 const C = Array(l)
125 .fill(null)
126 .map(() => Array(n).fill())
127 for (let i = 0; i < l; i++) {
128 for (let j = 0; j < n; j++) {
129 C[i][j] = isBigInt ? 0n : 0
130 for (let k = 0; k < m; k++) {
131 C[i][j] += A[i][k] * B[k][j]
132 }
133 }
134 }
135 return C
136}
137
138/**
139 * Computes A raised to the power n i.e. pow(A, n) where A is a square matrix

Callers 2

matrixExpoFunction · 0.85
FibonacciMatrixExpoFunction · 0.85

Calls 1

copyMatrixFunction · 0.85

Tested by

no test coverage detected