Jump to content

Matrix transposition: Difference between revisions

m
→‎{{header|Tcl}}: Improve the matrix printer to the level used in the QR decomposition task
m (→‎{{header|Tcl}}: Improve the matrix printer to the level used in the QR decomposition task)
Line 1,569:
return $new
}
proc print_matrix {m {fmt "%.17g"}} {
set max [widest $m $fmt]
lassign [size $m] rows cols
for {set i 0} {$i < $rows} {incr i} {
for {set j 0} {$j < $cols} {incr j} {
set puts -nonewlines [format "%*s " [lindex $max $j]fmt [lindex $m $i $j]]
puts -nonewline [format "%*s " [lindex $max $j] $s]
}
puts ""
}
}
proc widest {m {fmt "%.17g"}} {
lassign [size $m] rows cols
set max [lrepeat $cols 0]
for {set i 0} {$i < $rows} {incr i} {
for {set j 0} {$j < $cols} {incr j} {
set lset max $js [max [lindexformat $max $j] [string lengthfmt [lindex $m $i $j]]]
lset max $j [max [lindex $max $j] [string length $s]]
}
}
Line 1,591 ⟶ 1,593:
 
set m {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}
print_matrix $m "%d"
print_matrix [transpose $m] "%d"</lang>
outputs
<pre>1 1 1 1
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.