annotate complex.yeti @ 79:e47d5adb6564

Use RDF description in inferred structure; also report whether it was found in plugin data
author Chris Cannam
date Mon, 04 Mar 2013 17:53:42 +0000
parents 3a13af4bd8ba
children
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@71 6 class Cplx(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@71 33 c = other unsafely_as ~Cplx;
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@71 40 typedef opaque cplx = ~Cplx;
Chris@44 41
Chris@71 42 real c1 is ~Cplx -> number =
Chris@46 43 c1#getReal();
Chris@46 44
Chris@71 45 imaginary c1 is ~Cplx -> number =
Chris@46 46 c1#getImag();
Chris@46 47
Chris@71 48 complex re im is number -> number -> ~Cplx =
Chris@71 49 new Cplx(re, im);
Chris@46 50
Chris@71 51 magnitude c is ~Cplx -> number =
Chris@53 52 c#getMagnitude();
Chris@53 53
Chris@71 54 angle c is ~Cplx -> number =
Chris@53 55 c#getAngle();
Chris@53 56
Chris@71 57 add c1 c2 is ~Cplx -> ~Cplx -> ~Cplx =
Chris@46 58 complex (real c1 + real c2) (imaginary c1 + imaginary c2);
Chris@46 59
Chris@71 60 scale r c is number -> ~Cplx -> ~Cplx =
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