view 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
line wrap: on
line source

module complex;

import java.lang: ClassCastException;

class Cplx(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 ~Cplx;
            c#getReal() == real and c#getImag() == imag
        catch ClassCastException:
            false
        yrt,
end;

typedef opaque cplx = ~Cplx;

real c1 is ~Cplx -> number =
    c1#getReal();

imaginary c1 is ~Cplx -> number =
    c1#getImag();

complex re im is number -> number -> ~Cplx =
    new Cplx(re, im);

magnitude c is ~Cplx -> number =
    c#getMagnitude();

angle c is ~Cplx -> number =
    c#getAngle();

add c1 c2 is ~Cplx -> ~Cplx -> ~Cplx =
    complex (real c1 + real c2) (imaginary c1 + imaginary c2);

scale r c is number -> ~Cplx -> ~Cplx =
    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,
}