ndlist

N-Dimensional Lists

AMB: The package "ndlist" is a pure-Tcl package providing data structures including vectors, matrices, tables, and multi-dimensional tensors.

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

Installing

ndlist can be installed with Tin, with the following code:

package require tin 2.1
tin autoadd ndlist https://github.com/ambaker1/ndlist install.tcl
tin install ndlist

Examples

Below are just a few examples of what is possible with ndlist. Full documentation and examples are available on GitHub.

package require ndlist
namespace import ::ndlist::*

# Get distance between elements in a vector
vector new x {1 2 4 7 11 16}
puts [nexpr {@x(1:end) - @x(0:end-1)}]; # 1 2 3 4 5

# Element-wise multiplication of column and row matrices
matrix new x {1 2 3}
matrix new y {{4 5 6}}
puts [nexpr {@x * @y}]; # {4 5 6} {8 10 12} {12 15 18}

# Multi-dimensional mapping of Tcl nested lists
set x {{1 2 3} {4 5 6} {7 8 9}}
set indices {}
nmap 2D xi $x {
    if {$xi > 4} {
        lappend indices [list [i] [j]]
    }
}
puts $indices; # {1 1} {1 2} {2 0} {2 1} {2 2}

# Tabular data structure
table new myTable
$myTable define keys {1 2 3}
$myTable @ x = {1.0 2.0 3.0}
set a 20.0
$myTable @ y := {@x*2 + $a}
puts [$myTable @ y]; # 22.0 24.0 26.0