Skip to content

Commit f91b210

Browse files
authored
fix: mpow(2**n) for integer n (#197)
1 parent ec98866 commit f91b210

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/__tests__/matrix/mpow.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ describe('matrix power', () => {
1111
let x = new Matrix(3, 3);
1212
expect(() => x.mpow(-2)).toThrowError();
1313
});
14+
it('Small integer powers', () => {
15+
let m = new Matrix([
16+
[1, 2],
17+
[3, 4],
18+
]);
19+
let mpowByMmul = Matrix.eye(2);
20+
for (let i = 0; i < 10; ++i) {
21+
expect(m.mpow(i)).toStrictEqual(mpowByMmul);
22+
mpowByMmul = mpowByMmul.mmul(m);
23+
}
24+
});
25+
1426
it('A matrix to the power 0 is identity', () => {
1527
expect(
1628
new Matrix([

src/matrix.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ export class AbstractMatrix {
885885
let result = Matrix.eye(this.rows);
886886
let bb = this;
887887
// Note: Don't bit shift. In JS, that would truncate at 32 bits
888-
for (let e = scalar; e > 1; e /= 2) {
888+
for (let e = scalar; e >= 1; e /= 2) {
889889
if ((e & 1) !== 0) {
890890
result = result.mmul(bb);
891891
}

0 commit comments

Comments
 (0)