#!/usr/bin/wish # assume that the MathViews/Tcl package was loaded already, presumeably # in a startup script automatically sourced by Tcl. Otherwise, uncomment # the appropriate load command below: # on unix: # load libmxt.so # on windows: # load mxt.dll # transform the original matrix by scaling and translating it. the # elements of the resulting matrix should range between [ 0 max_int ]. # here, we use mexpr to create a MathViews function dynamically. mexpr { function [ r , g , b ] = clrmap( num_colors ) % black-red-yellow-white colormap third_num_colors = floor( num_colors / 3 ); r = ones(num_colors,1); r(1:third_num_colors+1) = [ 0 : 1/(third_num_colors) : 1 ]'; r = round( r * 255 ); g = r >> third_num_colors; b = g >> third_num_colors; } mexpr { function [ cx ] = normat( x, max_int ) % determine range max_x = max(x(:)); min_x = min(x(:)); range_x = max_x - min_x; % offset and scale nx = ( x - min_x ) / range_x; % map the values of nx into integers in the range [0 max_int] cx = round( nx * ( max_int - 1 ) ) + 1; end; } mexpr { function [ Rx, Gx, Bx ] = color_mat( x, r , g , b ) % transform the matrix tx = normat( x , length(r) ); Rx = x; Gx = x; Bx = x; % decompose the transformed matrix into color components Rx(:) = r(tx(:)); Gx(:) = g(tx(:)); Bx(:) = b(tx(:)); end; } proc compute_matrices { mat mat_size } { # generate a random matrix and assoicated color decompositions mexpr "$mat = rand($mat_size);" mexpr "\[ R$mat, G$mat, B$mat \] = color_mat( $mat, r, g, b );" # do the same for its inverse set inv ${mat}inv mexpr "$inv = inv($mat);" mexpr "\[ R$inv, G$inv, B$inv \] = color_mat( $inv, r, g, b );" # return the name of the inverse matrix return $inv } proc draw_matrix { mat mat_size pixel_size cnvs } { # extract the r,g,b matrices in column order (the default) # and two character- wide hexidecimal format. # assume that the red, green, and blue matrices associated with # the input matrix 'mat' are R$mat, G$mat, and B$mat set r [ m2t -format %0.2x R$mat ] set g [ m2t -format %0.2x G$mat ] set b [ m2t -format %0.2x B$mat ] set canv_size [ expr $pixel_size * $mat_size ] # create two canvases, one for the matrix x and another for its # inverse $cnvs configure -height $canv_size -width $canv_size for { set xp 0; set j 0} { $j < $mat_size } { incr xp $pixel_size; incr j } { set r_j [ lindex $r $j ] set g_j [ lindex $g $j ] set b_j [ lindex $b $j ] for { set yp 0; set i 0 } { $i < $mat_size } { incr yp $pixel_size; incr i } { set r_color [ lindex $r_j $i ] set g_color [ lindex $g_j $i ] set b_color [ lindex $b_j $i ] set color \#$r_color$g_color$b_color $cnvs create rectangle $xp $yp [ expr $xp + $pixel_size ] \ [ expr $yp + $pixel_size ] -fill $color -outline "" } } } set mat_size 40 set pix_size 4 set canv_size [ expr $mat_size * $pix_size ] canvas .org -height $canv_size -width $canv_size canvas .inv -height $canv_size -width $canv_size pack .org .inv -side left # create the colormap once mexpr { [ r, g, b ] = clrmap( 128 ); } proc doit {} { global mat_size global pix_size set mat_size [ expr abs($mat_size) ] puts -nonewline "computing matrices ... " flush stdout set etime [ clock seconds ] set invx [ compute_matrices x $mat_size ] set etime [ expr [ clock seconds ] - $etime ] puts "done ($etime seconds)." puts -nonewline "drawing matrices ..... " flush stdout set etime [ clock seconds ] draw_matrix x $mat_size $pix_size .org draw_matrix $invx $mat_size $pix_size .inv set etime [ expr [ clock seconds ] - $etime ] puts "done ($etime seconds)." } # build the gui widgets toplevel .t entry .t.eMatSize -textvariable mat_size -width 4 entry .t.ePixSize -textvariable pix_size -width 5 label .t.lMatSize -text "Matrix size:" label .t.lPixSize -text "Pixels per element:" button .t.comp -text "Compute" -command { doit } button .t.exit -text "Quit" -command { exit } # arrange them in a 3x2 grid grid .t.lMatSize -row 0 -column 0 -sticky nsew grid .t.eMatSize -row 0 -column 1 -sticky nsew grid .t.lPixSize -row 1 -column 0 -sticky nsew grid .t.ePixSize -row 1 -column 1 -sticky nsew grid .t.comp -row 2 -column 0 -sticky nsew grid .t.exit -row 2 -column 1 -sticky nsew