Standard Tcl Library: Difference between revisions

From Micro and Nano Mechanics Group
Jump to navigation Jump to search
(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...)
 
No edit summary
 
Line 1: Line 1:
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.
If you've used Tcl, sometimes you may feel that Tcl would be more convenient if it can support some vector or matrix operations such as matrix summation, transpose, dot/cross product, norm, etc. I introduce Tcl standard library, '''Tcllib''' that can resolve our troubles.


Install
==Install==


Download Tcllib from http://www.tcl.tk/software/tcllib/ and install it
Download Tcllib from http://www.tcl.tk/software/tcllib/ and install it
Line 11: Line 11:
# ./installer.tcl
# ./installer.tcl


Usage
==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.
There are on-line documentations at http://tcllib.sourceforge.net/doc/ . Below is shown a sample Tcl script, [[Media:Matrix.tcl | Matrix.tcl]] to show how to use linear algebra library in Tcl.


<pre>
<pre>
Line 82: Line 82:
set AtA [::math::linearalgebra::matmul $At $A]
set AtA [::math::linearalgebra::matmul $At $A]
puts "[::math::linearalgebra::show $AtA ]"
puts "[::math::linearalgebra::show $AtA ]"

# Is a given matrix symmetric?
puts "isSymmetric( At * A ) = [::math::linearalgebra::symmetric $AtA]"


# Matrix determinant
# Matrix determinant
Line 110: Line 113:
If you run the script, the result will look like the following.
If you run the script, the result will look like the following.
<pre>
<pre>
$ ./matrix.tcl
$ ./Matrix.tcl
Tcl script name: ./matrix.tcl, with 0 arguments
Tcl script name: ./Matrix.tcl, with 0 arguments


Durer's magic square
Durer's magic square
Line 138: Line 141:
360.0000 206.0000 212.0000 378.0000
360.0000 206.0000 212.0000 378.0000


isSymmetric( At * A ) = 1
det(A) = 0.
det(A) = 0.



Latest revision as of 09:03, 1 March 2009

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

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 to use linear algebra library in Tcl.

#!/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 ]"

# Is a given matrix symmetric?
puts "isSymmetric( At * A ) = [::math::linearalgebra::symmetric $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 

isSymmetric( At * A ) = 1
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.