@thi.ng/tensors 0.1.0 → 0.2.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 (122) hide show
  1. package/CHANGELOG.md +26 -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 +119 -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/defopbtt.d.ts +11 -0
  19. package/defopbtt.js +153 -0
  20. package/defopn.d.ts +4 -7
  21. package/defopn.js +17 -7
  22. package/defoprt.d.ts +4 -6
  23. package/defoprt.js +17 -7
  24. package/defoprtt.d.ts +5 -6
  25. package/defoprtt.js +22 -20
  26. package/defopt.d.ts +4 -7
  27. package/defopt.js +17 -20
  28. package/defoptn.d.ts +4 -7
  29. package/defoptn.js +17 -20
  30. package/defoptnn.d.ts +4 -7
  31. package/defoptnn.js +20 -16
  32. package/defoptt.d.ts +5 -6
  33. package/defoptt.js +36 -27
  34. package/defopttt.d.ts +5 -7
  35. package/defopttt.js +43 -37
  36. package/diagonal.d.ts +16 -0
  37. package/diagonal.js +18 -0
  38. package/div.d.ts +4 -35
  39. package/div.js +2 -11
  40. package/divn.d.ts +1 -33
  41. package/divn.js +2 -11
  42. package/dot.d.ts +4 -26
  43. package/dot.js +2 -11
  44. package/errors.d.ts +2 -0
  45. package/errors.js +3 -0
  46. package/exp.d.ts +1 -29
  47. package/exp.js +2 -11
  48. package/exp2.d.ts +1 -29
  49. package/exp2.js +2 -11
  50. package/identity.d.ts +7 -0
  51. package/identity.js +3 -2
  52. package/index.d.ts +3 -0
  53. package/index.js +3 -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 +2 -11
  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 +39 -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 +2 -11
  85. package/rand-distrib.d.ts +13 -10
  86. package/rand-distrib.js +55 -23
  87. package/relu.d.ts +1 -29
  88. package/relu.js +2 -11
  89. package/relun.d.ts +1 -33
  90. package/relun.js +2 -11
  91. package/select.d.ts +6 -6
  92. package/select.js +26 -23
  93. package/set.d.ts +1 -6
  94. package/set.js +3 -11
  95. package/setn.d.ts +1 -6
  96. package/setn.js +3 -11
  97. package/sigmoid.d.ts +1 -29
  98. package/sigmoid.js +2 -11
  99. package/sin.d.ts +1 -29
  100. package/sin.js +2 -11
  101. package/softmax.d.ts +1 -1
  102. package/softplus.d.ts +1 -33
  103. package/softplus.js +2 -11
  104. package/sqrt.d.ts +1 -29
  105. package/sqrt.js +2 -11
  106. package/step.d.ts +1 -33
  107. package/step.js +2 -11
  108. package/sub.d.ts +4 -35
  109. package/sub.js +2 -11
  110. package/subn.d.ts +1 -33
  111. package/subn.js +2 -11
  112. package/sum.d.ts +1 -25
  113. package/sum.js +2 -11
  114. package/svd.d.ts +33 -0
  115. package/svd.js +246 -0
  116. package/tan.d.ts +1 -29
  117. package/tan.js +2 -11
  118. package/tanh.d.ts +1 -29
  119. package/tanh.js +2 -11
  120. package/tensor.d.ts +3 -0
  121. package/tensor.js +45 -21
  122. package/top.d.ts +2 -6
package/clamp.d.ts CHANGED
@@ -1,47 +1,13 @@
1
1
  /**
2
2
  * Componentwise clamps nD tensor `a` to closed interval defined by `[b,c]`.
3
- * Writes result to `out`. If `out` is null, mutates `a`. Multi-method.
3
+ * Writes result to `out`. If `out` is null, creates a new tensor using `a`'s
4
+ * type and storage provider and shape as determined by broadcasting rules (see
5
+ * {@link broadcast} for details).
4
6
  *
5
7
  * @param out - output tensor
6
8
  * @param a - input tensor
7
9
  * @param b - input tensor (min)
8
10
  * @param c - input tensor (max)
9
11
  */
