aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-19 13:05:20 +0000
committerKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-19 13:05:20 +0000
commit647e3098d58e047968f6ecd09cc6697957726318 (patch)
tree052f2415dd9049cd3c42ba9115428c0410d573da
parent1cc9c2d456f26142c577fca7d233870d2f3586ea (diff)
New library clojure.contrib.complex-numbers
-rw-r--r--build.xml1
-rw-r--r--src/clojure/contrib/complex_numbers.clj224
-rw-r--r--src/clojure/contrib/gen_html_docs.clj1
-rw-r--r--src/clojure/contrib/load_all.clj1
-rw-r--r--src/clojure/contrib/test_contrib.clj2
-rw-r--r--src/clojure/contrib/test_contrib/complex_numbers.clj271
6 files changed, 499 insertions, 1 deletions
diff --git a/build.xml b/build.xml
index ca8ee1bd..8eb925f4 100644
--- a/build.xml
+++ b/build.xml
@@ -79,6 +79,7 @@
<sysproperty key="clojure.compile.path" value="${build}"/>
<arg value="clojure.contrib.accumulators"/>
<arg value="clojure.contrib.command-line"/>
+ <arg value="clojure.contrib.complex-numbers"/>
<arg value="clojure.contrib.cond"/>
<arg value="clojure.contrib.def"/>
<arg value="clojure.contrib.duck-streams"/>
diff --git a/src/clojure/contrib/complex_numbers.clj b/src/clojure/contrib/complex_numbers.clj
new file mode 100644
index 00000000..579aba10
--- /dev/null
+++ b/src/clojure/contrib/complex_numbers.clj
@@ -0,0 +1,224 @@
+;; Complex numbers
+
+;; by Konrad Hinsen
+;; last updated March 19, 2009
+
+;; Copyright (c) Konrad Hinsen, 2009. 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.complex-numbers
+ "Complex numbers
+
+ NOTE: This library is in evolution. It may change with future releases."
+ (:use [clojure.contrib.types :only (deftype)]
+ [clojure.contrib.generic :only (root-type)])
+ (:require [clojure.contrib.generic.arithmetic :as ga]
+ [clojure.contrib.generic.comparison :as gc]
+ [clojure.contrib.generic.math-functions :as gm]))
+
+;
+; Complex numbers are represented as struct maps. The real and imaginary
+; parts can be of any type for which arithmetic and maths functions
+; are defined.
+;
+(defstruct complex-struct :real :imag)
+
+;
+; The general complex number type
+;
+(deftype ::complex complex
+ (fn [real imag] (struct complex-struct real imag))
+ (fn [c] (vals c)))
+
+(derive ::complex root-type)
+
+;
+; A specialized subtype for pure imaginary numbers. Introducing this type
+; reduces the number of operations by eliminating additions with and
+; multiplications by zero.
+;
+(deftype ::pure-imaginary imaginary
+ (fn [imag] (struct complex-struct 0 imag))
+ (fn [c] (list (:imag c))))
+
+(derive ::pure-imaginary ::complex)
+
+;
+; Extraction of real and imaginary parts
+;
+(def real (accessor complex-struct :real))
+(def imag (accessor complex-struct :imag))
+
+;
+; Equality and zero test
+;
+(defmethod gc/zero? ::complex
+ [x]
+ (let [[rx ix] (vals x)]
+ (and (zero? rx) (zero? ix))))
+
+(defmethod gc/= [::complex ::complex]
+ [x y]
+ (let [[rx ix] (vals x)
+ [ry iy] (vals y)]
+ (and (gc/= rx ry) (gc/= ix iy))))
+
+(defmethod gc/= [::pure-imaginary ::pure-imaginary]
+ [x y]
+ (gc/= (imag x) (imag y)))
+
+(defmethod gc/= [::complex ::pure-imaginary]
+ [x y]
+ (let [[rx ix] (vals x)]
+ (and (gc/zero? rx) (gc/= ix (imag y)))))
+
+(defmethod gc/= [::pure-imaginary ::complex]
+ [x y]
+ (let [[ry iy] (vals y)]
+ (and (gc/zero? ry) (gc/= (imag x) iy))))
+
+(defmethod gc/= [::complex root-type]
+ [x y]
+ (let [[rx ix] (vals x)]
+ (and (gc/zero? ix) (gc/= rx y))))
+
+(defmethod gc/= [root-type ::complex]
+ [x y]
+ (let [[ry iy] (vals y)]
+ (and (gc/zero? iy) (gc/= x ry))))
+
+(defmethod gc/= [::pure-imaginary root-type]
+ [x y]
+ (and (gc/zero? (imag x)) (gc/zero? y)))
+
+(defmethod gc/= [root-type ::pure-imaginary]
+ [x y]
+ (and (gc/zero? x) (gc/zero? (imag y))))
+
+;
+; Addition
+;
+(defmethod ga/+ [::complex ::complex]
+ [x y]
+ (let [[rx ix] (vals x)
+ [ry iy] (vals y)]
+ (complex (ga/+ rx ry) (ga/+ ix iy))))
+
+(defmethod ga/+ [::pure-imaginary ::pure-imaginary]
+ [x y]
+ (imaginary (ga/+ (imag x) (imag y))))
+
+(defmethod ga/+ [::complex ::pure-imaginary]
+ [x y]
+ (let [[rx ix] (vals x)]
+ (complex rx (ga/+ ix (imag y)))))
+
+(defmethod ga/+ [::pure-imaginary ::complex]
+ [x y]
+ (let [[ry iy] (vals y)]
+ (complex ry (ga/+ (imag x) iy))))
+
+(defmethod ga/+ [::complex root-type]
+ [x y]
+ (let [[rx ix] (vals x)]
+ (complex (ga/+ rx y) ix)))
+
+(defmethod ga/+ [root-type ::complex]
+ [x y]
+ (let [[ry iy] (vals y)]
+ (complex (ga/+ x ry) iy)))
+
+(defmethod ga/+ [::pure-imaginary root-type]
+ [x y]
+ (complex y (imag x)))
+
+(defmethod ga/+ [root-type ::pure-imaginary]
+ [x y]
+ (complex x (imag y)))
+
+;
+; Negation
+;
+(defmethod ga/- ::complex
+ [x]
+ (let [[rx ix] (vals x)]
+ (complex (ga/- rx) (ga/- ix))))
+
+(defmethod ga/- ::pure-imaginary
+ [x]
+ (imaginary (ga/- (imag x))))
+
+;
+; Subtraction is automatically supplied by ga/-, optimized implementations
+; can be added later...
+;
+
+;
+; Multiplication
+;
+(defmethod ga/* [::complex ::complex]
+ [x y]
+ (let [[rx ix] (vals x)
+ [ry iy] (vals y)]
+ (complex (ga/- (ga/* rx ry) (ga/* ix iy))
+ (ga/+ (ga/* rx iy) (ga/* ix ry)))))
+
+(defmethod ga/* [::pure-imaginary ::pure-imaginary]
+ [x y]
+ (ga/- (ga/* (imag x) (imag y))))
+
+(defmethod ga/* [::complex ::pure-imaginary]
+ [x y]
+ (let [[rx ix] (vals x)
+ iy (imag y)]
+ (complex (ga/- (ga/* ix iy))
+ (ga/* rx iy))))
+
+(defmethod ga/* [::pure-imaginary ::complex]
+ [x y]
+ (let [ix (imag x)
+ [ry iy] (vals y)]
+ (complex (ga/- (ga/* ix iy))
+ (ga/* ix ry))))
+
+(defmethod ga/* [::complex root-type]
+ [x y]
+ (let [[rx ix] (vals x)]
+ (complex (ga/* rx y) (ga/* ix y))))
+
+(defmethod ga/* [root-type ::complex]
+ [x y]
+ (let [[ry iy] (vals y)]
+ (complex (ga/* x ry) (ga/* x iy))))
+
+(defmethod ga/* [::pure-imaginary root-type]
+ [x y]
+ (imaginary (ga/* (imag x) y)))
+
+(defmethod ga/* [root-type ::pure-imaginary]
+ [x y]
+ (imaginary (ga/* x (imag y))))
+
+;
+; Inversion
+;
+(ga/defmethod* ga / ::complex
+ [x]
+ (let [[rx ix] (vals x)
+ den ((ga/qsym ga /) (ga/+ (ga/* rx rx) (ga/* ix ix)))]
+ (complex (ga/* rx den) (ga/- (ga/* ix den)))))
+
+(ga/defmethod* ga / ::pure-imaginary
+ [x]
+ (imaginary (ga/- ((ga/qsym ga /) (imag x)))))
+
+;
+; Division is automatically supplied by ga//, optimized implementations
+; can be added later...
+;
+
diff --git a/src/clojure/contrib/gen_html_docs.clj b/src/clojure/contrib/gen_html_docs.clj
index 631081e5..7a6d2729 100644
--- a/src/clojure/contrib/gen_html_docs.clj
+++ b/src/clojure/contrib/gen_html_docs.clj
@@ -474,6 +474,7 @@ emits the generated HTML to the path named by path."
'clojure.contrib.auto-agent
'clojure.contrib.combinatorics
'clojure.contrib.command-line
+ 'clojure.contrib.complex-numbers
'clojure.contrib.cond
'clojure.contrib.condt
'clojure.contrib.def
diff --git a/src/clojure/contrib/load_all.clj b/src/clojure/contrib/load_all.clj
index cfe706af..ea1f2f1d 100644
--- a/src/clojure/contrib/load_all.clj
+++ b/src/clojure/contrib/load_all.clj
@@ -35,6 +35,7 @@ apply-macro
auto-agent
;; combinatorics
command-line
+complex-numbers
cond
condt
def
diff --git a/src/clojure/contrib/test_contrib.clj b/src/clojure/contrib/test_contrib.clj
index 15d8003f..15337dbb 100644
--- a/src/clojure/contrib/test_contrib.clj
+++ b/src/clojure/contrib/test_contrib.clj
@@ -15,7 +15,7 @@
(ns clojure.contrib.test-contrib
(:use clojure.contrib.test-is))
-(def tests [:str-utils :shell-out :test-graph :test-dataflow])
+(def tests [:complex-numbers :str-utils :shell-out :test-graph :test-dataflow])
(defn test-name
[test]
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)))