beetlebug▾
/

ztcl::tensor argsort

Gives the indices that sort a tensor of rank 1.

ztcl::tensor argsort TENSOR ?-decreasing?

Description

Gives the indices of the elements of TENSOR in rising order of the elements, or in falling order with -decreasing. The sort is stable: equal elements keep the order of their indices, as in lsort -indices. A NaN goes after each number in both orders. The cost is n * log2(n) operations.

Returns

An i32 tensor of rank 1.

Examples

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

% ztcl::tensor argsort [ztcl::tensor of {0.2 0.5 0.1 0.5}] -decreasing
tensor i32 4 {1 3 0 2}

Errors

% ztcl::tensor argsort [ztcl::tensor ones {2 2}]
tensor argsort: the tensor has rank 2, and the command needs rank 1
-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.