10
- export declare const clamp: import("./api.js").MultiTensorOpTTT<number>;
11
- /**
12
- * Same as {@link clamp} for 1D tensors.
13
- *
14
- * @param out - output tensor
15
- * @param a - input tensor
16
- * @param b - input tensor (min)
17
- * @param c - input tensor (max)
18
- */
19
- export declare const clamp1: import("./api.js").TensorOpTTT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
20
- /**
21
- * Same as {@link clamp} for 2D tensors.
22
- *
23
- * @param out - output tensor
24
- * @param a - input tensor
25
- * @param b - input tensor (min)
26
- * @param c - input tensor (max)
27
- */
28
- export declare const clamp2: import("./api.js").TensorOpTTT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
29
- /**
30
- * Same as {@link clamp} for 3D tensors.
31
- *
32
- * @param out - output tensor
33
- * @param a - input tensor
34
- * @param b - input tensor (min)
35
- * @param c - input tensor (max)
36
- */
37
- export declare const clamp3: import("./api.js").TensorOpTTT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
38
- /**
39
- * Same as {@link clamp} for 4D tensors.
40
- *
41
- * @param out - output tensor
42
- * @param a - input tensor
43
- * @param b - input tensor (min)
44
- * @param c - input tensor (max)
45
- */
46
- export declare const clamp4: import("./api.js").TensorOpTTT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
12
+ export declare const clamp: import("./api.js").TensorOpTTT<number>;
47
13
  //# sourceMappingURL=clamp.d.ts.map
package/clamp.js CHANGED
@@ -1,15 +1,6 @@
1
1
  import { clamp as op } from "@thi.ng/math/interval";
2
2
  import { defOpTTT } from "./defopttt.js";
3
- const [a, b, c, d, e] = defOpTTT(op);
4
- const clamp = a;
5
- const clamp1 = b;
6
- const clamp2 = c;
7
- const clamp3 = d;
8
- const clamp4 = e;
3
+ const clamp = defOpTTT(op);
9
4
  export {
10
- clamp,
11
- clamp1,
12
- clamp2,
13
- clamp3,
14
- clamp4
5
+ clamp
15
6
  };
package/clampn.d.ts CHANGED
@@ -7,41 +7,5 @@
7
7
  * @param b - scalar (min)
8
8
  * @param c - scalar (max)
9
9
  */
10
- export declare const clampN: import("./api.js").MultiTensorOpTNN<number>;
11
- /**
12
- * Same as {@link clampN} for 1D tensors.
13
- *
14
- * @param out - output tensor
15
- * @param a - input tensor
16
- * @param b - scalar (min)
17
- * @param c - scalar (max)
18
- */
19
- export declare const clampN1: import("./api.js").TensorOpTNN<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
20
- /**
21
- * Same as {@link clampN} for 2D tensors.
22
- *
23
- * @param out - output tensor
24
- * @param a - input tensor
25
- * @param b - scalar (min)
26
- * @param c - scalar (max)
27
- */
28
- export declare const clampN2: import("./api.js").TensorOpTNN<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
29
- /**
30
- * Same as {@link clampN} for 3D tensors.
31
- *
32
- * @param out - output tensor
33
- * @param a - input tensor
34
- * @param b - scalar (min)
35
- * @param c - scalar (max)
36
- */
37
- export declare const clampN3: import("./api.js").TensorOpTNN<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
38
- /**
39
- * Same as {@link clampN} for 4D tensors.
40
- *
41
- * @param out - output tensor
42
- * @param a - input tensor
43
- * @param b - scalar (min)
44
- * @param c - scalar (max)
45
- */
46
- export declare const clampN4: import("./api.js").TensorOpTNN<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
10
+ export declare const clampN: import("./api.js").MultiTensorOpImpl<import("./api.js").TensorOpTNN<number>>;
47
11
  //# sourceMappingURL=clampn.d.ts.map
package/clampn.js CHANGED
@@ -1,15 +1,6 @@
1
1
  import { clamp as op } from "@thi.ng/math/interval";
2
2
  import { defOpTNN } from "./defoptnn.js";
3
- const [a, b, c, d, e] = defOpTNN(op);
4
- const clampN = a;
5
- const clampN1 = b;
6
- const clampN2 = c;
7
- const clampN3 = d;
8
- const clampN4 = e;
3
+ const clampN = defOpTNN(op);
9
4
  export {
10
- clampN,
11
- clampN1,
12
- clampN2,
13
- clampN3,
14
- clampN4
5
+ clampN
15
6
  };
package/cos.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 cos: import("./api.js").MultiTensorOpT<number>;
9
- /**
10
- * Same as {@link cos} for 1D tensors.
11
- *
12
- * @param out - output tensor
13
- * @param a - input tensor
14
- */
15
- export declare const cos1: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
16
- /**
17
- * Same as {@link cos} for 2D tensors.
18
- *
19
- * @param out - output tensor
20
- * @param a - input tensor
21
- */
22
- export declare const cos2: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
23
- /**
24
- * Same as {@link cos} for 3D tensors.
25
- *
26
- * @param out - output tensor
27
- * @param a - input tensor
28
- */
29
- export declare const cos3: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
30
- /**
31
- * Same as {@link cos} for 4D tensors.
32
- *
33
- * @param out - output tensor
34
- * @param a - input tensor
35
- */
36
- export declare const cos4: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
8
+ export declare const cos: import("./api.js").MultiTensorOpImpl<import("./api.js").TensorOpT<number>>;
37
9
  //# sourceMappingURL=cos.d.ts.map
package/cos.js CHANGED
@@ -1,14 +1,5 @@
1
1
  import { defOpT } from "./defopt.js";
2
- const [a, b, c, d, e] = defOpT(Math.cos);
3
- const cos = a;
4
- const cos1 = b;
5
- const cos2 = c;
6
- const cos3 = d;
7
- const cos4 = e;
2
+ const cos = defOpT(Math.cos);
8
3
  export {
9
- cos,
10
- cos1,
11
- cos2,
12
- cos3,
13
- cos4
4
+ cos
14
5
  };
package/defopbtt.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { FnU2 } from "@thi.ng/api";
2
+ import type { ITensor } from "./api.js";
3
+ /**
4
+ * Higher order tensor op factory. Takes given `fn` and returns a
5
+ * {@link TensorOpTT}s applying the given function component-wise.
6
+ *
7
+ * @param fn
8
+ * @param dispatch
9
+ */
10
+ export declare const defOpBTT: <T = number>(fn: FnU2<T>) => (out: ITensor<T> | null, a: ITensor<T>, b: ITensor<T>) => ITensor<T>;
11
+ //# sourceMappingURL=defopbtt.d.ts.map
package/defopbtt.js ADDED
@@ -0,0 +1,153 @@
1
+ import { equals } from "@thi.ng/vectors/equals";
2
+ import { broadcast } from "./broadcast.js";
3
+ import { illegalShape } from "./errors.js";
4
+ import { tensor } from "./tensor.js";
5
+ const defOpBTT = (fn) => {
6
+ const f1 = (out, a, b) => {
7
+ !out && (out = a);
8
+ const {
9
+ data: odata,
10
+ offset: oo,
11
+ stride: [txo]
12
+ } = out;
13
+ const {
14
+ data: adata,
15
+ offset: oa,
16
+ shape: [sx],
17
+ stride: [txa]
18
+ } = a;
19
+ const {
20
+ data: bdata,
21
+ offset: ob,
22
+ stride: [txb]
23
+ } = b;
24
+ for (let x = 0; x < sx; x++) {
25
+ odata[oo + x * txo] = fn(adata[oa + x * txa], bdata[ob + x * txb]);
26
+ }
27
+ return out;
28
+ };
29
+ const f2 = (out, a, b) => {
30
+ !out && (out = a);
31
+ const {
32
+ data: odata,
33
+ offset: oo,
34
+ stride: [txo, tyo]
35
+ } = out;
36
+ const {
37
+ data: adata,
38
+ offset: oa,
39
+ shape: [sx, sy],
40
+ stride: [txa, tya]
41
+ } = a;
42
+ const {
43
+ data: bdata,
44
+ offset: ob,
45
+ stride: [txb, tyb]
46
+ } = b;
47
+ let oox, oax, obx;
48
+ for (let x = 0; x < sx; x++) {
49
+ oox = oo + x * txo;
50
+ oax = oa + x * txa;
51
+ obx = ob + x * txb;
52
+ for (let y = 0; y < sy; y++) {
53
+ odata[oox + y * tyo] = fn(
54
+ adata[oax + y * tya],
55
+ bdata[obx + y * tyb]
56
+ );
57
+ }
58
+ }
59
+ return out;
60
+ };
61
+ const f3 = (out, a, b) => {
62
+ !out && (out = a);
63
+ const {
64
+ data: odata,
65
+ offset: oo,
66
+ stride: [txo, tyo, tzo]
67
+ } = out;
68
+ const {
69
+ data: adata,
70
+ offset: oa,
71
+ shape: [sx, sy, sz],
72
+ stride: [txa, tya, tza]
73
+ } = a;
74
+ const {
75
+ data: bdata,
76
+ offset: ob,
77
+ stride: [txb, tyb, tzb]
78
+ } = b;
79
+ let oox, oax, obx, ooy, oay, oby;
80
+ for (let x = 0; x < sx; x++) {
81
+ oox = oo + x * txo;
82
+ oax = oa + x * txa;
83
+ obx = ob + x * txb;
84
+ for (let y = 0; y < sy; y++) {
85
+ ooy = oox + y * tyo;
86
+ oay = oax + y * tya;
87
+ oby = obx + y * tyb;
88
+ for (let z = 0; z < sz; z++) {
89
+ odata[ooy + z * tzo] = fn(
90
+ adata[oay + z * tza],
91
+ bdata[oby + z * tzb]
92
+ );
93
+ }
94
+ }
95
+ }
96
+ return out;
97
+ };
98
+ const f4 = (out, a, b) => {
99
+ !out && (out = a);
100
+ const {
101
+ data: odata,
102
+ offset: oo,
103
+ stride: [txo, tyo, tzo, two]
104
+ } = out;
105
+ const {
106
+ data: adata,
107
+ offset: oa,
108
+ shape: [sx, sy, sz, sw],
109
+ stride: [txa, tya, tza, twa]
110
+ } = a;
111
+ const {
112
+ data: bdata,
113
+ offset: ob,
114
+ stride: [txb, tyb, tzb, twb]
115
+ } = b;
116
+ let oox, oax, obx, ooy, oay, oby, ooz, oaz, obz;
117
+ for (let x = 0; x < sx; x++) {
118
+ oox = oo + x * txo;
119
+ oax = oa + x * txa;
120
+ obx = ob + x * txb;
121
+ for (let y = 0; y < sy; y++) {
122
+ ooy = oox + y * tyo;
123
+ oay = oax + y * tya;
124
+ oby = obx + y * tyb;
125
+ for (let z = 0; z < sz; z++) {
126
+ ooz = ooy + z * tzo;
127
+ oaz = oay + z * tza;
128
+ obz = oby + z * tzb;
129
+ for (let w = 0; w < sw; w++) {
130
+ odata[ooz + w * two] = fn(
131
+ adata[oaz + w * twa],
132
+ bdata[obz + w * twb]
133
+ );
134
+ }
135
+ }
136
+ }
137
+ }
138
+ return out;
139
+ };
140
+ const impls = [, f1, f2, f3, f4];
141
+ return (out, a, b) => {
142
+ const { shape, a: $a, b: $b } = broadcast(a, b);
143
+ if (out) {
144
+ if (!equals(out.shape, shape)) illegalShape(out.shape);
145
+ } else {
146
+ out = tensor(a.type, shape, { storage: a.storage });
147
+ }
148
+ return impls[shape.length](out, $a, $b);
149
+ };
150
+ };
151
+ export {
152
+ defOpBTT
153
+ };
package/defopn.d.ts CHANGED
@@ -1,13 +1,10 @@
1
1
  import type { Fn } from "@thi.ng/api";
2
- import type { MultiTensorOpN, TensorOpN } from "./api.js";
3
- import type { Tensor1, Tensor2, Tensor3, Tensor4 } from "./tensor.js";
2
+ import type { TensorOpN } from "./api.js";
4
3
  /**
5
- * Higher order tensor op factory. Takes given `fn` and returns a 4-tuple of
6
- * {@link TensorOpN}s applying the given function component-wise. The result
7
- * tuple uses this order: `[polymorphic, 1d, 2d, 3d, 4d]`.
4
+ * Higher order tensor op factory. Takes given `fn` and returns a
5
+ * {@link TensorOpN} applying the given function component-wise.
8
6
  *
9
7
  * @param fn
10
- * @param dispatch
11
8
  */
12
- export declare const defOpN: <A = number, B = A>(fn: Fn<A, B>) => [MultiTensorOpN<A, B>, TensorOpN<A, B, Tensor1<B>>, TensorOpN<A, B, Tensor2<B>>, TensorOpN<A, B, Tensor3<B>>, TensorOpN<A, B, Tensor4<B>>];
9
+ export declare const defOpN: <A = number, B = A>(fn: Fn<A, B>) => import("./api.js").MultiTensorOpImpl<TensorOpN<A, B>>;
13
10
  //# sourceMappingURL=defopn.d.ts.map
