Skip to content

Commit

Permalink
PR iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Oct 14, 2024
1 parent 7e3e4b3 commit 6df16de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions src/__tests__/matrix/mpow.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect } from 'vitest';

import { Matrix } from '../..';

Expand Down Expand Up @@ -31,17 +31,23 @@ describe('matrix power', () => {
expect(x.size).toBe(1);
expect(x.get(0, 0)).toBeCloseTo(1.5 ** 50);
});
it('Matches scalar exponentiation', () => {
expect(
new Matrix([
[1, 2],
[3, -1],
])
.mpow(21)
.to2DArray(),
).toStrictEqual([
[282475249, 564950498],
[847425747, -282475249],
it('Handles high integer exponent', () => {
let e = Number.MAX_SAFE_INTEGER;
let x = new Matrix([[-1]]).mpow(e);
expect(x.size).toBe(1);
expect(x.get(0, 0)).toBe(-1);
});
it('Exponentiates a small matrix correctly', () => {
let u = new Matrix([
[1, 2],
[3, -1],
]);
let e = 21;
let resultLoop = Matrix.eye(2);
for (let i = 0; i < e; ++i) {
resultLoop = resultLoop.mmul(u);
}
let resultMpow = u.mpow(e);
expect(resultLoop.to2DArray()).toStrictEqual(resultMpow.to2DArray());
});
});
2 changes: 1 addition & 1 deletion src/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ export class AbstractMatrix {
let bb = this;
// Note: Don't bit shift. In JS, that would truncate at 32 bits
for (let e = scalar; e > 1; e /= 2) {
if (e & (1 !== 0)) {
if ((e & 1) !== 0) {
result = result.mmul(bb);
}
bb = bb.mmul(bb);
Expand Down

0 comments on commit 6df16de

Please sign in to comment.