@thi.ng/tensors 0.6.3 → 0.7.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.
- package/CHANGELOG.md +12 -1
- package/README.md +20 -1
- package/api.d.ts +15 -0
- package/convert.d.ts +25 -0
- package/convert.js +8 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/integrate.d.ts +34 -0
- package/integrate.js +31 -0
- package/package.json +6 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2025-
|
|
3
|
+
- **Last updated**: 2025-07-09T09:42:08Z
|
|
4
4
|
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
|
|
5
5
|
|
|
6
6
|
All notable changes to this project will be documented in this file.
|
|
@@ -11,6 +11,17 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
|
|
|
11
11
|
**Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
|
|
12
12
|
and/or version bumps of transitive dependencies.
|
|
13
13
|
|
|
14
|
+
## [0.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/tensors@0.7.0) (2025-07-09)
|
|
15
|
+
|
|
16
|
+
#### 🚀 Features
|
|
17
|
+
|
|
18
|
+
- add `integrate()` tensor op ([fe39a4a](https://github.com/thi-ng/umbrella/commit/fe39a4a))
|
|
19
|
+
- add `TensorLike` and `asTensor()` ([f76d9a6](https://github.com/thi-ng/umbrella/commit/f76d9a6))
|
|
20
|
+
|
|
21
|
+
#### ♻️ Refactoring
|
|
22
|
+
|
|
23
|
+
- migrate `asTensor()` to convert.ts ([fcb83c5](https://github.com/thi-ng/umbrella/commit/fcb83c5))
|
|
24
|
+
|
|
14
25
|
## [0.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/tensors@0.6.0) (2025-06-05)
|
|
15
26
|
|
|
16
27
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -40,6 +40,23 @@
|
|
|
40
40
|
|
|
41
41
|
## Built-in tensor operations
|
|
42
42
|
|
|
43
|
+
The [`ITensor`
|
|
44
|
+
interface](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html) shared
|
|
45
|
+
by all tensor implementations provides the following methods (non-exhaustive
|
|
46
|
+
list here):
|
|
47
|
+
|
|
48
|
+
- [.get(pos)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#get-1): Get value at position
|
|
49
|
+
- [.hi(pos)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#hi-1): Crop tensor (high end, zero copy)
|
|
50
|
+
- [.index(pos)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#index-1): Get index for position
|
|
51
|
+
- [.lo(pos)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#lo-1): Crop tensor (low end, zero copy)
|
|
52
|
+
- [.pack(storage?)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#pack-1): Copy tensor with data tightly packed
|
|
53
|
+
- [.pick(axes)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#pick-1): Select axes only (zero copy)
|
|
54
|
+
- [.position(index)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#position-1): Get position for index
|
|
55
|
+
- [.reshape(newShape, newStride?)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#reshape-1): Reshape tensor (zero copy)
|
|
56
|
+
- [.set(pos)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#set-1): Set value at position
|
|
57
|
+
- [.step(axes)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#step-1): Skip axes (zero copy)
|
|
58
|
+
- [.transpose(order)](https://docs.thi.ng/umbrella/tensors/interfaces/ITensor.html#transpose-1): Re-order axes (zero copy)
|
|
59
|
+
|
|
43
60
|
The set of tensor polymorphic component-wise ops is easily extensible via
|
|
44
61
|
provided higher-order functions in the `defOpXX()` family. Most of the ops
|
|
45
62
|
listed below are also based on this approach. The function signatures and naming
|
|
@@ -51,6 +68,7 @@ conventions are closely aligned to the ones used by the
|
|
|
51
68
|
- [addN](https://docs.thi.ng/umbrella/tensors/variables/addN.html): Tensor-scalar addition
|
|
52
69
|
- [argMax](https://docs.thi.ng/umbrella/tensors/variables/argMax.html): Maximum component index/value
|
|
53
70
|
- [argMin](https://docs.thi.ng/umbrella/tensors/variables/argMin.html): Minimum component index/value
|
|
71
|
+
- [asTensor](https://docs.thi.ng/umbrella/tensors/variables/asTensor.html): Convert/wrap data as tensor
|
|
54
72
|
- [clamp](https://docs.thi.ng/umbrella/tensors/variables/clamp.html): Tensor-tensor interval clamping
|
|
55
73
|
- [clampN](https://docs.thi.ng/umbrella/tensors/variables/clampN.html): Tensor-scalar interval clamping
|
|
56
74
|
- [convolve](https://docs.thi.ng/umbrella/tensors/variables/convolve.html): Tensor convolution (1D/2D/3D only)
|
|
@@ -61,6 +79,7 @@ conventions are closely aligned to the ones used by the
|
|
|
61
79
|
- [dot](https://docs.thi.ng/umbrella/tensors/variables/dot.html): Dot product
|
|
62
80
|
- [exp](https://docs.thi.ng/umbrella/tensors/variables/exp.html): Componentwise `Math.exp`
|
|
63
81
|
- [exp2](https://docs.thi.ng/umbrella/tensors/variables/exp2.html): Componentwise `2^x`
|
|
82
|
+
- [integrate](https://docs.thi.ng/umbrella/tensors/variables/dot.html): Integrate tensor along innermost dimension
|
|
64
83
|
- [log](https://docs.thi.ng/umbrella/tensors/variables/log.html): Componentwise `Math.log`
|
|
65
84
|
- [log2](https://docs.thi.ng/umbrella/tensors/variables/log2.html): Componentwise `Math.log2`
|
|
66
85
|
- [mag](https://docs.thi.ng/umbrella/tensors/variables/mag.html): Tensor magnitude
|
|
@@ -218,7 +237,7 @@ For Node.js REPL:
|
|
|
218
237
|
const ten = await import("@thi.ng/tensors");
|
|
219
238
|
```
|
|
220
239
|
|
|
221
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 9.
|
|
240
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 9.63 KB
|
|
222
241
|
|
|
223
242
|
## Dependencies
|
|
224
243
|
|
package/api.d.ts
CHANGED
|
@@ -58,6 +58,21 @@ export interface TensorFromArrayOpts<T extends Type, V> {
|
|
|
58
58
|
type: T;
|
|
59
59
|
storage?: ITensorStorage<V>;
|
|
60
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Source data type for tensor conversion via {@link asTensor}.
|
|
63
|
+
*/
|
|
64
|
+
export interface TensorLike<T extends Type, S extends Shape> {
|
|
65
|
+
/** Data type */
|
|
66
|
+
type: T;
|
|
67
|
+
/** Tensor data/values (MUST match `type`) */
|
|
68
|
+
data: TensorData<TypeMap[T]>;
|
|
69
|
+
/** Tensor shape */
|
|
70
|
+
shape: S;
|
|
71
|
+
/** Stride/layout information of data */
|
|
72
|
+
stride: S;
|
|
73
|
+
/** Start index (default: 0) */
|
|
74
|
+
offset?: number;
|
|
75
|
+
}
|
|
61
76
|
export interface ITensor<T = number> extends ICopy<ITensor<T>>, IEquiv, IEqualsDelta<ITensor<T>>, IRelease {
|
|
62
77
|
readonly type: Type;
|
|
63
78
|
readonly storage: ITensorStorage<T>;
|
package/convert.d.ts
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
import type { NumericArray } from "@thi.ng/api";
|
|
2
|
+
import type { Shape, ShapeTensor, TensorLike, TensorOpts, Type, TypeMap } from "./api.js";
|
|
3
|
+
/**
|
|
4
|
+
* Converts/wraps given {@link TensorLike} `src` into a matching tensor
|
|
5
|
+
* implementation. By default the source data is NOT copied.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts tangle:../export/as-tensor.ts
|
|
9
|
+
* import { asTensor, print } from "@thi.ng/tensors";
|
|
10
|
+
*
|
|
11
|
+
* const src = {
|
|
12
|
+
* data: [1, 2, 3, 4],
|
|
13
|
+
* type: <const>"num",
|
|
14
|
+
* shape: <[number,number]>[2, 2],
|
|
15
|
+
* stride: <[number,number]>[2, 1],
|
|
16
|
+
* };
|
|
17
|
+
*
|
|
18
|
+
* print(asTensor(src));
|
|
19
|
+
* // 1.0000 2.0000
|
|
20
|
+
* // 3.0000 4.0000
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @param src
|
|
24
|
+
* @param opts
|
|
25
|
+
*/
|
|
26
|
+
export declare const asTensor: <T extends Type, S extends Shape>(src: TensorLike<T, S>, opts?: Pick<TensorOpts<TypeMap[T], S>, "storage" | "copy">) => ShapeTensor<S, TypeMap[T]>;
|
|
2
27
|
/**
|
|
3
28
|
* Simplified interface of thi.ng/pixel `FloatBuffer`, only defining parts
|
|
4
29
|
* relevant to the conversion.
|
package/convert.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { typedArrayType } from "@thi.ng/api/typedarray";
|
|
2
2
|
import { tensor } from "./tensor.js";
|
|
3
|
+
const asTensor = (src, opts) => tensor(src.type, src.shape, {
|
|
4
|
+
copy: false,
|
|
5
|
+
data: src.data,
|
|
6
|
+
stride: src.stride,
|
|
7
|
+
offset: src.offset,
|
|
8
|
+
...opts
|
|
9
|
+
});
|
|
3
10
|
const fromFloatBuffer = ({
|
|
4
11
|
size: [sx, sy],
|
|
5
12
|
stride: [tx, ty],
|
|
@@ -18,5 +25,6 @@ const fromFloatBuffer = ({
|
|
|
18
25
|
});
|
|
19
26
|
};
|
|
20
27
|
export {
|
|
28
|
+
asTensor,
|
|
21
29
|
fromFloatBuffer
|
|
22
30
|
};
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/integrate.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Fn } from "@thi.ng/api";
|
|
2
|
+
import type { ITensor } from "./api.js";
|
|
3
|
+
import { Tensor1 } from "./tensor.js";
|
|
4
|
+
/**
|
|
5
|
+
* Integrates given tensor along innermost dimension and writes result to 1D
|
|
6
|
+
* tensor `out` (or creates new one).
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* The output tensor shape must match the innermost shape of the input: e.g. if
|
|
10
|
+
* input shape is `[2,3,4]`, then the output tensor must have a shape of `[4]`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts tangle:../export/integrate.ts
|
|
14
|
+
* import { integrate, print, product, sum, tensor } from "@thi.ng/tensors";
|
|
15
|
+
*
|
|
16
|
+
* const input = tensor([[1, 2], [10, 20], [100, 200]]);
|
|
17
|
+
*
|
|
18
|
+
* // integrate using `sum()` (also default)
|
|
19
|
+
* // i.e. [1 + 10 + 100, 2 + 20 + 200]
|
|
20
|
+
* print(integrate(null, input, sum));
|
|
21
|
+
* // 111.0000 222.0000
|
|
22
|
+
*
|
|
23
|
+
* // integrate using `product()`
|
|
24
|
+
* // i.e. [1 * 10 * 100, 2 * 20 * 200]
|
|
25
|
+
* print(integrate(null, input, product));
|
|
26
|
+
* // 1000.0000 8000.0000
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @param out
|
|
30
|
+
* @param a
|
|
31
|
+
* @param fn
|
|
32
|
+
*/
|
|
33
|
+
export declare const integrate: (out: Tensor1 | null, a: ITensor, fn?: Fn<ITensor, number>) => Tensor1;
|
|
34
|
+
//# sourceMappingURL=integrate.d.ts.map
|
package/integrate.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ensureShape } from "./errors.js";
|
|
2
|
+
import { Tensor1 } from "./tensor.js";
|
|
3
|
+
import { sum } from "./sum.js";
|
|
4
|
+
const integrate = (out, a, fn = sum) => {
|
|
5
|
+
const { shape, dim } = a;
|
|
6
|
+
const odim = shape[shape.length - 1];
|
|
7
|
+
if (!out) {
|
|
8
|
+
out = new Tensor1(
|
|
9
|
+
a.type,
|
|
10
|
+
a.storage,
|
|
11
|
+
a.storage.alloc(odim),
|
|
12
|
+
[odim],
|
|
13
|
+
[1]
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
ensureShape(out, [odim]);
|
|
17
|
+
const {
|
|
18
|
+
data: odata,
|
|
19
|
+
stride: [tx],
|
|
20
|
+
offset: ox
|
|
21
|
+
} = out;
|
|
22
|
+
const select = new Array(dim).fill(-1);
|
|
23
|
+
for (let i = 0; i < odim; i++) {
|
|
24
|
+
select[dim - 1] = i;
|
|
25
|
+
odata[ox + i * tx] = fn(a.pick(select));
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
};
|
|
29
|
+
export {
|
|
30
|
+
integrate
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/tensors",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "1D/2D/3D/4D tensors with extensible polymorphic operations and customizable storage",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@thi.ng/api": "^8.11.29",
|
|
43
|
-
"@thi.ng/arrays": "^2.13.
|
|
43
|
+
"@thi.ng/arrays": "^2.13.1",
|
|
44
44
|
"@thi.ng/checks": "^3.7.9",
|
|
45
45
|
"@thi.ng/equiv": "^2.1.85",
|
|
46
46
|
"@thi.ng/errors": "^2.5.35",
|
|
@@ -197,6 +197,9 @@
|
|
|
197
197
|
"./identity": {
|
|
198
198
|
"default": "./identity.js"
|
|
199
199
|
},
|
|
200
|
+
"./integrate": {
|
|
201
|
+
"default": "./integrate.js"
|
|
202
|
+
},
|
|
200
203
|
"./kernels": {
|
|
201
204
|
"default": "./kernels.js"
|
|
202
205
|
},
|
|
@@ -325,5 +328,5 @@
|
|
|
325
328
|
"status": "alpha",
|
|
326
329
|
"year": 2018
|
|
327
330
|
},
|
|
328
|
-
"gitHead": "
|
|
331
|
+
"gitHead": "e657c2d66574c18343a6797aef4585945729093e\n"
|
|
329
332
|
}
|