Matrix transposition: Difference between revisions

Content added Content deleted
m (→‎{{header|Tcl}}: Improve the matrix printer to the level used in the QR decomposition task)
Line 1,569: Line 1,569:
return $new
return $new
}
}
proc print_matrix {m} {
proc print_matrix {m {fmt "%.17g"}} {
set max [widest $m]
set max [widest $m $fmt]
lassign [size $m] rows cols
lassign [size $m] rows cols
for {set i 0} {$i < $rows} {incr i} {
for {set i 0} {$i < $rows} {incr i} {
for {set j 0} {$j < $cols} {incr j} {
for {set j 0} {$j < $cols} {incr j} {
puts -nonewline [format "%*s " [lindex $max $j] [lindex $m $i $j]]
set s [format $fmt [lindex $m $i $j]]
puts -nonewline [format "%*s " [lindex $max $j] $s]
}
}
puts ""
puts ""
}
}
}
}
proc widest {m} {
proc widest {m {fmt "%.17g"}} {
lassign [size $m] rows cols
lassign [size $m] rows cols
set max [lrepeat $cols 0]
set max [lrepeat $cols 0]
for {set i 0} {$i < $rows} {incr i} {
for {set i 0} {$i < $rows} {incr i} {
for {set j 0} {$j < $cols} {incr j} {
for {set j 0} {$j < $cols} {incr j} {
lset max $j [max [lindex $max $j] [string length [lindex $m $i $j]]]
set s [format $fmt [lindex $m $i $j]]
lset max $j [max [lindex $max $j] [string length $s]]
}
}
}
}
Line 1,591: Line 1,593:


set m {{1 1 1 1} {2 4 8 16} {3 9 27 81} {4 16 64 256} {5 25 125 625}}
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
print_matrix $m "%d"
print_matrix [transpose $m]</lang>
print_matrix [transpose $m] "%d"</lang>
outputs
outputs
<pre>1 1 1 1
<pre>1 1 1 1