Re: [q-lang-users] Complex numbers
Brought to you by:
agraef
From: Rob H. <hub...@gm...> - 2006-06-13 10:53:57
|
On 13/06/06, Albert Graef <Dr....@t-...> wrote: > type Rat = private const rat P Q; > public (over) P Q @ (/); > P:Int over Q:Int = rat (P div D) (Q div D) where D = gcd P Q; > unparse (rat P Q) = '(P over Q); Should that go into rational.q, or is there a separate location for the 'unparse' stuff? > Also, I'm about to turn complex numbers into an algebraic type now. If > anyone wants to complain, now is your last opportunity. ;-) In the Rational code, the construction function rational takes a pair. For 'deconstruction', I provide numerator denominator and in the update I'm preparing, there's now a num_den function returning the numerator and denominator in a pair. It would be good if the Complex type could follow similarly, and for both rectangular and polar coordinates as has been suggested. So: public type Complex = private const cplx_rect X Y; //rectangular complex_rect (X, Y) = cplx_rect X Y; //takes a pair //polar complex_pol (R, Theta) = cplx_rect (R * cos Theta) (R * sin Theta); re (cplx_rect X Y) = X; im (cplx_rect X Y) = Y; rect (cplx_rect X Y) = (X, Y); //returns a pair abs (cplx_rect X Y) = sqrt (X^2 + Y^2); arg (cplx_rect X Y) = atan2 Y X; pol (cplx_rect X Y) = (abs (cplx_rect X Y), arg (cplx_rect X Y)); This means that, as rational and num_den are inverses; so are complex_rect and rect, and complex_pol and pol. I'm not sure about the naming, but I'd like to keep Rational as consistent as possible with Complex. I'm happy to consider renaming my symbols if desired (e.g. 'Rat' rather than 'Rational', 'num' rather than 'numerator', swapping 'rational' with 'rat' to have the shorter symbol public, num_den as something else,...). Rob. |