'Tcl library'에 해당되는 글 1건

  1. 2009.03.01 Tcl Standard Library 3
Tcl을 사용하다 보면, 때때로 Tcl에서 간단한 벡터나 행렬 연산이 되면 좋겠다하고 생각하곤 했다. 예를 들어 두 행렬 간의 합이나 곱 구하기, 행렬의 행과 열을 치환하기, 두 벡터 간의 내적이나 외적 구하기, 또는 주어진 벡터 회전하기 등등을 하기 위해서 일일이 관련 함수를 만드는 것이 이만저만한 시간 낭비가 아닐 수 없다. 다행히도 Tcl 표준 라이브러리인 Tcllib 덕분에, Matlab 에 비할 바는 아니지만, Tcl에서도 선형대수와 관련된 기본적인 함수들을 사용할 수 있다.

1. 설치하기

우선 Tcl 라이브러인 Tcllib를 http://www.tcl.tk/software/tcllib/ 에서 내려 받은 다음, 아래의 일련의 명령어들을 실행함으로서 라이브러리를 설치한다. 이 때, 관리자 계정으로 로그인 한 상태이어야 한다.  

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


2. 사용하기

간단한 Tcl 스크립트를 통하여 어떻게 Tcl에서 선형대수 함수를 사용할 수 있는지 보자. 이 스크립트를 다운 받으려면 오른쪽의 Matrix.tcl을 클릭하면 된다.

선형대수 함수를 사용하기 전에 관련된 라이브러리 모듈을 package require이란 명령어를 사용하여 불러온다. 그 다음은 주어진 행렬의 determinant를 계산하는 함수를 정의하는 부분이다. 이 함수는 Richard Suchenwirth에 의해 쓰여졌다. (출처: http://wiki.tcl.tk/11095 ) 이 후, 스크립트에 포함된 주석문을 참고한다. 
#!/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]"
위 스크립트를 실행시키면 그 결과가 아래와 같이 출력이 된다.
$ ./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 라이브러리는 단순한 선형대수 함수만 제공하는 것이 아니다. Tcllib 온라인 문서를 참조하면, 더욱 다양한 모듈이 제공됨을 알 수 있다.
Posted by 참향그늘
,