Matrix transposition: Difference between revisions

No edit summary
(→‎{{header|Wren}}: Minor tidy)
 
(13 intermediate revisions by 8 users not shown)
Line 442:
{{trans|JavaScript}}
 
<syntaxhighlight lang="applescript">-- TRANSPOSE ------------------------------------------ TRANSPOSE -----------------------
 
-- transpose :: [[a]] -> [[a]]
Line 462:
 
 
-- TEST --------------------------------------------- TEST -------------------------
on run
transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
Line 469:
end run
 
 
-- GENERIC FUNCTIONS ---------------------------------------------------------
-------------------- GENERIC FUNCTIONS -------------------
 
-- map :: (a -> b) -> [a] -> [b]
Line 1,370 ⟶ 1,371:
=={{header|Delphi}}==
See [[#Pascal]];
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
proc transpose . m[][] .
len n[][] len m[1][]
for i to len n[][]
for j to len m[][]
n[i][] &= m[j][i]
.
.
swap n[][] m[][]
.
m[][] = [ [ 1 2 3 4 ] [ 5 6 7 8 ] [ 9 10 11 12 ] ]
print m[][]
print ""
transpose m[][]
print m[][]
</syntaxhighlight>
{{out}}
<pre>
[
[ 1 2 3 4 ]
[ 5 6 7 8 ]
[ 9 10 11 12 ]
]
 
[
[ 1 5 9 ]
[ 2 6 10 ]
[ 3 7 11 ]
[ 4 8 12 ]
]
</pre>
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
Line 1,663 ⟶ 1,697:
(4 6 9)
(5 9 9))
</pre>
 
Implementation using seq library:
 
<syntaxhighlight lang="lisp">
(defun matrix-transposition (m)
(apply #'seq-mapn (append (list #'list) m)) )
 
(let ((m '(( 2 0 -5 -1)
(-3 -2 -4 7)
(-1 -3 0 -6))))
(message "%s" (matrix-transposition m)) )
</syntaxhighlight>
 
{{out}}
 
<pre>
((2 -3 -1) (0 -2 -3) (-5 -4 0) (-1 7 -6))
</pre>
 
Line 1,990 ⟶ 2,042:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Matrix_transposition}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
Matrix transposition is an intrsinec operation in Fōrmulæ, through the Transpose expression:
In '''[https://formulae.org/?example=Matrix_transposition this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Matrix transposition 01.png]]
 
[[File:Fōrmulæ - Matrix transposition 02.png]]
 
However, a matrix transposition can be coded:
 
[[File:Fōrmulæ - Matrix transposition 03.png]]
 
[[File:Fōrmulæ - Matrix transposition 04.png]]
 
[[File:Fōrmulæ - Matrix transposition 02.png]]
 
=={{header|GAP}}==
Line 2,283 ⟶ 2,347:
[[1,4,7],[2,5,8],[3,6,9]]
</pre>
 
or, in terms of Data.Matrix:
 
<syntaxhighlight lang="haskell">import Data.Matrix
 
main :: IO ()
main = print matrix >> print (transpose matrix)
where
matrix = fromList 3 4 [1 ..]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
│ 1 2 3 4 │
│ 5 6 7 8 │
│ 9 10 11 12 │
└ ┘
┌ ┐
│ 1 5 9 │
│ 2 6 10 │
│ 3 7 11 │
│ 4 8 12 │
└ ┘</pre>
 
===With Numeric.LinearAlgebra===
Line 2,405 ⟶ 2,490:
<syntaxhighlight lang="idris">Idris> transpose [[1,2],[3,4],[5,6]]
[[1, 3, 5], [2, 4, 6]] : List (List Integer)</syntaxhighlight>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(var transpose2d @(... map vec))
 
(transpose2d [[1 1 1 1] [2 4 8 16] [3 9 27 81] [4 16 64 256] [5 25 125 625]])
</syntaxhighlight>
 
{{out}}
 
<pre>
[[1 2 3 4 5] [1 4 9 16 25] [1 8 27 64 125] [1 16 81 256 625]]
</pre>
 
=={{header|J}}==
Line 2,734 ⟶ 2,833:
}
}</syntaxhighlight>
 
 
=={{header|Lambdatalk}}==
 
<syntaxhighlight lang="scheme">
{require lib_matrix}
 
{M.disp
{M.transp
[[1, 1, 1, 1],
[2, 4, 8, 16],
[3, 9, 27, 81],
[4, 16, 64, 256],
[5, 25,125, 625]]
}}
->
[[1,2,3,4,5],
[1,4,9,16,25],
[1,8,27,64,125],
[1,16,81,256,625]]
 
</syntaxhighlight>
 
=={{header|Lang5}}==
Line 3,067 ⟶ 3,188:
[| 5; 6; 7; 8 |];
|]
;;
 
array2_display (Printf.printf " %d") print_newline (transpose a) ;;</syntaxhighlight>
Line 4,133 ⟶ 4,255:
0.226388925 0.693482786 0.11557427
0.963880314 0.203839405 0.0442493069</syntaxhighlight>
 
=={{header|RPL}}==
[[1 2 3 4][5 6 7 8][9 10 11 12]] TRN
{{out}}
<pre>
[[1 5 9] [2 6 10] [3 7 11] [4 8 12]]
</pre>
 
=={{header|Ruby}}==
Line 4,896 ⟶ 5,025:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./matrix" for Matrix
import "./fmt" for Fmt
 
var m = Matrix.new([
9,476

edits