@thi.ng/tensors 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/CHANGELOG.md +45 -1
  2. package/README.md +93 -34
  3. package/abs.d.ts +1 -29
  4. package/abs.js +2 -11
  5. package/add.d.ts +4 -35
  6. package/add.js +2 -11
  7. package/addn.d.ts +1 -33
  8. package/addn.js +2 -11
  9. package/api.d.ts +138 -15
  10. package/broadcast.d.ts +24 -0
  11. package/broadcast.js +54 -0
  12. package/clamp.d.ts +4 -38
  13. package/clamp.js +2 -11
  14. package/clampn.d.ts +1 -37
  15. package/clampn.js +2 -11
  16. package/cos.d.ts +1 -29
  17. package/cos.js +2 -11
  18. package/defopn.d.ts +4 -7
  19. package/defopn.js +17 -7
  20. package/defoprt.d.ts +5 -7
  21. package/defoprt.js +21 -11
  22. package/defoprtt.d.ts +7 -7
  23. package/defoprtt.js +30 -26
  24. package/defopt.d.ts +4 -7
  25. package/defopt.js +17 -20
  26. package/defoptn.d.ts +4 -7
  27. package/defoptn.js +17 -20
  28. package/defoptnn.d.ts +4 -7
  29. package/defoptnn.js +20 -16
  30. package/defoptt.d.ts +5 -6
  31. package/defoptt.js +36 -27
  32. package/defopttt.d.ts +5 -7
  33. package/defopttt.js +43 -37
  34. package/diagonal.d.ts +16 -0
  35. package/diagonal.js +18 -0
  36. package/div.d.ts +4 -35
  37. package/div.js +2 -11
  38. package/divn.d.ts +1 -33
  39. package/divn.js +2 -11
  40. package/dot.d.ts +4 -26
  41. package/dot.js +3 -12
  42. package/errors.d.ts +2 -0
  43. package/errors.js +3 -0
  44. package/exp.d.ts +1 -29
  45. package/exp.js +2 -11
  46. package/exp2.d.ts +1 -29
  47. package/exp2.js +2 -11
  48. package/filtered-indices.d.ts +34 -0
  49. package/filtered-indices.js +17 -0
  50. package/identity.d.ts +7 -0
  51. package/identity.js +3 -2
  52. package/index.d.ts +6 -0
  53. package/index.js +6 -0
  54. package/log.d.ts +1 -29
  55. package/log.js +2 -11
  56. package/log2.d.ts +1 -29
  57. package/log2.js +2 -11
  58. package/mag.d.ts +5 -0
  59. package/magsq.d.ts +1 -25
  60. package/magsq.js +3 -12
  61. package/max.d.ts +5 -30
  62. package/max.js +2 -11
  63. package/maxn.d.ts +1 -33
  64. package/maxn.js +2 -11
  65. package/min.d.ts +5 -30
  66. package/min.js +2 -11
  67. package/minn.d.ts +1 -33
  68. package/minn.js +2 -11
  69. package/mul.d.ts +4 -35
  70. package/mul.js +2 -11
  71. package/mulm.d.ts +1 -1
  72. package/mulm.js +21 -20
  73. package/muln.d.ts +1 -33
  74. package/muln.js +2 -11
  75. package/mulv.d.ts +1 -1
  76. package/mulv.js +16 -15
  77. package/normalize.d.ts +9 -1
  78. package/package.json +48 -4
  79. package/pow.d.ts +5 -30
  80. package/pow.js +2 -11
  81. package/pown.d.ts +1 -33
  82. package/pown.js +2 -11
  83. package/product.d.ts +1 -25
  84. package/product.js +5 -12
  85. package/rand-distrib.d.ts +13 -10
  86. package/rand-distrib.js +55 -23
  87. package/range.d.ts +20 -0
  88. package/range.js +28 -0
  89. package/relu.d.ts +1 -29
  90. package/relu.js +2 -11
  91. package/relun.d.ts +1 -33
  92. package/relun.js +2 -11
  93. package/select.d.ts +6 -6
  94. package/select.js +26 -23
  95. package/set.d.ts +1 -6
  96. package/set.js +3 -11
  97. package/setn.d.ts +1 -6
  98. package/setn.js +3 -11
  99. package/sigmoid.d.ts +1 -29
  100. package/sigmoid.js +2 -11
  101. package/sin.d.ts +1 -29
  102. package/sin.js +2 -11
  103. package/softmax.d.ts +1 -1
  104. package/softplus.d.ts +1 -33
  105. package/softplus.js +2 -11
  106. package/sqrt.d.ts +1 -29
  107. package/sqrt.js +2 -11
  108. package/step.d.ts +1 -33
  109. package/step.js +2 -11
  110. package/sub.d.ts +4 -35
  111. package/sub.js +2 -11
  112. package/subn.d.ts +1 -33
  113. package/subn.js +2 -11
  114. package/sum.d.ts +1 -25
  115. package/sum.js +5 -12
  116. package/svd.d.ts +33 -0
  117. package/svd.js +246 -0
  118. package/swap.d.ts +26 -0
  119. package/swap.js +15 -0
  120. package/tan.d.ts +1 -29
  121. package/tan.js +2 -11
  122. package/tanh.d.ts +1 -29
  123. package/tanh.js +2 -11
  124. package/tensor.d.ts +6 -1
  125. package/tensor.js +58 -21
  126. package/top.d.ts +2 -6
package/tanh.d.ts CHANGED
@@ -5,33 +5,5 @@
5
5
  * @param out - output tensor
6
6
  * @param a - input tensor
7
7
  */
8
- export declare const tanh: import("./api.js").MultiTensorOpT<number>;
9
- /**
10
- * Same as {@link tanh} for 1D tensors.
11
- *
12
- * @param out - output tensor
13
- * @param a - input tensor
14
- */
15
- export declare const tanh1: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
16
- /**
17
- * Same as {@link tanh} for 2D tensors.
18
- *
19
- * @param out - output tensor
20
- * @param a - input tensor
21
- */
22
- export declare const tanh2: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
23
- /**
24
- * Same as {@link tanh} for 3D tensors.
25
- *
26
- * @param out - output tensor
27
- * @param a - input tensor
28
- */
29
- export declare const tanh3: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
30
- /**
31
- * Same as {@link tanh} for 4D tensors.
32
- *
33
- * @param out - output tensor
34
- * @param a - input tensor
35
- */
36
- export declare const tanh4: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
8
+ export declare const tanh: import("./api.js").MultiTensorOpImpl<import("./api.js").TensorOpT<number>>;
37
9
  //# sourceMappingURL=tanh.d.ts.map
package/tanh.js CHANGED
@@ -1,14 +1,5 @@
1
1
  import { defOpT } from "./defopt.js";
2
- const [a, b, c, d, e] = defOpT(Math.tanh);
3
- const tanh = a;
4
- const tanh1 = b;
5
- const tanh2 = c;
6
- const tanh3 = d;
7
- const tanh4 = e;
2
+ const tanh = defOpT(Math.tanh);
8
3
  export {
9
- tanh,
10
- tanh1,
11
- tanh2,
12
- tanh3,
13
- tanh4
4
+ tanh
14
5
  };
package/tensor.d.ts CHANGED
@@ -14,11 +14,13 @@ export declare abstract class ATensor<T = number> implements ITensor<T> {
14
14
  get orderedShape(): number[];
15
15
  get orderedStride(): number[];
16
16
  abstract [Symbol.iterator](): IterableIterator<T>;
17
+ broadcast<S extends Shape>(shape: S, stride: S): ShapeTensor<S, T>;
17
18
  copy(): typeof this;
18
19
  empty(storage?: ITensorStorage<T>): typeof this;
19
20
  equiv(o: any): boolean;
20
21
  eqDelta(o: ITensor<T>, eps?: number): boolean;
21
22
  abstract index(pos: NumericArray): number;
23
+ position(index: number): any[];
22
24
  abstract get(pos: NumericArray): T;
23
25
  abstract set(pos: NumericArray, v: T): this;
24
26
  hi(pos: NumericArray): this;
@@ -42,6 +44,7 @@ export declare class Tensor1<T = number> extends ATensor<T> {
42
44
  get order(): number[];
43
45
  get length(): number;
44
46
  index([x]: NumericArray): number;
47
+ position(index: number): number[];
45
48
  get([x]: NumericArray): T;
46
49
  set([x]: NumericArray, v: T): this;
47
50
  pick([x]: NumericArray): Tensor1<T>;
@@ -122,6 +125,8 @@ export declare function tensorFromArray<T extends NumType, N extends Nested<numb
122
125
  * @param opts
123
126
  */
124
127
  export declare function tensorFromArray<N extends Nested<string>>(data: N, opts?: TensorFromArrayOpts<"str", string>): NestedTensor<N, string>;
125
- export declare const shapeToStride: (shape: NumericArray) => any[];
128
+ export declare const zeroes: <S extends Shape>(shape: S, type?: NumType, storage?: ITensorStorage<number>) => ShapeTensor<S, number>;
129
+ export declare const ones: <S extends Shape>(shape: S, type?: NumType, storage?: ITensorStorage<number>) => ShapeTensor<S, number>;
130
+ export declare const shapeToStride: (shape: number[]) => any[];
126
131
  export declare const strideOrder: (strides: number[]) => number[];
127
132
  //# sourceMappingURL=tensor.d.ts.map
package/tensor.js CHANGED
@@ -29,6 +29,16 @@ class ATensor {
29
29
  get orderedStride() {
30
30
  return swizzle(this.order)(this.stride);
31
31
  }
32
+ broadcast(shape, stride) {
33
+ return new TENSOR_IMPLS[shape.length](
34
+ this.type,
35
+ this.storage,
36
+ this.data,
37
+ shape,
38
+ stride,
39
+ this.offset
40
+ );
41
+ }
32
42
  copy() {
33
43
  return new this.constructor(
34
44
  this.type,
@@ -54,6 +64,16 @@ class ATensor {
54
64
  eqDelta(o, eps = 1e-6) {
55
65
  return this === o || equiv(this.shape, o.shape) && _eqDelta([...this], [...o], this.length, eps);
56
66
  }
67
+ position(index) {
68
+ const { order, stride } = this;
69
+ index -= this.offset;
70
+ const idx = order.map((o) => {
71
+ const i = ~~(index / stride[o]);
72
+ index -= i * stride[o];
73
+ return i;
74
+ });
75
+ return swizzle(order)(idx);
76
+ }
57
77
  hi(pos) {
58
78
  return new this.constructor(
59
79
  this.type,
@@ -139,11 +159,11 @@ class Tensor1 extends ATensor {
139
159
  *[Symbol.iterator]() {
140
160
  let {
141
161
  data,
142
- length,
162
+ shape: [sx],
143
163
  stride: [tx],
144
164
  offset
145
165
  } = this;
146
- for (; length-- > 0; offset += tx) yield data[offset];
166
+ for (; sx-- > 0; offset += tx) yield data[offset];
147
167
  }
148
168
  get dim() {
149
169
  return 1;
@@ -157,6 +177,9 @@ class Tensor1 extends ATensor {
157
177
  index([x]) {
158
178
  return this.offset + x * this.stride[0];
159
179
  }
180
+ position(index) {
181
+ return [~~((index - this.offset) / this.stride[0])];
182
+ }
160
183
  get([x]) {
161
184
  return this.data[this.offset + x * this.stride[0]];
162
185
  }
@@ -211,8 +234,9 @@ class Tensor2 extends ATensor {
211
234
  shape: [sx, sy],
212
235
  stride: [tx, ty]
213
236
  } = this;
214
- for (let ox = this.offset, x = 0; x < sx; x++, ox += tx) {
215
- for (let y = 0; y < sy; y++) {
237
+ let ox, x, y;
238
+ for (ox = this.offset, x = 0; x < sx; x++, ox += tx) {
239
+ for (y = 0; y < sy; y++) {
216
240
  yield data[ox + y * ty];
217
241
  }
218
242
  }
@@ -246,8 +270,9 @@ class Tensor2 extends ATensor {
246
270
  stride: [tx, ty]
247
271
  } = this;
248
272
  const n = min(this.length, newLength);
249
- for (let ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
250
- for (let y = 0; y < sy && i < n; y++, i++) {
273
+ let ox, x, y, i;
274
+ for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
275
+ for (y = 0; y < sy && i < n; y++, i++) {
251
276
  newData[i] = data[ox + y * ty];
252
277
  }
253
278
  }
@@ -273,9 +298,10 @@ class Tensor3 extends ATensor {
273
298
  shape: [sx, sy, sz],
274
299
  stride: [tx, ty, tz]
275
300
  } = this;
276
- for (let ox = this.offset, x = 0; x < sx; x++, ox += tx) {
277
- for (let oy = ox, y = 0; y < sy; y++, oy += ty) {
278
- for (let z = 0; z < sz; z++) {
301
+ let ox, oy, x, y, z;
302
+ for (ox = this.offset, x = 0; x < sx; x++, ox += tx) {
303
+ for (oy = ox, y = 0; y < sy; y++, oy += ty) {
304
+ for (z = 0; z < sz; z++) {
279
305
  yield data[oy + z * tz];
280
306
  }
281
307
  }
@@ -307,9 +333,10 @@ class Tensor3 extends ATensor {
307
333
  stride: [tx, ty, tz]
308
334
  } = this;
309
335
  const n = min(this.length, newLength);
310
- for (let ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
311
- for (let oy = ox, y = 0; y < sy; y++, oy += ty) {
312
- for (let z = 0; z < sz && i < n; z++, i++) {
336
+ let ox, oy, x, y, z, i;
337
+ for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
338
+ for (oy = ox, y = 0; y < sy; y++, oy += ty) {
339
+ for (z = 0; z < sz && i < n; z++, i++) {
313
340
  newData[i] = data[oy + z * tz];
314
341
  }
315
342
  }
@@ -337,10 +364,11 @@ class Tensor4 extends ATensor {
337
364
  stride: [tx, ty, tz, tw],
338
365
  offset
339
366
  } = this;
340
- for (let ox = offset, x = 0; x < sx; x++, ox += tx) {
341
- for (let oy = ox, y = 0; y < sy; y++, oy += ty) {
342
- for (let oz = oy, z = 0; z < sz; z++, oz += tz) {
343
- for (let w = 0; w < sw; w++) {
367
+ let ox, oy, oz, x, y, z, w;
368
+ for (ox = offset, x = 0; x < sx; x++, ox += tx) {
369
+ for (oy = ox, y = 0; y < sy; y++, oy += ty) {
370
+ for (oz = oy, z = 0; z < sz; z++, oz += tz) {
371
+ for (w = 0; w < sw; w++) {
344
372
  yield data[oz + w * tw];
345
373
  }
346
374
  }
@@ -373,10 +401,11 @@ class Tensor4 extends ATensor {
373
401
  stride: [tx, ty, tz, tw]
374
402
  } = this;
375
403
  const n = min(this.length, newLength);
376
- for (let ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
377
- for (let oy = ox, y = 0; y < sy; y++, oy += ty) {
378
- for (let oz = oy, z = 0; z < sz; z++, oz += tz) {
379
- for (let w = 0; w < sw && i < n; w++, i++) {
404
+ let ox, oy, oz, x, y, z, w, i;
405
+ for (ox = this.offset, i = 0, x = 0; x < sx; x++, ox += tx) {
406
+ for (oy = ox, y = 0; y < sy; y++, oy += ty) {
407
+ for (oz = oy, z = 0; z < sz; z++, oz += tz) {
408
+ for (w = 0; w < sw && i < n; w++, i++) {
380
409
  newData[i] = data[oz + w * tw];
381
410
  }
382
411
  }
@@ -445,6 +474,12 @@ function tensorFromArray(data, opts) {
445
474
  storage: opts?.storage
446
475
  });
447
476
  }
477
+ const zeroes = (shape, type = "num", storage) => tensor(type, shape, { storage });
478
+ const ones = (shape, type = "num", storage) => {
479
+ const res = tensor(type, shape, { storage });
480
+ res.data.fill(1);
481
+ return res;
482
+ };
448
483
  const shapeToStride = (shape) => {
449
484
  const n = shape.length;
450
485
  const stride = new Array(n);
@@ -510,8 +545,10 @@ export {
510
545
  Tensor2,
511
546
  Tensor3,
512
547
  Tensor4,
548
+ ones,
513
549
  shapeToStride,
514
550
  strideOrder,
515
551
  tensor,
516
- tensorFromArray
552
+ tensorFromArray,
553
+ zeroes
517
554
  };
package/top.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import type { Maybe } from "@thi.ng/api";
2
+ import type { MultiTensorOpImpl } from "./api.js";
2
3
  /**
3
4
  * Specialized / optimized version of
4
5
  * [`thi.ng/defmulti`](https://thi.ng/defmulti) for tensor operations. Uses
@@ -7,10 +8,5 @@ import type { Maybe } from "@thi.ng/api";
7
8
  *
8
9
  * @param dispatch - arg index (default: 1)
9
10
  */
10
- export declare const top: <T extends Function>(dispatch?: number, fallback?: T, ...optimized: Maybe<T>[]) => {
11
- (...args: any[]): any;
12
- add(dim: number, fn: T): T;
13
- default(fn: T): T;
14
- impl(dim?: number): Maybe<T>;
15
- };
11
+ export declare const top: <T extends Function>(dispatch?: number, fallback?: T, ...optimized: Maybe<T>[]) => MultiTensorOpImpl<T>;
16
12
  //# sourceMappingURL=top.d.ts.map