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,13 @@
open Complex
let print_complex z =
Printf.printf "%f + %f i\n" z.re z.im
let () =
let a = { re = 1.0; im = 1.0 }
and b = { re = 3.14159; im = 1.25 } in
print_complex (add a b);
print_complex (mul a b);
print_complex (inv a);
print_complex (neg a);
print_complex (conj a)

View file

@ -0,0 +1,14 @@
let () =
Complex.(
let print txt z = Printf.printf "%s = %s\n" txt (to_string z) in
let a = 1 + I
and b = 3 + 7I in
print "a + b" (a + b);
print "a - b" (a - b);
print "a * b" (a * b);
print "a / b" (a / b);
print "-a" (- a);
print "conj a" (conj a);
print "a^b" (a**b);
Printf.printf "norm a = %g\n" (float(abs a));
)

View file

@ -0,0 +1,14 @@
z1 = 1.5 + 3i;
z2 = 1.5 + 1.5i;
disp(z1 + z2); % 3.0 + 4.5i
disp(z1 - z2); % 0.0 + 1.5i
disp(z1 * z2); % -2.25 + 6.75i
disp(z1 / z2); % 1.5 + 0.5i
disp(-z1); % -1.5 - 3i
disp(z1'); % 1.5 - 3i
disp(abs(z1)); % 3.3541 = sqrt(z1*z1')
disp(z1 ^ z2); % -1.10248 - 0.38306i
disp( exp(z1) ); % -4.43684 + 0.63246i
disp( imag(z1) ); % 3
disp( real(z2) ); % 1.5
%...

View file

@ -0,0 +1,132 @@
'COMPLEX OPERATIONS
'=================
type tcomplex double x,y
class Complex
'============
has tcomplex
static sys i,pp
static tcomplex accum[32]
def operands
tcomplex*a,*b
@a=@accum+i
if pp then
@b=@a+sizeof accum
pp=0
else
@b=@this
end if
end def
method "load"()
operands
a.x=b.x
a.y=b.y
end method
method "push"()
i+=sizeof accum
end method
method "pop"()
pp=1
i-=sizeof accum
end method
method "="()
operands
b.x=a.x
b.y=a.y
end method
method "+"()
operands
a.x+=b.x
a.y+=b.y
end method
method "-"()
operands
a.x-=b.x
a.y-=b.y
end method
method "*"()
operands
double d
d=a.x
a.x = a.x * b.x - a.y * b.y
a.y = a.y * b.x + d * b.y
end method
method "/"()
operands
double d,v
v=1/(b.x * b.x + b.y * b.y)
d=a.x
a.x = (a.x * b.x + a.y * b.y) * v
a.y = (a.y * b.x - d * b.y) * v
end method
method power(double n)
operands
'Using DeMoivre theorem
double r,an,mg
r = hypot(b.x,b.y)
mg = r^n
if b.x=0 then
ay=.5*pi
if b.y<0 then ay=-ay
else
an = atan(b.y,b.x)
end if
an *= n
a.x = mg * cos(an)
a.y = mg * sin(an)
end method
method show() as string
return str(x,14) ", " str(y,14)
end method
end class
'#recordof complexop
'====
'TEST
'====
complex z1,z2,z3,z4,z5
'ENTER VALUES
z1 <= 0, 0
z2 <= 2, 1
z3 <= -2, 1
z4 <= 2, 4
z5 <= 1, 1
'EVALUATE COMPLEX EXPRESSIONS
z1 = z2 * z3
print "Z1 = "+z1.show 'RESULT -5.0, 0
z1 = z3+(z2.power(2))
print "Z1 = "+z1.show 'RESULT 1.0, 5.0
z1 = z5/z4
print "Z1 = "+z1.show 'RESULT 0.3, 0.1
z1 = z5/z1
print "Z1 = "+z1.show 'RESULT 2.0, 4.0
z1 = z2/z4
print "Z1 = "+z1.show 'RESULT -0.4, -0.3
z1 = z1*z4
print "Z1 = "+z1.show 'RESULT 2.0, 1.0

View file

@ -0,0 +1,4 @@
add(a,b)=a+b;
mult(a,b)=a*b;
neg(a)=-a;
inv(a)=1/a;

View file

@ -0,0 +1,21 @@
/* PL/I complex numbers may be integer or floating-point. */
/* In this example, the variables are floating-pint. */
/* For integer variables, change 'float' to 'fixed binary' */
declare (a, b) complex float;
a = 2+5i;
b = 7-6i;
put skip list (a+b);
put skip list (a - b);
put skip list (a*b);
put skip list (a/b);
put skip list (a**b);
put skip list (1/a);
put skip list (conjg(a)); /* gives the conjugate of 'a'. */
/* Functions exist for extracting the real and imaginary parts */
/* of a complex number. */
/* As well, trigonometric functions may be used with complex */
/* numbers, such as SIN, COS, TAN, ATAN, and so on. */

View file

@ -0,0 +1,68 @@
program showcomplex(output);
type
complex = record
re,im: real
end;
var
z1, z2, zr: complex;
procedure set(var result: complex; re, im: real);
begin
result.re := re;
result.im := im
end;
procedure print(a: complex);
begin
write('(', a.re , ',', a.im, ')')
end;
procedure add(var result: complex; a, b: complex);
begin
result.re := a.re + b.re;
result.im := a.im + b.im;
end;
procedure neg(var result: complex; a: complex);
begin
result.re := -a.re;
result.im := -a.im
end;
procedure mult(var result: complex; a, b: complex);
begin
result.re := a.re*b.re - a.im*b.im;
result.im := a.re*b.im + a.im*b.re
end;
procedure inv(var result: complex; a: complex);
var
anorm: real;
begin
anorm := a.re*a.re + a.im*a.im;
result.re := a.re/anorm;
result.im := -a.im/anorm
end;
begin
set(z1, 3, 4);
set(z2, 5, 6);
neg(zr, z1);
print(zr); { prints (-3,-4) }
writeln;
add(zr, z1, z2);
print(zr); { prints (8,10) }
writeln;
inv(zr, z1);
print(zr); { prints (0.12,-0.16) }
writeln;
mul(zr, z1, z2);
print(zr); { prints (-9,38) }
writeln
end.

View file

@ -0,0 +1,28 @@
Program ComplexDemo;
uses
ucomplex;
var
a, b, absum, abprod, aneg, ainv, acong: complex;
function complex(const re, im: real): ucomplex.complex; overload;
begin
complex.re := re;
complex.im := im;
end;
begin
a := complex(5, 3);
b := complex(0.5, 6.0);
absum := a + b;
writeln ('(5 + i3) + (0.5 + i6.0): ', absum.re:3:1, ' + i', absum.im:3:1);
abprod := a * b;
writeln ('(5 + i3) * (0.5 + i6.0): ', abprod.re:5:1, ' + i', abprod.im:4:1);
aneg := -a;
writeln ('-(5 + i3): ', aneg.re:3:1, ' + i', aneg.im:3:1);
ainv := 1.0 / a;
writeln ('1/(5 + i3): ', ainv.re:3:1, ' + i', ainv.im:3:1);
acong := cong(a);
writeln ('conj(5 + i3): ', acong.re:3:1, ' + i', acong.im:3:1);
end.

View file

@ -0,0 +1,5 @@
my $a = 1 + i;
my $b = pi + 1.25i;
.say for $a + $b, $a * $b, -$a, 1 / $a, $a.conj;
.say for $a.abs, $a.sqrt, $a.re, $a.im;

View file

@ -0,0 +1,19 @@
lvars a = 1.0 +: 1.0, b = 2.0 +: 5.0 ;
a+b =>
a*b =>
1/a =>
a-b =>
a-a =>
a/b =>
a/a =>
;;; The same, but using exact values
1 +: 1 -> a;
2 +: 5 -> b;
a+b =>
a*b =>
1/a =>
a-b =>
a-a =>
a/b =>
a/a =>

View file

@ -0,0 +1,47 @@
%Adding two complex numbers
/addcomp{
/x exch def
/y exch def
/z [0 0] def
z 0 x 0 get y 0 get add put
z 1 x 1 get y 1 get add put
z pstack
}def
%Subtracting one complex number from another
/subcomp{
/x exch def
/y exch def
/z [0 0] def
z 0 x 0 get y 0 get sub put
z 1 x 1 get y 1 get sub put
z pstack
}def
%Multiplying two complex numbers
/mulcomp{
/x exch def
/y exch def
/z [0 0] def
z 0 x 0 get y 0 get mul x 1 get y 1 get mul sub put
z 1 x 1 get y 0 get mul x 0 get y 1 get mul add put
z pstack
}def
%Negating a complex number
/negcomp{
/x exch def
/z [0 0] def
z 0 x 0 get neg put
z 1 x 1 get neg put
z pstack
}def
%Inverting a complex number
/invcomp{
/x exch def
/z [0 0] def
z 0 x 0 get x 0 get 2 exp x 1 get 2 exp add div put
z 0 x 1 get neg x 0 get 2 exp x 1 get 2 exp add div put
z pstack
}def

View file

@ -0,0 +1,65 @@
Structure Complex
real.d
imag.d
EndStructure
Procedure Add_Complex(*A.Complex, *B.Complex)
Protected *R.Complex=AllocateMemory(SizeOf(Complex))
If *R
*R\real=*A\real+*B\real
*R\imag=*A\imag+*B\imag
EndIf
ProcedureReturn *R
EndProcedure
Procedure Inv_Complex(*A.Complex)
Protected *R.Complex=AllocateMemory(SizeOf(Complex)), denom.d
If *R
denom = *A\real * *A\real + *A\imag * *A\imag
*R\real= *A\real / denom
*R\imag=-*A\imag / denom
EndIf
ProcedureReturn *R
EndProcedure
Procedure Mul_Complex(*A.Complex, *B.Complex)
Protected *R.Complex=AllocateMemory(SizeOf(Complex))
If *R
*R\real=*A\real * *B\real - *A\imag * *B\imag
*R\imag=*A\real * *B\imag + *A\imag * *B\real
EndIf
ProcedureReturn *R
EndProcedure
Procedure Neg_Complex(*A.Complex)
Protected *R.Complex=AllocateMemory(SizeOf(Complex))
If *R
*R\real=-*A\real
*R\imag=-*A\imag
EndIf
ProcedureReturn *R
EndProcedure
Procedure ShowAndFree(Header$, *Complex.Complex)
If *Complex
Protected.d i=*Complex\imag, r=*Complex\real
Print(LSet(Header$,7))
Print("= "+StrD(r,3))
If i>=0: Print(" + ")
Else: Print(" - ")
EndIf
PrintN(StrD(Abs(i),3)+"i")
FreeMemory(*Complex)
EndIf
EndProcedure
If OpenConsole()
Define.Complex a, b, *c
a\real=1.0: a\imag=1.0
b\real=#PI: b\imag=1.2
*c=Add_Complex(a,b): ShowAndFree("a+b", *c)
*c=Mul_Complex(a,b): ShowAndFree("a*b", *c)
*c=Inv_Complex(a): ShowAndFree("Inv(a)", *c)
*c=Neg_Complex(a): ShowAndFree("-a", *c)
Print(#CRLF$+"Press ENTER to exit"):Input()
EndIf

View file

@ -0,0 +1,8 @@
>> x = sqrt(-1)
0 + 1i
>> y = 10 + 5i
10 + 5i
>> z = 5*x-y
-10 + 0i
>> isreal(z)
1

View file

@ -0,0 +1,40 @@
* # Define complex datatype
data('complex(r,i)')
* # Addition
define('addx(x1,x2)a,b,c,d') :(addx_end)
addx a = r(x1); b = i(x1); c = r(x2); d = i(x2)
addx = complex(a + c, b + d) :(return)
addx_end
* # Multiplication
define('multx(x1,x2)a,b,c,d') :(multx_end)
multx a = r(x1); b = i(x1); c = r(x2); d = i(x2)
multx = complex(a * c - b * d, b * c + a * d) :(return)
multx_end
* # Negation
define('negx(x)') :(negx_end)
negx negx = complex(-r(x), -i(x)) :(return)
negx_end
* # Inverse
define('invx(x)d') :(invx_end)
invx d = (r(x) * r(x)) + (i(x) * i(x))
invx = complex(1.0 * r(x) / d, 1.0 * -i(x) / d) :(return)
invx_end
* # Print compex number: a+bi / a-bi
define('printx(x)sign') :(printx_end)
printx sign = ge(i(x),0) '+'
printx = r(x) sign i(x) 'i' :(return)
printx_end
* # Test and display
a = complex(1,1)
b = complex(3.14159, 1.2)
output = printx( addx(a,b) )
output = printx( multx(a,b) )
output = printx( negx(a) ) ', ' printx( negx(b) )
output = printx( invx(a) ) ', ' printx( invx(b) )
end

View file

@ -0,0 +1,20 @@
$ include "seed7_05.s7i";
include "float.s7i";
include "complex.s7i";
const proc: main is func
local
var complex: a is complex(1.0, 1.0);
var complex: b is complex(3.14159, 1.2);
begin
writeln("a=" <& a digits 5);
writeln("b=" <& b digits 5);
# addition
writeln("a+b=" <& a + b digits 5);
# multiplication
writeln("a*b=" <& a * b digits 5);
# inversion
writeln("1/a=" <& complex(1.0) / a digits 5);
# negation
writeln("-a=" <& -a digits 5);
end func;

View file

@ -0,0 +1,11 @@
[| a b |
a: 1 + 1 i.
b: Pi + 1.2 i.
print: a + b.
print: a * b.
print: a / b.
print: a reciprocal.
print: a conjugated.
print: a abs.
print: a negated.
].

View file

@ -0,0 +1,43 @@
(* Signature for complex numbers *)
signature COMPLEX = sig
type num
val complex : real * real -> num
val negative : num -> num
val plus : num -> num -> num
val minus : num -> num -> num
val times : num -> num -> num
val invert : num -> num
val print_number : num -> unit
end;
(* Actual implementation *)
structure Complex :> COMPLEX = struct
type num = real * real
fun complex (a, b) = (a, b)
fun negative (a, b) = (Real.~a, Real.~b)
fun plus (a1, b1) (a2, b2) = (Real.+ (a1, a2), Real.+(b1, b2))
fun minus i1 i2 = plus i1 (negative i2)
fun times (a1, b1) (a2, b2)= (Real.*(a1, a2) - Real.*(b1, b2), Real.*(a1, b2) + Real.*(a2, b1))
fun invert (a, b) =
let
val denom = a * a + b * b
in
(a / denom, ~b / denom)
end
fun print_number (a, b) =
print (Real.toString(a) ^ " + " ^ Real.toString(b) ^ "i\n")
end;
val i1 = Complex.complex(1.0,2.0); (* 1 + 2i *)
val i2 = Complex.complex(3.0,4.0); (* 3 + 4i *)
Complex.print_number(Complex.negative(i1)); (* -1 - 2i *)
Complex.print_number(Complex.plus i1 i2); (* 4 + 6i *)
Complex.print_number(Complex.minus i2 i1); (* 2 + 2i *)
Complex.print_number(Complex.times i1 i2); (* -5 + 10i *)
Complex.print_number(Complex.invert i1); (* 1/5 - 2i/5 *)

View file

@ -0,0 +1,12 @@
u = 3.785e+00-1.969e+00i
v = 9.545e-01-3.305e+00j
#cast %jL
examples =
<
complex..add (u,v),
complex..mul (u,v),
complex..sub (0.,u),
complex..div (1.,v)>

View file

@ -0,0 +1,57 @@
include c:\cxpl\codes;
func real CAdd(A, B, C); \Return complex sum of two complex numbers
real A, B, C;
[C(0):= A(0) + B(0);
C(1):= A(1) + B(1);
return C;
];
func real CMul(A, B, C); \Return complex product of two complex numbers
real A, B, C;
[C(0):= A(0)*B(0) - A(1)*B(1);
C(1):= A(1)*B(0) + A(0)*B(1);
return C;
];
func real CNeg(A, C); \Return negative of a complex number
real A, C;
[C(0):= -A(0);
C(1):= -A(1);
return C;
];
func real CInv(A, C); \Return inversion (reciprical) of complex number
real A, C;
real D;
[D:= sq(A(0)) + sq(A(1));
C(0):= A(0)/D;
C(1):=-A(1)/D;
return C;
];
func real Conj(A, C); \Return conjugate of a complex number
real A, C;
[C(0):= A(0);
C(1):=-A(1);
return C;
];
proc COut(D, A); \Output a complex number to specified device
int D; real A;
[RlOut(D, A(0));
Text(D, if A(1)>=0.0 then " +" else " -");
RlOut(D, abs(A(1)));
ChOut(D, ^i);
];
real U, V, W(2);
[Format(2,2);
U:= [1.0, 1.0];
V:= [3.14, 1.2];
COut(0, CAdd(U,V,W)); CrLf(0);
COut(0, CMul(U,V,W)); CrLf(0);
COut(0, CNeg(U,W)); CrLf(0);
COut(0, CInv(U,W)); CrLf(0);
COut(0, Conj(U,W)); CrLf(0);
]