2 (c) 2012 Copyright Mekeor Melire
10------------------------------ Rectangular form
13data Complex a = (:+) a a
15realPart : Complex a -> a
18imagPart : Complex a -> a
21instance Eq a => Eq (Complex a) where
22 (==) a b = realPart a == realPart b && imagPart a == imagPart b
24instance Show a => Show (Complex a) where
25 show (r:+i) = "("++show r++":+"++show i++")"
29-- when we have a type class 'Fractional' (which contains Float and Double),
32instance Fractional a => Fractional (Complex a) where
33 (/) (a:+b) (c:+d) = let
34 real = (a*c+b*d)/(c*c+d*d)
35 imag = (b*c-a*d)/(c*c+d*d)
42------------------------------ Polarform
44mkPolar : Float -> Float -> Complex Float
45mkPolar radius angle = radius * cos angle :+ radius * sin angle
47cis : Float -> Complex Float
48cis angle = cos angle :+ sin angle
50magnitude : Complex Float -> Float
51magnitude (r:+i) = sqrt (r*r+i*i)
53phase : Complex Float -> Float
54phase (x:+y) = atan2 y x
57------------------------------ Conjugate
59conjugate : Num a => Complex a -> Complex a
60conjugate (r:+i) = (r :+ (0-i))
62-- We can't do "instance Num a => Num (Complex a)" because
63-- we need "abs" which needs "magnitude" which needs "sqrt" which needs Float
64instance Num (Complex Float) where
65 (+) (a:+b) (c:+d) = ((a+b):+(c+d))
66 (-) (a:+b) (c:+d) = ((a-b):+(c-d))
67 (*) (a:+b) (c:+d) = ((a*c-b*d):+(b*c+a*d))
68 fromInteger x = (fromInteger x:+0)
69 abs (a:+b) = (magnitude (a:+b):+0)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list