-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapes.ts
More file actions
295 lines (265 loc) · 5.46 KB
/
Copy pathshapes.ts
File metadata and controls
295 lines (265 loc) · 5.46 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* Shape Type
*/
export type Shape<R extends Rank> = [number, ...number[]] & { length: R; };
/**
* 1st dimentional shape.
*/
export type Shape1D = Shape<1>;
/**
* 2nd dimentional shape.
*/
export type Shape2D = Shape<2>;
/**
* 3th dimentional shape.
*/
export type Shape3D = Shape<3>;
/**
* 4th dimentional shape.
*/
export type Shape4D = Shape<4>;
/**
* 5th dimentional shape.
*/
export type Shape5D = Shape<5>;
/**
* 6th dimentional shape.
*/
export type Shape6D = Shape<6>;
/**
* Rank Types.
*/
export enum Rank {
/**
* Scalar (magnitude only).
*/
R1 = 1,
/**
* Vector (magnitude and direction).
*/
R2 = 2,
/**
* Matrix (table of numbers).
*/
R3 = 3,
/**
* 3-Tensor (cube of numbers)
*/
R4 = 4,
/**
* Rank 5 Tensor
*/
R5 = 5,
/**
* Rank 6 Tensor
*/
R6 = 6,
}
/**
* Array Map Types.
*/
export type ArrayMap =
| Array1D
| Array2D
| Array3D
| Array4D
| Array5D
| Array6D;
/**
* 1D Array.
*/
export type Array1D = number[];
/**
* 2D Array.
*/
export type Array2D = number[][];
/**
* 3D Array.
*/
export type Array3D = number[][][];
/**
* 4D Array.
*/
export type Array4D = number[][][][];
/**
* 5D Array.
*/
export type Array5D = number[][][][][];
/**
* 6D Array.
*/
export type Array6D = number[][][][][][];
/**
* Infer the shape of an array.
*
* @example
* ```ts
* inferShape([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]]); // [1, 2, 2, 2]
* ```
*/
export function inferShape(arr: ArrayMap): number[] {
const shape = [];
let elem: ArrayMap | number = arr;
while (Array.isArray(elem)) {
shape.push(elem.length);
elem = elem[0];
}
return shape;
}
/**
* return the length of a shape.
*
* @example
* ```ts
* shapeLength([1, 2, 3, 4]); // 24
* ```
*/
export function shapeLength(shape: Shape<Rank>): number {
let length = 1;
shape.forEach((i) => length *= i);
return length;
}
/**
* convert a shape to a specific rank.
*
* @example
* ```ts
* toShape([1, 2, 3], Rank.R1); // [1]
* toShape([1, 2, 3], Rank.R2); // [1, 2]
* toShape([1, 2, 3], Rank.R3); // [1, 2, 3]
* toShape([1, 2, 3], Rank.R4); // [1, 2, 3, 1]
* ```
*/
export function toShape<R extends Rank>(shape: Shape<Rank>, rank: R): Shape<R> {
if (rank < shape.length) {
const res = new Array(rank).fill(1);
for (let i = 1; i < shape.length + 1; i++) {
if (i < rank) {
res[rank - i] = shape[shape.length - i];
} else {
res[0] *= shape[shape.length - i];
}
}
return res as Shape<R>;
} else if (rank > shape.length) {
const res = new Array(rank).fill(1);
for (let i = 1; i < shape.length + 1; i++) {
res[rank - i] = shape[shape.length - i];
}
return res as Shape<R>;
} else {
return shape as Shape<R>;
}
}
/**
* convert a shape to a 1D shape.
*
* @example
* ```ts
* shapeTo1D([1, 2, 3]); // [1]
* ```
*/
export function shapeTo1D(shape: Shape<Rank>): Shape<1> {
return toShape(shape, Rank.R1);
}
/**
* convert a shape to a 2D shape.
*
* @example
* ```ts
* shapeTo2D([1, 2, 3]); // [1, 2]
* ```
*/
export function shapeTo2D(shape: Shape<Rank>): Shape<2> {
return toShape(shape, Rank.R2);
}
/**
* convert a shape to a 3D shape.
*
* @example
* ```ts
* shapeTo3D([1, 2, 3]); // [1, 2, 3]
* ```
*/
export function shapeTo3D(shape: Shape<Rank>): Shape<3> {
return toShape(shape, Rank.R3);
}
/**
* convert a shape to a 4D shape.
*
* @example
* ```ts
* shapeTo4D([1, 2, 3]); // [1, 2, 3, 1]
* ```
*/
export function shapeTo4D(shape: Shape<Rank>): Shape<4> {
return toShape(shape, Rank.R4);
}
/**
* iterate over a 1D array.
*
* @example
* ```ts
* iterate1D(5, (i) => console.log(i)); // 0, 1, 2, 3, 4
* ```
*/
export function iterate1D(length: number, callback: (i: number) => void): void {
for (let i = 0; i < length; i++) {
callback(i);
}
}
/**
* iterate over a 2D array.
*
* @example
* ```ts
* iterate2D([2, 2], (i, j) => console.log(i, j)); // 0, 0, 0, 1, 1, 0, 1, 1
* ```
*/
export function iterate2D(
shape: Shape2D,
callback: (i: number, j: number) => void,
): void {
iterate1D(shape[0], (i) => iterate1D(shape[1], (j) => callback(i, j)));
}
/**
* iterate over a 3D array.
*
* @example
* ```ts
* iterate3D([2, 2, 2], (i, j, k) => console.log(i, j, k)); // 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1
* ```
*/
export function iterate3D(
shape: Shape3D,
callback: (i: number, j: number, k: number) => void,
): void {
iterate1D(
shape[0],
(i) =>
iterate1D(shape[1], (j) => iterate1D(shape[2], (k) => callback(i, j, k))),
);
}
/**
* iterate over a 4D array.
*
* @example
* ```ts
* iterate4D([2, 2, 2, 2], (i, j, k, l) => console.log(i, j, k, l)); // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0
* ```
*/
export function iterate4D(
shape: Shape4D,
callback: (i: number, j: number, k: number, l: number) => void,
): void {
iterate1D(
shape[0],
(i) =>
iterate1D(
shape[1],
(j) =>
iterate1D(shape[2], (k) =>
iterate1D(shape[3], (l) => callback(i, j, k, l))),
),
);
}