ndlist

N-Dimensional Lists

AMB: The package "ndlist" is a pure-Tcl tensor manipulation package.

GitHub Repo: https://github.com/ambaker1/ndlist

Features

Over 60 commands for tensor manipulation, including:

  • ND-list initialization and expansion.
  • ND-list index access/modification.
  • ND-list shape access/manipulation.
  • ND-list mapping and looping.
  • Various utilities for vectors and matrices.

Installing/Importing:

This package is a Tin package, so it can be easily installed and imported on the fly.

package require tin
tin add -auto ndlist https://github.com/ambaker1/ndlist install.tcl
tin import ndlist

Examples:

Here's a few examples of what "ndlist" can do.

Full documentation is available here

Swap the rows and columns of a matrix:

set a {{1 2 3} {4 5 6} {7 8 9}}
nset a {1 0} : [nget $a {0 1} :]; # {4 5 6} {1 2 3} {7 8 9}
nset a : {1 0} [nget $a : {0 1}]; # {5 4 6} {2 1 3} {8 7 9}

Flatten and reshape a matrix into a 3D tensor:

set x [nflatten 2D {{1 2 3 4} {5 6 7 8}}]; # 1 2 3 4 5 6 7 8
set x [nreshape $x 2 2 2]; # {{1 2} {3 4}} {{5 6} {7 8}}

Filter a list by removing elements:

set x [range 10]; # 0 1 2 3 4 5 6 7 8 9
set x [nremove $x [find $x > 4]]; # 0 1 2 3 4

Adding matrices together:

set x {{1 2 3} {4 5 6}}
set y {{1 1 1} {2 2 2}}
set z [nop2 2D $x + $y]; # {2 3 4} {6 7 8}