Time for an 2014 update…

This commit is contained in:
Ingy döt Net 2014-01-17 05:32:22 +00:00
parent 372c577f83
commit 09687c4926
2520 changed files with 34227 additions and 7318 deletions

View file

@ -0,0 +1,98 @@
$SET SOURCEFORMAT "FREE"
class-id Prog.
method-id. Main static.
procedure division.
declare a as type Complex = new Complex(1, 1)
declare b as type Complex = new Complex(3.14159, 1.25)
display "a = " a
display "b = " b
display space
declare result as type Complex = a + b
display "a + b = " result
move (a - b) to result
display "a - b = " result
move (a * b) to result
display "a * b = " result
move (a / b) to result
display "a / b = " result
move (- b) to result
display "-b = " result
display space
display "Inverse of b: " type Complex::Inverse(b)
display "Conjugate of b: " type Complex::Conjugate(b)
end method.
end class.
class-id Complex.
01 Real float-long property.
01 Imag float-long property.
method-id new.
set Real, Imag to 0
end method.
method-id new.
procedure division using value real-val as float-long, imag-val as float-long.
set Real to real-val
set Imag to imag-val
end method.
method-id Norm static.
procedure division using value a as type Complex returning ret as float-long.
compute ret = a::Real ** 2 + a::Imag ** 2
end method.
method-id Inverse static.
procedure division using value a as type Complex returning ret as type Complex.
declare norm as float-long = type Complex::Norm(a)
set ret to new Complex(a::Real / norm, (0 - a::Imag) / norm)
end method.
method-id Conjugate static.
procedure division using value a as type Complex returning c as type Complex.
set c to new Complex(a::Real, 0 - a::Imag)
end method.
method-id ToString override.
procedure division returning str as string.
set str to type String::Format("{0}{1:+#0;-#}i", Real, Imag)
end method.
operator-id + .
procedure division using value a as type Complex, b as type Complex
returning c as type Complex.
set c to new Complex(a::Real + b::Real, a::Imag + b::Imag)
end operator.
operator-id - .
procedure division using value a as type Complex, b as type Complex
returning c as type Complex.
set c to new Complex(a::Real - b::Real, a::Imag - b::Imag)
end operator.
operator-id * .
procedure division using value a as type Complex, b as type Complex
returning c as type Complex.
set c to new Complex(a::Real * b::Real - a::Imag * b::Imag,
a::Real * b::Imag + a::Imag * b::Real)
end operator.
operator-id / .
procedure division using value a as type Complex, b as type Complex
returning c as type Complex.
set c to new Complex()
declare b-norm as float-long = type Complex::Norm(b)
compute c::Real = (a::Real * b::Real + a::Imag * b::Imag) / b-norm
compute c::Imag = (a::Imag * b::Real - a::Real * b::Imag) / b-norm
end operator.
operator-id - .
procedure division using value a as type Complex returning ret as type Complex.
set ret to new Complex(- a::Real, 0 - a::Imag)
end operator.
end class.

View file

@ -0,0 +1,50 @@
class complex {
num real=0;
num imag=0;
complex(num r,num i){
this.real=r;
this.imag=i;
}
complex add(complex b){
return new complex(this.real + b.real, this.imag + b.imag);
}
complex mult(complex b){
//FOIL of (a+bi)(c+di) with i*i = -1
return new complex(this.real * b.real - this.imag * b.imag, this.real * b.imag + this.imag * b.real);
}
complex inv(){
//1/(a+bi) * (a-bi)/(a-bi) = 1/(a+bi) but it's more workable
num denom = real * real + imag * imag;
double r =real/denom;
double i= -imag/denom;
return new complex( r,-i);
}
complex neg(){
return new complex(-real, -imag);
}
complex conj(){
return new complex(real, -imag);
}
String toString(){
return this.real.toString()+' + '+ this.imag.toString()+'*i';
}
}
void main() {
var cl= new complex(1,2);
var cl2= new complex(3,-1);
print(cl.toString());
print(cl2.toString());
print(cl.inv().toString());
print(cl2.mult(cl).toString());
}

View file

@ -0,0 +1,22 @@
julia> z1 = 1.5 + 3im
julia> z2 = 1.5 + 1.5im
julia> z1 + z2
3.0 + 4.5im
julia> z1 - z2
0.0 + 1.5im
julia> z1 * z2
-2.25 + 6.75im
julia> z1 / z2
1.5 + 0.5im
julia> - z1
-1.5 - 3.0im
julia> conj(z1), z1' # two ways to conjugate
(1.5 - 3.0im,1.5 - 3.0im)
julia> abs(z1)
3.3541019662496847
julia> z1^z2
-1.102482955327779 - 0.38306415117199305im
julia> real(z1)
1.5
julia> imag(z1)
3.0

View file

@ -0,0 +1,10 @@
import complex
var a: TComplex = (1.0,1.0)
var b: TComplex = (3.1415,1.2)
echo ("a : " & $a)
echo ("b : " & $b)
echo ("a + b: " & $(a + b))
echo ("a * b: " & $(a * b))
echo ("1/a : " & $(1/a))
echo ("-a : " & $(-a))

View file

@ -6,6 +6,9 @@ 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)
#Ruby 2.1 introduced a suffix to create a complex:
c = 1.0/2+3/4i # (0.5-(3/4)*i)
# Operations:
puts a + b # addition
puts a * b # multiplication