This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -0,0 +1,23 @@
(deftype Complex [real imag]
Object
(toString [this] (str real " " imag "j")))
(defn c-add [^Complex a ^Complex b]
(Complex. (+ (.real a) (.real b))
(+ (.imag a) (.imag b))))
(defn c-mul [^Complex a ^Complex b]
(Complex. (- (* (.real a) (.real b)) (* (.imag a) (.imag b)))
(+ (* (.real a) (.imag b)) (* (.imag a) (.real b)))))
(defn c-neg [^Complex a]
(Complex. (- (.real a)) (- (.imag a))))
(defn c-inv [^Complex a]
(let [r (.real a)
i (.imag a)
m (+ (* r r) (* i i))]
(Complex. (/ r m) (- (/ i m)))))
(defn c-conj [^Complex a]
(Complex. (.real a) (- (.imag a))))

View file

@ -0,0 +1,17 @@
(Complex. 1 1)
#<Complex 1 1j>
(c-add (Complex. 3.17 7) (Complex. 1 1.77))
#<Complex 4.17 8.77j>
(c-mul (Complex. 0 1) (Complex. 0 1))
#<Complex -1 0j>
(c-neg (Complex. 1 2))
#<Complex -1 -2j>
(c-inv (Complex. 1 1))
#<Complex 1/2 -1/2j>
(c-conj (Complex. 1 1))
#<Complex 1 -1j>

View file

@ -0,0 +1,86 @@
MODULE Complex;
IMPORT StdLog;
TYPE
Complex* = POINTER TO ComplexDesc;
ComplexDesc = RECORD
r-,i-: REAL;
END;
VAR
r,x,y: Complex;
PROCEDURE New(x,y: REAL): Complex;
VAR
r: Complex;
BEGIN
NEW(r);r.r := x;r.i := y;
RETURN r
END New;
PROCEDURE (x: Complex) Add*(y: Complex): Complex,NEW;
BEGIN
RETURN New(x.r + y.r,x.i + y.i)
END Add;
PROCEDURE ( x: Complex) Sub*( y: Complex): Complex, NEW;
BEGIN
RETURN New(x.r - y.r,x.i - y.i)
END Sub;
PROCEDURE ( x: Complex) Mul*( y: Complex): Complex, NEW;
BEGIN
RETURN New(x.r*y.r - x.i*y.i,x.r*y.i + x.i*y.r)
END Mul;
PROCEDURE ( x: Complex) Div*( y: Complex): Complex, NEW;
VAR
d: REAL;
BEGIN
d := y.r * y.r + y.i * y.i;
RETURN New((x.r*y.r + x.i*y.i)/d,(x.i*y.r - x.r*y.i)/d)
END Div;
(* Reciprocal *)
PROCEDURE (x: Complex) Rec*(): Complex,NEW;
VAR
d: REAL;
BEGIN
d := x.r * x.r + x.i * x.i;
RETURN New(x.r/d,(-1.0 * x.i)/d);
END Rec;
(* Conjugate *)
PROCEDURE (x: Complex) Con*(): Complex,NEW;
BEGIN
RETURN New(x.r, (-1.0) * x.i);
END Con;
PROCEDURE (x: Complex) Out(),NEW;
BEGIN
StdLog.String("Complex(");
StdLog.Real(x.r);StdLog.String(',');StdLog.Real(x.i);
StdLog.String("i );")
END Out;
PROCEDURE Do*;
BEGIN
x := New(1.5,3);
y := New(1.0,1.0);
StdLog.String("x: ");x.Out();StdLog.Ln;
StdLog.String("y: ");y.Out();StdLog.Ln;
r := x.Add(y);
StdLog.String("x + y: ");r.Out();StdLog.Ln;
r := x.Sub(y);
StdLog.String("x - y: ");r.Out();StdLog.Ln;
r := x.Mul(y);
StdLog.String("x * y: ");r.Out();StdLog.Ln;
r := x.Div(y);
StdLog.String("x / y: ");r.Out();StdLog.Ln;
r := y.Rec();
StdLog.String("1 / y: ");r.Out();StdLog.Ln;
r := x.Con();
StdLog.String("x': ");r.Out();StdLog.Ln;
END Do;
END Complex.

View file

@ -0,0 +1 @@
=IMSUM(A1;B1)

View file

@ -0,0 +1 @@
=IMPRODUCT(A1;B1)

View file

@ -0,0 +1 @@
=IMSUB(0;D1)

View file

@ -0,0 +1 @@
=IMDIV(1;E28)

View file

@ -0,0 +1 @@
=IMCONJUGATE(C28)

View file

@ -0,0 +1 @@
1+2i 3+5i 4+7i -7+11i 7-11i 0,0411764705882353+0,0647058823529412i 4-7i

View file

@ -0,0 +1,26 @@
using System;
using System.Console;
using System.Numerics;
using System.Numerics.Complex;
module RCComplex
{
PrettyPrint(this c : Complex) : string
{
mutable sign = '+';
when (c.Imaginary < 0) sign = '-';
$"$(c.Real) $sign $(Math.Abs(c.Imaginary))i"
}
Main() : void
{
def complex1 = Complex(1.0, 1.0);
def complex2 = Complex(3.14159, 1.2);
WriteLine(Add(complex1, complex2).PrettyPrint());
WriteLine(Multiply(complex1, complex2).PrettyPrint());
WriteLine(Negate(complex2).PrettyPrint());
WriteLine(Reciprocal(complex2).PrettyPrint());
WriteLine(Conjugate(complex2).PrettyPrint());
}
}

View file

@ -0,0 +1,85 @@
MODULE Complex;
IMPORT Files,Out;
TYPE
Complex* = POINTER TO ComplexDesc;
ComplexDesc = RECORD
r-,i-: REAL;
END;
PROCEDURE (CONST x: Complex) Add*(CONST y: Complex): Complex;
BEGIN
RETURN New(x.r + y.r,x.i + y.i)
END Add;
PROCEDURE (CONST x: Complex) Sub*(CONST y: Complex): Complex;
BEGIN
RETURN New(x.r - y.r,x.i - y.i)
END Sub;
PROCEDURE (CONST x: Complex) Mul*(CONST y: Complex): Complex;
BEGIN
RETURN New(x.r*y.r - x.i*y.i,x.r*y.i + x.i*y.r)
END Mul;
PROCEDURE (CONST x: Complex) Div*(CONST y: Complex): Complex;
VAR
d: REAL;
BEGIN
d := y.r * y.r + y.i * y.i;
RETURN New((x.r*y.r + x.i*y.i)/d,(x.i*y.r - x.r*y.i)/d)
END Div;
(* Reciprocal *)
PROCEDURE (CONST x: Complex) Rec*(): Complex;
VAR
d: REAL;
BEGIN
d := x.r * x.r + y.i * y.i;
RETURN New(x.r/d,(-1.0 * x.i)/d);
END Rec;
(* Conjugate *)
PROCEDURE (x: Complex) Con*(): Complex;
BEGIN
RETURN New(x.r, (-1.0) * x.i);
END Con;
PROCEDURE (x: Complex) Out(out : Files.File);
BEGIN
Files.WriteString(out,"(");
Files.WriteReal(out,x.r);
Files.WriteString(out,",");
Files.WriteReal(out,x.i);
Files.WriteString(out,"i)")
END Out;
PROCEDURE New(x,y: REAL): Complex;
VAR
r: Complex;
BEGIN
NEW(r);r.r := x;r.i := y;
RETURN r
END New;
VAR
r,x,y: Complex;
BEGIN
x := New(1.5,3);
y := New(1.0,1.0);
Out.String("x: ");x.Out(Files.stdout);Out.Ln;
Out.String("y: ");y.Out(Files.stdout);Out.Ln;
r := x.Add(y);
Out.String("x + y: ");r.Out(Files.stdout);Out.Ln;
r := x.Sub(y);
Out.String("x - y: ");r.Out(Files.stdout);Out.Ln;
r := x.Mul(y);
Out.String("x * y: ");r.Out(Files.stdout);Out.Ln;
r := x.Div(y);
Out.String("x / y: ");r.Out(Files.stdout);Out.Ln;
r := y.Rec();
Out.String("1 / y: ");r.Out(Files.stdout);Out.Ln;
r := x.Con();
Out.String("x': ");r.Out(Files.stdout);Out.Ln;
END Complex.

View file

@ -1,9 +1,10 @@
require 'complex' # With Ruby 1.9, this line is optional.
# Two ways to write complex numbers:
# Three ways to write complex numbers:
a = Complex(1, 1) # 1. call Kernel#Complex
i = Complex::I # 2. use Complex::I
b = 3.14159 + 1.25 * i
c = '1/2+3/4i'.to_c # 3. Use the .to_c method from String, result ((1/2)+(3/4)*i)
# Operations:
puts a + b # addition