Standard Tcl Library

From Micro and Nano Mechanics Group
Revision as of 08:36, 1 March 2009 by Kwkang (talk | contribs) (New page: If you've used Tcl, sometimes you may feel that Tcl would be more convenient if it can support some matrix operations such as matrix summation, transpose, dot/cross product, norm, etc. I i...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

If you've used Tcl, sometimes you may feel that Tcl would be more convenient if it can support some matrix operations such as matrix summation, transpose, dot/cross product, norm, etc. I introduce Tcl standard library that can alleviate our burdens.

Install

Download Tcllib from http://www.tcl.tk/software/tcllib/ and install it

# mv tcllib-1.11.1.tar.gz /usr/local/src/
# cd /usr/local/src/
# tar -zxvf tcllib-1.11.1.tar.gz
# cd tcllib-1.11.1
# ./installer.tcl

Usage

There are on-line documentations at http://tcllib.sourceforge.net/doc/ . Below is shown a sample Tcl script, matrix.tcl to show how we can use Tcl linear algebra library.

#!/bin/sh
# the next line is executed by /bin/sh, but not tcl \
echo Tcl script name: $0, with $# arguments; echo ; exec tclsh "$0" "$@"

#*******************************************
# Call necessary pacakges
#*******************************************
package require math::linearalgebra

#*******************************************
# Definition of procedures
#*******************************************
proc det matrix {
    if {[llength $matrix] != [llength [lindex $matrix 0]]} {
       error "non-square matrix"
    }
    switch [llength $matrix] {
       2 {
           foreach {a b c d} [join $matrix] break
           expr {$a*$d - $b*$c}
       } default {
           set i 0
           set mat2 [lrange $matrix 1 end]
           set res 0
           foreach element [lindex $matrix 0] {
              if $element {
                 set sign [expr {$i%2? -1: 1}]
                 set res [expr {$res + $sign*$element* [det [cancelcol $i $mat2]]}]
              }
              incr i
           }
           set res
       }
    }
 }
proc cancelcol {n matrix} {
    set res {}
    foreach row $matrix {
        lappend res [lreplace $row $n $n]
    }
    set res
}

 
#*******************************************
# Main program starts here
#*******************************************
# Durer's magic square
puts "Durer's magic square"
set A {{16 3 2 13} {5 10 11 8} {9 6 7 12} {4 15 14 1}}
puts "A = \n[::math::linearalgebra::show $A %2d]" 

# Transpose the matrix A
puts "Transpose of the matrix A, At ="
set At [::math::linearalgebra::transpose $A]
puts "[::math::linearalgebra::show $At %2d]"

# Matrix Summation
puts "A + At ="
puts "[::math::linearalgebra::show \
      [::math::linearalgebra::add $A $At] %2d]"

# Matrix multiplication
puts "At * A ="
set AtA [::math::linearalgebra::matmul $At $A]
puts "[::math::linearalgebra::show $AtA ]"

# Matrix determinant
set detA [det $A]
puts "det(A) = $detA.\n"

# Create a 3X3 matrix O whose elements are all zeros. 
puts "Create a 3X3 matrix O whose elements are all zeros."
set O [::math::linearalgebra::mkMatrix 3 3 0]
puts "O = \n[::math::linearalgebra::show $O]"

# Create a 3X3 identity matrix I
puts "Create a 3X3 identity matrix I."
set I [::math::linearalgebra::mkIdentity 3]
puts "I = \n[::math::linearalgebra::show $I]"

# Create a 3X3 random matrix R
puts "Create a 3X3 random matrix R."
set R [::math::linearalgebra::mkRandom 3]
puts "R = \n[::math::linearalgebra::show $R]"

# Get the 2nd column from the matrix R.
puts "Get the 2nd column from the matrix R."
set c2 [::math::linearalgebra::getcol $R 1]
puts "c2 = \n[::math::linearalgebra::show $c2]"

If you run the script, the result will look like the following.

$ ./matrix.tcl 
Tcl script name: ./matrix.tcl, with 0 arguments

Durer's magic square
A = 
16  3  2 13 
 5 10 11  8 
 9  6  7 12 
 4 15 14  1 

Transpose of the matrix A, At =
16  5  9  4 
 3 10  6 15 
 2 11  7 14 
13  8 12  1 

A + At =
32  8 11 17 
 8 20 17 23 
11 17 14 26 
17 23 26  2 

At * A =
378.0000 212.0000 206.0000 360.0000 
212.0000 370.0000 368.0000 206.0000 
206.0000 368.0000 370.0000 212.0000 
360.0000 206.0000 212.0000 378.0000 

det(A) = 0.

Create a 3X3 matrix O whose elements are all zeros.
O = 
0.0000 0.0000 0.0000 
0.0000 0.0000 0.0000 
0.0000 0.0000 0.0000 

Create a 3X3 identity matrix I.
I = 
1.0000 0.0000 0.0000 
0.0000 1.0000 0.0000 
0.0000 0.0000 1.0000 

Create a 3X3 random matrix R.
R = 
0.3562 0.3277 0.0830 
0.6627 0.9329 0.0609 
0.9916 0.1730 0.3524 

Get the 2nd column from the matrix R.
c2 = 
0.3277
0.9329
0.1730

Tcl library can do more than what is explained here. Refer its documentation.