Hi Andrew, Joel
I'll throw in one more implementation of transpose. Way back last January
and December, Gerald Goertzel and I wrote many routines for linear algebra
and matrix manipulation. We tested many versions of basic matrix functions
like add, multiply, transpose and inverse for speed. Jerry won the speed
contest for transpose with this function which assumes matrices are in the
form created by the ARRAY function. (row major order)
transpose: func [m /local r c mt] [
r: length? m
c: length? m/1
mt: array reduce [c r]
repeat i r [
repeat j c [
poke pick mt j i pick pick m i j
]
]
mt
]
Enjoy
-Larry
----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, October 06, 2000 2:11 AM
Subject: [REBOL] Transpose
> Joel wrote:
> > Finally, I'd considered handling the structure-traversing issues
> separately from the data-manipulation issues, by defining (this version is
> VERY quick and dirty):
> >
> > >> transpose: function [b [block!]] [w i s r] [
> > [ r: make block! w: accum length? b/1 map b :length? :min
> > [ repeat i w [
> > [ s: make block! length? b
> > [ foreach c b [append/only s c/:i]
> > [ append/only r s
> > [ ]
> > [ r
> > [ ]
> > >> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]]
> > == [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
> > >> transpose [[1 2 3] [14 15 16] [27 28 29]]
> > == [[1 14 27] [2 15 28] [3 16 29]]
>
> I've cleaned it up a bit:
>
> [
> Rebol [
> Name: 'Transpose
> Title: "Transpose"
> File: %"Transpose.r"
> Author: "Andrew Martin"
> eMail: [EMAIL PROTECTED]
> Date: 5/October/2000
> Enhancement: 'Transpose
> Acknowledgements: "Joel Neely"
> Example: [
> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]]
> ; [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
> transpose [[1 2 3] [14 15 16] [27 28 29]]
> ; [[1 14 27] [2 15 28] [3 16 29]]
> ]
> ]
>
> Transpose: function [
> [catch]
> "Transposes Matrices"
> Matrix [block!]
> ][
> Results Width Height Column
> ][
> Results: make block! Width: length? Matrix/1
> Height: length? Matrix
> repeat Index Width [
> Column: make block! Width
> foreach Row Matrix [
> insert/only tail Column Row/:Index
> ]
> insert/only tail Results Column
> ]
> Results
> ]
>
> ]
>
> >> transpose [[1 2 3 4 5] ["a" "b" "c" "d" "e"]]
> == [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
> >> ; [[1 "a"] [2 "b"] [3 "c"] [4 "d"] [5 "e"]]
> >> transpose [[1 2 3] [14 15 16] [27 28 29]]
> == [[1 14 27] [2 15 28] [3 16 29]]
> >> ; [[1 14 27] [2 15 28] [3 16 29]]
>
> > Critiques/comments/debugging welcome!
>
> I totally agree. :-)
>
> Andrew Martin
> ICQ: 26227169
> http://members.nbci.com/AndrewMartin/
> -><-
>
>