diff options
author | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-19 13:05:20 +0000 |
---|---|---|
committer | Konrad Hinsen <konrad.hinsen@laposte.net> | 2009-03-19 13:05:20 +0000 |
commit | 647e3098d58e047968f6ecd09cc6697957726318 (patch) | |
tree | 052f2415dd9049cd3c42ba9115428c0410d573da /src/clojure/contrib/test_contrib/complex_numbers.clj | |
parent | 1cc9c2d456f26142c577fca7d233870d2f3586ea (diff) |
New library clojure.contrib.complex-numbers
Diffstat (limited to 'src/clojure/contrib/test_contrib/complex_numbers.clj')
-rw-r--r-- | src/clojure/contrib/test_contrib/complex_numbers.clj | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/src/clojure/contrib/test_contrib/complex_numbers.clj b/src/clojure/contrib/test_contrib/complex_numbers.clj new file mode 100644 index 00000000..ef874ba1 --- /dev/null +++ b/src/clojure/contrib/test_contrib/complex_numbers.clj @@ -0,0 +1,271 @@ +;; Test routines for complex-numbers.clj + +;; by Konrad Hinsen +;; last updated March 19, 2009 + +;; Copyright (c) Konrad Hinsen, 2008. All rights reserved. The use +;; and distribution terms for this software are covered by the Eclipse +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) +;; which can be found in the file epl-v10.html at the root of this +;; distribution. By using this software in any fashion, you are +;; agreeing to be bound by the terms of this license. You must not +;; remove this notice, or any other, from this software. + +(ns clojure.contrib.test-contrib.complex-numbers + (:refer-clojure :exclude [+ - * / =]) + (:use [clojure.contrib.test-is + :only (deftest is are run-tests)] + [clojure.contrib.generic.arithmetic + :only (+ - * /)] + [clojure.contrib.generic.comparison + :only (=)] + [clojure.contrib.complex-numbers + :only (complex imaginary real imag)])) + +(deftest complex-addition + (is (= (+ (complex 1 2) (complex 1 2)) (complex 2 4))) + (is (= (+ (complex 1 2) (complex -3 -7)) (complex -2 -5))) + (is (= (+ (complex -3 -7) (complex 1 2)) (complex -2 -5))) + (is (= (+ (complex 1 2) 3) (complex 4 2))) + (is (= (+ 3 (complex 1 2)) (complex 4 2))) + (is (= (+ (complex 1 2) -1) (imaginary 2))) + (is (= (+ -1 (complex 1 2)) (imaginary 2))) + (is (= (+ (complex 1 2) (imaginary -2)) 1)) + (is (= (+ (imaginary -2) (complex 1 2)) 1)) + (is (= (+ (complex 1 2) (imaginary 5)) (complex 1 7))) + (is (= (+ (imaginary 5) (complex 1 2)) (complex 1 7))) + (is (= (+ (complex -3 -7) (complex 1 2)) (complex -2 -5))) + (is (= (+ (complex 1 2) (complex -3 -7)) (complex -2 -5))) + (is (= (+ (complex -3 -7) (complex -3 -7)) (complex -6 -14))) + (is (= (+ (complex -3 -7) 3) (imaginary -7))) + (is (= (+ 3 (complex -3 -7)) (imaginary -7))) + (is (= (+ (complex -3 -7) -1) (complex -4 -7))) + (is (= (+ -1 (complex -3 -7)) (complex -4 -7))) + (is (= (+ (complex -3 -7) (imaginary -2)) (complex -3 -9))) + (is (= (+ (imaginary -2) (complex -3 -7)) (complex -3 -9))) + (is (= (+ (complex -3 -7) (imaginary 5)) (complex -3 -2))) + (is (= (+ (imaginary 5) (complex -3 -7)) (complex -3 -2))) + (is (= (+ 3 (complex 1 2)) (complex 4 2))) + (is (= (+ (complex 1 2) 3) (complex 4 2))) + (is (= (+ 3 (complex -3 -7)) (imaginary -7))) + (is (= (+ (complex -3 -7) 3) (imaginary -7))) + (is (= (+ 3 (imaginary -2)) (complex 3 -2))) + (is (= (+ (imaginary -2) 3) (complex 3 -2))) + (is (= (+ 3 (imaginary 5)) (complex 3 5))) + (is (= (+ (imaginary 5) 3) (complex 3 5))) + (is (= (+ -1 (complex 1 2)) (imaginary 2))) + (is (= (+ (complex 1 2) -1) (imaginary 2))) + (is (= (+ -1 (complex -3 -7)) (complex -4 -7))) + (is (= (+ (complex -3 -7) -1) (complex -4 -7))) + (is (= (+ -1 (imaginary -2)) (complex -1 -2))) + (is (= (+ (imaginary -2) -1) (complex -1 -2))) + (is (= (+ -1 (imaginary 5)) (complex -1 5))) + (is (= (+ (imaginary 5) -1) (complex -1 5))) + (is (= (+ (imaginary -2) (complex 1 2)) 1)) + (is (= (+ (complex 1 2) (imaginary -2)) 1)) + (is (= (+ (imaginary -2) (complex -3 -7)) (complex -3 -9))) + (is (= (+ (complex -3 -7) (imaginary -2)) (complex -3 -9))) + (is (= (+ (imaginary -2) 3) (complex 3 -2))) + (is (= (+ 3 (imaginary -2)) (complex 3 -2))) + (is (= (+ (imaginary -2) -1) (complex -1 -2))) + (is (= (+ -1 (imaginary -2)) (complex -1 -2))) + (is (= (+ (imaginary -2) (imaginary -2)) (imaginary -4))) + (is (= (+ (imaginary -2) (imaginary 5)) (imaginary 3))) + (is (= (+ (imaginary 5) (imaginary -2)) (imaginary 3))) + (is (= (+ (imaginary 5) (complex 1 2)) (complex 1 7))) + (is (= (+ (complex 1 2) (imaginary 5)) (complex 1 7))) + (is (= (+ (imaginary 5) (complex -3 -7)) (complex -3 -2))) + (is (= (+ (complex -3 -7) (imaginary 5)) (complex -3 -2))) + (is (= (+ (imaginary 5) 3) (complex 3 5))) + (is (= (+ 3 (imaginary 5)) (complex 3 5))) + (is (= (+ (imaginary 5) -1) (complex -1 5))) + (is (= (+ -1 (imaginary 5)) (complex -1 5))) + (is (= (+ (imaginary 5) (imaginary -2)) (imaginary 3))) + (is (= (+ (imaginary -2) (imaginary 5)) (imaginary 3))) + (is (= (+ (imaginary 5) (imaginary 5)) (imaginary 10)))) + +(deftest complex-subtraction + (is (= (- (complex 1 2) (complex 1 2)) 0)) + (is (= (- (complex 1 2) (complex -3 -7)) (complex 4 9))) + (is (= (- (complex -3 -7) (complex 1 2)) (complex -4 -9))) + (is (= (- (complex 1 2) 3) (complex -2 2))) + (is (= (- 3 (complex 1 2)) (complex 2 -2))) + (is (= (- (complex 1 2) -1) (complex 2 2))) + (is (= (- -1 (complex 1 2)) (complex -2 -2))) + (is (= (- (complex 1 2) (imaginary -2)) (complex 1 4))) + (is (= (- (imaginary -2) (complex 1 2)) (complex -1 -4))) + (is (= (- (complex 1 2) (imaginary 5)) (complex 1 -3))) + (is (= (- (imaginary 5) (complex 1 2)) (complex -1 3))) + (is (= (- (complex -3 -7) (complex 1 2)) (complex -4 -9))) + (is (= (- (complex 1 2) (complex -3 -7)) (complex 4 9))) + (is (= (- (complex -3 -7) (complex -3 -7)) 0)) + (is (= (- (complex -3 -7) 3) (complex -6 -7))) + (is (= (- 3 (complex -3 -7)) (complex 6 7))) + (is (= (- (complex -3 -7) -1) (complex -2 -7))) + (is (= (- -1 (complex -3 -7)) (complex 2 7))) + (is (= (- (complex -3 -7) (imaginary -2)) (complex -3 -5))) + (is (= (- (imaginary -2) (complex -3 -7)) (complex 3 5))) + (is (= (- (complex -3 -7) (imaginary 5)) (complex -3 -12))) + (is (= (- (imaginary 5) (complex -3 -7)) (complex 3 12))) + (is (= (- 3 (complex 1 2)) (complex 2 -2))) + (is (= (- (complex 1 2) 3) (complex -2 2))) + (is (= (- 3 (complex -3 -7)) (complex 6 7))) + (is (= (- (complex -3 -7) 3) (complex -6 -7))) + (is (= (- 3 (imaginary -2)) (complex 3 2))) + (is (= (- (imaginary -2) 3) (complex -3 -2))) + (is (= (- 3 (imaginary 5)) (complex 3 -5))) + (is (= (- (imaginary 5) 3) (complex -3 5))) + (is (= (- -1 (complex 1 2)) (complex -2 -2))) + (is (= (- (complex 1 2) -1) (complex 2 2))) + (is (= (- -1 (complex -3 -7)) (complex 2 7))) + (is (= (- (complex -3 -7) -1) (complex -2 -7))) + (is (= (- -1 (imaginary -2)) (complex -1 2))) + (is (= (- (imaginary -2) -1) (complex 1 -2))) + (is (= (- -1 (imaginary 5)) (complex -1 -5))) + (is (= (- (imaginary 5) -1) (complex 1 5))) + (is (= (- (imaginary -2) (complex 1 2)) (complex -1 -4))) + (is (= (- (complex 1 2) (imaginary -2)) (complex 1 4))) + (is (= (- (imaginary -2) (complex -3 -7)) (complex 3 5))) + (is (= (- (complex -3 -7) (imaginary -2)) (complex -3 -5))) + (is (= (- (imaginary -2) 3) (complex -3 -2))) + (is (= (- 3 (imaginary -2)) (complex 3 2))) + (is (= (- (imaginary -2) -1) (complex 1 -2))) + (is (= (- -1 (imaginary -2)) (complex -1 2))) + (is (= (- (imaginary -2) (imaginary -2)) 0)) + (is (= (- (imaginary -2) (imaginary 5)) (imaginary -7))) + (is (= (- (imaginary 5) (imaginary -2)) (imaginary 7))) + (is (= (- (imaginary 5) (complex 1 2)) (complex -1 3))) + (is (= (- (complex 1 2) (imaginary 5)) (complex 1 -3))) + (is (= (- (imaginary 5) (complex -3 -7)) (complex 3 12))) + (is (= (- (complex -3 -7) (imaginary 5)) (complex -3 -12))) + (is (= (- (imaginary 5) 3) (complex -3 5))) + (is (= (- 3 (imaginary 5)) (complex 3 -5))) + (is (= (- (imaginary 5) -1) (complex 1 5))) + (is (= (- -1 (imaginary 5)) (complex -1 -5))) + (is (= (- (imaginary 5) (imaginary -2)) (imaginary 7))) + (is (= (- (imaginary -2) (imaginary 5)) (imaginary -7))) + (is (= (- (imaginary 5) (imaginary 5)) 0))) + +(deftest complex-multiplication + (is (= (* (complex 1 2) (complex 1 2)) (complex -3 4))) + (is (= (* (complex 1 2) (complex -3 -7)) (complex 11 -13))) + (is (= (* (complex -3 -7) (complex 1 2)) (complex 11 -13))) + (is (= (* (complex 1 2) 3) (complex 3 6))) + (is (= (* 3 (complex 1 2)) (complex 3 6))) + (is (= (* (complex 1 2) -1) (complex -1 -2))) + (is (= (* -1 (complex 1 2)) (complex -1 -2))) + (is (= (* (complex 1 2) (imaginary -2)) (complex 4 -2))) + (is (= (* (imaginary -2) (complex 1 2)) (complex 4 -2))) + (is (= (* (complex 1 2) (imaginary 5)) (complex -10 5))) + (is (= (* (imaginary 5) (complex 1 2)) (complex -10 5))) + (is (= (* (complex -3 -7) (complex 1 2)) (complex 11 -13))) + (is (= (* (complex 1 2) (complex -3 -7)) (complex 11 -13))) + (is (= (* (complex -3 -7) (complex -3 -7)) (complex -40 42))) + (is (= (* (complex -3 -7) 3) (complex -9 -21))) + (is (= (* 3 (complex -3 -7)) (complex -9 -21))) + (is (= (* (complex -3 -7) -1) (complex 3 7))) + (is (= (* -1 (complex -3 -7)) (complex 3 7))) + (is (= (* (complex -3 -7) (imaginary -2)) (complex -14 6))) + (is (= (* (imaginary -2) (complex -3 -7)) (complex -14 6))) + (is (= (* (complex -3 -7) (imaginary 5)) (complex 35 -15))) + (is (= (* (imaginary 5) (complex -3 -7)) (complex 35 -15))) + (is (= (* 3 (complex 1 2)) (complex 3 6))) + (is (= (* (complex 1 2) 3) (complex 3 6))) + (is (= (* 3 (complex -3 -7)) (complex -9 -21))) + (is (= (* (complex -3 -7) 3) (complex -9 -21))) + (is (= (* 3 (imaginary -2)) (imaginary -6))) + (is (= (* (imaginary -2) 3) (imaginary -6))) + (is (= (* 3 (imaginary 5)) (imaginary 15))) + (is (= (* (imaginary 5) 3) (imaginary 15))) + (is (= (* -1 (complex 1 2)) (complex -1 -2))) + (is (= (* (complex 1 2) -1) (complex -1 -2))) + (is (= (* -1 (complex -3 -7)) (complex 3 7))) + (is (= (* (complex -3 -7) -1) (complex 3 7))) + (is (= (* -1 (imaginary -2)) (imaginary 2))) + (is (= (* (imaginary -2) -1) (imaginary 2))) + (is (= (* -1 (imaginary 5)) (imaginary -5))) + (is (= (* (imaginary 5) -1) (imaginary -5))) + (is (= (* (imaginary -2) (complex 1 2)) (complex 4 -2))) + (is (= (* (complex 1 2) (imaginary -2)) (complex 4 -2))) + (is (= (* (imaginary -2) (complex -3 -7)) (complex -14 6))) + (is (= (* (complex -3 -7) (imaginary -2)) (complex -14 6))) + (is (= (* (imaginary -2) 3) (imaginary -6))) + (is (= (* 3 (imaginary -2)) (imaginary -6))) + (is (= (* (imaginary -2) -1) (imaginary 2))) + (is (= (* -1 (imaginary -2)) (imaginary 2))) + (is (= (* (imaginary -2) (imaginary -2)) -4)) + (is (= (* (imaginary -2) (imaginary 5)) 10)) + (is (= (* (imaginary 5) (imaginary -2)) 10)) + (is (= (* (imaginary 5) (complex 1 2)) (complex -10 5))) + (is (= (* (complex 1 2) (imaginary 5)) (complex -10 5))) + (is (= (* (imaginary 5) (complex -3 -7)) (complex 35 -15))) + (is (= (* (complex -3 -7) (imaginary 5)) (complex 35 -15))) + (is (= (* (imaginary 5) 3) (imaginary 15))) + (is (= (* 3 (imaginary 5)) (imaginary 15))) + (is (= (* (imaginary 5) -1) (imaginary -5))) + (is (= (* -1 (imaginary 5)) (imaginary -5))) + (is (= (* (imaginary 5) (imaginary -2)) 10)) + (is (= (* (imaginary -2) (imaginary 5)) 10)) + (is (= (* (imaginary 5) (imaginary 5)) -25))) + +(deftest complex-division + (is (= (/ (complex 1 2) (complex 1 2)) 1)) + (is (= (/ (complex 1 2) (complex -3 -7)) (complex -17/58 1/58))) + (is (= (/ (complex -3 -7) (complex 1 2)) (complex -17/5 -1/5))) + (is (= (/ (complex 1 2) 3) (complex 1/3 2/3))) + (is (= (/ 3 (complex 1 2)) (complex 3/5 -6/5))) + (is (= (/ (complex 1 2) -1) (complex -1 -2))) + (is (= (/ -1 (complex 1 2)) (complex -1/5 2/5))) + (is (= (/ (complex 1 2) (imaginary -2)) (complex -1 1/2))) + (is (= (/ (imaginary -2) (complex 1 2)) (complex -4/5 -2/5))) + (is (= (/ (complex 1 2) (imaginary 5)) (complex 2/5 -1/5))) + (is (= (/ (imaginary 5) (complex 1 2)) (complex 2 1))) + (is (= (/ (complex -3 -7) (complex 1 2)) (complex -17/5 -1/5))) + (is (= (/ (complex 1 2) (complex -3 -7)) (complex -17/58 1/58))) + (is (= (/ (complex -3 -7) (complex -3 -7)) 1)) + (is (= (/ (complex -3 -7) 3) (complex -1 -7/3))) + (is (= (/ 3 (complex -3 -7)) (complex -9/58 21/58))) + (is (= (/ (complex -3 -7) -1) (complex 3 7))) + (is (= (/ -1 (complex -3 -7)) (complex 3/58 -7/58))) + (is (= (/ (complex -3 -7) (imaginary -2)) (complex 7/2 -3/2))) + (is (= (/ (imaginary -2) (complex -3 -7)) (complex 7/29 3/29))) + (is (= (/ (complex -3 -7) (imaginary 5)) (complex -7/5 3/5))) + (is (= (/ (imaginary 5) (complex -3 -7)) (complex -35/58 -15/58))) + (is (= (/ 3 (complex 1 2)) (complex 3/5 -6/5))) + (is (= (/ (complex 1 2) 3) (complex 1/3 2/3))) + (is (= (/ 3 (complex -3 -7)) (complex -9/58 21/58))) + (is (= (/ (complex -3 -7) 3) (complex -1 -7/3))) + (is (= (/ 3 (imaginary -2)) (imaginary 1.5))) + (is (= (/ (imaginary -2) 3) (imaginary -2/3))) + (is (= (/ 3 (imaginary 5)) (imaginary -3/5))) + (is (= (/ (imaginary 5) 3) (imaginary 5/3))) + (is (= (/ -1 (complex 1 2)) (complex -1/5 2/5))) + (is (= (/ (complex 1 2) -1) (complex -1 -2))) + (is (= (/ -1 (complex -3 -7)) (complex 3/58 -7/58))) + (is (= (/ (complex -3 -7) -1) (complex 3 7))) + (is (= (/ -1 (imaginary -2)) (imaginary -1/2))) + (is (= (/ (imaginary -2) -1) (imaginary 2))) + (is (= (/ -1 (imaginary 5)) (imaginary 1/5))) + (is (= (/ (imaginary 5) -1) (imaginary -5))) + (is (= (/ (imaginary -2) (complex 1 2)) (complex -4/5 -2/5))) + (is (= (/ (complex 1 2) (imaginary -2)) (complex -1 1/2))) + (is (= (/ (imaginary -2) (complex -3 -7)) (complex 7/29 3/29))) + (is (= (/ (complex -3 -7) (imaginary -2)) (complex 7/2 -3/2))) + (is (= (/ (imaginary -2) 3) (imaginary -2/3))) + (is (= (/ 3 (imaginary -2)) (imaginary 3/2))) + (is (= (/ (imaginary -2) -1) (imaginary 2))) + (is (= (/ -1 (imaginary -2)) (imaginary -1/2))) + (is (= (/ (imaginary -2) (imaginary -2)) 1)) + (is (= (/ (imaginary -2) (imaginary 5)) -2/5)) + (is (= (/ (imaginary 5) (imaginary -2)) -5/2)) + (is (= (/ (imaginary 5) (complex 1 2)) (complex 2 1))) + (is (= (/ (complex 1 2) (imaginary 5)) (complex 2/5 -1/5))) + (is (= (/ (imaginary 5) (complex -3 -7)) (complex -35/58 -15/58))) + (is (= (/ (complex -3 -7) (imaginary 5)) (complex -7/5 3/5))) + (is (= (/ (imaginary 5) 3) (imaginary 5/3))) + (is (= (/ 3 (imaginary 5)) (imaginary -3/5))) + (is (= (/ (imaginary 5) -1) (imaginary -5))) + (is (= (/ -1 (imaginary 5)) (imaginary 1/5))) + (is (= (/ (imaginary 5) (imaginary -2)) -5/2)) + (is (= (/ (imaginary -2) (imaginary 5)) -2/5)) + (is (= (/ (imaginary 5) (imaginary 5)) 1))) |