annotate yetilab/complex.yeti @ 296:ec6a36d88f57

Move complex tests to complex package
author Chris Cannam
date Fri, 31 May 2013 22:02:03 +0100
parents 197d23954a4e
children afce0ab788d1
rev   line source
Chris@44 1
Chris@273 2 module yetilab.complex;
Chris@44 3
Chris@273 4 load yetilab.vector.type;
Chris@273 5 load yetilab.complex.type;
Chris@195 6
Chris@273 7 vec = load yetilab.vector;
Chris@145 8
Chris@46 9 import java.lang: ClassCastException;
Chris@46 10
Chris@71 11 class Cplx(double real, double imag)
Chris@44 12 int getReal()
Chris@44 13 real,
Chris@44 14 int getImag()
Chris@44 15 imag,
Chris@53 16 double getMagnitude()
Chris@53 17 sqrt (real * real + imag * imag),
Chris@53 18 double getAngle()
Chris@53 19 Math#atan2(imag, real),
Chris@44 20 String toString()
Chris@48 21 if real == int real and imag == int imag then
Chris@48 22 if imag < 0 then
Chris@48 23 " \(int real) - \(int (-imag))i"
Chris@48 24 else
Chris@48 25 " \(int real) + \(int imag)i"
Chris@48 26 fi
Chris@48 27 else
Chris@48 28 if imag < 0 then
Chris@48 29 " \(real) - \((-imag))i"
Chris@48 30 else
Chris@48 31 " \(real) + \(imag)i"
Chris@48 32 fi
Chris@48 33 fi,
Chris@46 34 int hashCode()
Chris@46 35 Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(),
Chris@46 36 boolean equals(Object other)
Chris@46 37 try
Chris@71 38 c = other unsafely_as ~Cplx;
Chris@46 39 c#getReal() == real and c#getImag() == imag
Chris@46 40 catch ClassCastException:
Chris@46 41 false
Chris@46 42 yrt,
Chris@44 43 end;
Chris@44 44
Chris@71 45 real c1 is ~Cplx -> number =
Chris@46 46 c1#getReal();
Chris@46 47
Chris@71 48 imaginary c1 is ~Cplx -> number =
Chris@46 49 c1#getImag();
Chris@46 50
Chris@71 51 complex re im is number -> number -> ~Cplx =
Chris@71 52 new Cplx(re, im);
Chris@46 53
Chris@71 54 magnitude c is ~Cplx -> number =
Chris@53 55 c#getMagnitude();
Chris@53 56
Chris@71 57 angle c is ~Cplx -> number =
Chris@53 58 c#getAngle();
Chris@53 59
Chris@71 60 add c1 c2 is ~Cplx -> ~Cplx -> ~Cplx =
Chris@46 61 complex (real c1 + real c2) (imaginary c1 + imaginary c2);
Chris@46 62
Chris@71 63 scale r c is number -> ~Cplx -> ~Cplx =
Chris@46 64 complex (r * real c) (r * imaginary c);
Chris@44 65
Chris@142 66 zeros n is number -> array<~Cplx> =
Chris@142 67 array (map \(complex 0 0) [1..n]);
Chris@142 68
Chris@213 69 magnitudes cc is list?<~Cplx> -> vector =
Chris@213 70 vec.fromList (map magnitude cc);
Chris@145 71
Chris@213 72 angles cc is list?<~Cplx> -> vector =
Chris@213 73 vec.fromList (map angle cc);
Chris@145 74
Chris@44 75 {
Chris@199 76 real,
Chris@199 77 imaginary,
Chris@199 78 complex,
Chris@199 79 magnitude,
Chris@199 80 angle,
Chris@199 81 add,
Chris@199 82 scale,
Chris@199 83 zeros,
Chris@199 84 magnitudes,
Chris@199 85 angles,
Chris@199 86 } as {
Chris@46 87 real is cplx -> number,
Chris@46 88 imaginary is cplx -> number,
Chris@46 89 complex is number -> number -> cplx,
Chris@53 90 magnitude is cplx -> number,
Chris@53 91 angle is cplx -> number,
Chris@46 92 add is cplx -> cplx -> cplx,
Chris@46 93 scale is number -> cplx -> cplx,
Chris@142 94 zeros is number -> array<cplx>,
Chris@213 95 magnitudes is list?<cplx> -> vector,
Chris@213 96 angles is list?<cplx> -> vector,
Chris@44 97 }
Chris@44 98