-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.hs
More file actions
32 lines (26 loc) · 734 Bytes
/
complex.hs
File metadata and controls
32 lines (26 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
data Complex a = Complex {
re :: a,
im :: a
} deriving (Eq, Show)
--cplus :: (Num a) => Complex a -> Complex a -> Complex a
--
--cplus z1 z2 = Complex (re z1 + re z2) (im z1 + im z2)
instance (Num a) => Num (Complex a) where
(+) z1 z2 = Complex (re z1 + re z2) (im z1 + im z2)
(*) z1 z2 = Complex (x1*x2 - y1*y2) (x1*y2 + x2*y1)
where x1 = re z1
x2 = re z2
y1 = im z1
y2 = im z2
-- abs :: (Floating a) => Complex a -> Complex a
abs z = Complex (x*x + y*y) 0 -- sqrt needs typeclass Floating
where x = re z
y = im z
--magsq :: Complex -> Double
--
--magsq z = x*x + y*y
-- where x = re z
-- y = im z
--mag :: Complex -> Double
--
--mag z = (sqrt . magsq) z