-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshape.ts
More file actions
132 lines (115 loc) · 2.48 KB
/
Copy pathshape.ts
File metadata and controls
132 lines (115 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
export function getSize(shape: ReadonlyArray<number>, zeroSize = 0) {
if (shape.length === 0) {
return zeroSize;
}
let size = 1;
for (let i = 0; i < shape.length; i += 1) {
size *= shape[i];
}
return size;
}
export function computeStrides(shape: ReadonlyArray<number>) {
const rank = shape.length;
if (rank === 0) {
return [];
}
if (rank === 1) {
if (shape[0] === 1) {
return [0];
} else {
return [1];
}
}
const strides = new Array(rank);
strides[rank - 1] = 1;
if (shape[rank - 1] === 1) {
strides[rank - 1] = 0;
}
let lastStride = 1;
for (let i = rank - 2; i >= 0; i -= 1) {
lastStride = shape[i + 1] * lastStride;
if (shape[i] === 1) {
strides[i] = 0;
} else {
strides[i] = lastStride;
}
}
return strides;
}
export function indexToPos(
index: ReadonlyArray<number>,
strides: ReadonlyArray<number>,
shape?: ReadonlyArray<number>
) {
let ix = 0;
for (let i = 0; i < index.length; i += 1) {
if (shape) {
if (index[i] < 0 || (index[i] >= shape[i] && shape[i] !== 1)) {
throw new Error('Invalid index');
}
}
ix += index[i] * strides[i];
}
return ix;
}
export function posToIndex(
pos: number,
strides: ReadonlyArray<number>
): number[] {
let res = pos;
const rank = strides.length;
const index = new Array(rank);
for (let i = 0; i < index.length; i += 1) {
index[i] = Math.floor(res / strides[i]);
res %= strides[i];
}
return index;
}
export function compareShapes(
a: ReadonlyArray<number>,
b: ReadonlyArray<number>
) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
export function checkEquivShapes(
a: ReadonlyArray<number>,
b: ReadonlyArray<number>
) {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i += 1) {
if (a[i] !== b[i] && a[i] !== 1 && b[i] !== 1) {
return false;
}
}
return true;
}
export function incrementIndex(index: number[], shape: readonly number[]) {
for (let i = index.length - 1; i >= 0; i--) {
index[i] += 1;
if (index[i] >= shape[i]) {
index[i] = 0;
} else {
break;
}
}
}
export function decrementIndex(index: number[], shape: readonly number[]) {
for (let i = index.length - 1; i >= 0; i--) {
index[i] -= 1;
if (index[i] < 0) {
index[i] = shape[i] - 1;
} else {
break;
}
}
}