ztcl::tensor map
Runs one expression for each element of the broadcast of its arguments.
ztcl::tensor map ?-interp|-compiled? ?-dtype DTYPE? PARAMS EXPR ?ARG ...?
Description
Runs the expr expression EXPR once for each element of the broadcast of the arguments, with each name of PARAMS set to the element of its argument as a double. A plain number is a scalar. The result rounds once to DTYPE, by default the dtype of the first tensor argument.
The reference path (-interp) calls apply {PARAMS {expr {EXPR}}} for each element, so EXPR can use each command of Tcl. The compiled path (-compiled) runs EXPR as the vector program of a point proc over blocks of 1,024 elements: it accepts the operators, the math functions, and at most 10 parameters, and gives the same bits as the reference path. With no option, the compiled path runs when it accepts EXPR, and the reference path runs for each other EXPR. When the compiled program makes a NaN or divides integers by zero, the reference path runs, so a domain error is the error of Tcl on each path. Both paths charge one formula of the count of elements and the operations of EXPR, when the check accepts EXPR.
Returns
A tensor.
Examples
Each example of this page runs after package require ztcl::tensor.
% ztcl::tensor map {x y} {$x * $y + 1} [ztcl::tensor of {1 2 3}] 2
tensor f32 3 {3.0 5.0 7.0}
% set x [ztcl::tensor randn 1000 -seed 1]; ztcl::tensor equal [ztcl::tensor map -interp {x} {$x / (1 + exp(-$x))} $x] [ztcl::tensor map -compiled {x} {$x / (1 + exp(-$x))} $x]
1
% ztcl::tensor map {x} {[string length $x]} [ztcl::tensor of {1.5 20}]
tensor f32 2 {3.0 4.0}
Errors
% ztcl::tensor map -compiled {x} {[string length $x]} [ztcl::tensor of 1]
tensor kernel map, line 1: "string" is not allowed in a point proc
-errorcode NONE
% ztcl::tensor map {x y} {$x} [ztcl::tensor of 1]
tensor map: 2 parameters and 1 arguments
-errorcode ZTCL TENSOR
Facts
- Capability
- None.
- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.4
- Compute cost
- A cost for each element operation by its kind (a copy, an add, a division, a math function, a multiply-add of a matmul), set so that one compute unit takes about the time of one unit of plain Tcl. docs/modules/tensor.md has the table.