comparison src/clojure/contrib/test_contrib/test_complex_numbers.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
comparison
equal deleted inserted replaced
9:35cf337adfcf 10:ef7dbbd6452c
1 ;; Test routines for complex-numbers.clj
2
3 ;; by Konrad Hinsen
4 ;; last updated April 2, 2009
5
6 ;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use
7 ;; and distribution terms for this software are covered by the Eclipse
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
9 ;; which can be found in the file epl-v10.html at the root of this
10 ;; distribution. By using this software in any fashion, you are
11 ;; agreeing to be bound by the terms of this license. You must not
12 ;; remove this notice, or any other, from this software.
13
14 (ns clojure.contrib.test-complex-numbers
15 (:refer-clojure :exclude [+ - * / = < > <= >=])
16 (:use [clojure.test
17 :only (deftest is are run-tests)]
18 [clojure.contrib.generic.arithmetic
19 :only (+ - * /)]
20 [clojure.contrib.generic.comparison
21 :only (= < > <= >=)]
22 [clojure.contrib.generic.math-functions
23 :only (abs approx= conjugate exp sqr sqrt)]
24 [clojure.contrib.complex-numbers
25 :only (complex imaginary real imag)]))
26
27 (deftest complex-addition
28 (is (= (+ (complex 1 2) (complex 1 2)) (complex 2 4)))
29 (is (= (+ (complex 1 2) (complex -3 -7)) (complex -2 -5)))
30 (is (= (+ (complex -3 -7) (complex 1 2)) (complex -2 -5)))
31 (is (= (+ (complex 1 2) 3) (complex 4 2)))
32 (is (= (+ 3 (complex 1 2)) (complex 4 2)))
33 (is (= (+ (complex 1 2) -1) (imaginary 2)))
34 (is (= (+ -1 (complex 1 2)) (imaginary 2)))
35 (is (= (+ (complex 1 2) (imaginary -2)) 1))
36 (is (= (+ (imaginary -2) (complex 1 2)) 1))
37 (is (= (+ (complex 1 2) (imaginary 5)) (complex 1 7)))
38 (is (= (+ (imaginary 5) (complex 1 2)) (complex 1 7)))
39 (is (= (+ (complex -3 -7) (complex 1 2)) (complex -2 -5)))
40 (is (= (+ (complex 1 2) (complex -3 -7)) (complex -2 -5)))
41 (is (= (+ (complex -3 -7) (complex -3 -7)) (complex -6 -14)))
42 (is (= (+ (complex -3 -7) 3) (imaginary -7)))
43 (is (= (+ 3 (complex -3 -7)) (imaginary -7)))
44 (is (= (+ (complex -3 -7) -1) (complex -4 -7)))
45 (is (= (+ -1 (complex -3 -7)) (complex -4 -7)))
46 (is (= (+ (complex -3 -7) (imaginary -2)) (complex -3 -9)))
47 (is (= (+ (imaginary -2) (complex -3 -7)) (complex -3 -9)))
48 (is (= (+ (complex -3 -7) (imaginary 5)) (complex -3 -2)))
49 (is (= (+ (imaginary 5) (complex -3 -7)) (complex -3 -2)))
50 (is (= (+ 3 (complex 1 2)) (complex 4 2)))
51 (is (= (+ (complex 1 2) 3) (complex 4 2)))
52 (is (= (+ 3 (complex -3 -7)) (imaginary -7)))
53 (is (= (+ (complex -3 -7) 3) (imaginary -7)))
54 (is (= (+ 3 (imaginary -2)) (complex 3 -2)))
55 (is (= (+ (imaginary -2) 3) (complex 3 -2)))
56 (is (= (+ 3 (imaginary 5)) (complex 3 5)))
57 (is (= (+ (imaginary 5) 3) (complex 3 5)))
58 (is (= (+ -1 (complex 1 2)) (imaginary 2)))
59 (is (= (+ (complex 1 2) -1) (imaginary 2)))
60 (is (= (+ -1 (complex -3 -7)) (complex -4 -7)))
61 (is (= (+ (complex -3 -7) -1) (complex -4 -7)))
62 (is (= (+ -1 (imaginary -2)) (complex -1 -2)))
63 (is (= (+ (imaginary -2) -1) (complex -1 -2)))
64 (is (= (+ -1 (imaginary 5)) (complex -1 5)))
65 (is (= (+ (imaginary 5) -1) (complex -1 5)))
66 (is (= (+ (imaginary -2) (complex 1 2)) 1))
67 (is (= (+ (complex 1 2) (imaginary -2)) 1))
68 (is (= (+ (imaginary -2) (complex -3 -7)) (complex -3 -9)))
69 (is (= (+ (complex -3 -7) (imaginary -2)) (complex -3 -9)))
70 (is (= (+ (imaginary -2) 3) (complex 3 -2)))
71 (is (= (+ 3 (imaginary -2)) (complex 3 -2)))
72 (is (= (+ (imaginary -2) -1) (complex -1 -2)))
73 (is (= (+ -1 (imaginary -2)) (complex -1 -2)))
74 (is (= (+ (imaginary -2) (imaginary -2)) (imaginary -4)))
75 (is (= (+ (imaginary -2) (imaginary 5)) (imaginary 3)))
76 (is (= (+ (imaginary 5) (imaginary -2)) (imaginary 3)))
77 (is (= (+ (imaginary 5) (complex 1 2)) (complex 1 7)))
78 (is (= (+ (complex 1 2) (imaginary 5)) (complex 1 7)))
79 (is (= (+ (imaginary 5) (complex -3 -7)) (complex -3 -2)))
80 (is (= (+ (complex -3 -7) (imaginary 5)) (complex -3 -2)))
81 (is (= (+ (imaginary 5) 3) (complex 3 5)))
82 (is (= (+ 3 (imaginary 5)) (complex 3 5)))
83 (is (= (+ (imaginary 5) -1) (complex -1 5)))
84 (is (= (+ -1 (imaginary 5)) (complex -1 5)))
85 (is (= (+ (imaginary 5) (imaginary -2)) (imaginary 3)))
86 (is (= (+ (imaginary -2) (imaginary 5)) (imaginary 3)))
87 (is (= (+ (imaginary 5) (imaginary 5)) (imaginary 10))))
88
89 (deftest complex-subtraction
90 (is (= (- (complex 1 2) (complex 1 2)) 0))
91 (is (= (- (complex 1 2) (complex -3 -7)) (complex 4 9)))
92 (is (= (- (complex -3 -7) (complex 1 2)) (complex -4 -9)))
93 (is (= (- (complex 1 2) 3) (complex -2 2)))
94 (is (= (- 3 (complex 1 2)) (complex 2 -2)))
95 (is (= (- (complex 1 2) -1) (complex 2 2)))
96 (is (= (- -1 (complex 1 2)) (complex -2 -2)))
97 (is (= (- (complex 1 2) (imaginary -2)) (complex 1 4)))
98 (is (= (- (imaginary -2) (complex 1 2)) (complex -1 -4)))
99 (is (= (- (complex 1 2) (imaginary 5)) (complex 1 -3)))
100 (is (= (- (imaginary 5) (complex 1 2)) (complex -1 3)))
101 (is (= (- (complex -3 -7) (complex 1 2)) (complex -4 -9)))
102 (is (= (- (complex 1 2) (complex -3 -7)) (complex 4 9)))
103 (is (= (- (complex -3 -7) (complex -3 -7)) 0))
104 (is (= (- (complex -3 -7) 3) (complex -6 -7)))
105 (is (= (- 3 (complex -3 -7)) (complex 6 7)))
106 (is (= (- (complex -3 -7) -1) (complex -2 -7)))
107 (is (= (- -1 (complex -3 -7)) (complex 2 7)))
108 (is (= (- (complex -3 -7) (imaginary -2)) (complex -3 -5)))
109 (is (= (- (imaginary -2) (complex -3 -7)) (complex 3 5)))
110 (is (= (- (complex -3 -7) (imaginary 5)) (complex -3 -12)))
111 (is (= (- (imaginary 5) (complex -3 -7)) (complex 3 12)))
112 (is (= (- 3 (complex 1 2)) (complex 2 -2)))
113 (is (= (- (complex 1 2) 3) (complex -2 2)))
114 (is (= (- 3 (complex -3 -7)) (complex 6 7)))
115 (is (= (- (complex -3 -7) 3) (complex -6 -7)))
116 (is (= (- 3 (imaginary -2)) (complex 3 2)))
117 (is (= (- (imaginary -2) 3) (complex -3 -2)))
118 (is (= (- 3 (imaginary 5)) (complex 3 -5)))
119 (is (= (- (imaginary 5) 3) (complex -3 5)))
120 (is (= (- -1 (complex 1 2)) (complex -2 -2)))
121 (is (= (- (complex 1 2) -1) (complex 2 2)))
122 (is (= (- -1 (complex -3 -7)) (complex 2 7)))
123 (is (= (- (complex -3 -7) -1) (complex -2 -7)))
124 (is (= (- -1 (imaginary -2)) (complex -1 2)))
125 (is (= (- (imaginary -2) -1) (complex 1 -2)))
126 (is (= (- -1 (imaginary 5)) (complex -1 -5)))
127 (is (= (- (imaginary 5) -1) (complex 1 5)))
128 (is (= (- (imaginary -2) (complex 1 2)) (complex -1 -4)))
129 (is (= (- (complex 1 2) (imaginary -2)) (complex 1 4)))
130 (is (= (- (imaginary -2) (complex -3 -7)) (complex 3 5)))
131 (is (= (- (complex -3 -7) (imaginary -2)) (complex -3 -5)))
132 (is (= (- (imaginary -2) 3) (complex -3 -2)))
133 (is (= (- 3 (imaginary -2)) (complex 3 2)))
134 (is (= (- (imaginary -2) -1) (complex 1 -2)))
135 (is (= (- -1 (imaginary -2)) (complex -1 2)))
136 (is (= (- (imaginary -2) (imaginary -2)) 0))
137 (is (= (- (imaginary -2) (imaginary 5)) (imaginary -7)))
138 (is (= (- (imaginary 5) (imaginary -2)) (imaginary 7)))
139 (is (= (- (imaginary 5) (complex 1 2)) (complex -1 3)))
140 (is (= (- (complex 1 2) (imaginary 5)) (complex 1 -3)))
141 (is (= (- (imaginary 5) (complex -3 -7)) (complex 3 12)))
142 (is (= (- (complex -3 -7) (imaginary 5)) (complex -3 -12)))
143 (is (= (- (imaginary 5) 3) (complex -3 5)))
144 (is (= (- 3 (imaginary 5)) (complex 3 -5)))
145 (is (= (- (imaginary 5) -1) (complex 1 5)))
146 (is (= (- -1 (imaginary 5)) (complex -1 -5)))
147 (is (= (- (imaginary 5) (imaginary -2)) (imaginary 7)))
148 (is (= (- (imaginary -2) (imaginary 5)) (imaginary -7)))
149 (is (= (- (imaginary 5) (imaginary 5)) 0)))
150
151 (deftest complex-multiplication
152 (is (= (* (complex 1 2) (complex 1 2)) (complex -3 4)))
153 (is (= (* (complex 1 2) (complex -3 -7)) (complex 11 -13)))
154 (is (= (* (complex -3 -7) (complex 1 2)) (complex 11 -13)))
155 (is (= (* (complex 1 2) 3) (complex 3 6)))
156 (is (= (* 3 (complex 1 2)) (complex 3 6)))
157 (is (= (* (complex 1 2) -1) (complex -1 -2)))
158 (is (= (* -1 (complex 1 2)) (complex -1 -2)))
159 (is (= (* (complex 1 2) (imaginary -2)) (complex 4 -2)))
160 (is (= (* (imaginary -2) (complex 1 2)) (complex 4 -2)))
161 (is (= (* (complex 1 2) (imaginary 5)) (complex -10 5)))
162 (is (= (* (imaginary 5) (complex 1 2)) (complex -10 5)))
163 (is (= (* (complex -3 -7) (complex 1 2)) (complex 11 -13)))
164 (is (= (* (complex 1 2) (complex -3 -7)) (complex 11 -13)))
165 (is (= (* (complex -3 -7) (complex -3 -7)) (complex -40 42)))
166 (is (= (* (complex -3 -7) 3) (complex -9 -21)))
167 (is (= (* 3 (complex -3 -7)) (complex -9 -21)))
168 (is (= (* (complex -3 -7) -1) (complex 3 7)))
169 (is (= (* -1 (complex -3 -7)) (complex 3 7)))
170 (is (= (* (complex -3 -7) (imaginary -2)) (complex -14 6)))
171 (is (= (* (imaginary -2) (complex -3 -7)) (complex -14 6)))
172 (is (= (* (complex -3 -7) (imaginary 5)) (complex 35 -15)))
173 (is (= (* (imaginary 5) (complex -3 -7)) (complex 35 -15)))
174 (is (= (* 3 (complex 1 2)) (complex 3 6)))
175 (is (= (* (complex 1 2) 3) (complex 3 6)))
176 (is (= (* 3 (complex -3 -7)) (complex -9 -21)))
177 (is (= (* (complex -3 -7) 3) (complex -9 -21)))
178 (is (= (* 3 (imaginary -2)) (imaginary -6)))
179 (is (= (* (imaginary -2) 3) (imaginary -6)))
180 (is (= (* 3 (imaginary 5)) (imaginary 15)))
181 (is (= (* (imaginary 5) 3) (imaginary 15)))
182 (is (= (* -1 (complex 1 2)) (complex -1 -2)))
183 (is (= (* (complex 1 2) -1) (complex -1 -2)))
184 (is (= (* -1 (complex -3 -7)) (complex 3 7)))
185 (is (= (* (complex -3 -7) -1) (complex 3 7)))
186 (is (= (* -1 (imaginary -2)) (imaginary 2)))
187 (is (= (* (imaginary -2) -1) (imaginary 2)))
188 (is (= (* -1 (imaginary 5)) (imaginary -5)))
189 (is (= (* (imaginary 5) -1) (imaginary -5)))
190 (is (= (* (imaginary -2) (complex 1 2)) (complex 4 -2)))
191 (is (= (* (complex 1 2) (imaginary -2)) (complex 4 -2)))
192 (is (= (* (imaginary -2) (complex -3 -7)) (complex -14 6)))
193 (is (= (* (complex -3 -7) (imaginary -2)) (complex -14 6)))
194 (is (= (* (imaginary -2) 3) (imaginary -6)))
195 (is (= (* 3 (imaginary -2)) (imaginary -6)))
196 (is (= (* (imaginary -2) -1) (imaginary 2)))
197 (is (= (* -1 (imaginary -2)) (imaginary 2)))
198 (is (= (* (imaginary -2) (imaginary -2)) -4))
199 (is (= (* (imaginary -2) (imaginary 5)) 10))
200 (is (= (* (imaginary 5) (imaginary -2)) 10))
201 (is (= (* (imaginary 5) (complex 1 2)) (complex -10 5)))
202 (is (= (* (complex 1 2) (imaginary 5)) (complex -10 5)))
203 (is (= (* (imaginary 5) (complex -3 -7)) (complex 35 -15)))
204 (is (= (* (complex -3 -7) (imaginary 5)) (complex 35 -15)))
205 (is (= (* (imaginary 5) 3) (imaginary 15)))
206 (is (= (* 3 (imaginary 5)) (imaginary 15)))
207 (is (= (* (imaginary 5) -1) (imaginary -5)))
208 (is (= (* -1 (imaginary 5)) (imaginary -5)))
209 (is (= (* (imaginary 5) (imaginary -2)) 10))
210 (is (= (* (imaginary -2) (imaginary 5)) 10))
211 (is (= (* (imaginary 5) (imaginary 5)) -25)))
212
213 (deftest complex-division
214 (is (= (/ (complex 1 2) (complex 1 2)) 1))
215 (is (= (/ (complex 1 2) (complex -3 -7)) (complex -17/58 1/58)))
216 (is (= (/ (complex -3 -7) (complex 1 2)) (complex -17/5 -1/5)))
217 (is (= (/ (complex 1 2) 3) (complex 1/3 2/3)))
218 (is (= (/ 3 (complex 1 2)) (complex 3/5 -6/5)))
219 (is (= (/ (complex 1 2) -1) (complex -1 -2)))
220 (is (= (/ -1 (complex 1 2)) (complex -1/5 2/5)))
221 (is (= (/ (complex 1 2) (imaginary -2)) (complex -1 1/2)))
222 (is (= (/ (imaginary -2) (complex 1 2)) (complex -4/5 -2/5)))
223 (is (= (/ (complex 1 2) (imaginary 5)) (complex 2/5 -1/5)))
224 (is (= (/ (imaginary 5) (complex 1 2)) (complex 2 1)))
225 (is (= (/ (complex -3 -7) (complex 1 2)) (complex -17/5 -1/5)))
226 (is (= (/ (complex 1 2) (complex -3 -7)) (complex -17/58 1/58)))
227 (is (= (/ (complex -3 -7) (complex -3 -7)) 1))
228 (is (= (/ (complex -3 -7) 3) (complex -1 -7/3)))
229 (is (= (/ 3 (complex -3 -7)) (complex -9/58 21/58)))
230 (is (= (/ (complex -3 -7) -1) (complex 3 7)))
231 (is (= (/ -1 (complex -3 -7)) (complex 3/58 -7/58)))
232 (is (= (/ (complex -3 -7) (imaginary -2)) (complex 7/2 -3/2)))
233 (is (= (/ (imaginary -2) (complex -3 -7)) (complex 7/29 3/29)))
234 (is (= (/ (complex -3 -7) (imaginary 5)) (complex -7/5 3/5)))
235 (is (= (/ (imaginary 5) (complex -3 -7)) (complex -35/58 -15/58)))
236 (is (= (/ 3 (complex 1 2)) (complex 3/5 -6/5)))
237 (is (= (/ (complex 1 2) 3) (complex 1/3 2/3)))
238 (is (= (/ 3 (complex -3 -7)) (complex -9/58 21/58)))
239 (is (= (/ (complex -3 -7) 3) (complex -1 -7/3)))
240 #_(is (= (/ 3 (imaginary -2)) (imaginary 1.5)))
241 (is (= (/ (imaginary -2) 3) (imaginary -2/3)))
242 (is (= (/ 3 (imaginary 5)) (imaginary -3/5)))
243 (is (= (/ (imaginary 5) 3) (imaginary 5/3)))
244 (is (= (/ -1 (complex 1 2)) (complex -1/5 2/5)))
245 (is (= (/ (complex 1 2) -1) (complex -1 -2)))
246 (is (= (/ -1 (complex -3 -7)) (complex 3/58 -7/58)))
247 (is (= (/ (complex -3 -7) -1) (complex 3 7)))
248 (is (= (/ -1 (imaginary -2)) (imaginary -1/2)))
249 (is (= (/ (imaginary -2) -1) (imaginary 2)))
250 (is (= (/ -1 (imaginary 5)) (imaginary 1/5)))
251 (is (= (/ (imaginary 5) -1) (imaginary -5)))
252 (is (= (/ (imaginary -2) (complex 1 2)) (complex -4/5 -2/5)))
253 (is (= (/ (complex 1 2) (imaginary -2)) (complex -1 1/2)))
254 (is (= (/ (imaginary -2) (complex -3 -7)) (complex 7/29 3/29)))
255 (is (= (/ (complex -3 -7) (imaginary -2)) (complex 7/2 -3/2)))
256 (is (= (/ (imaginary -2) 3) (imaginary -2/3)))
257 (is (= (/ 3 (imaginary -2)) (imaginary 3/2)))
258 (is (= (/ (imaginary -2) -1) (imaginary 2)))
259 (is (= (/ -1 (imaginary -2)) (imaginary -1/2)))
260 (is (= (/ (imaginary -2) (imaginary -2)) 1))
261 (is (= (/ (imaginary -2) (imaginary 5)) -2/5))
262 (is (= (/ (imaginary 5) (imaginary -2)) -5/2))
263 (is (= (/ (imaginary 5) (complex 1 2)) (complex 2 1)))
264 (is (= (/ (complex 1 2) (imaginary 5)) (complex 2/5 -1/5)))
265 (is (= (/ (imaginary 5) (complex -3 -7)) (complex -35/58 -15/58)))
266 (is (= (/ (complex -3 -7) (imaginary 5)) (complex -7/5 3/5)))
267 (is (= (/ (imaginary 5) 3) (imaginary 5/3)))
268 (is (= (/ 3 (imaginary 5)) (imaginary -3/5)))
269 (is (= (/ (imaginary 5) -1) (imaginary -5)))
270 (is (= (/ -1 (imaginary 5)) (imaginary 1/5)))
271 (is (= (/ (imaginary 5) (imaginary -2)) -5/2))
272 (is (= (/ (imaginary -2) (imaginary 5)) -2/5))
273 (is (= (/ (imaginary 5) (imaginary 5)) 1)))
274
275 (deftest complex-conjugate
276 (is (= (conjugate (complex 1 2)) (complex 1 -2)))
277 (is (= (conjugate (complex -3 -7)) (complex -3 7)))
278 (is (= (conjugate (imaginary -2)) (imaginary 2)))
279 (is (= (conjugate (imaginary 5)) (imaginary -5))))
280
281 (deftest complex-abs
282 (doseq [c [(complex 1 2) (complex -2 3) (complex 4 -2)
283 (complex -3 -7) (imaginary -2) (imaginary 5)]]
284 (is (approx= (* c (conjugate c))
285 (sqr (abs c))
286 1e-14))))
287
288 (deftest complex-sqrt
289 (doseq [c [(complex 1 2) (complex -2 3) (complex 4 -2)
290 (complex -3 -7) (imaginary -2) (imaginary 5)]]
291 (let [r (sqrt c)]
292 (is (approx= c (sqr r) 1e-14))
293 (is (>= (real r) 0)))))
294
295 (deftest complex-exp
296 (is (approx= (exp (complex 1 2))
297 (complex -1.1312043837568135 2.4717266720048188)
298 1e-14))
299 (is (approx= (exp (complex 2 3))
300 (complex -7.3151100949011028 1.0427436562359045)
301 1e-14))
302 (is (approx= (exp (complex 4 -2))
303 (complex -22.720847417619233 -49.645957334580565)
304 1e-14))
305 (is (approx= (exp (complex 3 -7))
306 (complex 15.142531566086868 -13.195928586605717)
307 1e-14))
308 (is (approx= (exp (imaginary -2))
309 (complex -0.41614683654714241 -0.90929742682568171)
310 1e-14))
311 (is (approx= (exp (imaginary 5))
312 (complex 0.2836621854632263 -0.95892427466313845)
313 1e-14)))