Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Matrix-transposition/00-META.yaml
Normal file
3
Task/Matrix-transposition/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Matrix_transposition
|
||||
note: Matrices
|
||||
2
Task/Matrix-transposition/00-TASK.txt
Normal file
2
Task/Matrix-transposition/00-TASK.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[[wp:Transpose|Transpose]] an arbitrarily sized rectangular [[wp:Matrix (mathematics)|Matrix]].
|
||||
<br><br>
|
||||
12
Task/Matrix-transposition/11l/matrix-transposition.11l
Normal file
12
Task/Matrix-transposition/11l/matrix-transposition.11l
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
F transpose(&matrix)
|
||||
V toRet = [[0] * matrix.len] * matrix[0].len
|
||||
L(row) (0 .< matrix.len)
|
||||
L(col) (0 .< matrix[row].len)
|
||||
toRet[col][row] = matrix[row][col]
|
||||
R toRet
|
||||
|
||||
V m = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
|
||||
print("Original")
|
||||
print(m)
|
||||
print("After Transposition")
|
||||
print(transpose(&m))
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
...
|
||||
KN EQU 3
|
||||
KM EQU 5
|
||||
N DC AL2(KN)
|
||||
M DC AL2(KM)
|
||||
A DS (KN*KM)F matrix a(n,m)
|
||||
B DS (KM*KN)F matrix b(m,n)
|
||||
...
|
||||
* b(j,i)=a(i,j)
|
||||
* transposition using Horner's formula
|
||||
LA R4,0 i,from 1
|
||||
LA R7,KN to n
|
||||
LA R6,1 step 1
|
||||
LOOPI BXH R4,R6,ELOOPI do i=1 to n
|
||||
LA R5,0 j,from 1
|
||||
LA R9,KM to m
|
||||
LA R8,1 step 1
|
||||
LOOPJ BXH R5,R8,ELOOPJ do j=1 to m
|
||||
LR R1,R4 i
|
||||
BCTR R1,0 i-1
|
||||
MH R1,M (i-1)*m
|
||||
LR R2,R5 j
|
||||
BCTR R2,0 j-1
|
||||
AR R1,R2 r1=(i-1)*m+(j-1)
|
||||
SLA R1,2 r1=((i-1)*m+(j-1))*itemlen
|
||||
L R0,A(R1) r0=a(i,j)
|
||||
LR R1,R5 j
|
||||
BCTR R1,0 j-1
|
||||
MH R1,N (j-1)*n
|
||||
LR R2,R4 i
|
||||
BCTR R2,0 i-1
|
||||
AR R1,R2 r1=(j-1)*n+(i-1)
|
||||
SLA R1,2 r1=((j-1)*n+(i-1))*itemlen
|
||||
ST R0,B(R1) b(j,i)=r0
|
||||
B LOOPJ next j
|
||||
ELOOPJ EQU * out of loop j
|
||||
B LOOPI next i
|
||||
ELOOPI EQU * out of loop i
|
||||
...
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
Transpose2DArray_B:
|
||||
;INPUT:
|
||||
;A0 = POINTER TO SOURCE ARRAY
|
||||
;A1 = POINTER TO BACKUP AREA
|
||||
; (YOU NEED THE SAME AMOUNT OF FREE SPACE AS THE SOURCE ARRAY.)
|
||||
; (IT'S YOUR RESPONSIBILITY TO KNOW WHERE THAT IS.)
|
||||
;D0.W = ARRAY ROW LENGTH-1
|
||||
;D1.W = ARRAY COLUMN HEIGHT-1
|
||||
|
||||
MOVEM.L D2-D7,-(SP)
|
||||
MOVE.W D0,D4 ;width - this copy is our loop counter
|
||||
|
||||
.outerloop:
|
||||
MOVE.W D1,D7 ;height
|
||||
MOVEQ.L #0,D3
|
||||
MOVE.W D0,D6 ;width - this copy is used to offset the array
|
||||
ADDQ.L #1,D6
|
||||
|
||||
.innerloop:
|
||||
MOVE.B (A0,D3),(A1)+
|
||||
ADD.W D6,D3
|
||||
DBRA D7,.innerloop
|
||||
|
||||
ADDA.L #1,A0
|
||||
DBRA D4,.outerloop
|
||||
|
||||
MOVEM.L (SP)+,D2-D7
|
||||
RTS
|
||||
17
Task/Matrix-transposition/ACL2/matrix-transposition.acl2
Normal file
17
Task/Matrix-transposition/ACL2/matrix-transposition.acl2
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(defun cons-each (xs xss)
|
||||
(if (or (endp xs) (endp xss))
|
||||
nil
|
||||
(cons (cons (first xs) (first xss))
|
||||
(cons-each (rest xs) (rest xss)))))
|
||||
|
||||
(defun list-each (xs)
|
||||
(if (endp xs)
|
||||
nil
|
||||
(cons (list (first xs))
|
||||
(list-each (rest xs)))))
|
||||
|
||||
(defun transpose-list (xss)
|
||||
(if (endp (rest xss))
|
||||
(list-each (first xss))
|
||||
(cons-each (first xss)
|
||||
(transpose-list (rest xss)))))
|
||||
27
Task/Matrix-transposition/ALGOL-68/matrix-transposition.alg
Normal file
27
Task/Matrix-transposition/ALGOL-68/matrix-transposition.alg
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
main:(
|
||||
|
||||
[,]REAL m=((1, 1, 1, 1),
|
||||
(2, 4, 8, 16),
|
||||
(3, 9, 27, 81),
|
||||
(4, 16, 64, 256),
|
||||
(5, 25,125, 625));
|
||||
|
||||
OP ZIP = ([,]REAL in)[,]REAL:(
|
||||
[2 LWB in:2 UPB in,1 LWB in:1UPB in]REAL out;
|
||||
FOR i FROM LWB in TO UPB in DO
|
||||
out[,i]:=in[i,]
|
||||
OD;
|
||||
out
|
||||
);
|
||||
|
||||
PROC pprint = ([,]REAL m)VOID:(
|
||||
FORMAT real fmt = $g(-6,2)$; # width of 6, with no '+' sign, 2 decimals #
|
||||
FORMAT vec fmt = $"("n(2 UPB m-1)(f(real fmt)",")f(real fmt)")"$;
|
||||
FORMAT matrix fmt = $x"("n(UPB m-1)(f(vec fmt)","lxx)f(vec fmt)");"$;
|
||||
# finally print the result #
|
||||
printf((matrix fmt,m))
|
||||
);
|
||||
|
||||
printf(($x"Transpose:"l$));
|
||||
pprint((ZIP m))
|
||||
)
|
||||
8
Task/Matrix-transposition/APL/matrix-transposition.apl
Normal file
8
Task/Matrix-transposition/APL/matrix-transposition.apl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
3 3⍴⍳10
|
||||
1 2 3
|
||||
4 5 6
|
||||
7 8 9
|
||||
⍉ 3 3⍴⍳10
|
||||
1 4 7
|
||||
2 5 8
|
||||
3 6 9
|
||||
14
Task/Matrix-transposition/AWK/matrix-transposition-1.awk
Normal file
14
Task/Matrix-transposition/AWK/matrix-transposition-1.awk
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# syntax: GAWK -f MATRIX_TRANSPOSITION.AWK filename
|
||||
{ if (NF > nf) {
|
||||
nf = NF
|
||||
}
|
||||
for (i=1; i<=nf; i++) {
|
||||
row[i] = row[i] $i " "
|
||||
}
|
||||
}
|
||||
END {
|
||||
for (i=1; i<=nf; i++) {
|
||||
printf("%s\n",row[i])
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
29
Task/Matrix-transposition/AWK/matrix-transposition-2.awk
Normal file
29
Task/Matrix-transposition/AWK/matrix-transposition-2.awk
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Usage: GAWK -f MATRIX_TRANSPOSITION.AWK filename
|
||||
{
|
||||
i = NR
|
||||
for (j = 1; j <= NF; j++) {
|
||||
a[i,j] = $j
|
||||
}
|
||||
ranka1 = i
|
||||
ranka2 = max(ranka2, NF)
|
||||
}
|
||||
END {
|
||||
rankb1 = ranka2
|
||||
rankb2 = ranka1
|
||||
b[rankb1, rankb2] = 0
|
||||
transpose_matrix(b, a)
|
||||
for (i = 1; i <= rankb1; i++) {
|
||||
for (j = 1; j <= rankb2; j++) {
|
||||
printf("%g%c", b[i,j], j < rankb2 ? " " : "\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
function transpose_matrix(target, source, key, idx) {
|
||||
for (key in source) {
|
||||
split(key, idx, SUBSEP)
|
||||
target[idx[2], idx[1]] = source[idx[1], idx[2]]
|
||||
}
|
||||
}
|
||||
function max(m, n) {
|
||||
return m > n ? m : n
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
DEFINE PTR="CARD"
|
||||
|
||||
TYPE Matrix=[
|
||||
BYTE width,height
|
||||
PTR data] ;BYTE ARRAY
|
||||
|
||||
PROC PrintB2(BYTE b)
|
||||
IF b<10 THEN Put(32) FI
|
||||
PrintB(b)
|
||||
RETURN
|
||||
|
||||
PROC PrintMatrix(Matrix POINTER m)
|
||||
BYTE i,j
|
||||
BYTE ARRAY d
|
||||
|
||||
d=m.data
|
||||
FOR j=0 TO m.height-1
|
||||
DO
|
||||
FOR i=0 TO m.width-1
|
||||
DO
|
||||
PrintB2(d(j*m.width+i)) Put(32)
|
||||
OD
|
||||
PutE()
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Create(MATRIX POINTER m BYTE w,h BYTE ARRAY a)
|
||||
m.width=w
|
||||
m.height=h
|
||||
m.data=a
|
||||
RETURN
|
||||
|
||||
PROC Transpose(Matrix POINTER in,out)
|
||||
BYTE i,j
|
||||
BYTE ARRAY din,dout
|
||||
|
||||
din=in.data
|
||||
dout=out.data
|
||||
out.width=in.height
|
||||
out.height=in.width
|
||||
FOR j=0 TO in.height-1
|
||||
DO
|
||||
FOR i=0 TO in.width-1
|
||||
DO
|
||||
dout(i*out.width+j)=din(j*in.width+i)
|
||||
OD
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
MATRIX in,out
|
||||
BYTE ARRAY din(35),dout(35)
|
||||
BYTE i
|
||||
|
||||
FOR i=0 TO 34
|
||||
DO
|
||||
din(i)=i
|
||||
OD
|
||||
Create(in,7,5,din)
|
||||
Create(out,0,0,dout)
|
||||
Transpose(in,out)
|
||||
|
||||
PrintE("Input:")
|
||||
PrintMatrix(in)
|
||||
PutE() PrintE("Transpose:")
|
||||
PrintMatrix(out)
|
||||
RETURN
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
function transpose( m:Array):Array
|
||||
{
|
||||
//Assume each element in m is an array. (If this were production code, use typeof to be sure)
|
||||
|
||||
//Each element in m is a row, so this gets the length of a row in m,
|
||||
//which is the same as the number of rows in m transpose.
|
||||
var mTranspose = new Array(m[0].length);
|
||||
for(var i:uint = 0; i < mTranspose.length; i++)
|
||||
{
|
||||
//create a row
|
||||
mTranspose[i] = new Array(m.length);
|
||||
//set the row to the appropriate values
|
||||
for(var j:uint = 0; j < mTranspose[i].length; j++)
|
||||
mTranspose[i][j] = m[j][i];
|
||||
}
|
||||
return mTranspose;
|
||||
}
|
||||
var m:Array = [[1, 2, 3, 10],
|
||||
[4, 5, 6, 11],
|
||||
[7, 8, 9, 12]];
|
||||
var M:Array = transpose(m);
|
||||
for(var i:uint = 0; i < M.length; i++)
|
||||
trace(M[i]);
|
||||
27
Task/Matrix-transposition/Ada/matrix-transposition.ada
Normal file
27
Task/Matrix-transposition/Ada/matrix-transposition.ada
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
with Ada.Numerics.Real_Arrays; use Ada.Numerics.Real_Arrays;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
|
||||
procedure Matrix_Transpose is
|
||||
procedure Put (X : Real_Matrix) is
|
||||
type Fixed is delta 0.01 range -500.0..500.0;
|
||||
begin
|
||||
for I in X'Range (1) loop
|
||||
for J in X'Range (2) loop
|
||||
Put (Fixed'Image (Fixed (X (I, J))));
|
||||
end loop;
|
||||
New_Line;
|
||||
end loop;
|
||||
end Put;
|
||||
|
||||
Matrix : constant Real_Matrix :=
|
||||
( (0.0, 0.1, 0.2, 0.3),
|
||||
(0.4, 0.5, 0.6, 0.7),
|
||||
(0.8, 0.9, 1.0, 1.1)
|
||||
);
|
||||
begin
|
||||
Put_Line ("Before Transposition:");
|
||||
Put (Matrix);
|
||||
New_Line;
|
||||
Put_Line ("After Transposition:");
|
||||
Put (Transpose (Matrix));
|
||||
end Matrix_Transpose;
|
||||
14
Task/Matrix-transposition/Agda/matrix-transposition-1.agda
Normal file
14
Task/Matrix-transposition/Agda/matrix-transposition-1.agda
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
module Matrix where
|
||||
|
||||
open import Data.Nat
|
||||
open import Data.Vec
|
||||
|
||||
Matrix : (A : Set) → ℕ → ℕ → Set
|
||||
Matrix A m n = Vec (Vec A m) n
|
||||
|
||||
transpose : ∀ {A m n} → Matrix A m n → Matrix A n m
|
||||
transpose [] = replicate []
|
||||
transpose (xs ∷ xss) = zipWith _∷_ xs (transpose xss)
|
||||
|
||||
a = (1 ∷ 2 ∷ 3 ∷ []) ∷ (4 ∷ 5 ∷ 6 ∷ []) ∷ []
|
||||
b = transpose a
|
||||
|
|
@ -0,0 +1 @@
|
|||
(1 ∷ 4 ∷ []) ∷ (2 ∷ 5 ∷ []) ∷ (3 ∷ 6 ∷ []) ∷ []
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include<hopper.h>
|
||||
#proto showarraydata(_X_)
|
||||
main:
|
||||
.stack 12
|
||||
nCols=0, nRows=0,nDims=0
|
||||
A=-1,{5,11} rand array(A),mulby(10),ceil,mov(A)
|
||||
{"ORIGINAL ARRAY :\n",A}
|
||||
|
||||
_show array data(A)
|
||||
|
||||
/* transpose */
|
||||
TA=0,{nCols,nRows} nan array(TA)
|
||||
Limit = nRows
|
||||
{nRows}gthan(nCols) do{ Limit = nCols }
|
||||
|
||||
for (i=1, {i} lethan (Limit), ++i)
|
||||
[i,i:end]get(A), [i:end,i]put(TA)
|
||||
[i:end,i]get(A), [i,i:end]put(TA)
|
||||
next
|
||||
clear mark
|
||||
{"ARRAY TRANSPOSE:\n",TA}println
|
||||
_show array data(TA)
|
||||
exit(0)
|
||||
|
||||
.locals
|
||||
show array data(A)
|
||||
{"\nSIZE ARRAY : "},size=0,size(A),cpy(size),
|
||||
dims(size,nDims)
|
||||
rows(size,nRows)
|
||||
cols(size,nCols)
|
||||
{"\nDIMENSION = ",nDims,"; ROWS = ",nRows,"; COLS = ",nCols,"\n"}, println
|
||||
back
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
on run
|
||||
transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
|
||||
|
||||
--> {{1, 4, 7, 10}, {2, 5, 8, 11}, {3, 6, 9, 12}}
|
||||
end run
|
||||
|
||||
on transpose(xss)
|
||||
set lstTrans to {}
|
||||
|
||||
repeat with iCol from 1 to length of item 1 of xss
|
||||
set lstCol to {}
|
||||
|
||||
repeat with iRow from 1 to length of xss
|
||||
set end of lstCol to item iCol of item iRow of xss
|
||||
end repeat
|
||||
|
||||
set end of lstTrans to lstCol
|
||||
end repeat
|
||||
|
||||
return lstTrans
|
||||
end transpose
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
------------------------ TRANSPOSE -----------------------
|
||||
|
||||
-- transpose :: [[a]] -> [[a]]
|
||||
on transpose(xss)
|
||||
script column
|
||||
on |λ|(_, iCol)
|
||||
script row
|
||||
on |λ|(xs)
|
||||
item iCol of xs
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(row, xss)
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(column, item 1 of xss)
|
||||
end transpose
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
on run
|
||||
transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
|
||||
|
||||
--> {{1, 4, 7, 10}, {2, 5, 8, 11}, {3, 6, 9, 12}}
|
||||
end run
|
||||
|
||||
|
||||
-------------------- GENERIC FUNCTIONS -------------------
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1 @@
|
|||
{{1, 4, 7, 10}, {2, 5, 8, 11}, {3, 6, 9, 12}}
|
||||
22
Task/Matrix-transposition/Arturo/matrix-transposition.arturo
Normal file
22
Task/Matrix-transposition/Arturo/matrix-transposition.arturo
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
transpose: function [a][
|
||||
X: size a
|
||||
Y: size first a
|
||||
result: array.of: @[Y X] 0
|
||||
|
||||
loop 0..X-1 'i [
|
||||
loop 0..Y-1 'j [
|
||||
result\[j]\[i]: a\[i]\[j]
|
||||
]
|
||||
]
|
||||
return result
|
||||
]
|
||||
|
||||
arr: [
|
||||
[ 0 1 2 3 4 ]
|
||||
[ 5 6 7 8 9 ]
|
||||
[ 1 0 0 0 42 ]
|
||||
]
|
||||
|
||||
loop arr 'row -> print row
|
||||
print "-------------"
|
||||
loop transpose arr 'row -> print row
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
a = a
|
||||
m = 10
|
||||
n = 10
|
||||
Loop, 10
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, 10
|
||||
{
|
||||
j := A_Index - 1
|
||||
%a%%i%%j% := i - j
|
||||
}
|
||||
}
|
||||
before := matrix_print("a", m, n)
|
||||
transpose("a", m, n)
|
||||
after := matrix_print("a", m, n)
|
||||
MsgBox % before . "`ntransposed:`n" . after
|
||||
Return
|
||||
|
||||
transpose(a, m, n)
|
||||
{
|
||||
Local i, j, row, matrix
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
temp%i%%j% := %a%%j%%i%
|
||||
}
|
||||
}
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
%a%%i%%j% := temp%i%%j%
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
matrix_print(a, m, n)
|
||||
{
|
||||
Local i, j, row, matrix
|
||||
Loop, % m
|
||||
{
|
||||
i := A_Index - 1
|
||||
row := ""
|
||||
Loop, % n
|
||||
{
|
||||
j := A_Index - 1
|
||||
row .= %a%%i%%j% . ","
|
||||
}
|
||||
StringTrimRight, row, row, 1
|
||||
matrix .= row . "`n"
|
||||
}
|
||||
Return matrix
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Transpose(M){
|
||||
R := []
|
||||
for i, row in M
|
||||
for j, col in row
|
||||
R[j,i] := col
|
||||
return R
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
Matrix := [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
|
||||
MsgBox % ""
|
||||
. "Original Matrix :`n" Print(Matrix)
|
||||
. "`nTransposed Matrix :`n" Print(Transpose(Matrix))
|
||||
|
||||
Print(M){
|
||||
for i, row in M
|
||||
for j, col in row
|
||||
Res .= (A_Index=1?"":"`t") col (Mod(A_Index,M[1].MaxIndex())?"":"`n")
|
||||
return Trim(Res,"`n")
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
arraybase 1
|
||||
dim matriz= {{78,19,30,12,36}, {49,10,65,42,50}, {30,93,24,78,10}, {39,68,27,64,29}}
|
||||
dim mtranspuesta(matriz[,?], matriz[?,])
|
||||
|
||||
for fila = 1 to matriz[?,]
|
||||
for columna = 1 to matriz[,?]
|
||||
print matriz[fila, columna]; " ";
|
||||
mtranspuesta[columna, fila] = matriz[fila, columna]
|
||||
next columna
|
||||
print
|
||||
next fila
|
||||
print
|
||||
|
||||
for fila = 1 to mtranspuesta[?,]
|
||||
for columna = 1 to mtranspuesta[,?]
|
||||
print mtranspuesta[fila, columna]; " ";
|
||||
next columna
|
||||
print
|
||||
next fila
|
||||
end
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
INSTALL @lib$+"ARRAYLIB"
|
||||
|
||||
DIM matrix(3,4), transpose(4,3)
|
||||
matrix() = 78,19,30,12,36,49,10,65,42,50,30,93,24,78,10,39,68,27,64,29
|
||||
|
||||
PROC_transpose(matrix(), transpose())
|
||||
|
||||
FOR row% = 0 TO DIM(matrix(),1)
|
||||
FOR col% = 0 TO DIM(matrix(),2)
|
||||
PRINT ;matrix(row%,col%) " ";
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT row%
|
||||
|
||||
PRINT
|
||||
|
||||
FOR row% = 0 TO DIM(transpose(),1)
|
||||
FOR col% = 0 TO DIM(transpose(),2)
|
||||
PRINT ;transpose(row%,col%) " ";
|
||||
NEXT
|
||||
PRINT
|
||||
NEXT row%
|
||||
12
Task/Matrix-transposition/BQN/matrix-transposition.bqn
Normal file
12
Task/Matrix-transposition/BQN/matrix-transposition.bqn
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
3‿3⥊↕9
|
||||
┌─
|
||||
╵ 0 1 2
|
||||
3 4 5
|
||||
6 7 8
|
||||
┘
|
||||
⍉3‿3⥊↕9
|
||||
┌─
|
||||
╵ 0 3 6
|
||||
1 4 7
|
||||
2 5 8
|
||||
┘
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
blsq ) {{78 19 30 12 36}{49 10 65 42 50}{30 93 24 78 10}{39 68 27 64 29}}tpsp
|
||||
78 49 30 39
|
||||
19 10 93 68
|
||||
30 65 24 27
|
||||
12 42 78 64
|
||||
36 50 10 29
|
||||
15
Task/Matrix-transposition/C++/matrix-transposition-1.cpp
Normal file
15
Task/Matrix-transposition/C++/matrix-transposition-1.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#include <boost/numeric/ublas/matrix.hpp>
|
||||
#include <boost/numeric/ublas/io.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::numeric::ublas;
|
||||
|
||||
matrix<double> m(3,3);
|
||||
|
||||
for(int i=0; i!=m.size1(); ++i)
|
||||
for(int j=0; j!=m.size2(); ++j)
|
||||
m(i,j)=3*i+j;
|
||||
|
||||
std::cout << trans(m) << std::endl;
|
||||
}
|
||||
39
Task/Matrix-transposition/C++/matrix-transposition-2.cpp
Normal file
39
Task/Matrix-transposition/C++/matrix-transposition-2.cpp
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#include <iostream>
|
||||
#include "matrix.h"
|
||||
|
||||
#if !defined(ARRAY_SIZE)
|
||||
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
template<class T>
|
||||
void printMatrix(const Matrix<T>& m) {
|
||||
std::cout << "rows = " << m.rowNum() << " columns = " << m.colNum() << std::endl;
|
||||
for (unsigned int i = 0; i < m.rowNum(); i++) {
|
||||
for (unsigned int j = 0; j < m.colNum(); j++) {
|
||||
std::cout << m[i][j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
} /* printMatrix() */
|
||||
|
||||
int main() {
|
||||
int am[2][3] = {
|
||||
{1,2,3},
|
||||
{4,5,6},
|
||||
};
|
||||
|
||||
Matrix<int> a(ARRAY_SIZE(am), ARRAY_SIZE(am[0]), am[0], ARRAY_SIZE(am)*ARRAY_SIZE(am[0]));
|
||||
|
||||
try {
|
||||
std::cout << "Before transposition:" << std::endl;
|
||||
printMatrix(a);
|
||||
std::cout << std::endl;
|
||||
a.transpose();
|
||||
std::cout << "After transposition:" << std::endl;
|
||||
printMatrix(a);
|
||||
} catch (MatrixException& e) {
|
||||
std::cerr << e.message() << std::endl;
|
||||
return e.errorCode();
|
||||
}
|
||||
|
||||
} /* main() */
|
||||
195
Task/Matrix-transposition/C++/matrix-transposition-3.cpp
Normal file
195
Task/Matrix-transposition/C++/matrix-transposition-3.cpp
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
#ifndef _MATRIX_H
|
||||
#define _MATRIX_H
|
||||
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#define MATRIX_ERROR_CODE_COUNT 5
|
||||
#define MATRIX_ERR_UNDEFINED "1 Undefined exception!"
|
||||
#define MATRIX_ERR_WRONG_ROW_INDEX "2 The row index is out of range."
|
||||
#define MATRIX_ERR_MUL_ROW_AND_COL_NOT_EQUAL "3 The row number of second matrix must be equal with the column number of first matrix!"
|
||||
#define MATRIX_ERR_MUL_ROW_AND_COL_BE_GREATER_THAN_ZERO "4 The number of rows and columns must be greater than zero!"
|
||||
#define MATRIX_ERR_TOO_FEW_DATA "5 Too few data in matrix."
|
||||
|
||||
class MatrixException {
|
||||
private:
|
||||
std::string message_;
|
||||
int errorCode_;
|
||||
public:
|
||||
MatrixException(std::string message = MATRIX_ERR_UNDEFINED);
|
||||
|
||||
inline std::string message() {
|
||||
return message_;
|
||||
};
|
||||
|
||||
inline int errorCode() {
|
||||
return errorCode_;
|
||||
};
|
||||
};
|
||||
|
||||
MatrixException::MatrixException(std::string message) {
|
||||
errorCode_ = MATRIX_ERROR_CODE_COUNT + 1;
|
||||
std::stringstream ss(message);
|
||||
ss >> errorCode_;
|
||||
if (errorCode_ < 1) {
|
||||
errorCode_ = MATRIX_ERROR_CODE_COUNT + 1;
|
||||
}
|
||||
std::string::size_type pos = message.find(' ');
|
||||
if (errorCode_ <= MATRIX_ERROR_CODE_COUNT && pos != std::string::npos) {
|
||||
message_ = message.substr(pos + 1);
|
||||
} else {
|
||||
message_ = message + " (This an unknown and unsupported exception!)";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic class for matrices.
|
||||
*/
|
||||
template <class T>
|
||||
class Matrix {
|
||||
private:
|
||||
std::vector<T> v; // the data of matrix
|
||||
unsigned int m; // the number of rows
|
||||
unsigned int n; // the number of columns
|
||||
protected:
|
||||
|
||||
virtual void clear() {
|
||||
v.clear();
|
||||
m = n = 0;
|
||||
}
|
||||
public:
|
||||
|
||||
Matrix() {
|
||||
clear();
|
||||
}
|
||||
Matrix(unsigned int, unsigned int, T* = 0, unsigned int = 0);
|
||||
Matrix(unsigned int, unsigned int, const std::vector<T>&);
|
||||
|
||||
virtual ~Matrix() {
|
||||
clear();
|
||||
}
|
||||
Matrix& operator=(const Matrix&);
|
||||
std::vector<T> operator[](unsigned int) const;
|
||||
Matrix operator*(const Matrix&);
|
||||
void transpose();
|
||||
|
||||
inline unsigned int rowNum() const {
|
||||
return m;
|
||||
}
|
||||
|
||||
inline unsigned int colNum() const {
|
||||
return n;
|
||||
}
|
||||
|
||||
inline unsigned int size() const {
|
||||
return v.size();
|
||||
}
|
||||
|
||||
inline void add(const T& t) {
|
||||
v.push_back(t);
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
Matrix<T>::Matrix(unsigned int row, unsigned int col, T* data, unsigned int dataLength) {
|
||||
clear();
|
||||
if (row > 0 && col > 0) {
|
||||
m = row;
|
||||
n = col;
|
||||
unsigned int mxn = m * n;
|
||||
if (dataLength && data) {
|
||||
for (unsigned int i = 0; i < dataLength && i < mxn; i++) {
|
||||
v.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Matrix<T>::Matrix(unsigned int row, unsigned int col, const std::vector<T>& data) {
|
||||
clear();
|
||||
if (row > 0 && col > 0) {
|
||||
m = row;
|
||||
n = col;
|
||||
unsigned int mxn = m * n;
|
||||
if (data.size() > 0) {
|
||||
for (unsigned int i = 0; i < mxn && i < data.size(); i++) {
|
||||
v.push_back(data[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Matrix<T>& Matrix<T>::operator=(const Matrix<T>& other) {
|
||||
clear();
|
||||
if (other.m > 0 && other.n > 0) {
|
||||
m = other.m;
|
||||
n = other.n;
|
||||
unsigned int mxn = m * n;
|
||||
for (unsigned int i = 0; i < mxn && i < other.size(); i++) {
|
||||
v.push_back(other.v[i]);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::vector<T> Matrix<T>::operator[](unsigned int index) const {
|
||||
std::vector<T> result;
|
||||
if (index >= m) {
|
||||
throw MatrixException(MATRIX_ERR_WRONG_ROW_INDEX);
|
||||
} else if ((index + 1) * n > size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
unsigned int begin = index * n;
|
||||
unsigned int end = begin + n;
|
||||
for (unsigned int i = begin; i < end; i++) {
|
||||
result.push_back(v[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Matrix<T> Matrix<T>::operator*(const Matrix<T>& other) {
|
||||
Matrix result(m, other.n);
|
||||
if (n != other.m) {
|
||||
throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_NOT_EQUAL);
|
||||
} else if (m <= 0 || n <= 0 || other.n <= 0) {
|
||||
throw MatrixException(MATRIX_ERR_MUL_ROW_AND_COL_BE_GREATER_THAN_ZERO);
|
||||
} else if (m * n > size() || other.m * other.n > other.size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
for (unsigned int i = 0; i < m; i++) {
|
||||
for (unsigned int j = 0; j < other.n; j++) {
|
||||
T temp = v[i * n] * other.v[j];
|
||||
for (unsigned int k = 1; k < n; k++) {
|
||||
temp += v[i * n + k] * other.v[k * other.n + j];
|
||||
}
|
||||
result.v.push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Matrix<T>::transpose() {
|
||||
if (m * n > size()) {
|
||||
throw MatrixException(MATRIX_ERR_TOO_FEW_DATA);
|
||||
} else {
|
||||
std::vector<T> v2;
|
||||
std::swap(v, v2);
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
for (unsigned int j = 0; j < m; j++) {
|
||||
v.push_back(v2[j * n + i]);
|
||||
}
|
||||
}
|
||||
std::swap(m, n);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* _MATRIX_H */
|
||||
36
Task/Matrix-transposition/C++/matrix-transposition-4.cpp
Normal file
36
Task/Matrix-transposition/C++/matrix-transposition-4.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <iostream>
|
||||
|
||||
int main(){
|
||||
const int l = 5;
|
||||
const int w = 3;
|
||||
int m1[l][w] = {{1,2,3}, {4,5,6}, {7,8,9}, {10,11,12}, {13,14,15}};
|
||||
int m2[w][l];
|
||||
|
||||
for(int i=0; i<w; i++){
|
||||
for(int x=0; x<l; x++){
|
||||
m2[i][x]=m1[x][i];
|
||||
}
|
||||
}
|
||||
|
||||
// This is just output...
|
||||
|
||||
std::cout << "Before:";
|
||||
for(int i=0; i<l; i++){
|
||||
std::cout << std::endl;
|
||||
for(int x=0; x<w; x++){
|
||||
std::cout << m1[i][x] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "\n\nAfter:";
|
||||
for(int i=0; i<w; i++){
|
||||
std::cout << std::endl;
|
||||
for(int x=0; x<l; x++){
|
||||
std::cout << m2[i][x] << " ";
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
32
Task/Matrix-transposition/C-sharp/matrix-transposition.cs
Normal file
32
Task/Matrix-transposition/C-sharp/matrix-transposition.cs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace prog
|
||||
{
|
||||
class MainClass
|
||||
{
|
||||
public static void Main (string[] args)
|
||||
{
|
||||
double[,] m = { {1,2,3},{4,5,6},{7,8,9} };
|
||||
|
||||
double[,] t = Transpose( m );
|
||||
|
||||
for( int i=0; i<t.GetLength(0); i++ )
|
||||
{
|
||||
for( int j=0; j<t.GetLength(1); j++ )
|
||||
Console.Write( t[i,j] + " " );
|
||||
Console.WriteLine("");
|
||||
}
|
||||
}
|
||||
|
||||
public static double[,] Transpose( double[,] m )
|
||||
{
|
||||
double[,] t = new double[m.GetLength(1),m.GetLength(0)];
|
||||
for( int i=0; i<m.GetLength(0); i++ )
|
||||
for( int j=0; j<m.GetLength(1); j++ )
|
||||
t[j,i] = m[i,j];
|
||||
|
||||
return t;
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Task/Matrix-transposition/C/matrix-transposition-1.c
Normal file
25
Task/Matrix-transposition/C/matrix-transposition-1.c
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void transpose(void *dest, void *src, int src_h, int src_w)
|
||||
{
|
||||
int i, j;
|
||||
double (*d)[src_h] = dest, (*s)[src_w] = src;
|
||||
for (i = 0; i < src_h; i++)
|
||||
for (j = 0; j < src_w; j++)
|
||||
d[j][i] = s[i][j];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, j;
|
||||
double a[3][5] = {{ 0, 1, 2, 3, 4 },
|
||||
{ 5, 6, 7, 8, 9 },
|
||||
{ 1, 0, 0, 0, 42}};
|
||||
double b[5][3];
|
||||
transpose(b, a, 3, 5);
|
||||
|
||||
for (i = 0; i < 5; i++)
|
||||
for (j = 0; j < 3; j++)
|
||||
printf("%g%c", b[i][j], j == 2 ? '\n' : ' ');
|
||||
return 0;
|
||||
}
|
||||
50
Task/Matrix-transposition/C/matrix-transposition-2.c
Normal file
50
Task/Matrix-transposition/C/matrix-transposition-2.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void transpose(double *m, int w, int h)
|
||||
{
|
||||
int start, next, i;
|
||||
double tmp;
|
||||
|
||||
for (start = 0; start <= w * h - 1; start++) {
|
||||
next = start;
|
||||
i = 0;
|
||||
do { i++;
|
||||
next = (next % h) * w + next / h;
|
||||
} while (next > start);
|
||||
if (next < start || i == 1) continue;
|
||||
|
||||
tmp = m[next = start];
|
||||
do {
|
||||
i = (next % h) * w + next / h;
|
||||
m[next] = (i == start) ? tmp : m[i];
|
||||
next = i;
|
||||
} while (next > start);
|
||||
}
|
||||
}
|
||||
|
||||
void show_matrix(double *m, int w, int h)
|
||||
{
|
||||
int i, j;
|
||||
for (i = 0; i < h; i++) {
|
||||
for (j = 0; j < w; j++)
|
||||
printf("%2g ", m[i * w + j]);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
double m[15];
|
||||
for (i = 0; i < 15; i++) m[i] = i + 1;
|
||||
|
||||
puts("before transpose:");
|
||||
show_matrix(m, 3, 5);
|
||||
|
||||
transpose(m, 3, 5);
|
||||
|
||||
puts("\nafter transpose:");
|
||||
show_matrix(m, 5, 3);
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
Task/Matrix-transposition/Clojure/matrix-transposition.clj
Normal file
11
Task/Matrix-transposition/Clojure/matrix-transposition.clj
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
(defmulti matrix-transpose
|
||||
"Switch rows with columns."
|
||||
class)
|
||||
|
||||
(defmethod matrix-transpose clojure.lang.PersistentList
|
||||
[mtx]
|
||||
(apply map list mtx))
|
||||
|
||||
(defmethod matrix-transpose clojure.lang.PersistentVector
|
||||
[mtx]
|
||||
(apply mapv vector mtx))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
transpose = (matrix) ->
|
||||
(t[i] for t in matrix) for i in [0...matrix[0].length]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(defun transpose (m)
|
||||
(apply #'mapcar #'list m))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
;; Transpose a mxn matrix A to a nxm matrix B=A'.
|
||||
(defun mtp (A)
|
||||
(let* ((m (array-dimension A 0))
|
||||
(n (array-dimension A 1))
|
||||
(B (make-array `(,n ,m) :initial-element 0)))
|
||||
(loop for i from 0 below m do
|
||||
(loop for j from 0 below n do
|
||||
(setf (aref B j i)
|
||||
(aref A i j))))
|
||||
B))
|
||||
8
Task/Matrix-transposition/D/matrix-transposition-1.d
Normal file
8
Task/Matrix-transposition/D/matrix-transposition-1.d
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
void main() {
|
||||
import std.stdio, std.range;
|
||||
|
||||
/*immutable*/ auto M = [[10, 11, 12, 13],
|
||||
[14, 15, 16, 17],
|
||||
[18, 19, 20, 21]];
|
||||
writefln("%(%(%2d %)\n%)", M.transposed);
|
||||
}
|
||||
16
Task/Matrix-transposition/D/matrix-transposition-2.d
Normal file
16
Task/Matrix-transposition/D/matrix-transposition-2.d
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
T[][] transpose(T)(in T[][] m) pure nothrow {
|
||||
auto r = new typeof(return)(m[0].length, m.length);
|
||||
foreach (immutable nr, const row; m)
|
||||
foreach (immutable nc, immutable c; row)
|
||||
r[nc][nr] = c;
|
||||
return r;
|
||||
}
|
||||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
|
||||
immutable M = [[10, 11, 12, 13],
|
||||
[14, 15, 16, 17],
|
||||
[18, 19, 20, 21]];
|
||||
writefln("%(%(%2d %)\n%)", M.transpose);
|
||||
}
|
||||
12
Task/Matrix-transposition/D/matrix-transposition-3.d
Normal file
12
Task/Matrix-transposition/D/matrix-transposition-3.d
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import std.stdio, std.algorithm, std.range, std.functional;
|
||||
|
||||
auto transpose(T)(in T[][] m) pure nothrow {
|
||||
return m[0].length.iota.map!(curry!(transversal, m));
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable M = [[10, 11, 12, 13],
|
||||
[14, 15, 16, 17],
|
||||
[18, 19, 20, 21]];
|
||||
writefln("%(%(%2d %)\n%)", M.transpose);
|
||||
}
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
[Demo of matrix transposition. Not in place, creates a new matrix.
|
||||
EDSAC, Initial Orders 2.]
|
||||
..PZ [blank tape and terminator]
|
||||
T 50 K [to call matrix transpose subroutine with 'G X']
|
||||
P 200 F [address of matrix transpose subroutine]
|
||||
T 47 K [to call matrix print subroutine with 'G M']
|
||||
P 100 F [address of matrix print subroutine]
|
||||
T 46 K [to call print subroutine with 'G N']
|
||||
P 56 F [address of print subroutine (EDSAC library P1)]
|
||||
|
||||
[Subroutine to transpose a matrix of 17-bit real numbers, not in place.
|
||||
Caller must ensure original and transpose don't overlap.
|
||||
Parameters, all in the address field (i.e. denote n by P n F)
|
||||
10F = width (number of columns)
|
||||
11F = height (number of rows)
|
||||
12F = start address of input matrix
|
||||
13F = start address of output matrix]
|
||||
E25K TX GK
|
||||
|
||||
[The subroutine loads elements by working down each column in turn.
|
||||
Elements are stored at consecutive locations in the transposed matrix.]
|
||||
A3F T31@ [set up return to caller]
|
||||
A13F A33@ T14@ [initialize T order for storing transpose]
|
||||
A12F A32@ U13@ [initialize A order for loading original]
|
||||
T36@ [also save as A order for top of current column]
|
||||
S10 F [negative of width]
|
||||
[10] T35@ [initialize negative counter]
|
||||
S11 F [negative of height]
|
||||
[12] T34@ [initialize negative counter]
|
||||
[13] AF [maunfactured order; load matrix element]
|
||||
[14] TF [maunfactured order; store matrix element]
|
||||
A14@ A2F T14@ [update address in T order]
|
||||
A13@ A10F T13@ [update address in A order]
|
||||
A34@ A2F G12@ [inner loop till finished this column]
|
||||
A36@ A2F U36@ T13@ [update address for start of column]
|
||||
A35@ A2F G10@ [outer loop till finished all columns]
|
||||
[31] ZF [exit]
|
||||
[32] AF [added to an address to make A order for that address]
|
||||
[33] TF [added to an address to make T order for that address]
|
||||
[34] PF [negative counter for rows]
|
||||
[35] PF [negative counter for columns]
|
||||
[36] AF [load order for first element in current column]
|
||||
|
||||
[Subroutine to print a matrix of 17-bit real numbers.
|
||||
Straightforward, so given in condensed form.
|
||||
Parameters (in the address field, i.e. pass n as PnF):
|
||||
10F = width (number of columns)
|
||||
11F = height (number of rows)
|
||||
12F = start address of matrix
|
||||
13F = number of decimals]
|
||||
E25K TM
|
||||
GKA3FT30@A13FT18@A12FA31@T14@S11FT36@S10FT37@O34@O35@TDAFT1FA16@
|
||||
GN [call library subroutine P1]
|
||||
PFA14@A2FT14@A37@A2FG10@O32@O33@A36@A2FG8@ZFAF@F&F!FMFPFPF
|
||||
|
||||
[Library subroutine P1.
|
||||
Prints number in 0D to n places of decimals, where
|
||||
n is specified by 'P n F' pseudo-order after subroutine call.]
|
||||
E25K TN
|
||||
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F
|
||||
|
||||
[Main routine]
|
||||
PK T300K GK
|
||||
[Constants]
|
||||
[0] #F [figures shift on teleprinter]
|
||||
[1] @F [carriage return]
|
||||
[2] &F [line feed]
|
||||
[3] P3F [number of columns (in address field)]
|
||||
[4] P5F [number of rows (in address field)]
|
||||
[5] P400F [address of matrix]
|
||||
[6] P500F [address of transposed matrix]
|
||||
[7] P2F [number of decimals when printing matrix]
|
||||
[8] TF [add to address to make T order]
|
||||
[9] P328F [0.0100097...., matrix elements are multiples of this]
|
||||
[Variables]
|
||||
[10] PF [matrix element, initialized to 0.00]
|
||||
[11] PF [negative counter]
|
||||
|
||||
[Enter with acc = 0]
|
||||
[12] O@ [set figures mode on teleprinter]
|
||||
A5@ [address of matrix]
|
||||
A8@ [make T order to store first elememt]
|
||||
T24@ [plant in code]
|
||||
H4@ N3@ L64F L32F [acc := negative number of entries]
|
||||
[20] T11@ [initialize negative counter]
|
||||
A10@ A9@ U10@ [increment matrix element]
|
||||
[24] TF [store in matrix]
|
||||
A24@ A2F T24@ [inc store address]
|
||||
A11@ A2F G20@ [inc negative counter, loop till zero]
|
||||
|
||||
[Matrix is set up, now print it]
|
||||
A3@ T10F [10F := width]
|
||||
A4@ T11F [11F := height]
|
||||
A5@ T12F [12F := address of matrix]
|
||||
A7@ T13F [13F := number of decimals]
|
||||
[39] A39@ GM [call print subroutine]
|
||||
O1@ O2@ [add CR LF]
|
||||
|
||||
[Transpose matrix: 10F, 11F, 12F stay the same]
|
||||
A6@ T13F [13F := address of transpose]
|
||||
[45] A45@ GX [call transpose routine]
|
||||
|
||||
[Print transpose]
|
||||
A10F TF A11F T10F AF T11F [swap width and height]
|
||||
A13F T12F [12F := address of transpose]
|
||||
A7@ T13F [13F := number of decimals]
|
||||
[57] A57@ GM [call print subroutine]
|
||||
|
||||
O@ [figures mode, dummy to flush teleprinter buffer]
|
||||
ZF [stop]
|
||||
E12Z [enter at 12 (relative)]
|
||||
PF [accumulator = 0 on entry]
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
[Transpose a matrix in place. EDSAC, Initial Orders 2.]
|
||||
..PZ [blank tape and terminator]
|
||||
T50K [to call matrix transpose with 'G X']
|
||||
P160F [address of matrix transpose subroutine]
|
||||
T47K [to call matrix print subroutine with 'G M']
|
||||
P120F [address of matrix print subroutine]
|
||||
T46K [to call print subroutine with 'G N']
|
||||
P56F [address of print subroutine (P1 in EDSAC library)]
|
||||
T48K [to call division subroutine with 'G &']
|
||||
P77F [address of division subroutine]
|
||||
|
||||
[Subroutine to transpose a matrix of 17-bit values in place.
|
||||
Translated and slightly modified from C version on Rosetta Code website.
|
||||
Parameters, all in the address field (i.e. n is stored as P n F):
|
||||
10F = width (number of columns, "w" in C program)
|
||||
11F = height (number of rows, "h" in C program)
|
||||
12F = start address of matrix]
|
||||
E25K TX GK
|
||||
A3F T64@ [set up return to caller]
|
||||
H10F V11F L32F L64F [acc := size of matrix as width*height]
|
||||
T84@ T85@ [store size; C variable start := 0]
|
||||
[8] TF A85@ T86@ T87@ [set C variables, next := start, i := 0]
|
||||
[12] TF A87@ A2F T87@ [i++]
|
||||
A16@ G65@ [call subroutine to update "next"]
|
||||
A85@ S86@ G12@ [acc := start - next, loop back if < 0]
|
||||
[Skip to location 58 if acc > 0 or i = 1.
|
||||
We already know that acc >= 0 and i > 0.]
|
||||
S2F E58@ [subtract 1 from acc, skip if still >= 0]
|
||||
S2F A87@ G58@ [acc := -2 + i, skip if < 0]
|
||||
[The assignment next := start in the C program is unnecessary]
|
||||
TF A86@ A12F A81@ T31@ [make and plant order to load m{next}]
|
||||
[31] AF T83@ [tmp := m{next}]
|
||||
[33] TF [clear acc; also added to an address to make T order for that address]
|
||||
[34] A86@ A12F A33@ T54@ [make and plant order to store m{next}]
|
||||
A38@ G65@ [call subroutine to update "next"]
|
||||
A86@ S85@ G48@ [go to 48 if i < start]
|
||||
S2F E48@ [go to 48 if i > start]
|
||||
TF A82@ G52@ [make order to load tmp, and go to 52]
|
||||
[48] TF A86@ A12F A81@ [make order to load m{next}]
|
||||
[52] T53@ [plant order to load tmp or m{next}]
|
||||
[53] AF [manufactured order; if i = start loads tmp, else loads m{next}]
|
||||
[54] TF [manufactured order; stores m{next}, using old value of "next"]
|
||||
A85@ S86@ G33@ [acc := start - next, loop back if < 0]
|
||||
[58] TF A85@ A2F U85@ [start++]
|
||||
S84@ G8@ [loop until start = size]
|
||||
[64] ZF [overwritten by return to caller]
|
||||
[Subroutine to execute next = (next % h) * w + next / h in C program]
|
||||
[65] A3F T80@ [set up return to caller]
|
||||
A86@ T4F A11F T5F [set up parameters to divide "next" by "h"]
|
||||
A71@ G& [call division subroutine]
|
||||
[In case anybody is following this in detail, note that "next" and "h" are
|
||||
stored in the address field, so we need to shift the quotient 1 left]
|
||||
H4F V10F L64F L16F A5F LD T86@ [compute RHS and store in "next"]
|
||||
[80] ZF [overwritten by return to caller]
|
||||
[Constants]
|
||||
[81] AF [added to an address to make A order for that address]
|
||||
[82] A83@ [order to load C variable "tmp"]
|
||||
[Variables; integers are stored in the address field for convenience.]
|
||||
[83] PF [C variable "tmp" (holds value of a matrix element)]
|
||||
[84] PF [size of matrix, width*height]
|
||||
[85] PF [C variable "start"]
|
||||
[86] PF [C variable "next"]
|
||||
[87] PF [C variable "i"]
|
||||
|
||||
[Subroutine to print a matrix of 17-bit real numbers.]
|
||||
E25K TM
|
||||
GKA3FT30@A13FT18@A12FA31@T14@S11FT36@S10FT37@O34@O35@TDAFT1FA16@
|
||||
GN
|
||||
PFA14@A2FT14@A37@A2FG10@O32@O33@A36@A2FG8@ZFAF@F&F!FMFPFPF
|
||||
|
||||
[Library subroutine P1.
|
||||
Prints positive number in 0D to n places of decimals, where
|
||||
n is specified by 'P n F' pseudo-order after subroutine call.]
|
||||
E25K TN
|
||||
GKA18@U17@S20@T5@H19@PFT5@VDUFOFFFSFL4FTDA5@A2FG6@EFU3FJFM1F
|
||||
|
||||
[Integer division: number at 4F, divisor at 5F
|
||||
Returns remainder at 4F, quotient at 5F
|
||||
Working location 0D. 37 locations.]
|
||||
E25K T&
|
||||
GKA3FT34@A5FUFT35@A4FRDS35@G13@T1FA35@LDE4@T1FT5FA4FS35@G22@
|
||||
T4FA5FA36@T5FT1FAFS35@E34@T1FA35@RDT35@A5FLDT5FE15@EFPFPD
|
||||
|
||||
[Main routine]
|
||||
[Given in condensed form, since it's the same as in part 1, except
|
||||
that the address of the transposed matrix is not required.]
|
||||
PKT250KGK#F@F&FP7FP4FP320FP2FTFP328FPFPFO@A5@A7@T23@H4@N3@L64FL32F
|
||||
T10@A9@A8@U9@TFA23@A2FT23@A10@A2FG19@A3@T10FA4@T11FA5@T12FA6@T13F
|
||||
A38@GMO1@O2@A42@GXA10FTFA11FT10FAFT11FA50@GMO@ZFE11ZPF
|
||||
2
Task/Matrix-transposition/ELLA/matrix-transposition.ella
Normal file
2
Task/Matrix-transposition/ELLA/matrix-transposition.ella
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
MAC TRANSPOSE = ([INT n][INT m]TYPE t: matrix) -> [m][n]t:
|
||||
[INT i = 1..m] [INT j = 1..n] matrix[j][i].
|
||||
10
Task/Matrix-transposition/EchoLisp/matrix-transposition.l
Normal file
10
Task/Matrix-transposition/EchoLisp/matrix-transposition.l
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(lib 'matrix)
|
||||
|
||||
(define M (list->array (iota 6) 3 2))
|
||||
(array-print M)
|
||||
0 1
|
||||
2 3
|
||||
4 5
|
||||
(array-print (matrix-transpose M))
|
||||
0 2 4
|
||||
1 3 5
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
m = [[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25,125, 625]]
|
||||
|
||||
transpose = fn(m)-> List.zip(m) |> Enum.map(&Tuple.to_list(&1)) end
|
||||
|
||||
IO.inspect transpose.(m)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(require 'cl-lib)
|
||||
|
||||
(defun transpose (m)
|
||||
(apply #'cl-mapcar #'list m))
|
||||
|
||||
;;test for transposition function
|
||||
(transpose '((2 3 4 5) (3 5 6 9) (9 9 9 9)))
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(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)) )
|
||||
18
Task/Matrix-transposition/Erlang/matrix-transposition.erl
Normal file
18
Task/Matrix-transposition/Erlang/matrix-transposition.erl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
-module(transmatrix).
|
||||
-export([trans/1,transL/1]).
|
||||
|
||||
% using built-ins hd = head, tl = tail
|
||||
|
||||
trans([[]|_]) -> [];
|
||||
trans(M) ->
|
||||
[ lists:map(fun hd/1, M) | transpose( lists:map(fun tl/1, M) ) ].
|
||||
|
||||
% Purist version
|
||||
|
||||
transL( [ [Elem | Rest] | List] ) ->
|
||||
[ [Elem | [H || [H | _] <- List] ] |
|
||||
transL( [Rest |
|
||||
[ T || [_ | T] <- List ] ]
|
||||
) ];
|
||||
transL([ [] | List] ) -> transL(List);
|
||||
transL([]) -> [].
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
function transpose(sequence in)
|
||||
sequence out
|
||||
out = repeat(repeat(0,length(in)),length(in[1]))
|
||||
for n = 1 to length(in) do
|
||||
for m = 1 to length(in[1]) do
|
||||
out[m][n] = in[n][m]
|
||||
end for
|
||||
end for
|
||||
return out
|
||||
end function
|
||||
|
||||
sequence m
|
||||
m = {
|
||||
{1,2,3,4},
|
||||
{5,6,7,8},
|
||||
{9,10,11,12}
|
||||
}
|
||||
|
||||
? transpose(m)
|
||||
|
|
@ -0,0 +1 @@
|
|||
let transpose (mtx : _ [,]) = Array2D.init (mtx.GetLength 1) (mtx.GetLength 0) (fun x y -> mtx.[y,x])
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
( scratchpad ) { { 1 2 3 } { 4 5 6 } } flip .
|
||||
{ { 1 4 } { 2 5 } { 3 6 } }
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Array a[3,1]
|
||||
[a]:=[(1,2,3)]
|
||||
[b]:=Trans([a])
|
||||
[a]
|
||||
[b]
|
||||
12
Task/Matrix-transposition/Forth/matrix-transposition.fth
Normal file
12
Task/Matrix-transposition/Forth/matrix-transposition.fth
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
S" fsl-util.fs" REQUIRED
|
||||
S" fsl/dynmem.seq" REQUIRED
|
||||
: F+! ( addr -- ) ( F: r -- ) DUP F@ F+ F! ;
|
||||
: FSQR ( F: r1 -- r2 ) FDUP F* ;
|
||||
S" fsl/gaussj.seq" REQUIRED
|
||||
|
||||
5 3 float matrix a{{
|
||||
1e 2e 3e 4e 5e 6e 7e 8e 9e 10e 11e 12e 13e 14e 15e 5 3 a{{ }}fput
|
||||
float dmatrix b{{
|
||||
|
||||
a{{ 5 3 & b{{ transpose
|
||||
3 5 b{{ }}fprint
|
||||
13
Task/Matrix-transposition/Fortran/matrix-transposition-1.f
Normal file
13
Task/Matrix-transposition/Fortran/matrix-transposition-1.f
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
integer, parameter :: n = 3, m = 5
|
||||
real, dimension(n,m) :: a = reshape( (/ (i,i=1,n*m) /), (/ n, m /) )
|
||||
real, dimension(m,n) :: b
|
||||
|
||||
b = transpose(a)
|
||||
|
||||
do i = 1, n
|
||||
print *, a(i,:)
|
||||
end do
|
||||
|
||||
do j = 1, m
|
||||
print *, b(j,:)
|
||||
end do
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL A(3,5), B(5,3)
|
||||
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
|
||||
|
||||
DO I = 1, 3
|
||||
DO J = 1, 5
|
||||
B(J,I) = A(I,J)
|
||||
END DO
|
||||
END DO
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
REAL A(3,5), B(5,3)
|
||||
DATA ((A(I,J),I=1,3),J=1,5) /1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15/
|
||||
|
||||
DO 10 I = 1, 3
|
||||
DO 20 J = 1, 5
|
||||
B(J,I) = A(I,J)
|
||||
20 CONTINUE
|
||||
10 CONTINUE
|
||||
28
Task/Matrix-transposition/Fortran/matrix-transposition-4.f
Normal file
28
Task/Matrix-transposition/Fortran/matrix-transposition-4.f
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
DIMENSION A(3,5),B(5,3),C(5,3)
|
||||
EQUIVALENCE (A,C) !Occupy the same storage.
|
||||
DATA A/
|
||||
1 1, 2, 3, 4, 5,
|
||||
2 6, 7, 8, 9,10,
|
||||
3 11,12,13,14,15/ !Supplies values in storage order.
|
||||
|
||||
WRITE (6,*) "Three rows of five values:"
|
||||
WRITE (6,1) A !This shows values in storage order.
|
||||
WRITE (6,*) "...written as C(row,column):"
|
||||
WRITE (6,2) ((C(I,J),J = 1,3),I = 1,5)
|
||||
WRITE (6,*) "... written as A(row,column):"
|
||||
WRITE (6,1) ((A(I,J),J = 1,5),I = 1,3)
|
||||
|
||||
WRITE (6,*)
|
||||
WRITE (6,*) "B = Transpose(A)"
|
||||
DO 10 I = 1,3
|
||||
DO 10 J = 1,5
|
||||
10 B(J,I) = A(I,J)
|
||||
|
||||
WRITE (6,*) "Five rows of three values:"
|
||||
WRITE (6,2) B
|
||||
WRITE (6,*) "... written as B(row,column):"
|
||||
WRITE (6,2) ((B(I,J),J = 1,3),I = 1,5)
|
||||
|
||||
1 FORMAT (5F6.1) !Five values per line.
|
||||
2 FORMAT (3F6.1) !Three values per line.
|
||||
END
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
Dim matriz(0 To 3, 0 To 4) As Integer = {{78,19,30,12,36},_
|
||||
{49,10,65,42,50},_
|
||||
{30,93,24,78,10},_
|
||||
{39,68,27,64,29}}
|
||||
Dim As Integer mtranspuesta(Lbound(matriz, 2) To Ubound(matriz, 2), Lbound(matriz, 1) To Ubound(matriz, 1))
|
||||
Dim As Integer fila, columna
|
||||
|
||||
For fila = Lbound(matriz,1) To Ubound(matriz,1)
|
||||
For columna = Lbound(matriz,2) To Ubound(matriz,2)
|
||||
mtranspuesta(columna, fila) = matriz(fila, columna)
|
||||
Print ; matriz(fila,columna); " ";
|
||||
Next columna
|
||||
Print
|
||||
Next fila
|
||||
Print
|
||||
|
||||
For fila = Lbound(mtranspuesta,1) To Ubound(mtranspuesta,1)
|
||||
For columna = Lbound(mtranspuesta,2) To Ubound(mtranspuesta,2)
|
||||
Print ; mtranspuesta(fila,columna); " ";
|
||||
Next columna
|
||||
Print
|
||||
Next fila
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
a = [[1,2,3],
|
||||
[4,5,6],
|
||||
[7,8,9]]
|
||||
joinln[a.transpose[]]
|
||||
6
Task/Matrix-transposition/GAP/matrix-transposition.gap
Normal file
6
Task/Matrix-transposition/GAP/matrix-transposition.gap
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
originalMatrix := [[1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625]];
|
||||
transposedMatrix := TransposedMat(originalMatrix);
|
||||
17
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
17
Task/Matrix-transposition/Go/matrix-transposition-1.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gonum.org/v1/gonum/mat"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := mat.NewDense(2, 3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
})
|
||||
fmt.Println(mat.Formatted(m))
|
||||
fmt.Println()
|
||||
fmt.Println(mat.Formatted(m.T()))
|
||||
}
|
||||
19
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
19
Task/Matrix-transposition/Go/matrix-transposition-2.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mat "github.com/skelterjohn/go.matrix"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := mat.MakeDenseMatrixStacked([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
fmt.Println("original:")
|
||||
fmt.Println(m)
|
||||
m = m.Transpose()
|
||||
fmt.Println("transpose:")
|
||||
fmt.Println(m)
|
||||
}
|
||||
35
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
35
Task/Matrix-transposition/Go/matrix-transposition-3.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type row []float64
|
||||
type matrix []row
|
||||
|
||||
func main() {
|
||||
m := matrix{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
}
|
||||
printMatrix(m)
|
||||
t := transpose(m)
|
||||
printMatrix(t)
|
||||
}
|
||||
|
||||
func printMatrix(m matrix) {
|
||||
for _, s := range m {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
|
||||
func transpose(m matrix) matrix {
|
||||
r := make(matrix, len(m[0]))
|
||||
for x, _ := range r {
|
||||
r[x] = make(row, len(m))
|
||||
}
|
||||
for y, s := range m {
|
||||
for x, e := range s {
|
||||
r[x][y] = e
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
51
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
51
Task/Matrix-transposition/Go/matrix-transposition-4.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
ele []float64
|
||||
stride int
|
||||
}
|
||||
|
||||
// construct new matrix from slice of slices
|
||||
func matrixFromRows(rows [][]float64) *matrix {
|
||||
if len(rows) == 0 {
|
||||
return &matrix{nil, 0}
|
||||
}
|
||||
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
|
||||
for rx, row := range rows {
|
||||
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrixFromRows([][]float64{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
})
|
||||
m.print("original:")
|
||||
m.transpose().print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transpose() *matrix {
|
||||
r := &matrix{make([]float64, len(m.ele)), len(m.ele) / m.stride}
|
||||
rx := 0
|
||||
for _, e := range m.ele {
|
||||
r.ele[rx] = e
|
||||
rx += r.stride
|
||||
if rx >= len(r.ele) {
|
||||
rx -= len(r.ele) - 1
|
||||
}
|
||||
}
|
||||
return r
|
||||
}
|
||||
61
Task/Matrix-transposition/Go/matrix-transposition-5.go
Normal file
61
Task/Matrix-transposition/Go/matrix-transposition-5.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type matrix struct {
|
||||
stride int
|
||||
ele []float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := matrix{3, []float64{
|
||||
1, 2, 3,
|
||||
4, 5, 6,
|
||||
}}
|
||||
m.print("original:")
|
||||
m.transposeInPlace()
|
||||
m.print("transpose:")
|
||||
}
|
||||
|
||||
func (m *matrix) print(heading string) {
|
||||
if heading > "" {
|
||||
fmt.Print("\n", heading, "\n")
|
||||
}
|
||||
for e := 0; e < len(m.ele); e += m.stride {
|
||||
fmt.Println(m.ele[e : e+m.stride])
|
||||
}
|
||||
}
|
||||
|
||||
func (m *matrix) transposeInPlace() {
|
||||
h := len(m.ele) / m.stride
|
||||
for start := range m.ele {
|
||||
next := start
|
||||
i := 0
|
||||
for {
|
||||
i++
|
||||
next = (next%h)*m.stride + next/h
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
if next < start || i == 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
next = start
|
||||
tmp := m.ele[next]
|
||||
for {
|
||||
i = (next%h)*m.stride + next/h
|
||||
if i == start {
|
||||
m.ele[next] = tmp
|
||||
} else {
|
||||
m.ele[next] = m.ele[i]
|
||||
}
|
||||
next = i
|
||||
if next <= start {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
m.stride = h
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def matrix = [ [ 1, 2, 3, 4 ],
|
||||
[ 5, 6, 7, 8 ] ]
|
||||
|
||||
matrix.each { println it }
|
||||
println()
|
||||
def transpose = matrix.transpose()
|
||||
|
||||
transpose.each { println it }
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
*Main> transpose [[1,2],[3,4],[5,6]]
|
||||
[[1,3,5],[2,4,6]]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import Data.Array
|
||||
|
||||
swap (x,y) = (y,x)
|
||||
|
||||
transpArray :: (Ix a, Ix b) => Array (a,b) e -> Array (b,a) e
|
||||
transpArray a = ixmap (swap l, swap u) swap a where
|
||||
(l,u) = bounds a
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
tpose [ms] = [[m] | m <- ms]
|
||||
tpose (ms:mss) = zipWith (:) ms (tpose mss)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
import Data.Matrix
|
||||
|
||||
main :: IO ()
|
||||
main = print matrix >> print (transpose matrix)
|
||||
where
|
||||
matrix = fromList 3 4 [1 ..]
|
||||
11
Task/Matrix-transposition/Haskell/matrix-transposition-5.hs
Normal file
11
Task/Matrix-transposition/Haskell/matrix-transposition-5.hs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import Numeric.LinearAlgebra
|
||||
|
||||
a :: Matrix I
|
||||
a = (3><2)
|
||||
[1,2
|
||||
,3,4
|
||||
,5,6]
|
||||
|
||||
main = do
|
||||
print $ a
|
||||
print $ tr a
|
||||
17
Task/Matrix-transposition/Haxe/matrix-transposition.haxe
Normal file
17
Task/Matrix-transposition/Haxe/matrix-transposition.haxe
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class Matrix {
|
||||
static function main() {
|
||||
var m = [ [1, 1, 1, 1],
|
||||
[2, 4, 8, 16],
|
||||
[3, 9, 27, 81],
|
||||
[4, 16, 64, 256],
|
||||
[5, 25, 125, 625] ];
|
||||
var t = [ for (i in 0...m[0].length)
|
||||
[ for (j in 0...m.length) 0 ] ];
|
||||
for(i in 0...m.length)
|
||||
for(j in 0...m[0].length)
|
||||
t[j][i] = m[i][j];
|
||||
|
||||
for(aa in [m, t])
|
||||
for(a in aa) Sys.println(a);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
REAL :: mtx(2, 4)
|
||||
|
||||
mtx = 1.1 * $
|
||||
WRITE() mtx
|
||||
|
||||
SOLVE(Matrix=mtx, Transpose=mtx)
|
||||
WRITE() mtx
|
||||
4
Task/Matrix-transposition/Hope/matrix-transposition.hope
Normal file
4
Task/Matrix-transposition/Hope/matrix-transposition.hope
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
uses lists;
|
||||
dec transpose : list (list alpha) -> list (list alpha);
|
||||
--- transpose ([]::_) <= [];
|
||||
--- transpose n <= map head n :: transpose (map tail n);
|
||||
2
Task/Matrix-transposition/IDL/matrix-transposition.idl
Normal file
2
Task/Matrix-transposition/IDL/matrix-transposition.idl
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
m=[[1,1,1,1],[2, 4, 8, 16],[3, 9,27, 81],[5, 25,125, 625]]
|
||||
print,transpose(m)
|
||||
28
Task/Matrix-transposition/Icon/matrix-transposition.icon
Normal file
28
Task/Matrix-transposition/Icon/matrix-transposition.icon
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
procedure transpose_matrix (matrix)
|
||||
result := []
|
||||
# for each column
|
||||
every (i := 1 to *matrix[1]) do {
|
||||
col := []
|
||||
# extract the number in each row for that column
|
||||
every (row := !matrix) do put (col, row[i])
|
||||
# and push that column as a row in the result matrix
|
||||
put (result, col)
|
||||
}
|
||||
return result
|
||||
end
|
||||
|
||||
procedure print_matrix (matrix)
|
||||
every (row := !matrix) do {
|
||||
every writes (!row || " ")
|
||||
write ()
|
||||
}
|
||||
end
|
||||
|
||||
procedure main ()
|
||||
matrix := [[1,2,3],[4,5,6]]
|
||||
write ("Start:")
|
||||
print_matrix (matrix)
|
||||
transposed := transpose_matrix (matrix)
|
||||
write ("Transposed:")
|
||||
print_matrix (transposed)
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
Idris> transpose [[1,2],[3,4],[5,6]]
|
||||
[[1, 3, 5], [2, 4, 6]] : List (List Integer)
|
||||
11
Task/Matrix-transposition/J/matrix-transposition.j
Normal file
11
Task/Matrix-transposition/J/matrix-transposition.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
]matrix=: (^/ }:) >:i.5 NB. make and show example matrix
|
||||
1 1 1 1
|
||||
2 4 8 16
|
||||
3 9 27 81
|
||||
4 16 64 256
|
||||
5 25 125 625
|
||||
|: matrix
|
||||
1 2 3 4 5
|
||||
1 4 9 16 25
|
||||
1 8 27 64 125
|
||||
1 16 81 256 625
|
||||
19
Task/Matrix-transposition/Java/matrix-transposition.java
Normal file
19
Task/Matrix-transposition/Java/matrix-transposition.java
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import java.util.Arrays;
|
||||
public class Transpose{
|
||||
public static void main(String[] args){
|
||||
double[][] m = {{1, 1, 1, 1},
|
||||
{2, 4, 8, 16},
|
||||
{3, 9, 27, 81},
|
||||
{4, 16, 64, 256},
|
||||
{5, 25, 125, 625}};
|
||||
double[][] ans = new double[m[0].length][m.length];
|
||||
for(int rows = 0; rows < m.length; rows++){
|
||||
for(int cols = 0; cols < m[0].length; cols++){
|
||||
ans[cols][rows] = m[rows][cols];
|
||||
}
|
||||
}
|
||||
for(double[] i:ans){//2D arrays are arrays of arrays
|
||||
System.out.println(Arrays.toString(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
function Matrix(ary) {
|
||||
this.mtx = ary
|
||||
this.height = ary.length;
|
||||
this.width = ary[0].length;
|
||||
}
|
||||
|
||||
Matrix.prototype.toString = function() {
|
||||
var s = []
|
||||
for (var i = 0; i < this.mtx.length; i++)
|
||||
s.push( this.mtx[i].join(",") );
|
||||
return s.join("\n");
|
||||
}
|
||||
|
||||
// returns a new matrix
|
||||
Matrix.prototype.transpose = function() {
|
||||
var transposed = [];
|
||||
for (var i = 0; i < this.width; i++) {
|
||||
transposed[i] = [];
|
||||
for (var j = 0; j < this.height; j++) {
|
||||
transposed[i][j] = this.mtx[j][i];
|
||||
}
|
||||
}
|
||||
return new Matrix(transposed);
|
||||
}
|
||||
|
||||
var m = new Matrix([[1,1,1,1],[2,4,8,16],[3,9,27,81],[4,16,64,256],[5,25,125,625]]);
|
||||
print(m);
|
||||
print();
|
||||
print(m.transpose());
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
function transpose(lst) {
|
||||
return lst[0].map(function (_, iCol) {
|
||||
return lst.map(function (row) {
|
||||
return row[iCol];
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return transpose(
|
||||
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
|
||||
);
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// transpose :: [[a]] -> [[a]]
|
||||
const transpose = xs =>
|
||||
0 < xs.length ? (
|
||||
xs[0].map(
|
||||
(_, iCol) => xs.map(
|
||||
row => row[iCol]
|
||||
)
|
||||
)
|
||||
) : [];
|
||||
|
||||
|
||||
// ---------------------- TEST -----------------------
|
||||
const main = () =>
|
||||
JSON.stringify(
|
||||
transpose([
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9]
|
||||
])
|
||||
);
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
[[1,4,7],[2,5,8],[3,6,9]]
|
||||
5
Task/Matrix-transposition/Joy/matrix-transposition.joy
Normal file
5
Task/Matrix-transposition/Joy/matrix-transposition.joy
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
DEFINE transpose == [[null] [true] [[null] some] ifte]
|
||||
[pop []]
|
||||
[[[first] map] [[rest] map] cleave]
|
||||
[cons]
|
||||
linrec.
|
||||
4
Task/Matrix-transposition/Jq/matrix-transposition.jq
Normal file
4
Task/Matrix-transposition/Jq/matrix-transposition.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
def transpose:
|
||||
if (.[0] | length) == 0 then []
|
||||
else [map(.[0])] + (map(.[1:]) | transpose)
|
||||
end ;
|
||||
59
Task/Matrix-transposition/Jsish/matrix-transposition-1.jsish
Normal file
59
Task/Matrix-transposition/Jsish/matrix-transposition-1.jsish
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/* Matrix transposition, multiplication, identity, and exponentiation, in Jsish */
|
||||
function Matrix(ary) {
|
||||
this.mtx = ary;
|
||||
this.height = ary.length;
|
||||
this.width = ary[0].length;
|
||||
}
|
||||
|
||||
Matrix.prototype.toString = function() {
|
||||
var s = [];
|
||||
for (var i = 0; i < this.mtx.length; i++) s.push(this.mtx[i].join(","));
|
||||
return s.join("\n");
|
||||
};
|
||||
|
||||
// returns a transposed matrix
|
||||
Matrix.prototype.transpose = function() {
|
||||
var transposed = [];
|
||||
for (var i = 0; i < this.width; i++) {
|
||||
transposed[i] = [];
|
||||
for (var j = 0; j < this.height; j++) transposed[i][j] = this.mtx[j][i];
|
||||
}
|
||||
return new Matrix(transposed);
|
||||
};
|
||||
|
||||
// returns a matrix as the product of two others
|
||||
Matrix.prototype.mult = function(other) {
|
||||
if (this.width != other.height) throw "error: incompatible sizes";
|
||||
|
||||
var result = [];
|
||||
for (var i = 0; i < this.height; i++) {
|
||||
result[i] = [];
|
||||
for (var j = 0; j < other.width; j++) {
|
||||
var sum = 0;
|
||||
for (var k = 0; k < this.width; k++) sum += this.mtx[i][k] * other.mtx[k][j];
|
||||
result[i][j] = sum;
|
||||
}
|
||||
}
|
||||
return new Matrix(result);
|
||||
};
|
||||
|
||||
// IdentityMatrix is a "subclass" of Matrix
|
||||
function IdentityMatrix(n) {
|
||||
this.height = n;
|
||||
this.width = n;
|
||||
this.mtx = [];
|
||||
for (var i = 0; i < n; i++) {
|
||||
this.mtx[i] = [];
|
||||
for (var j = 0; j < n; j++) this.mtx[i][j] = (i == j ? 1 : 0);
|
||||
}
|
||||
}
|
||||
IdentityMatrix.prototype = Matrix.prototype;
|
||||
|
||||
// the Matrix exponentiation function
|
||||
Matrix.prototype.exp = function(n) {
|
||||
var result = new IdentityMatrix(this.height);
|
||||
for (var i = 1; i <= n; i++) result = result.mult(this);
|
||||
return result;
|
||||
};
|
||||
|
||||
provide('Matrix', '0.60');
|
||||
15
Task/Matrix-transposition/Jsish/matrix-transposition-2.jsish
Normal file
15
Task/Matrix-transposition/Jsish/matrix-transposition-2.jsish
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
/* Matrix transposition, in Jsish */
|
||||
require('Matrix');
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
var m = new Matrix([[1,1,1,1],[2,4,8,16],[3,9,27,81],[4,16,64,256],[5,25,125,625]]);
|
||||
; m;
|
||||
; m.transpose();
|
||||
}
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
m ==> { height:5, mtx:[ [ 1, 1, 1, 1 ], [ 2, 4, 8, 16 ], [ 3, 9, 27, 81 ], [ 4, 16, 64, 256 ], [ 5, 25, 125, 625 ] ], width:4 }
|
||||
m.transpose() ==> { height:4, mtx:[ [ 1, 2, 3, 4, 5 ], [ 1, 4, 9, 16, 25 ], [ 1, 8, 27, 64, 125 ], [ 1, 16, 81, 256, 625 ] ], width:5 }
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
10
Task/Matrix-transposition/Julia/matrix-transposition.julia
Normal file
10
Task/Matrix-transposition/Julia/matrix-transposition.julia
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
julia> [1 2 3 ; 4 5 6] # a 2x3 matrix
|
||||
2x3 Array{Int64,2}:
|
||||
1 2 3
|
||||
4 5 6
|
||||
|
||||
julia> [1 2 3 ; 4 5 6]' # note the quote
|
||||
3x2 LinearAlgebra.Adjoint{Int64,Array{Int64,2}}:
|
||||
1 4
|
||||
2 5
|
||||
3 6
|
||||
12
Task/Matrix-transposition/K/matrix-transposition.k
Normal file
12
Task/Matrix-transposition/K/matrix-transposition.k
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
{x^\:-1_ x}1+!:5
|
||||
(1 1 1 1.0
|
||||
2 4 8 16.0
|
||||
3 9 27 81.0
|
||||
4 16 64 256.0
|
||||
5 25 125 625.0)
|
||||
|
||||
+{x^\:-1_ x}1+!:5
|
||||
(1 2 3 4 5.0
|
||||
1 4 9 16 25.0
|
||||
1 8 27 64 125.0
|
||||
1 16 81 256 625.0)
|
||||
13
Task/Matrix-transposition/Klong/matrix-transposition.klong
Normal file
13
Task/Matrix-transposition/Klong/matrix-transposition.klong
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
[5 5]:^!25
|
||||
[[0 1 2 3 4]
|
||||
[5 6 7 8 9]
|
||||
[10 11 12 13 14]
|
||||
[15 16 17 18 19]
|
||||
[20 21 22 23 24]]
|
||||
|
||||
+[5 5]:^!25
|
||||
[[0 5 10 15 20]
|
||||
[1 6 11 16 21]
|
||||
[2 7 12 17 22]
|
||||
[3 8 13 18 23]
|
||||
[4 9 14 19 24]]
|
||||
24
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
24
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
// version 1.1.3
|
||||
|
||||
typealias Vector = DoubleArray
|
||||
typealias Matrix = Array<Vector>
|
||||
|
||||
fun Matrix.transpose(): Matrix {
|
||||
val rows = this.size
|
||||
val cols = this[0].size
|
||||
val trans = Matrix(cols) { Vector(rows) }
|
||||
for (i in 0 until cols) {
|
||||
for (j in 0 until rows) trans[i][j] = this[j][i]
|
||||
}
|
||||
return trans
|
||||
}
|
||||
|
||||
// Alternate version
|
||||
typealias Matrix<T> = List<List<T>>
|
||||
fun <T> Matrix<T>.transpose(): Matrix<T> {
|
||||
return (0 until this[0].size).map { x ->
|
||||
(this.indices).map { y ->
|
||||
this[y][x]
|
||||
}
|
||||
}
|
||||
}
|
||||
13
Task/Matrix-transposition/LFE/matrix-transposition-1.lfe
Normal file
13
Task/Matrix-transposition/LFE/matrix-transposition-1.lfe
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(defun transpose (matrix)
|
||||
(transpose matrix '()))
|
||||
|
||||
(defun transpose (matrix acc)
|
||||
(cond
|
||||
((lists:any
|
||||
(lambda (x) (== x '()))
|
||||
matrix)
|
||||
acc)
|
||||
('true
|
||||
(let ((heads (lists:map #'car/1 matrix))
|
||||
(tails (lists:map #'cdr/1 matrix)))
|
||||
(transpose tails (++ acc `(,heads)))))))
|
||||
8
Task/Matrix-transposition/LFE/matrix-transposition-2.lfe
Normal file
8
Task/Matrix-transposition/LFE/matrix-transposition-2.lfe
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
> (transpose '((1 2 3)
|
||||
(4 5 6)
|
||||
(7 8 9)
|
||||
(10 11 12)
|
||||
(13 14 15)
|
||||
(16 17 18)))
|
||||
((1 4 7 10 13 16) (2 5 8 11 14 17) (3 6 9 12 15 18))
|
||||
>
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
{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]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
12 iota [3 4] reshape 1 + dup .
|
||||
1 transpose .
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
MatrixC$ ="4, 3, 0, 0.10, 0.20, 0.30, 0.40, 0.50, 0.60, 0.70, 0.80, 0.90, 1.00, 1.10"
|
||||
|
||||
print "Transpose of matrix"
|
||||
call DisplayMatrix MatrixC$
|
||||
print " ="
|
||||
MatrixT$ =MatrixTranspose$( MatrixC$)
|
||||
call DisplayMatrix MatrixT$
|
||||
23
Task/Matrix-transposition/Lua/matrix-transposition-1.lua
Normal file
23
Task/Matrix-transposition/Lua/matrix-transposition-1.lua
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
function Transpose( m )
|
||||
local res = {}
|
||||
|
||||
for i = 1, #m[1] do
|
||||
res[i] = {}
|
||||
for j = 1, #m do
|
||||
res[i][j] = m[j][i]
|
||||
end
|
||||
end
|
||||
|
||||
return res
|
||||
end
|
||||
|
||||
-- a test for Transpose(m)
|
||||
mat = { { 1, 2, 3 }, { 4, 5, 6 } }
|
||||
erg = Transpose( mat )
|
||||
for i = 1, #erg do
|
||||
for j = 1, #erg[1] do
|
||||
io.write( erg[i][j] )
|
||||
io.write( " " )
|
||||
end
|
||||
io.write( "\n" )
|
||||
end
|
||||
27
Task/Matrix-transposition/Lua/matrix-transposition-2.lua
Normal file
27
Task/Matrix-transposition/Lua/matrix-transposition-2.lua
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
function map(f, a)
|
||||
local b = {}
|
||||
for k,v in ipairs(a) do b[k] = f(v) end
|
||||
return b
|
||||
end
|
||||
|
||||
function mapn(f, ...)
|
||||
local c = {}
|
||||
local k = 1
|
||||
local aarg = {...}
|
||||
local n = #aarg
|
||||
while true do
|
||||
local a = map(function(b) return b[k] end, aarg)
|
||||
if #a < n then return c end
|
||||
c[k] = f(unpack(a))
|
||||
k = k + 1
|
||||
end
|
||||
end
|
||||
|
||||
function apply(f1, f2, a)
|
||||
return f1(f2, unpack(a))
|
||||
end
|
||||
|
||||
xy = {{1,2,3,4},{1,2,3,4},{1,2,3,4}}
|
||||
yx = apply(mapn, function(...) return {...} end, xy)
|
||||
print(table.concat(map(function(a) return table.concat(a,",") end, xy), "\n"),"\n")
|
||||
print(table.concat(map(function(a) return table.concat(a,",") end, yx), "\n"))
|
||||
13
Task/Matrix-transposition/MATLAB/matrix-transposition-1.m
Normal file
13
Task/Matrix-transposition/MATLAB/matrix-transposition-1.m
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
>> transpose([1 2;3 4])
|
||||
|
||||
ans =
|
||||
|
||||
1 3
|
||||
2 4
|
||||
|
||||
>> [1 2;3 4].'
|
||||
|
||||
ans =
|
||||
|
||||
1 3
|
||||
2 4
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue