TABLE OF CONTENTS
::pwtk::matricize
SYNOPSIS
proc ::pwtk::matricize {m} {
PURPOSE
Transform a 2x2, 2x3, or 3x3 matrix written in various representations into the math::linearalgebra representation. A 3x3 matrix in the math::linearalgebra representation is:
{{x1 y1 z1} {x2 y2 z2} {x3 y3 z3}}
For 2D, the routine transforms:
"NxM" into "{N 0} {M 0}"
or
x1.y1_x2.y2 into "{x1 y1} {x2 y2}"
or
"1 2 3 4" into "{1 2} {3 4}"
For 3D, the routine tranforms:
"NxMxL" into "{N 0 0} {0 M 0} {0 0 L}"
or
x1.y1.z1_x2.y2.z2_x3.y3.z3 into "{x1 y1 z1} {x2 y2 z2} {x3 y3 z3}"
or
"1 2 3 4 5 6 7 8 9" into "{1 2 3} {4 5 6} {7 8 9}"
If the matrix is already in the math::linearalgebra representation, e.g., {{x1 y1 z1} {x2 y2 z2} {x3 y3 z3}}, it is returned verbatim.
BEWARE
Use the x1.y1.z1_x2.y2.z2_x3.y3.z3 form only for integer matrices because real matrices will be wrongly parsed due to too many "." characters.
RETURN VALUE
Matrix $m in the math::linearalgebra representation, i.e.:
{ {m11 m12 m13} {m21 m22 m23} {m31 m32 m33} }
SOURCE
if { [string match *x* $m] } { lassign [split $m x] a1 a2 a3 if { $a3 ne {} } { return [list "$a1 0.0 0.0" "0.0 $a2 0.0" "0.0 0.0 $a3"] } else { return [list "$a1 0.0" "0.0 $a2"] } } set len [llength $m] if { $len == 1 && [string match *_* $m] } { lassign [split $m _] v1 v2 v3 if { $v3 ne {} } { return [list [split $v1 .] [split $v2 .] [split $v3 .]]; # 3x3 matrix } else { return [list [split $v1 .] [split $v2 .]]; # 2x2 matix } } elseif { $len == 4 } { #return [::math::linearalgebra::from_LA [list 2 2 2 {*}$m]] return [list [lrange $m 0 1] [lrange $m 2 3]] } elseif { $len == 6 } { #return [::math::linearalgebra::from_LA [list 2 2 3 {*}$m]] return [list [lrange $m 0 2] [lrange $m 3 5]] } elseif { $len == 9 } { #return [::math::linearalgebra::from_LA [list 2 3 3 {*}$m]] return [list [lrange $m 0 2] [lrange $m 3 5] [lrange $m 6 8]] } return $m }