package/defopn.js CHANGED
@@ -17,8 +17,9 @@ const defOpN = (fn) => {
17
17
  stride: [tx, ty],
18
18
  offset
19
19
  } = out;
20
+ let ox;
20
21
  for (let x = 0; x < sx; x++) {
21
- const ox = offset + x * tx;
22
+ ox = offset + x * tx;
22
23
  for (let y = 0; y < sy; y++) data[ox + y * ty] = fn(a);
23
24
  }
24
25
  return out;
@@ -30,10 +31,11 @@ const defOpN = (fn) => {
30
31
  stride: [tx, ty, tz],
31
32
  offset
32
33
  } = out;
34
+ let ox, oy;
33
35
  for (let x = 0; x < sx; x++) {
34
- const ox = offset + x * tx;
36
+ ox = offset + x * tx;
35
37
  for (let y = 0; y < sy; y++) {
36
- const oy = ox + y * ty;
38
+ oy = ox + y * ty;
37
39
  for (let z = 0; z < sz; z++) data[oy + z * tz] = fn(a);
38
40
  }
39
41
  }
@@ -46,19 +48,27 @@ const defOpN = (fn) => {
46
48
  stride: [tx, ty, tz, tw],
47
49
  offset
48
50
  } = out;
51
+ let ox, oy, oz;
49
52
  for (let x = 0; x < sx; x++) {
50
- const ox = offset + x * tx;
53
+ ox = offset + x * tx;
51
54
  for (let y = 0; y < sy; y++) {
52
- const oy = ox + y * ty;
55
+ oy = ox + y * ty;
53
56
  for (let z = 0; z < sz; z++) {
54
- const oz = oy + z * tz;
57
+ oz = oy + z * tz;
55
58
  for (let w = 0; w < sw; w++) data[oz + w * tw] = fn(a);
56
59
  }
57
60
  }
58
61
  }
59
62
  return out;
60
63
  };
61
- return [top(0, void 0, f1, f2, f3, f4), f1, f2, f3, f4];
64
+ return top(
65
+ 0,
66
+ void 0,
67
+ f1,
68
+ f2,
69
+ f3,
70
+ f4
71
+ );
62
72
  };
