@thi.ng/tensors 0.1.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 (124) hide show
  1. package/CHANGELOG.md +46 -0
  2. package/LICENSE +201 -0
  3. package/README.md +307 -0
  4. package/abs.d.ts +37 -0
  5. package/abs.js +14 -0
  6. package/add.d.ts +42 -0
  7. package/add.js +15 -0
  8. package/addn.d.ts +42 -0
  9. package/addn.js +15 -0
  10. package/api.d.ts +137 -0
  11. package/api.js +0 -0
  12. package/clamp.d.ts +47 -0
  13. package/clamp.js +15 -0
  14. package/clampn.d.ts +47 -0
  15. package/clampn.js +15 -0
  16. package/cos.d.ts +37 -0
  17. package/cos.js +14 -0
  18. package/defopn.d.ts +13 -0
  19. package/defopn.js +65 -0
  20. package/defoprt.d.ts +14 -0
  21. package/defoprt.js +77 -0
  22. package/defoprtt.d.ts +14 -0
  23. package/defoprtt.js +113 -0
  24. package/defopt.d.ts +13 -0
  25. package/defopt.js +109 -0
  26. package/defoptn.d.ts +13 -0
  27. package/defoptn.js +109 -0
  28. package/defoptnn.d.ts +13 -0
  29. package/defoptnn.js +109 -0
  30. package/defoptt.d.ts +13 -0
  31. package/defoptt.js +144 -0
  32. package/defopttt.d.ts +13 -0
  33. package/defopttt.js +177 -0
  34. package/div.d.ts +42 -0
  35. package/div.js +15 -0
  36. package/divn.d.ts +42 -0
  37. package/divn.js +15 -0
  38. package/dot.d.ts +31 -0
  39. package/dot.js +17 -0
  40. package/errors.d.ts +15 -0
  41. package/errors.js +9 -0
  42. package/exp.d.ts +37 -0
  43. package/exp.js +14 -0
  44. package/exp2.d.ts +37 -0
  45. package/exp2.js +14 -0
  46. package/format.d.ts +14 -0
  47. package/format.js +37 -0
  48. package/identity.d.ts +4 -0
  49. package/identity.js +11 -0
  50. package/index.d.ts +60 -0
  51. package/index.js +59 -0
  52. package/log.d.ts +37 -0
  53. package/log.js +14 -0
  54. package/log2.d.ts +37 -0
  55. package/log2.js +14 -0
  56. package/mag.d.ts +3 -0
  57. package/mag.js +5 -0
  58. package/magsq.d.ts +31 -0
  59. package/magsq.js +17 -0
  60. package/max.d.ts +37 -0
  61. package/max.js +14 -0
  62. package/maxn.d.ts +42 -0
  63. package/maxn.js +14 -0
  64. package/min.d.ts +37 -0
  65. package/min.js +14 -0
  66. package/minn.d.ts +42 -0
  67. package/minn.js +14 -0
  68. package/mul.d.ts +42 -0
  69. package/mul.js +15 -0
  70. package/mulm.d.ts +12 -0
  71. package/mulm.js +49 -0
  72. package/muln.d.ts +42 -0
  73. package/muln.js +15 -0
  74. package/mulv.d.ts +11 -0
  75. package/mulv.js +39 -0
  76. package/normalize.d.ts +3 -0
  77. package/normalize.js +11 -0
  78. package/package.json +261 -0
  79. package/pow.d.ts +37 -0
  80. package/pow.js +14 -0
  81. package/pown.d.ts +42 -0
  82. package/pown.js +14 -0
  83. package/product.d.ts +31 -0
  84. package/product.js +15 -0
  85. package/rand-distrib.d.ts +49 -0
  86. package/rand-distrib.js +52 -0
  87. package/relu.d.ts +37 -0
  88. package/relu.js +14 -0
  89. package/relun.d.ts +43 -0
  90. package/relun.js +14 -0
  91. package/select.d.ts +91 -0
  92. package/select.js +111 -0
  93. package/set.d.ts +8 -0
  94. package/set.js +14 -0
  95. package/setn.d.ts +8 -0
  96. package/setn.js +14 -0
  97. package/sigmoid.d.ts +39 -0
  98. package/sigmoid.js +15 -0
  99. package/sin.d.ts +37 -0
  100. package/sin.js +14 -0
  101. package/softmax.d.ts +27 -0
  102. package/softmax.js +10 -0
  103. package/softplus.d.ts +48 -0
  104. package/softplus.js +15 -0
  105. package/sqrt.d.ts +37 -0
  106. package/sqrt.js +14 -0
  107. package/step.d.ts +48 -0
  108. package/step.js +14 -0
  109. package/storage.d.ts +3 -0
  110. package/storage.js +32 -0
  111. package/sub.d.ts +42 -0
  112. package/sub.js +15 -0
  113. package/subn.d.ts +42 -0
  114. package/subn.js +15 -0
  115. package/sum.d.ts +31 -0
  116. package/sum.js +15 -0
  117. package/tan.d.ts +37 -0
  118. package/tan.js +14 -0
  119. package/tanh.d.ts +37 -0
  120. package/tanh.js +14 -0
  121. package/tensor.d.ts +127 -0
  122. package/tensor.js +517 -0
  123. package/top.d.ts +16 -0
  124. package/top.js +15 -0
