Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change in index notation #8

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified doc/ndlist.pdf
Binary file not shown.
16 changes: 8 additions & 8 deletions doc/ndlist.tex
Original file line number Diff line number Diff line change
Expand Up @@ -655,11 +655,11 @@ \subsection{Index Notation}
\begin{args}
\$n & Number of elements in list. \\
\$input & Index input. Options are shown below: \\
\quad * or : & All indices \\
\quad : & All indices \\
\quad \$start:\$stop & Range of indices (e.g. 0:4 or 1:end-2).\\
\quad \$start:\$step:\$stop & Stepped range of indices (e.g. 0:2:-2 or 2:3:end). \\
\quad \$iList & List of indices (e.g. \{0 end-1 5\} or 3). \\
\quad \$i. & Single index with a dot, ``flattens'' the ndlist (e.g. 0. or end-3.).
\quad \$i* & Single index with a asterisk, ``flattens'' the ndlist (e.g. 0* or end-3*).
\end{args}
Additionally, indices get passed through the \cmdlink{::ndlist::Index2Integer} command, which converts the inputs ``end'', ``end$\pm$integer'', ``integer$\pm$integer'' and negative wrap-around indexing (where -1 is equivalent to ``end'') into normal integer indices.
Note that this command will return an error if the index is out of range.
Expand All @@ -674,11 +674,11 @@ \subsection{Index Notation}
\begin{example}{Index Notation}
\begin{lstlisting}
set n 10
puts [::ndlist::ParseIndex $n *]
puts [::ndlist::ParseIndex $n :]
puts [::ndlist::ParseIndex $n 1:8]
puts [::ndlist::ParseIndex $n 0:2:6]
puts [::ndlist::ParseIndex $n {0 5 end-1}]
puts [::ndlist::ParseIndex $n end.]
puts [::ndlist::ParseIndex $n end*]
\end{lstlisting}
\tcblower
\begin{lstlisting}
Expand Down Expand Up @@ -706,11 +706,11 @@ \subsection{Access}
\begin{example}{ND-list access}
\begin{lstlisting}
set A {{1 2 3} {4 5 6} {7 8 9}}
puts [nget $A 0 *]; # get row matrix
puts [nget $A 0. *]; # flatten row matrix to a vector
puts [nget $A 0 :]; # get row matrix
puts [nget $A 0* :]; # flatten row matrix to a vector
puts [nget $A 0:1 0:1]; # get matrix subset
puts [nget $A end:0 end:0]; # can have reverse ranges
puts [nget $A {0 0 0} 1.]; # can repeat indices
puts [nget $A {0 0 0} 1*]; # can repeat indices
\end{lstlisting}
\tcblower
\begin{lstlisting}
Expand Down Expand Up @@ -753,7 +753,7 @@ \subsection{Modification}
\begin{example}{Swapping matrix rows}
\begin{lstlisting}
set a {{1 2 3} {4 5 6} {7 8 9}}
nset a {1 0} * [nget $a {0 1} *]; # Swap rows and columns (modify by reference)
nset a {1 0} : [nget $a {0 1} :]; # Swap rows and columns (modify by reference)
puts $a
\end{lstlisting}
\tcblower
Expand Down
8 changes: 4 additions & 4 deletions src/tensor.tin
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ proc ::ndlist::Get {list iType iList} {
#
# Arguments:
# dims Shape to index into
# indices Index inputs (e.g. *, {0 3}, 0:10, end.)
# indices Index inputs (e.g. :, {0 3}, 0:10, end*)
#
# Returns:
# iDims New dimensions (blank for flattened axis)
Expand Down Expand Up @@ -588,7 +588,7 @@ proc ::ndlist::ParseIndices {dims args} {
#
# Arguments:
# n Size of list
# index Index input (e.g. *, {0 3}, 0:10, end.)
# index Index input (e.g. :, {0 3}, 0:10, end*)
#
# Returns:
# iType Type of index (A, R, L, or S)
Expand All @@ -605,11 +605,11 @@ proc ::ndlist::ParseIndex {n index} {
return [list L [lmap index $index {Index2Integer $n $index}]]
}
# All index notation
if {$index in {* :}} {
if {$index eq {:}} {
return [list A ""]
}
# Single index notation
if {[string index $index end] eq {.}} {
if {[string index $index end] eq {*}} {
# Single index notation (flatten along this dimension)
return [list S [Index2Integer $n [string range $index 0 end-1]]]
}
Expand Down
8 changes: 4 additions & 4 deletions tensor.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ proc ::ndlist::Get {list iType iList} {
#
# Arguments:
# dims Shape to index into
# indices Index inputs (e.g. *, {0 3}, 0:10, end.)
# indices Index inputs (e.g. :, {0 3}, 0:10, end*)
#
# Returns:
# iDims New dimensions (blank for flattened axis)
Expand Down Expand Up @@ -588,7 +588,7 @@ proc ::ndlist::ParseIndices {dims args} {
#
# Arguments:
# n Size of list
# index Index input (e.g. *, {0 3}, 0:10, end.)
# index Index input (e.g. :, {0 3}, 0:10, end*)
#
# Returns:
# iType Type of index (A, R, L, or S)
Expand All @@ -605,11 +605,11 @@ proc ::ndlist::ParseIndex {n index} {
return [list L [lmap index $index {Index2Integer $n $index}]]
}
# All index notation
if {$index in {* :}} {
if {$index eq {:}} {
return [list A ""]
}
# Single index notation
if {[string index $index end] eq {.}} {
if {[string index $index end] eq {*}} {
# Single index notation (flatten along this dimension)
return [list S [Index2Integer $n [string range $index 0 end-1]]]
}
Expand Down
12 changes: 6 additions & 6 deletions tests/examples.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ puts -nonewline {}
test {Example 28} {Index Notation} -body {
puts {}
set n 10
puts [::ndlist::ParseIndex $n *]
puts [::ndlist::ParseIndex $n :]
puts [::ndlist::ParseIndex $n 1:8]
puts [::ndlist::ParseIndex $n 0:2:6]
puts [::ndlist::ParseIndex $n {0 5 end-1}]
puts [::ndlist::ParseIndex $n end.]
puts [::ndlist::ParseIndex $n end*]
puts -nonewline {}
} -output {
A {}
Expand All @@ -283,11 +283,11 @@ S 9
test {Example 29} {ND-list access} -body {
puts {}
set A {{1 2 3} {4 5 6} {7 8 9}}
puts [nget $A 0 *]; # get row matrix
puts [nget $A 0. *]; # flatten row matrix to a vector
puts [nget $A 0 :]; # get row matrix
puts [nget $A 0* :]; # flatten row matrix to a vector
puts [nget $A 0:1 0:1]; # get matrix subset
puts [nget $A end:0 end:0]; # can have reverse ranges
puts [nget $A {0 0 0} 1.]; # can repeat indices
puts [nget $A {0 0 0} 1*]; # can repeat indices
puts -nonewline {}
} -output {
{1 2 3}
Expand All @@ -308,7 +308,7 @@ puts -nonewline {}
test {Example 31} {Swapping matrix rows} -body {
puts {}
set a {{1 2 3} {4 5 6} {7 8 9}}
nset a {1 0} * [nget $a {0 1} *]; # Swap rows and columns (modify by reference)
nset a {1 0} : [nget $a {0 1} :]; # Swap rows and columns (modify by reference)
puts $a
puts -nonewline {}
} -output {
Expand Down
Loading