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,17 @@
$ ocaml
# let make_id_matrix n =
let m = Array.make_matrix n n 0.0 in
for i = 0 to pred n do
m.(i).(i) <- 1.0
done;
(m)
;;
val make_id_matrix : int -> float array array = <fun>
# make_id_matrix 4 ;;
- : float array array =
[| [|1.; 0.; 0.; 0.|];
[|0.; 1.; 0.; 0.|];
[|0.; 0.; 1.; 0.|];
[|0.; 0.; 0.; 1.|] |]

View file

@ -0,0 +1,13 @@
# let make_id_matrix n =
Array.init n (fun i ->
Array.init n (fun j ->
if i = j then 1.0 else 0.0))
;;
val make_id_matrix : int -> float array array = <fun>
# make_id_matrix 4 ;;
- : float array array =
[| [|1.; 0.; 0.; 0.|];
[|0.; 1.; 0.; 0.|];
[|0.; 0.; 1.; 0.|];
[|0.; 0.; 0.; 1.|] |]

View file

@ -0,0 +1 @@
I = eye(10)

View file

@ -0,0 +1,30 @@
Class SquareMatrix
'=================
double *Cell
sys size
method SetIdentity()
indexbase 0
sys e,i,j
e=size*size
for i=0 to <size
cell(i*size+j)=1 : j++
next
end method
method constructor(sys n)
@cell=getmemory n*n*sizeof double
size=n
end method
method destructor()
freememory @cell
end method
end class
new SquareMatrix M(8)
M.SetIdentity
'...
del M

View file

@ -0,0 +1 @@
matid(9)

View file

@ -0,0 +1 @@
matrix(9,9,i,j,i==j)

View file

@ -0,0 +1,6 @@
identity: procedure (A, n);
declare A(n,n) fixed controlled;
declare (i,n) fixed binary;
allocate A; A = 0;
do i = 1 to n; A(i,i) = 1; end;
end identity;

View file

@ -0,0 +1,21 @@
program IdentityMatrix(input, output);
var
matrix: array of array of integer;
n, i, j: integer;
begin
write('Size of matrix: ');
readln(n);
setlength(matrix, n, n);
for i := 0 to n - 1 do
matrix[i,i] := 1;
for i := 0 to n - 1 do
begin
for j := 0 to n - 1 do
write (matrix[i,j], ' ');
writeln;
end;
end.

View file

@ -0,0 +1,9 @@
sub identity-matrix($n) {
my @id;
for ^$n X ^$n -> $i, $j {
@id[$i][$j] = +($i == $j);
}
@id;
}
.say for identity-matrix(5);

View file

@ -0,0 +1,5 @@
sub identity-matrix($n) {
my @id = [0 xx $n] xx $n;
@id[$_][$_] = 1 for ^$n;
@id;
}

View file

@ -0,0 +1,16 @@
% n ident [identity-matrix]
% create an identity matrix of dimension n*n.
% Uses a local dictionary for its one parameter, perhaps overkill.
% Constructs arrays of arrays of integers using [], for loops, and stack manipulation.
/ident { 1 dict begin /n exch def
[
1 1 n { % [ i
[ exch % [ [ i
1 1 n { % [ [ i j
1 index eq { 1 }{ 0 } ifelse % [ [ i b
exch % [ [ b i
} for % [ [ b+ i
pop ] % [ [ b+ ]
} for % [ [b+]+ ]
]
end } def

View file

@ -0,0 +1,37 @@
>Procedure identityMatrix(Array i(2), size) ;valid only for size >= 0
;formats array i() as an identity matrix of size x size
Dim i(size - 1, size - 1)
Protected j
For j = 0 To size - 1
i(j, j) = 1
Next
EndProcedure
Procedure displayMatrix(Array a(2))
Protected rows = ArraySize(a(), 2), columns = ArraySize(a(), 1)
Protected i, j
For i = 0 To rows
For j = 0 To columns
Print(RSet(Str(a(i, j)), 3, " "))
Next
PrintN("")
Next
EndProcedure
If OpenConsole()
Dim i3(0, 0)
Dim i4(0, 0)
identityMatrix(i3(), 3)
identityMatrix(i4(), 4)
displayMatrix(i3())
PrintN("")
displayMatrix(i4())
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf

View file

@ -0,0 +1,20 @@
' formats array im() of size ims
for ims = 4 to 6
print :print "--- Size: ";ims;" ---"
Dim im(ims,ims)
For i = 1 To ims
im(i,i) = 1
next
For row = 1 To ims
print "[";
cma$ = ""
For col = 1 To ims
print cma$;im(row, col);
cma$ = ", "
next
print "]"
next
next ims

View file

@ -0,0 +1,6 @@
link matrix
procedure main(argv)
if not (integer(argv[1]) > 0) then stop("Argument must be a positive integer.")
matrix1 := identity_matrix(argv[1], argv[1])
write_matrix(&output,matrix1)
end

View file

@ -0,0 +1,17 @@
include c:\cxpl\codes;
def IntSize = 4; \number of bytes in an integer
int Matrix, Size, I, J;
[Text(0, "Size: "); Size:= IntIn(0);
Matrix:= Reserve(Size*IntSize); \reserve memory for 2D integer array
for I:= 0 to Size-1 do
Matrix(I):= Reserve(Size*IntSize);
for J:= 0 to Size-1 do \make array an identity matrix
for I:= 0 to Size-1 do
Matrix(I,J):= if I=J then 1 else 0;
for J:= 0 to Size-1 do \display the result
[for I:= 0 to Size-1 do
[IntOut(0, Matrix(I,J)); ChOut(0, ^ )];
CrLf(0);
];
]