Mercurial > hg > may
view 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 |
line wrap: on
line source
module complex; import java.lang: ClassCastException; class Complex(double real, double imag) int getReal() real, int getImag() imag, double getMagnitude() sqrt (real * real + imag * imag), double getAngle() Math#atan2(imag, real), String toString() if real == int real and imag == int imag then if imag < 0 then " \(int real) - \(int (-imag))i" else " \(int real) + \(int imag)i" fi else if imag < 0 then " \(real) - \((-imag))i" else " \(real) + \(imag)i" fi fi, int hashCode() Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(), boolean equals(Object other) try c = other unsafely_as ~Complex; c#getReal() == real and c#getImag() == imag catch ClassCastException: false yrt, end; typedef opaque cplx = ~Complex; real c1 is ~Complex -> number = c1#getReal(); imaginary c1 is ~Complex -> number = c1#getImag(); complex re im is number -> number -> ~Complex = new Complex(re, im); magnitude c is ~Complex -> number = c#getMagnitude(); angle c is ~Complex -> number = c#getAngle(); add c1 c2 is ~Complex -> ~Complex -> ~Complex = complex (real c1 + real c2) (imaginary c1 + imaginary c2); scale r c is number -> ~Complex -> ~Complex = complex (r * real c) (r * imaginary c); { real, imaginary, complex, magnitude, angle, add, scale, } as { real is cplx -> number, imaginary is cplx -> number, complex is number -> number -> cplx, magnitude is cplx -> number, angle is cplx -> number, add is cplx -> cplx -> cplx, scale is number -> cplx -> cplx, }