beetlebug▾
/

ztcl::tensor of

Makes a tensor of a list of numbers.

ztcl::tensor of LIST ?-dtype DTYPE? ?-shape SHAPE?

Description

Makes a tensor of the numbers of LIST. With no -shape, the shape is the nesting of the list: the length of the list, then the length of its first element, down to a number. With -shape, a flat list or a nested list with that count of numbers is read in row-major order. The default dtype is f32. A float rounds once to the dtype, and an i32 element must be an integer in its range.

Returns

A tensor.

Examples

Each example of this page runs after package require ztcl::tensor.

% ztcl::tensor of {{1 2} {3 4}}
tensor f32 {2 2} {1.0 2.0 3.0 4.0}

% ztcl::tensor of {1 2 3 4} -shape {2 2} -dtype i32
tensor i32 {2 2} {1 2 3 4}

% ztcl::tensor of 2.5
tensor f32 {} 2.5

Errors

% ztcl::tensor of {1 2 3} -shape {2 2}
tensor of: the shape {2 2} needs 4 values, and the list has 3
-errorcode ZTCL TENSOR

% ztcl::tensor of {1 x}
tensor: expected a number but got "x"
-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.