28 lines
1003 B
Plaintext
28 lines
1003 B
Plaintext
link complex # for complex number support
|
|
|
|
procedure SetupComplex() #: used to setup safe complex
|
|
COMPLEX() # replace complex record constructor
|
|
SetupComplex := 1 # never call here again
|
|
return
|
|
end
|
|
|
|
procedure COMPLEX(rpart,ipart) #: new safe record constructor and coercion
|
|
initial complex :=: COMPLEX # get in front of record constructor
|
|
return if /ipart & (type(rpart) == "complex")
|
|
then rpart # already complex
|
|
else COMPLEX( real(\rpart | 0.0), real(\ipart|0) ) # create a new complex number
|
|
end
|
|
|
|
procedure cpxneg(z) #: negate z
|
|
z := complex(z) # coerce
|
|
return complex( -z.rpart, -z.ipart)
|
|
end
|
|
|
|
procedure cpxinv(z) #: inverse of z
|
|
local denom
|
|
z := complex(z) # coerce
|
|
|
|
denom := z.rpart ^ 2 + z.ipart ^ 2
|
|
return complex(z.rpart / denom, z.ipart / denom)
|
|
end
|