-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.lisp
53 lines (44 loc) · 975 Bytes
/
complex.lisp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
;; This library implements complex numbers in common lisp
;; Complex Number Definition
(defun complex (Re Im)
(cons Re Im))
;; Get Real Part
(defun get-Re (c)
(car c))
;; Get Imagninary Part
(defun get-Im (c)
(cdr c))
;; Sum Complex
;; w = a + ib
;; z = c + id
;; w+z = (a + c) + i(b + d)
(defun sum-complex (c1 c2)
(let (Re (+ (get-Re c1)
(get-Re c2))
(let Im (+ (get-Im c1)
(get-Im c2))
(complex Re Im)))))
;; Subtraction Complex
;; w = a + ib
;; z = c + id
;; w*z = (a - c) + i(b - d)
(defun sub-complex (c1 c2)
(let (Re (- (get-Re c1)
(get-Re c2))
(let Im (- (get-Im c1)
(get-Im c2))
(complex Re Im)))))
;; Product Complex
;; w = a + ib
;; z = c + id
;; w*z = (ac - bd) + i(ad + bc)
(defun prod-complex (c1 c2)
(let (Re (- (* (get-Re c1)
(get-Im c2))
(* (get-Im c1)
(get-Re c2)))
(let Im (+ (* (get-Re c1)
(get-Im c2))
(* (get-Re c2)
(get-Im c1)))
(complex Re Im)))))