Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Maths/MatrixExponentiationRecursive.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const MatrixExponentiationRecursive = (mat, m) => {
return MatMult(tmp, mat)
} else {
// tmp = mat ^ m/2
const tmp = MatrixExponentiationRecursive(mat, m >> 1)
const tmp = MatrixExponentiationRecursive(mat, Math.floor(m / 2))
// return tmp * tmp = (mat ^ m/2) ^ 2 = mat ^ m
return MatMult(tmp, tmp)
}
Expand Down
21 changes: 21 additions & 0 deletions Maths/test/MatrixExponentiationRecursive.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MatrixExponentiationRecursive } from '../MatrixExponentiationRecursive'

describe('MatrixExponentiationRecursive', () => {
it.each([0, 1, 2, 2 ** 31, 2 ** 32, Number.MAX_SAFE_INTEGER])(
'should preserve the exponent %i without signed 32-bit truncation',
(exponent) => {
const matrix = [
[1, 1],
[0, 1]
]
expect(MatrixExponentiationRecursive(matrix, exponent)).toEqual([
[1, exponent],
[0, 1]
])
expect(matrix).toEqual([
[1, 1],
[0, 1]
])
}
)
})