Skip to content

Commit

Permalink
Merge pull request #4 from ambaker1/improved
Browse files Browse the repository at this point in the history
Improved
  • Loading branch information
ambaker1 authored Oct 14, 2023
2 parents f3b55c3 + 9411708 commit 1f08603
Show file tree
Hide file tree
Showing 21 changed files with 6,821 additions and 5,020 deletions.
678 changes: 22 additions & 656 deletions build.tcl

Large diffs are not rendered by default.

Binary file modified doc/ndlist.pdf
Binary file not shown.
1,148 changes: 785 additions & 363 deletions doc/ndlist.tex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion doc/template/version.tex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
\newcommand{\version}{0.2.1}
\newcommand{\version}{0.3}
119 changes: 0 additions & 119 deletions examples/doc_examples.tcl

This file was deleted.

6 changes: 3 additions & 3 deletions install.tcl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package require tin 1.0
tin depend vutil 1.1
set dir [tin mkdir -force ndlist 0.2.1]
file copy pkgIndex.tcl ndlist.tcl README.md LICENSE $dir
set dir [tin mkdir -force ndlist 0.3]
file copy pkgIndex.tcl ndlist.tcl vector.tcl matrix.tcl tensor.tcl $dir
file copy README.md LICENSE $dir
157 changes: 157 additions & 0 deletions matrix.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# matrix.tcl
################################################################################
# Utilities for matrices (2D lists)

# Copyright (C) 2023 Alex Baker, ambaker1@mtu.edu
# All rights reserved.

# See the file "LICENSE" in the top level directory for information on usage,
# redistribution, and for a DISCLAIMER OF ALL WARRANTIES.
################################################################################

# Define namespace and exported commands
namespace eval ::ndlist {
namespace export eye matmul transpose; # Matrix algebra
namespace export zip zip3 cartprod; # Iteration tools (e.g. tuples)
}

# eye --
#
# Generate an identity matrix of specified size
#
# Syntax:
# eye $n
#
# Arguments:
# n Size of matrix (nxn)

proc ::ndlist::eye {n} {
set x [nfull 0 $n $n]
foreach i [range $n] {
lset x $i $i 1
}
return $x
}

# transpose --
#
# Transpose a matrix
# Similar to math::linearalgebra::transpose and lsearch example on Tcl wiki
# written by MJ (https://wiki.tcl-lang.org/page/Transposing+a+matrix)
#
# Arguments:
# matrix: Matrix to transpose

proc ::ndlist::transpose {matrix} {
set n [llength $matrix]
# Null case
if {$n == 0} {
return
}
set m [llength [lindex $matrix 0]]
if {$n == 1 && $m == 1} {
return $matrix
} elseif {$n > $m} {
set i -1
lmap x [lindex $matrix 0] {
lsearch -all -inline -subindices -index [incr i] $matrix *
}
} else {
set i -1
lmap x [lindex $matrix 0] {
incr i
lmap row $matrix {lindex $row $i}
}
}
}

# matmul --
#
# Multiplies two matrices. Inner dimensions must agree.
# Similar to math::linearalgebra::matmul.
# Returns a nxm matrix, by computing the dot-product of rows and columns
#
# Syntax:
# matmul $A $B
#
# Arguments:
# A B Matrices, matching inner dimensions (e.g. nxq and qxm)

proc ::ndlist::matmul {A B} {
# Check dimensions
if {[llength [lindex $A 0]] != [llength $B]} {
return -code error "incompatible matrix dimensions"
}
# Transpose B matrix for easy multiplication
set BT [transpose $B]
# Perform dot-product of all rows and columns
lmap rowA $A {
lmap colB $BT {
dot $rowA $colB
}
}
}

# zip --
#
# Zip vectors (equal length) into a tuple list
#
# Syntax:
# zip $a $b ...
#
# Arguments:
# a b Vectors (equal length)

proc ::ndlist::zip {a b} {
if {[llength $a] != [llength $b]} {
return -code error "mismatched list lengths"
}
lmap ai $a bi $b {
list $ai $bi
}
}

# zip3 --
#
# Zip three vectors (equal length) into a triple
#
# Syntax:
# zip3 $a $b $c
#
# Arguments:
# a b c Vectors (equal length)

proc ::ndlist::zip3 {a b c} {
if {[llength $a] != [llength $b] || [llength $a] != [llength $c]} {
return -code error "mismatched list lengths"
}
lmap ai $a bi $b ci $c {
list $ai $bi $ci
}
}

# cartprod --
#
# Cartesian product of multiple vectors (can have duplicates)
# Returns a list of all combinations
# Modified from "cartesianNaryProduct", accessed on 12/15/2021 at
# https://rosettacode.org/wiki/Cartesian_product_of_two_or_more_lists
#
# Syntax:
# cartprod $arg ...
#
# Arguments:
# arg ... Vectors to take "cartesian product" of

proc ::ndlist::cartprod {args} {
foreach vector [lassign $args matrix] {
set newMatrix {}
foreach row $matrix {
foreach value $vector {
lappend newMatrix [linsert $row end $value]
}
}
set matrix $newMatrix
}
return $matrix
}
Loading

0 comments on commit 1f08603

Please sign in to comment.