September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,31 +1,31 @@
|
|||
-- TRANSPOSE -----------------------------------------------------------------
|
||||
|
||||
-- transpose :: [[a]] -> [[a]]
|
||||
on transpose(xss)
|
||||
script column
|
||||
on lambda(_, iCol)
|
||||
on |λ|(_, iCol)
|
||||
script row
|
||||
on lambda(xs)
|
||||
on |λ|(xs)
|
||||
item iCol of xs
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(row, xss)
|
||||
end lambda
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
map(column, item 1 of xss)
|
||||
end transpose
|
||||
|
||||
|
||||
-- TEST
|
||||
-- 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 LIBRARY FUNCTIONS
|
||||
-- GENERIC FUNCTIONS ---------------------------------------------------------
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
|
|
@ -33,7 +33,7 @@ on map(f, xs)
|
|||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
|
|
@ -46,7 +46,7 @@ on mReturn(f)
|
|||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
(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]
|
||||
(vec (apply map vector mtx)))
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
=> (matrix-transpose [[1 2 3] [4 5 6]])
|
||||
[[1 4] [2 5] [3 6]]
|
||||
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
|
||||
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
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
REAL :: mtx(2, 4)
|
||||
|
||||
mtx = 1.1 * $
|
||||
WRITE() mtx
|
||||
|
||||
SOLVE(Matrix=mtx, Transpose=mtx)
|
||||
WRITE() mtx
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
1.1 2.2 3.3 4.4
|
||||
5.5 6.6 7.7 8.8
|
||||
|
||||
1.1 5.5
|
||||
2.2 6.6
|
||||
3.3 7.7
|
||||
4.4 8.8
|
||||
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]]
|
||||
28
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
28
Task/Matrix-transposition/Kotlin/matrix-transposition.kotlin
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
// 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
|
||||
}
|
||||
|
||||
fun printMatrix(m: Matrix) {
|
||||
for (i in 0 until m.size) println(m[i].contentToString())
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val m = arrayOf(
|
||||
doubleArrayOf( 1.0, 2.0, 3.0),
|
||||
doubleArrayOf( 4.0, 5.0, 6.0),
|
||||
doubleArrayOf( 7.0, 8.0, 9.0),
|
||||
doubleArrayOf(10.0, 11.0, 12.0)
|
||||
)
|
||||
printMatrix(m.transpose())
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
sub transpose(@m)
|
||||
{
|
||||
my @t;
|
||||
for ^@m X ^@m[0] -> ($x, $y) { @t[$y][$x] = @m[$x][$y] }
|
||||
return @t;
|
||||
}
|
||||
|
||||
# creates a random matrix
|
||||
my @a;
|
||||
for ^5 X ^5 -> ($x, $y) { @a[$x][$y] = ('a'..'z').pick; }
|
||||
say "original:";
|
||||
.gist.say for @a;
|
||||
|
||||
my @b = transpose(@a);
|
||||
say "transposed:";
|
||||
.gist.say for @b;
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
sub transpose (@m) {
|
||||
([ @m[*;$_] ] for ^@m[0]);
|
||||
}
|
||||
|
||||
my @a = < a b c d e >,
|
||||
< f g h i j >,
|
||||
< k l m n o >,
|
||||
< p q r s t >;
|
||||
|
||||
.say for @a.&transpose;
|
||||
23
Task/Matrix-transposition/REXX/matrix-transposition-1.rexx
Normal file
23
Task/Matrix-transposition/REXX/matrix-transposition-1.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/*REXX program transposes any sized rectangular matrix, displays before & after matrices*/
|
||||
@.=; @.1 = 1.02 2.03 3.04 4.05 5.06 6.07 7.08
|
||||
@.2 = 111 2222 33333 444444 5555555 66666666 777777777
|
||||
do row=1 while @.row\==''
|
||||
do col=1 while @.row\==''; parse var @.row A.row.col @.row
|
||||
end /*c*/
|
||||
end /*r*/ /* [↑] build matrix A from matrix X*/
|
||||
rows=row-1; cols=col-1 /*adjust for the DO loop indices. */
|
||||
L=0; do j=1 for rows
|
||||
do k=1 for cols
|
||||
B.k.j=A.j.k; L=max( L, length(B.k.j) )
|
||||
end /*k*/ /* ↑ */
|
||||
end /*j*/ /* └─── is maximum width element value*/
|
||||
call showMat 'A',rows,cols
|
||||
call showMat 'B',cols,rows
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showMat: arg mat,rows,cols; say; say center( mat 'matrix', (L+1)*cols +4, "─")
|
||||
do r=1 for rows; _=
|
||||
do c=1 for cols; _=_ right( value( mat'.'r"."c), L)
|
||||
end /*c*/
|
||||
say _
|
||||
end /*r*/; return
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showMat: arg mat,rows,cols; say; say center( mat 'matrix', (L+1)*cols +4, "─")
|
||||
$=.
|
||||
do r=1 for rows; _=
|
||||
do c=1 for cols; _=_ right( value( mat || $ || r || $ || c), L)
|
||||
end /*c*/
|
||||
say _
|
||||
end /*r*/; return
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
/*REXX program transposes a matrix, and displays the before and after matrices. */
|
||||
x.=; x.1 = 1.02 2.03 3.04 4.05 5.06 6.07 7.07
|
||||
x.2 = 111 2222 33333 444444 5555555 66666666 777777777
|
||||
|
||||
do r=1 while x.r\=='' /*build matrix A from matrix X.*/
|
||||
do c=1 while x.r\==''
|
||||
parse var x.r a.r.c x.r
|
||||
end /*c*/
|
||||
end /*r*/
|
||||
rows=r-1; cols=c-1 /*adjust for DO loop indices. */
|
||||
L=0 /*L is max width element value. */
|
||||
do i=1 for rows
|
||||
do j=1 for cols
|
||||
b.j.i=a.i.j; L=max( L, length(b.j.i) )
|
||||
end /*j*/
|
||||
end /*i*/
|
||||
|
||||
call showMat 'A', rows, cols
|
||||
call showMat 'B', cols, rows
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showMat: parse arg mat,rows,cols; say; say center(mat 'matrix', cols*(L+1)+4, "─")
|
||||
do r=1 for rows; _=
|
||||
do c=1 for cols; _=_ right(value(mat'.'r"."c), L)
|
||||
end /*c*/
|
||||
say _
|
||||
end /*r*/
|
||||
return
|
||||
48
Task/Matrix-transposition/Rust/matrix-transposition.rust
Normal file
48
Task/Matrix-transposition/Rust/matrix-transposition.rust
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
struct Matrix {
|
||||
dat: [[i32; 3]; 3]
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl Matrix {
|
||||
pub fn transpose_m(a: Matrix) -> Matrix
|
||||
{
|
||||
let mut out = Matrix {
|
||||
dat: [[0, 0, 0],
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
]
|
||||
};
|
||||
|
||||
for i in 0..3{
|
||||
for j in 0..3{
|
||||
|
||||
out.dat[i][j] = a.dat[j][i];
|
||||
}
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn print(self)
|
||||
{
|
||||
for i in 0..3 {
|
||||
for j in 0..3 {
|
||||
print!("{} ", self.dat[i][j]);
|
||||
}
|
||||
print!("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main()
|
||||
{
|
||||
let a = Matrix {
|
||||
dat: [[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9] ]
|
||||
};
|
||||
|
||||
let c = Matrix::transpose_m(a);
|
||||
c.print();
|
||||
}
|
||||
6
Task/Matrix-transposition/Zkl/matrix-transposition-1.zkl
Normal file
6
Task/Matrix-transposition/Zkl/matrix-transposition-1.zkl
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library)
|
||||
GSL.Matrix(2,3).set(1,2,3, 4,5,6).transpose().format(5).println(); // in place
|
||||
println("---");
|
||||
GSL.Matrix(2,2).set(1,2, 3,4).transpose().format(5).println(); // in place
|
||||
println("---");
|
||||
GSL.Matrix(3,1).set(1,2,3).transpose().format(5).println(); // in place
|
||||
4
Task/Matrix-transposition/Zkl/matrix-transposition-2.zkl
Normal file
4
Task/Matrix-transposition/Zkl/matrix-transposition-2.zkl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fcn transpose(M){
|
||||
if(M.len()==1) M[0].pump(List,List.create); // 1 row --> n columns
|
||||
else M[0].zip(M.xplode(1));
|
||||
}
|
||||
5
Task/Matrix-transposition/Zkl/matrix-transposition-3.zkl
Normal file
5
Task/Matrix-transposition/Zkl/matrix-transposition-3.zkl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
m:=T(T(1,2,3),T(4,5,6)); transpose(m).println();
|
||||
m:=L(L(1,"a"),L(2,"b"),L(3,"c")); transpose(m).println();
|
||||
transpose(L(L(1,2,3))).println();
|
||||
transpose(L(L(1),L(2),L(3))).println();
|
||||
transpose(L(L(1))).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue