[Prev][Next][Index][Thread]

Re: Complex/Imaginary numbers



Rob Myers <robm@lostwax.com> writes:

> > Note that an i() function can be used as so:
> >   3 + 4.i
> That's cool.
> Could + be overidden usefully here?

That depends on what you mean with usefully. I'll sketch what one
could do:

define functional class <complex> (<number>)
  slot real :: <real>, required-init-keyword: real:;
  slot imaginary :: <real>, required-init-keyword: imaginary:;
end;

define method functional-== (a :: <complex>, b :: <complex)
 => (identical? :: <boolean>)
  a.real == b.real && a.imaginary == b.imaginary;
end;

define method i (imaginary :: <real>)
 => (complex :: <complex>)
  make(<complex>, real: 0.0, imaginary: imaginary);
end;

define method \+ (a :: <real>, b :: <complex>)
 => (result :: <complex>)
  make(<complex>, real: a + b.real, imaginary: b.imaginary);
end;

Of course one has to come up with a complete set of i functions, where
the real slot is initialized with a 0.0 value of the precision
matching the imaginary's precision, and every possible permutation of
operand types for all the numeric operations.

The compiler should be smart enough to optimize away all of the calls
in an expression like "3 + 4.i" and generate a constant, but I'm
afraid it isn't at the moment.

Andreas

P.S.: That functional stuff is Gwydion-specific. Basically it says
that two objects are to be treated identical if their slots are
identical. This allows some operations to be done more efficiently.

-- 
"We should be willing to look at the source code we produce not as the
end product of a more interesting process, but as an artifact in its
own right. It should look good stuck up on the wall."
 -- http://www.ftech.net/~honeyg/progstone/progstone.html



References: