TABLE OF CONTENTS


::pwtk::invert3x3

SYNOPSIS

proc ::pwtk::invert3x3 {m33} {

PURPOSE

Invert a 3x3 matrix.

ARGUMENT

RETURN VALUE

SOURCE

    if { [llength $m33] == 3 } {
        # do nothing
    } elseif { [llength $m33] == 9 } {
        set m33 [list [lrange $m33 0 2] [lrange $m33 3 5] [lrange $m33 6 8]]
    } else {
        ::pwtk::error "$m33 is not a 3x3 matrix" 1
    }
    for {set i 0} {$i<3} {incr i} {
        for {set j 0} {$j<3} {incr j} {
            set m($i,$j) [lindex [lindex $m33 $i] $j]
        }
    }
    
    set mm(0,0) [expr $m(1,1)*$m(2,2) - $m(1,2)*$m(2,1)]
    set mm(0,1) [expr $m(1,2)*$m(2,0) - $m(1,0)*$m(2,2)]
    set mm(0,2) [expr $m(1,0)*$m(2,1) - $m(2,0)*$m(1,1)]
    set mm(1,1) [expr $m(0,0)*$m(2,2) - $m(0,2)*$m(2,0)]
    set mm(1,2) [expr $m(2,0)*$m(0,1) - $m(0,0)*$m(2,1)]
    set mm(2,2) [expr $m(0,0)*$m(1,1) - $m(0,1)*$m(1,0)]
    set mm(1,0) [expr $m(2,1)*$m(0,2) - $m(0,1)*$m(2,2)]
    set mm(2,0) [expr $m(0,1)*$m(1,2) - $m(1,1)*$m(0,2)]
    set mm(2,1) [expr $m(0,2)*$m(1,0) - $m(0,0)*$m(1,2)]

    set det [expr double($m(0,0)*$mm(0,0) + $m(0,1)*$mm(0,1) + $m(0,2)*$mm(0,2))]
    if { $det < 1e-20 } {
        ::pwtk::error "colinear 3x3 matrix" 1
    }

    return [list \
                [list [expr $mm(0,0)/$det] [expr $mm(1,0)/$det] [expr $mm(2,0)/$det]] \
                [list [expr $mm(0,1)/$det] [expr $mm(1,1)/$det] [expr $mm(2,1)/$det]] \
                [list [expr $mm(0,2)/$det] [expr $mm(1,2)/$det] [expr $mm(2,2)/$det]] ]
}