langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,3 @@
|a := 2 3 reshape count 6
=1 2 3
=4 5 6

View file

@ -0,0 +1,4 @@
|transpose a
=1 4
=2 5
=3 6

View file

@ -0,0 +1,31 @@
open Bigarray
let transpose b =
let dim1 = Array2.dim1 b
and dim2 = Array2.dim2 b in
let kind = Array2.kind b
and layout = Array2.layout b in
let b' = Array2.create kind layout dim2 dim1 in
for i=0 to pred dim1 do
for j=0 to pred dim2 do
b'.{j,i} <- b.{i,j}
done;
done;
(b')
;;
let array2_display print newline b =
for i=0 to Array2.dim1 b - 1 do
for j=0 to Array2.dim2 b - 1 do
print b.{i,j}
done;
newline();
done;
;;
let a = Array2.of_array int c_layout [|
[| 1; 2; 3; 4 |];
[| 5; 6; 7; 8 |];
|]
array2_display (Printf.printf " %d") print_newline (transpose a) ;;

View file

@ -0,0 +1,6 @@
let rec transpose m =
assert (m <> []);
if List.mem [] m then
[]
else
List.map List.hd m :: transpose (List.map List.tl m)

View file

@ -0,0 +1,31 @@
bundle Default {
class Transpose {
function : Main(args : String[]) ~ Nil {
input := [[1, 1, 1, 1]
[2, 4, 8, 16]
[3, 9, 27, 81]
[4, 16, 64, 256]
[5, 25, 125, 625]];
dim := input->Size();
output := Int->New[dim[0],dim[1]];
for(i := 0; i < dim[0]; i+=1;) {
for(j := 0; j < dim[1]; j+=1;) {
output[i,j] := input[i,j];
};
};
Print(output);
}
function : Print(matrix : Int[,]) ~ Nil {
dim := matrix->Size();
for(i := 0; i < dim[0]; i+=1;) {
for(j := 0; j < dim[1]; j+=1;) {
IO.Console->Print(matrix[i,j])->Print('\t');
};
'\n'->Print();
};
}
}
}

View file

@ -0,0 +1,7 @@
a = [ 1, 1, 1, 1 ;
2, 4, 8, 16 ;
3, 9, 27, 81 ;
4, 16, 64, 256 ;
5, 25, 125, 625 ];
tranposed = a.'; % tranpose
ctransp = a'; % conjugate transpose

View file

@ -0,0 +1,42 @@
function Transpose(double *A,*B, sys nx,ny)
'==========================================
sys x,y
indexbase 0
for x=0 to <nx
for y=0 to <ny
B[y*nx+x]=A[x*ny+y]
next
next
end function
function MatrixShow(double*A, sys nx,ny) as string
'=================================================
sys x,y
indexbase 0
string pr="",tab=chr(9),cr=chr(13)+chr(10)
for y=0 to <ny
for x=0 to <nx
pr+=tab A[x*ny+y]
next
pr+=cr
next
return pr
end function
'====
'DEMO
'====
double A[5*4],B[4*5]
'columns x
'rows y
A <= 'y minor, x major
11,12,13,14,15,
21,22,23,24,25,
31,32,33,34,35,
41,42,43,44,45
print MatrixShow A,5,4
Transpose A,B,5,4
print MatrixShow B,4,5

View file

@ -0,0 +1 @@
M~

View file

@ -0,0 +1 @@
gtrans(M)

View file

@ -0,0 +1,3 @@
/* The short method: */
declare A(m, n) float, B (n,m) float defined (A(2sub, 1sub));
/* Any reference to B gives the transpose of matrix A. */

View file

@ -0,0 +1,15 @@
/* Transpose matrix A, result at B. */
transpose: procedure (a, b);
declare (a, b) (*,*) float controlled;
declare (m, n) fixed binary;
if allocation(b) > 0 then free b;
m = hbound(a,1); n = hbound(a,2);
allocate b(n,m);
do i = 1 to m;
b(*,i) = a(i,*);
end;
end transpose;

View file

@ -0,0 +1,32 @@
Program Transpose;
const
A: array[1..3,1..5] of integer = (( 1, 2, 3, 4, 5),
( 6, 7, 8, 9, 10),
(11, 12, 13, 14, 15)
);
var
B: array[1..5,1..3] of integer;
i, j: integer;
begin
for i := low(A) to high(A) do
for j := low(A[1]) to high(A[1]) do
B[j,i] := A[i,j];
writeln ('A:');
for i := low(A) to high(A) do
begin
for j := low(A[1]) to high(A[1]) do
write (A[i,j]:3);
writeln;
end;
writeln ('B:');
for i := low(B) to high(B) do
begin
for j := low(B[1]) to high(B[1]) do
write (B[i,j]:3);
writeln;
end;
end.

View file

@ -0,0 +1,18 @@
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 (^10).pick X (^10).pick -> $x, $y { @a[$x][$y] = (^100).pick; }
say "original: ";
.perl.say for @a;
my @b = transpose(@a);
say "transposed: ";
.perl.say for @b;

View file

@ -0,0 +1,10 @@
sub transpose (@m) {
@m[0].keys.map: {[ @m».[$_] ]};
}
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;

View file

@ -0,0 +1,14 @@
define transpose(m) -> res;
lvars bl = boundslist(m);
if length(bl) /= 4 then
throw([need_2d_array ^a])
endif;
lvars i, i0 = bl(1), i1 = bl(2);
lvars j, j0 = bl(3), j1 = bl(4);
newarray([^j0 ^j1 ^i0 ^i1], 0) -> res;
for i from i0 to i1 do
for j from j0 to j1 do
m(i, j) -> res(j, i);
endfor;
endfor;
enddefine;

View file

@ -0,0 +1,6 @@
/transpose {
[ exch {
{ {empty? exch pop} map all?} {pop exit} ift
[ exch {} {uncons {exch cons} dip exch} fold counttomark 1 roll] uncons
} loop ] {reverse} map
}.

View file

@ -0,0 +1,52 @@
Procedure transposeMatrix(Array a(2), Array trans(2))
Protected rows, cols
Protected ar = ArraySize(a(), 1) ;rows in original matrix
Protected ac = ArraySize(a(), 2) ;cols in original matrix
;size the matrix receiving the transposition
Dim trans(ac, ar)
;copy the values
For rows = 0 To ar
For cols = 0 To ac
trans(cols, rows) = a(rows, cols)
Next
Next
EndProcedure
Procedure displayMatrix(Array a(2), text.s = "")
Protected i, j
Protected cols = ArraySize(a(), 2), rows = ArraySize(a(), 1)
PrintN(text + ": (" + Str(rows + 1) + ", " + Str(cols + 1) + ")")
For i = 0 To rows
For j = 0 To cols
Print(LSet(Str(a(i, j)), 4, " "))
Next
PrintN("")
Next
PrintN("")
EndProcedure
;setup a matrix of arbitrary size
Dim m(random(5), random(5))
Define rows, cols
;fill matrix with 'random' data
For rows = 0 To ArraySize(m(),1) ;ArraySize() can take a dimension as its second argument
For cols = 0 To ArraySize(m(), 2)
m(rows, cols) = random(10) - 10
Next
Next
Dim t(0,0) ;this will be resized during transposition
If OpenConsole()
displayMatrix(m(), "matrix before transposition")
transposeMatrix(m(), t())
displayMatrix(t(), "matrix after transposition")
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,10 @@
>> m = rand(3,5)
0.41844289 0.476591435 0.75054022 0.226388925 0.963880314
0.91267171 0.941762397 0.464227895 0.693482786 0.203839405
0.261512966 0.157981873 0.26582235 0.11557427 0.0442493069
>> m'
0.41844289 0.91267171 0.261512966
0.476591435 0.941762397 0.157981873
0.75054022 0.464227895 0.26582235
0.226388925 0.693482786 0.11557427
0.963880314 0.203839405 0.0442493069

View file

@ -0,0 +1,10 @@
public rel[real, real, real] matrixTranspose(rel[real x, real y, real v] matrix){
return {<y, x, v> | <x, y, v> <- matrix};
}
//a matrix
public rel[real x, real y, real v] matrixA = {
<0.0,0.0,12.0>, <0.0,1.0, 6.0>, <0.0,2.0,-4.0>,
<1.0,0.0,-51.0>, <1.0,1.0,167.0>, <1.0,2.0,24.0>,
<2.0,0.0,4.0>, <2.0,1.0,-68.0>, <2.0,2.0,-41.0>
};

View file

@ -0,0 +1,36 @@
mtrx$ ="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 mtrx$
print " ="
MatrixT$ =MatrixTranspose$(mtrx$)
call DisplayMatrix MatrixT$
end
function MatrixTranspose$(in$)
w = val(word$(in$, 1, ",")) ' swap w and h parameters
h = val(word$(in$, 2, ","))
t$ = str$(h); ","; str$(w); ","
for i =1 to w
for j =1 to h
t$ = t$ +word$(in$, 2 +i +(j -1) *w, ",") +","
next j
next i
MatrixTranspose$ =left$(t$, len(t$) -1)
end function
sub DisplayMatrix in$ ' Display looking like a matrix!
html "<table border=2>"
w = val(word$(in$, 1, ","))
h = val(word$(in$, 2, ","))
for i =0 to h -1
html "<tr align=right>"
for j =1 to w
term$ = word$(in$, j +2 +i *w, ",")
html "<td>";val(term$);"</td>"
next j
html "</tr>"
next i
html "</table>"
end sub

View file

@ -0,0 +1,46 @@
$ include "seed7_05.s7i";
include "float.s7i";
const type: matrix is array array float;
const func matrix: transpose (in matrix: aMatrix) is func
result
var matrix: transposedMatrix is matrix.value;
local
var integer: i is 0;
var integer: j is 0;
begin
transposedMatrix := length(aMatrix[1]) times length(aMatrix) times 0.0;
for i range 1 to length(aMatrix) do
for j range 1 to length(aMatrix[1]) do
transposedMatrix[j][i] := aMatrix[i][j];
end for;
end for;
end func;
const proc: write (in matrix: aMatrix) is func
local
var integer: line is 0;
var integer: column is 0;
begin
for line range 1 to length(aMatrix) do
for column range 1 to length(aMatrix[line]) do
write(" " <& aMatrix[line][column] digits 2);
end for;
writeln;
end for;
end func;
const matrix: testMatrix is [] (
[] (0.0, 0.1, 0.2, 0.3),
[] (0.4, 0.5, 0.6, 0.7),
[] (0.8, 0.9, 1.0, 1.1));
const proc: main is func
begin
writeln("Before Transposition:");
write(testMatrix);
writeln;
writeln("After Transposition:");
write(transpose(testMatrix));
end func;

View file

@ -0,0 +1,8 @@
#cast %eLL
example =
~&K7 <
<1.,2.,3.,4.>,
<5.,6.,7.,8.>,
<9.,10.,11.,12.>>