annotate complex.yeti @ 57:08b2b9fce25c

Remove the complex constants; they're not very useful and pollute the namespace
author Chris Cannam
date Wed, 09 Jan 2013 21:35:37 +0000
parents d037211bf5d7
children 3a13af4bd8ba
rev   line source
Chris@44 1
Chris@44 2 module complex;
Chris@44 3
Chris@46 4 import java.lang: ClassCastException;
Chris@46 5
Chris@45 6 class Complex(double real, double imag)
Chris@44 7 int getReal()
Chris@44 8 real,
Chris@44 9 int getImag()
Chris@44 10 imag,
Chris@53 11 double getMagnitude()
Chris@53 12 sqrt (real * real + imag * imag),
Chris@53 13 double getAngle()
Chris@53 14 Math#atan2(imag, real),
Chris@44 15 String toString()
Chris@48 16 if real == int real and imag == int imag then
Chris@48 17 if imag < 0 then
Chris@48 18 " \(int real) - \(int (-imag))i"
Chris@48 19 else
Chris@48 20 " \(int real) + \(int imag)i"
Chris@48 21 fi
Chris@48 22 else
Chris@48 23 if imag < 0 then
Chris@48 24 " \(real) - \((-imag))i"
Chris@48 25 else
Chris@48 26 " \(real) + \(imag)i"
Chris@48 27 fi
Chris@48 28 fi,
Chris@46 29 int hashCode()
Chris@46 30 Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(),
Chris@46 31 boolean equals(Object other)
Chris@46 32 try
Chris@46 33 c = other unsafely_as ~Complex;
Chris@46 34 c#getReal() == real and c#getImag() == imag
Chris@46 35 catch ClassCastException:
Chris@46 36 false
Chris@46 37 yrt,
Chris@44 38 end;
Chris@44 39
Chris@44 40 typedef opaque cplx = ~Complex;
Chris@44 41
Chris@46 42 real c1 is ~Complex -> number =
Chris@46 43 c1#getReal();
Chris@46 44
Chris@46 45 imaginary c1 is ~Complex -> number =
Chris@46 46 c1#getImag();
Chris@46 47
Chris@46 48 complex re im is number -> number -> ~Complex =
Chris@46 49 new Complex(re, im);
Chris@46 50
Chris@53 51 magnitude c is ~Complex -> number =
Chris@53 52 c#getMagnitude();
Chris@53 53
Chris@53 54 angle c is ~Complex -> number =
Chris@53 55 c#getAngle();
Chris@53 56
Chris@46 57 add c1 c2 is ~Complex -> ~Complex -> ~Complex =
Chris@46 58 complex (real c1 + real c2) (imaginary c1 + imaginary c2);
Chris@46 59
Chris@46 60 scale r c is number -> ~Complex -> ~Complex =
Chris@46 61 complex (r * real c) (r * imaginary c);
Chris@44 62
Chris@44 63 {
Chris@46 64 real,
Chris@46 65 imaginary,
Chris@46 66 complex,
Chris@53 67 magnitude,
Chris@53 68 angle,
Chris@46 69 add,
Chris@46 70 scale,
Chris@44 71 } as {
Chris@46 72 real is cplx -> number,
Chris@46 73 imaginary is cplx -> number,
Chris@46 74 complex is number -> number -> cplx,
Chris@53 75 magnitude is cplx -> number,
Chris@53 76 angle is cplx -> number,
Chris@46 77 add is cplx -> cplx -> cplx,
Chris@46 78 scale is number -> cplx -> cplx,
Chris@44 79 }
Chris@44 80