¢« ¢¬ TclÎã (¹ÔÎó¤ÎÀÑ) ¢ª

proc mmult {m1 m2} {
    set m2rows [llength $m2];
    set m2cols [llength [lindex $m2 0]];
    set m1rows [llength $m1];
    set m1cols [llength [lindex $m1 0]];
    if { $m1cols != $m2rows || $m1rows != $m2cols } {
        error "Matrix dimensions do not match!";
    }
    foreach row1 $m1 {
        set row {};
        for { set i 0 } { $i < $m2cols } { incr i } {
            set j 0;
            set element 0;
            foreach row2 $m2 {
                incr element [expr [lindex $row1 $j] * [lindex $row2 $i]];
                incr j;
            }
            lappend row $element;
        }
        lappend result $row;
    }
    return $result;
}