package/defopttt.js ADDED
@@ -0,0 +1,177 @@
1
+ import { top } from "./top.js";
2
+ const defOpTTT = (fn, dispatch = 1) => {
3
+ const f1 = (out, a, b, c) => {
4
+ !out && (out = a);
5
+ const {
6
+ data: odata,
7
+ offset: oo,
8
+ stride: [txo]
9
+ } = out;
10
+ const {
11
+ data: adata,
12
+ offset: oa,
13
+ shape: [sx],
14
+ stride: [txa]
15
+ } = a;
16
+ const {
17
+ data: bdata,
18
+ offset: ob,
19
+ stride: [txb]
20
+ } = b;
21
+ const {
22
+ data: cdata,
23
+ offset: oc,
24
+ stride: [txc]
25
+ } = c;
26
+ for (let x = 0; x < sx; x++) {
27
+ odata[oo + x * txo] = fn(
28
+ adata[oa + x * txa],
29
+ bdata[ob + x * txb],
30
+ cdata[oc + x * txc]
31
+ );
32
+ }
33
+ return out;
34
+ };
35
+ const f2 = (out, a, b, c) => {
36
+ !out && (out = a);
37
+ const {
38
+ data: odata,
39
+ offset: oo,
40
+ stride: [txo, tyo]
41
+ } = out;
42
+ const {
43
+ data: adata,
44
+ offset: oa,
45
+ shape: [sx, sy],
46
+ stride: [txa, tya]
47
+ } = a;
48
+ const {
49
+ data: bdata,
50
+ offset: ob,
51
+ stride: [txb, tyb]
52
+ } = b;
53
+ const {
54
+ data: cdata,
55
+ offset: oc,
56
+ stride: [txc, tyc]
57
+ } = c;
58
+ for (let x = 0; x < sx; x++) {
59
+ const oox = oo + x * txo;
60
+ const oax = oa + x * txa;
61
+ const obx = ob + x * txb;
62
+ const ocx = oc + x * txc;
63
+ for (let y = 0; y < sy; y++) {
64
+ odata[oox + y * tyo] = fn(
65
+ adata[oax + y * tya],
66
+ bdata[obx + y * tyb],
67
+ cdata[ocx + y * tyc]
68
+ );
69
+ }
70
+ }
71
+ return out;
72
+ };
73
+ const f3 = (out, a, b, c) => {
74
+ !out && (out = a);
75
+ const {
76
+ data: odata,
77
+ offset: oo,
78
+ stride: [txo, tyo, tzo]
79
+ } = out;
80
+ const {
81
+ data: adata,
82
+ offset: oa,
83
+ shape: [sx, sy, sz],
84
+ stride: [txa, tya, tza]
85
+ } = a;
86
+ const {
87
+ data: bdata,
88
+ offset: ob,
89
+ stride: [txb, tyb, tzb]
90
+ } = b;
91
+ const {
92
+ data: cdata,
93
+ offset: oc,
94
+ stride: [txc, tyc, tzc]
95
+ } = c;
96
+ for (let x = 0; x < sx; x++) {
97
+ const oox = oo + x * txo;
98
+ const oax = oa + x * txa;
99
+ const obx = ob + x * txb;
100
+ const ocx = oc + x * txc;
101
+ for (let y = 0; y < sy; y++) {
102
+ const ooy = oox + y * tyo;
103
+ const oay = oax + y * tya;
104
+ const oby = obx + y * tyb;
105
+ const ocy = ocx + y * tyc;
106
+ for (let z = 0; z < sz; z++) {
107
+ odata[ooy + z * tzo] = fn(
108
+ adata[oay + z * tza],
109
+ bdata[oby + z * tzb],
110
+ cdata[ocy + z * tzc]
111
+ );
112
+ }
113
+ }
114
+ }
115
+ return out;
116
+ };
117
+ const f4 = (out, a, b, c) => {
118
+ !out && (out = a);
119
+ const {
120
+ data: odata,
121
+ offset: oo,
122
+ stride: [txo, tyo, tzo, two]
123
+ } = out;
124
+ const {
125
+ data: adata,
126
+ offset: oa,
127
+ shape: [sx, sy, sz, sw],
128
+ stride: [txa, tya, tza, twa]
129
+ } = a;
130
+ const {
131
+ data: bdata,
132
+ offset: ob,
133
+ stride: [txb, tyb, tzb, twb]
134
+ } = b;
135
+ const {
136
+ data: cdata,
137
+ offset: oc,
138
+ stride: [txc, tyc, tzc, twc]
139
+ } = c;
140
+ for (let x = 0; x < sx; x++) {
141
+ const oox = oo + x * txo;
142
+ const oax = oa + x * txa;
143
+ const obx = ob + x * txb;
144
+ const ocx = oc + x * txc;
145
+ for (let y = 0; y < sy; y++) {
146
+ const ooy = oox + y * tyo;
147
+ const oay = oax + y * tya;
148
+ const oby = obx + y * tyb;
149
+ const ocy = ocx + y * tyc;
150
+ for (let z = 0; z < sz; z++) {
151
+ const ooz = ooy + z * tzo;
152
+ const oaz = oay + z * tza;
153
+ const obz = oby + z * tzb;
154
+ const ocz = ocy + z * tzc;
155
+ for (let w = 0; w < sw; w++) {
156
+ odata[ooz + w * two] = fn(
157
+ adata[oaz + w * twa],
158
+ bdata[obz + w * twb],
159
+ cdata[ocz + w * twc]
160
+ );
161
+ }
162
+ }
163
+ }
164
+ }
165
+ return out;
166
+ };
167
+ return [
168
+ top(dispatch, void 0, f1, f2, f3, f4),
169
+ f1,
170
+ f2,
171
+ f3,
172
+ f4
173
+ ];
174
+ };
175
+ export {
176
+ defOpTTT
177
+ };
package/div.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Componentwise nD tensor division. Writes result to `out`. If `out` is null,
3
+ * mutates `a`. Multi-method.
4
+ *
5
+ * @param out - output tensor
6
+ * @param a - input tensor
7
+ * @param n - scalar
8
+ */
9
+ export declare const div: import("./api.js").MultiTensorOpTT<number>;
10
+ /**
11
+ * Same as {@link div} for 1D tensors.
12
+ *
13
+ * @param out - output tensor
14
+ * @param a - input tensor
15
+ * @param n - scalar
16
+ */
17
+ export declare const div1: import("./api.js").TensorOpTT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
18
+ /**
19
+ * Same as {@link div} for 2D tensors.
20
+ *
21
+ * @param out - output tensor
22
+ * @param a - input tensor
23
+ * @param n - scalar
24
+ */
25
+ export declare const div2: import("./api.js").TensorOpTT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
26
+ /**
27
+ * Same as {@link div} for 3D tensors.
28
+ *
29
+ * @param out - output tensor
30
+ * @param a - input tensor
31
+ * @param n - scalar
32
+ */
33
+ export declare const div3: import("./api.js").TensorOpTT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
34
+ /**
35
+ * Same as {@link div} for 4D tensors.
36
+ *
37
+ * @param out - output tensor
38
+ * @param a - input tensor
39
+ * @param n - scalar
40
+ */
41
+ export declare const div4: import("./api.js").TensorOpTT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
42
+ //# sourceMappingURL=div.d.ts.map
package/div.js ADDED
@@ -0,0 +1,15 @@
1
+ import { $div } from "@thi.ng/vectors/ops";
2
+ import { defOpTT } from "./defoptt.js";
3
+ const [a, b, c, d, e] = defOpTT($div);
4
+ const div = a;
5
+ const div1 = b;
6
+ const div2 = c;
7
+ const div3 = d;
8
+ const div4 = e;
9
+ export {
10
+ div,
11
+ div1,
12
+ div2,
13
+ div3,
14
+ div4
15
+ };
package/divn.d.ts ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Componentwise nD tensor division with uniform scalar `n`. Writes result to
3
+ * `out`. If `out` is null, mutates `a`. Multi-method.
4
+ *
5
+ * @param out - output tensor
6
+ * @param a - input tensor
7
+ * @param n - scalar
8
+ */
9
+ export declare const divN: import("./api.js").MultiTensorOpTN<number>;
10
+ /**
11
+ * Same as {@link divN} for 1D tensors.
12
+ *
13
+ * @param out - output tensor
14
+ * @param a - input tensor
15
+ * @param n - scalar
16
+ */
17
+ export declare const divN1: import("./api.js").TensorOpTN<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
18
+ /**
19
+ * Same as {@link divN} for 2D tensors.
20
+ *
21
+ * @param out - output tensor
22
+ * @param a - input tensor
23
+ * @param n - scalar
24
+ */
25
+ export declare const divN2: import("./api.js").TensorOpTN<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
26
+ /**
27
+ * Same as {@link divN} for 3D tensors.
28
+ *
29
+ * @param out - output tensor
30
+ * @param a - input tensor
31
+ * @param n - scalar
32
+ */
33
+ export declare const divN3: import("./api.js").TensorOpTN<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
34
+ /**
35
+ * Same as {@link divN} for 4D tensors.
36
+ *
37
+ * @param out - output tensor
38
+ * @param a - input tensor
39
+ * @param n - scalar
40
+ */
41
+ export declare const divN4: import("./api.js").TensorOpTN<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
42
+ //# sourceMappingURL=divn.d.ts.map
package/divn.js ADDED
@@ -0,0 +1,15 @@
1
+ import { $div } from "@thi.ng/vectors/ops";
2
+ import { defOpTN } from "./defoptn.js";
3
+ const [a, b, c, d, e] = defOpTN($div);
4
+ const divN = a;
5
+ const divN1 = b;
6
+ const divN2 = c;
7
+ const divN3 = d;
8
+ const divN4 = e;
9
+ export {
10
+ divN,
11
+ divN1,
12
+ divN2,
13
+ divN3,
14
+ divN4
15
+ };
package/dot.d.ts ADDED
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Dot product of given nD tensor. Multi-method.
3
+ *
4
+ * @param a - input tensor
5
+ */
6
+ export declare const dot: import("./api.js").MultiTensorOpRTT<number, number>;
7
+ /**
8
+ * Dot product of given 1D tensor.
9
+ *
10
+ * @param a - input tensor
11
+ */
12
+ export declare const dot1: import("./api.js").TensorOpRTT<number, number, import("./tensor.js").Tensor1<number>>;
13
+ /**
14
+ * Dot product of given 2D tensor.
15
+ *
16
+ * @param a - input tensor
17
+ */
18
+ export declare const dot2: import("./api.js").TensorOpRTT<number, number, import("./tensor.js").Tensor2<number>>;
19
+ /**
20
+ * Dot product of given 3D tensor.
21
+ *
22
+ * @param a - input tensor
23
+ */
24
+ export declare const dot3: import("./api.js").TensorOpRTT<number, number, import("./tensor.js").Tensor3<number>>;
25
+ /**
26
+ * Dot product of given 3D tensor.
27
+ *
28
+ * @param a - input tensor
29
+ */
30
+ export declare const dot4: import("./api.js").TensorOpRTT<number, number, import("./tensor.js").Tensor4<number>>;
31
+ //# sourceMappingURL=dot.d.ts.map
package/dot.js ADDED
@@ -0,0 +1,17 @@
1
+ import { defOpRTT } from "./defoprtt.js";
2
+ const [a, b, c, d, e] = defOpRTT(
3
+ (acc, x, y) => acc + x * y,
4
+ () => 0
5
+ );
6
+ const dot = a;
7
+ const dot1 = b;
8
+ const dot2 = c;
9
+ const dot3 = d;
10
+ const dot4 = e;
11
+ export {
12
+ dot,
13
+ dot1,
14
+ dot2,
15
+ dot3,
16
+ dot4
17
+ };
package/errors.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { NumericArray } from "@thi.ng/api";
2
+ export declare const IllegalShapeError: {
3
+ new (msg?: NumericArray | undefined): {
4
+ origMessage: string;
5
+ name: string;
6
+ message: string;
7
+ stack?: string;
8
+ cause?: unknown;
9
+ };
10
+ captureStackTrace(targetObject: object, constructorOpt?: Function): void;
11
+ prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
12
+ stackTraceLimit: number;
13
+ };
14
+ export declare const illegalShape: (shape: NumericArray) => never;
15
+ //# sourceMappingURL=errors.d.ts.map
package/errors.js ADDED
@@ -0,0 +1,9 @@
1
+ import { defError } from "@thi.ng/errors/deferror";
2
+ const IllegalShapeError = defError(() => "illegal shape");
3
+ const illegalShape = (shape) => {
4
+ throw new IllegalShapeError(shape);
5
+ };
6
+ export {
7
+ IllegalShapeError,
8
+ illegalShape
9
+ };
package/exp.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Componentwise computes `Math.exp` of given nD tensor and writes result to
3
+ * `out`. If `out` is null, mutates original. Multi-method.
4
+ *
5
+ * @param out - output tensor
6
+ * @param a - input tensor
7
+ */
8
+ export declare const exp: import("./api.js").MultiTensorOpT<number>;
9
+ /**
10
+ * Same as {@link exp} for 1D tensors.
11
+ *
12
+ * @param out - output tensor
13
+ * @param a - input tensor
14
+ */
15
+ export declare const exp_1: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
16
+ /**
17
+ * Same as {@link exp} for 2D tensors.
18
+ *
19
+ * @param out - output tensor
20
+ * @param a - input tensor
21
+ */
22
+ export declare const exp_2: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
23
+ /**
24
+ * Same as {@link exp} for 3D tensors.
25
+ *
26
+ * @param out - output tensor
27
+ * @param a - input tensor
28
+ */
29
+ export declare const exp_3: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
30
+ /**
31
+ * Same as {@link exp} for 4D tensors.
32
+ *
33
+ * @param out - output tensor
34
+ * @param a - input tensor
35
+ */
36
+ export declare const exp_4: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
37
+ //# sourceMappingURL=exp.d.ts.map
package/exp.js ADDED
@@ -0,0 +1,14 @@
1
+ import { defOpT } from "./defopt.js";
2
+ const [a, b, c, d, e] = defOpT(Math.exp);
3
+ const exp = a;
4
+ const exp_1 = b;
5
+ const exp_2 = c;
6
+ const exp_3 = d;
7
+ const exp_4 = e;
8
+ export {
9
+ exp,
10
+ exp_1,
11
+ exp_2,
12
+ exp_3,
13
+ exp_4
14
+ };
package/exp2.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Componentwise computes `2^x` of given nD tensor and writes result to
3
+ * `out`. If `out` is null, mutates original. Multi-method.
4
+ *
5
+ * @param out - output tensor
6
+ * @param a - input tensor
7
+ */
8
+ export declare const exp2: import("./api.js").MultiTensorOpT<number>;
9
+ /**
10
+ * Same as {@link exp2} for 1D tensors.
11
+ *
12
+ * @param out - output tensor
13
+ * @param a - input tensor
14
+ */
15
+ export declare const exp2_1: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor1<number>, import("./tensor.js").Tensor1<number>>;
16
+ /**
17
+ * Same as {@link exp2} for 2D tensors.
18
+ *
19
+ * @param out - output tensor
20
+ * @param a - input tensor
21
+ */
22
+ export declare const exp2_2: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor2<number>, import("./tensor.js").Tensor2<number>>;
23
+ /**
24
+ * Same as {@link exp2} for 3D tensors.
25
+ *
26
+ * @param out - output tensor
27
+ * @param a - input tensor
28
+ */
29
+ export declare const exp2_3: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor3<number>, import("./tensor.js").Tensor3<number>>;
30
+ /**
31
+ * Same as {@link exp2} for 4D tensors.
32
+ *
33
+ * @param out - output tensor
34
+ * @param a - input tensor
35
+ */
36
+ export declare const exp2_4: import("./api.js").TensorOpT<number, number, import("./tensor.js").Tensor4<number>, import("./tensor.js").Tensor4<number>>;
37
+ //# sourceMappingURL=exp2.d.ts.map
package/exp2.js ADDED
@@ -0,0 +1,14 @@
1
+ import { defOpT } from "./defopt.js";
2
+ const [a, b, c, d, e] = defOpT((x) => 2 ** x);
3
+ const exp2 = a;
4
+ const exp2_1 = b;
5
+ const exp2_2 = c;
6
+ const exp2_3 = d;
7
+ const exp2_4 = e;
8
+ export {
9
+ exp2,
10
+ exp2_1,
11
+ exp2_2,
12
+ exp2_3,
13
+ exp2_4
14
+ };
package/format.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { ITensor } from "./api.js";
2
+ export declare const format: (x: any) => string;
3
+ export declare const setFormat: (width: number, prec: number) => void;
4
+ export declare const debug: ({ data, shape, stride, offset }: ITensor<any>) => void;
5
+ /**
6
+ * Syntax sugar for: `console.log(a.toString())`.
7
+ *
8
+ * @remarks
9
+ * Also see {@link debug} and {@link setFormat}.
10
+ *
11
+ * @param a - tensor
12
+ */
13
+ export declare const print: (a: ITensor<any>) => void;
14
+ //# sourceMappingURL=format.d.ts.map
package/format.js ADDED
@@ -0,0 +1,37 @@
1
+ import { isNumber } from "@thi.ng/checks/is-number";
2
+ import { floatFixedWidth } from "@thi.ng/strings/float";
3
+ import { padLeft } from "@thi.ng/strings/pad-left";
4
+ import { truncate } from "@thi.ng/strings/truncate";
5
+ import { dot } from "@thi.ng/vectors/dot";
6
+ let formatNumber;
7
+ let pad;
8
+ let trunc;
9
+ const format = (x) => isNumber(x) ? formatNumber(x) : trunc(pad(x.toString()));
10
+ const setFormat = (width, prec) => {
11
+ formatNumber = floatFixedWidth(width, prec);
12
+ pad = padLeft(width);
13
+ trunc = truncate(width);
14
+ };
15
+ setFormat(9, 4);
16
+ const debug = ({ data, shape, stride, offset }) => {
17
+ let dim = shape.length;
18
+ const state = new Array(dim).fill(0);
19
+ dim--;
20
+ while (true) {
21
+ const idx = dot(state, stride) + offset;
22
+ console.log(state, idx, data[idx]);
23
+ if (++state[dim] >= shape[dim]) {
24
+ for (; dim-- > 0; ) if (++state[dim] < shape[dim]) break;
25
+ if (state[0] >= shape[0]) return;
26
+ state.fill(0, dim + 1);
27
+ dim = shape.length - 1;
28
+ }
29
+ }
30
+ };
31
+ const print = (a) => console.log(a.toString(), "\n");
32
+ export {
33
+ debug,
34
+ format,
35
+ print,
36
+ setFormat
37
+ };
package/identity.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { TensorOpts, Type } from "./api.js";
2
+ import { Tensor2 } from "./tensor.js";
3
+ export declare const identity: <T extends Exclude<Type, "str">>(type: T, size: number, opts?: Pick<TensorOpts<any, any>, "storage">) => Tensor2<number>;
4
+ //# sourceMappingURL=identity.d.ts.map
package/identity.js ADDED
@@ -0,0 +1,11 @@
1
+ import { STORAGE } from "./storage.js";
2
+ import { Tensor2 } from "./tensor.js";
3
+ const identity = (type, size, opts) => {
4
+ const storage = opts?.storage || STORAGE[type];
5
+ const data = storage.alloc(size * size);
6
+ for (let i = 0; i < data.length; i += size + 1) data[i] = 1;
7
+ return new Tensor2(type, storage, data, [size, size], [size, 1]);
8
+ };
9
+ export {
10
+ identity
11
+ };
package/index.d.ts ADDED
@@ -0,0 +1,60 @@
1
+ export * from "./abs.js";
2
+ export * from "./add.js";
3
+ export * from "./addn.js";
4
+ export * from "./api.js";
5
+ export * from "./clamp.js";
6
+ export * from "./clampn.js";
7
+ export * from "./cos.js";
8
+ export * from "./defopn.js";
9
+ export * from "./defoprt.js";
10
+ export * from "./defoprtt.js";
11
+ export * from "./defopt.js";
12
+ export * from "./defoptn.js";
13
+ export * from "./defoptnn.js";
14
+ export * from "./defoptt.js";
15
+ export * from "./defopttt.js";
16
+ export * from "./div.js";
17
+ export * from "./divn.js";
18
+ export * from "./dot.js";
19
+ export * from "./errors.js";
20
+ export * from "./exp.js";
21
+ export * from "./exp2.js";
22
+ export * from "./format.js";
23
+ export * from "./identity.js";
24
+ export * from "./log.js";
25
+ export * from "./log2.js";
26
+ export * from "./mag.js";
27
+ export * from "./magsq.js";
28
+ export * from "./max.js";
29
+ export * from "./maxn.js";
30
+ export * from "./min.js";
31
+ export * from "./minn.js";
32
+ export * from "./mul.js";
33
+ export * from "./mulm.js";
34
+ export * from "./muln.js";
35
+ export * from "./mulv.js";
36
+ export * from "./normalize.js";
37
+ export * from "./pow.js";
38
+ export * from "./pown.js";
39
+ export * from "./product.js";
40
+ export * from "./rand-distrib.js";
41
+ export * from "./relu.js";
42
+ export * from "./relun.js";
43
+ export * from "./select.js";
44
+ export * from "./set.js";
45
+ export * from "./setn.js";
46
+ export * from "./sigmoid.js";
47
+ export * from "./sin.js";
48
+ export * from "./softmax.js";
49
+ export * from "./softplus.js";
50
+ export * from "./sqrt.js";
51
+ export * from "./step.js";
52
+ export * from "./storage.js";
53
+ export * from "./sub.js";
54
+ export * from "./subn.js";
55
+ export * from "./sum.js";
56
+ export * from "./tan.js";
57
+ export * from "./tanh.js";
58
+ export * from "./tensor.js";
59
+ export * from "./top.js";
60
+ //# sourceMappingURL=index.d.ts.map
package/index.js ADDED
@@ -0,0 +1,59 @@
1
+ export * from "./abs.js";
2
+ export * from "./add.js";
3
+ export * from "./addn.js";
4
+ export * from "./api.js";
5
+ export * from "./clamp.js";
6
+ export * from "./clampn.js";
7
+ export * from "./cos.js";
8
+ export * from "./defopn.js";
9
+ export * from "./defoprt.js";
10
+ export * from "./defoprtt.js";
11
+ export * from "./defopt.js";
12
+ export * from "./defoptn.js";
13
+ export * from "./defoptnn.js";
14
+ export * from "./defoptt.js";
15
+ export * from "./defopttt.js";
16
+ export * from "./div.js";
17
+ export * from "./divn.js";
18
+ export * from "./dot.js";
19
+ export * from "./errors.js";
20
+ export * from "./exp.js";
21
+ export * from "./exp2.js";
22
+ export * from "./format.js";
23
+ export * from "./identity.js";
24
+ export * from "./log.js";
25
+ export * from "./log2.js";
26
+ export * from "./mag.js";
27
+ export * from "./magsq.js";
28
+ export * from "./max.js";
29
+ export * from "./maxn.js";
30
+ export * from "./min.js";
31
+ export * from "./minn.js";
32
+ export * from "./mul.js";
33
+ export * from "./mulm.js";
34
+ export * from "./muln.js";
35
+ export * from "./mulv.js";
36
+ export * from "./normalize.js";
37
+ export * from "./pow.js";
38
+ export * from "./pown.js";
39
+ export * from "./product.js";
40
+ export * from "./rand-distrib.js";
41
+ export * from "./relu.js";
42
+ export * from "./relun.js";
43
+ export * from "./select.js";
44
+ export * from "./set.js";
45
+ export * from "./setn.js";
46
+ export * from "./sigmoid.js";
47
+ export * from "./sin.js";
48
+ export * from "./softmax.js";
49
+ export * from "./softplus.js";
50
+ export * from "./sqrt.js";
51
+ export * from "./step.js";
52
+ export * from "./storage.js";
53
+ export * from "./sub.js";
54
+ export * from "./subn.js";
55
+ export * from "./sum.js";
56
+ export * from "./tan.js";
57
+ export * from "./tanh.js";
58
+ export * from "./tensor.js";
59
+ export * from "./top.js";