63
73
  export {
64
74
  defOpN
package/defoprt.d.ts CHANGED
@@ -1,14 +1,12 @@
1
1
  import type { Fn0, Fn2 } from "@thi.ng/api";
2
- import type { MultiTensorOpRT, TensorOpRT } from "./api.js";
3
- import type { Tensor1, Tensor2, Tensor3, Tensor4 } from "./tensor.js";
2
+ import type { TensorOpRT } from "./api.js";
4
3
  /**
5
4
  * Higher order tensor reduction op factory. Takes given reduction `rfn` and
6
- * `init` function to produce an initial result. Returns a 4-tuple of
7
- * {@link TensorOpRT}s applying the given function component-wise. The result
8
- * tuple uses this order: `[polymorphic, 1d, 2d, 3d]`.
5
+ * `init` function to produce an initial result. Returns a {@link TensorOpRT}
6
+ * applying the given function component-wise.
9
7
  *
10
8
  * @param rfn
11
9
  * @param init
12
10
  */
13
- export declare const defOpRT: <A = number, B = A>(rfn: Fn2<B, A, B>, init: Fn0<B>) => [MultiTensorOpRT<A, B>, TensorOpRT<A, B, Tensor1<A>>, TensorOpRT<A, B, Tensor2<A>>, TensorOpRT<A, B, Tensor3<A>>, TensorOpRT<A, B, Tensor4<A>>];
11
+ export declare const defOpRT: <A = number, B = A>(rfn: Fn2<B, A, B>, init: Fn0<B>) => import("./api.js").MultiTensorOpImpl<TensorOpRT<A, B>>;
14
12
  //# sourceMappingURL=defoprt.d.ts.map
package/defoprt.js CHANGED
@@ -21,8 +21,9 @@ const defOpRT = (rfn, init) => {
21
21
  stride: [tx, ty]
22
22
  } = a;
23
23
  let res = init();
24
+ let ox;
24
25
  for (let x = 0; x < sx; x++) {
25
- const ox = offset + x * tx;
26
+ ox = offset + x * tx;
26
27
  for (let y = 0; y < sy; y++) {
27
28
  res = rfn(res, data[ox + y * ty]);
28
29
  }
@@ -37,10 +38,11 @@ const defOpRT = (rfn, init) => {
37
38
  stride: [tx, ty, tz]
38
39
  } = a;
39
40
  let res = init();
41
+ let ox, oy;
40
42
  for (let x = 0; x < sx; x++) {
41
- const ox = offset + x * tx;
43
+ ox = offset + x * tx;
42
44
  for (let y = 0; y < sy; y++) {
43
- const oy = ox + y * ty;
45
+ oy = ox + y * ty;
44
46
  for (let z = 0; z < sz; z++) {
45
47
  res = rfn(res, data[oy + z * tz]);
46
48
  }
@@ -56,12 +58,13 @@ const defOpRT = (rfn, init) => {
56
58
  stride: [tx, ty, tz, tw]
57
59
  } = a;
58
60
  let res = init();
61
+ let ox, oy, oz;
59
62
  for (let x = 0; x < sx; x++) {
60
- const ox = offset + x * tx;
63
+ ox = offset + x * tx;
61
64
  for (let y = 0; y < sy; y++) {
62
- const oy = ox + y * ty;
65
+ oy = ox + y * ty;
63
66
  for (let z = 0; z < sz; z++) {
64
- const oz = oy + z * tz;
67
+ oz = oy + z * tz;
65
68
  for (let w = 0; w < sw; w++) {
66
69
  res = rfn(res, data[oz + w * tw]);
67
70
  }
@@ -70,7 +73,14 @@ const defOpRT = (rfn, init) => {
70
73
  }
71
74
  return res;
72
75
  };
73
- return [top(0, void 0, f1, f2, f3, f4), f1, f2, f3, f4];
76
+ return top(
77
+ 0,
78
+ void 0,
79
+ f1,
80
+ f2,
81
+ f3,
82
+ f4
83
+ );
74
84
  };
75
85
  export {
76
86
  defOpRT
package/defoprtt.d.ts CHANGED
@@ -1,14 +1,13 @@
1
1
  import type { Fn0, Fn3 } from "@thi.ng/api";
2
- import type { MultiTensorOpRTT, TensorOpRTT } from "./api.js";
3
- import type { Tensor1, Tensor2, Tensor3, Tensor4 } from "./tensor.js";
2
+ import type { TensorOpRTT } from "./api.js";
4
3
  /**
5
4
  * Higher order tensor reduction op factory. Takes given reduction `rfn` and
6
- * `init` function to produce an initial result. Returns a 4-tuple of
7
- * {@link TensorOpRTT}s applying the given function component-wise. The result
8
- * tuple uses this order: `[polymorphic, 1d, 2d, 3d]`.
5
+ * `init` function to produce an initial result. Returns a {@link TensorOpRTT}
6
+ * applying the given function componentwise with broadcasting rules (see
7
+ * {@link broadcast} for details).
9
8
  *
10
9
  * @param rfn
11
10
  * @param init
12
11
  */
13
- export declare const defOpRTT: <A = number, B = A>(rfn: Fn3<B, A, A, B>, init: Fn0<B>) => [MultiTensorOpRTT<A, B>, TensorOpRTT<A, B, Tensor1<A>>, TensorOpRTT<A, B, Tensor2<A>>, TensorOpRTT<A, B, Tensor3<A>>, TensorOpRTT<A, B, Tensor4<A>>];
12
+ export declare const defOpRTT: <A = number, B = A>(rfn: Fn3<B, A, A, B>, init: Fn0<B>) => TensorOpRTT<A, B>;
14
13
  //# sourceMappingURL=defoprtt.d.ts.map
package/defoprtt.js CHANGED
@@ -1,4 +1,4 @@
1
- import { top } from "./top.js";
1
+ import { broadcast } from "./broadcast.js";
2
2
  const defOpRTT = (rfn, init) => {
3
3
  const f1 = (a, b) => {
4
4
  const {
@@ -31,9 +31,10 @@ const defOpRTT = (rfn, init) => {
31
31
  stride: [txb, tyb]
32
32
  } = b;
33
33
  let res = init();
34
+ let oax, obx;
34
35
  for (let x = 0; x < sx; x++) {
35
- const oax = oa + x * txa;
36
- const obx = ob + x * txb;
36
+ oax = oa + x * txa;
37
+ obx = ob + x * txb;
37
38
  for (let y = 0; y < sy; y++) {
38
39
  res = rfn(res, adata[oax + y * tya], bdata[obx + y * tyb]);
39
40
  }
@@ -53,12 +54,13 @@ const defOpRTT = (rfn, init) => {
53
54
  stride: [txb, tyb, tzb]
54
55
  } = b;
55
56
  let res = init();
57
+ let oax, obx, oay, oby;
56
58
  for (let x = 0; x < sx; x++) {
57
- const oax = oa + x * txa;
58
- const obx = ob + x * txb;
59
+ oax = oa + x * txa;
60
+ obx = ob + x * txb;
59
61
  for (let y = 0; y < sy; y++) {
60
- const oay = oax + y * tya;
61
- const oby = obx + y * tyb;
62
+ oay = oax + y * tya;
63
+ oby = obx + y * tyb;
62
64
  for (let z = 0; z < sz; z++) {
63
65
  res = rfn(res, adata[oay + z * tza], bdata[oby + z * tzb]);
64
66
  }
@@ -79,15 +81,16 @@ const defOpRTT = (rfn, init) => {
79
81
  stride: [txb, tyb, tzb, twb]
80
82
  } = b;
81
83
  let res = init();
84
+ let oax, obx, oay, oby, oaz, obz;
82
85
  for (let x = 0; x < sx; x++) {
83
- const oax = oa + x * txa;
84
- const obx = ob + x * txb;
86
+ oax = oa + x * txa;
87
+ obx = ob + x * txb;
85
88
  for (let y = 0; y < sy; y++) {
86
- const oay = oax + y * tya;
87
- const oby = obx + y * tyb;
89
+ oay = oax + y * tya;
90
+ oby = obx + y * tyb;
88
91
  for (let z = 0; z < sz; z++) {
89
- const oaz = oay + z * tza;
90
- const obz = oby + z * tzb;
92
+ oaz = oay + z * tza;
93
+ obz = oby + z * tzb;
91
94
  for (let w = 0; w < sw; w++) {
92
95
  res = rfn(
93
96
  res,
@@ -100,13 +103,12 @@ const defOpRTT = (rfn, init) => {
100
103
  }
101
104
  return res;
102
105
  };
103
- return [
104
- top(0, void 0, f1, f2, f3, f4),
105
- f1,
106
- f2,
107
- f3,
108
- f4
109
- ];
106
+ const impls = [, f1, f2, f3, f4];
107
+ const wrapper = (a, b) => {
108
+ const { shape, a: $a, b: $b } = broadcast(a, b);
109
+ return impls[shape.length]($a, $b);
110
+ };
111
+ return wrapper;
110
112
  };
111
113
  export {
112
114
  defOpRTT
package/defopt.d.ts CHANGED
@@ -1,13 +1,10 @@
1
1
  import type { Fn } from "@thi.ng/api";
2
- import type { MultiTensorOpT, TensorOpT } from "./api.js";
3
- import type { Tensor1, Tensor2, Tensor3, Tensor4 } from "./tensor.js";
2
+ import type { TensorOpT } from "./api.js";
4
3
  /**
5
- * Higher order tensor op factory. Takes given `fn` and returns a 4-tuple of
6
- * {@link TensorOpT}s applying the given function component-wise. The result
7
- * tuple uses this order: `[polymorphic, 1d, 2d, 3d, 4d]`.
4
+ * Higher order tensor op factory. Takes given `fn` and returns a
5
+ * {@link TensorOpT} applying the given function component-wise.
8
6
  *
9
7
  * @param fn
10
- * @param dispatch
11
8
  */
12
- export declare const defOpT: <T = number>(fn: Fn<T, T>, dispatch?: number) => [MultiTensorOpT<T>, TensorOpT<T, T, Tensor1<T>, Tensor1<T>>, TensorOpT<T, T, Tensor2<T>, Tensor2<T>>, TensorOpT<T, T, Tensor3<T>, Tensor3<T>>, TensorOpT<T, T, Tensor4<T>, Tensor4<T>>];
9
+ export declare const defOpT: <T = number>(fn: Fn<T, T>) => import("./api.js").MultiTensorOpImpl<TensorOpT<T>>;
13
10
  //# sourceMappingURL=defopt.d.ts.map