Skip to content

Commit d061f28

Browse files
committed
lower matrix ordering needs to change
1 parent f0d73a8 commit d061f28

File tree

4 files changed

+136
-2
lines changed

4 files changed

+136
-2
lines changed

jest.config.cjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const testRegex = [
22
'lib/l3/complex/csyrk/__test__/test.ts',
3-
'lib/l3/complex/csyrk/__test__/wasm-test.ts'
3+
'lib/l3/complex/csyrk/__test__/wasm-test.ts',
4+
'lib/utils/__test__/matrix-triangular.test.ts'
45
];
56

67
const collectCoverageFrom = [
@@ -26,7 +27,7 @@ module.exports = {
2627
//testMatch: ['**/__tests__/**/*.[t]s?(x)', '**/?(*.)+(spec|test).[t]s?(x)'],
2728
testRegex,
2829
transform: {
29-
"\\.test\\.ts$" :["ts-jest", {
30+
"\\.test\\.ts$": ["ts-jest", {
3031
compiler: 'typescript',
3132
tsconfig: 'tsconfig.json',
3233
diagnostics: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { transformUpperPackedIdxToColumnRow, transformLowerPackedIdxToColumnRow } from '../transforms';
2+
import { back } from '../transforms';
3+
back;
4+
transformLowerPackedIdxToColumnRow;
5+
6+
describe('coordinate transform testing', function () {
7+
describe('upper packed', () => {
8+
9+
it('n=91*92/2 => col=91 row = 0w = 0', () => {
10+
/**0 1 2 3 4 etc
11+
* 0 1 3 6 10
12+
* . 2 4 7 11
13+
* . . 5 8 12
14+
* . . . 9 13
15+
* . . . . 14
16+
*/
17+
const col = 91;
18+
expect(transformUpperPackedIdxToColumnRow(col * (col + 1) / 2)).toEqual({ col, row: 0 });
19+
expect(transformUpperPackedIdxToColumnRow(col * (col + 1) / 2 - 1)).toEqual({ col: col - 1, row: col - 1 });
20+
});
21+
22+
describe('upper packed', () => {
23+
it('N=5, for idx= 7, 10 14, 0, 3 and 9', () => {
24+
/**
25+
* 0 1 .2 .3 .4
26+
* ----------
27+
* 0 . .. .. ..
28+
* 1 5 .. .. ..
29+
* 2 6 .9 .. ..
30+
* 3 7 10 12 ..
31+
* 4 8 11 13 14
32+
*/
33+
const N = 5;
34+
expect(transformLowerPackedIdxToColumnRow(7, N)).toEqual({ col: 1, row: 3 });
35+
expect(transformLowerPackedIdxToColumnRow(10, N)).toEqual({ col: 2, row: 3 });
36+
expect(transformLowerPackedIdxToColumnRow(14, N)).toEqual({ col: 4, row: 4 });
37+
expect(transformLowerPackedIdxToColumnRow(0, N)).toEqual({ col: 0, row: 0 });
38+
})
39+
});
40+
});
41+
});

lib/utils/isqrt.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
// from https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)
3+
export default function isqrt(n: number): number {
4+
// Xₙ₊₁
5+
let x = n;
6+
7+
// cₙ
8+
let c = 0;
9+
10+
// dₙ which starts at the highest power of four <= n
11+
let d = 1 << 30; // The second-to-top bit is set.
12+
// Same as ((unsigned) INT32_MAX + 1) / 2.
13+
while (d > n)
14+
d >>= 2;
15+
16+
// for dₙ … d₀
17+
while (d != 0) {
18+
if (x >= c + d) { // if Xₘ₊₁ ≥ Yₘ then aₘ = 2ᵐ
19+
x -= c + d; // Xₘ = Xₘ₊₁ - Yₘ
20+
c = (c >> 1) + d; // cₘ₋₁ = cₘ/2 + dₘ (aₘ is 2ᵐ)
21+
}
22+
else {
23+
c >>= 1; // cₘ₋₁ = cₘ/2 (aₘ is 0)
24+
}
25+
d >>= 2; // dₘ₋₁ = dₘ/4
26+
}
27+
return c; // c₋₁
28+
}

lib/utils/transforms.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import isqrt from './isqrt';
2+
3+
/*
4+
0 4 7 9
5+
1 5 8 .
6+
2 6 . .
7+
3 . . .
8+
*/
9+
10+
export function transformUpperPackedIdxToColumnRow(idx: number): { col: number, row: number } {
11+
const col = (isqrt((idx << 3) + 1) - 1) >> 1;
12+
/**
13+
* 0 1 2 3 4
14+
* ----------
15+
* 0 1 3 6 10
16+
* . 2 4 7 11
17+
* . . 5 8 12
18+
* . . . 9 13
19+
* . . . . 14
20+
*/
21+
const row = idx - (col * (col + 1) >> 1);
22+
return { col, row };
23+
}
24+
25+
/*
26+
0 . . .
27+
1 4 . .
28+
2 5 7 .
29+
3 6 8 9
30+
*/
31+
32+
export function transformLowerPackedIdxToColumnRow(idx: number, N: number): { col: number, row: number } {
33+
const b = ((N << 1) + 1);
34+
const bb = b * b;
35+
const ac4 = idx << 3;
36+
const D = bb - ac4;
37+
const col = (b - Math.sqrt(D)) >> 1;
38+
/**
39+
* 0 1 .2 .3 .4
40+
* ----------
41+
* 0 . .. .. ..
42+
* 1 5 .. .. ..
43+
* 2 6 .9 .. ..
44+
* 3 7 10 12 ..
45+
* 4 8 11 13 14
46+
*/
47+
const base = -col * (col - b) >> 1;
48+
const row = idx - base + col;
49+
return { col, row };
50+
}
51+
52+
53+
54+
export function back(N: number, r: number): number {
55+
const b = ((N << 1) + 1);
56+
const bf = (2 * N + 1);
57+
const bb = bf * bf;
58+
//const bb = ((N * N) << 2) + (N << 2) + 1
59+
const ac4 = r << 3;
60+
const D = bb - ac4;
61+
//console.log(`isqrt:${isqrt(D)}, sqrt:${Math.sqrt(D)}, b-isqrt:${b - isqrt(D)}, b-sqrt:${b - Math.sqrt(D)}`);
62+
const col1 = (b - Math.sqrt(D)) >> 1;
63+
return col1;
64+
}

0 commit comments

Comments
 (0)