Working with complex numbers

Working with complex numbers

How to:

Clojure provides built-in support for complex numbers through the clojure.lang.Numbers utility class. Use complex to create complex numbers and perform arithmetic.

;; Creating complex numbers
(def a (clojure.lang.Numbers/complex 3 4))  ; 3 + 4i
(def b (clojure.lang.Numbers/complex 1 -1)) ; 1 - i

;; Addition
(+ a b) ;=> #object[clojure.lang.Numbers.Complex 0x5c6cfe9 "4 + 3i"]

;; Subtraction
(- a b) ;=> #object[clojure.lang.Numbers.Complex 0x5e51118 "2 + 5i"]

;; Multiplication
(* a b) ;=> #object[clojure.lang.Numbers.Complex 0x6ec3f0df "7 + i"]

;; Division
(/ a b) ;=> #object[clojure.lang.Numbers.Complex 0x5db0cd10 "3.5 + 3.5i"]

;; Conjugate
(.conjugate a) ;=> #object[clojure.lang.Numbers.Complex 0x47c6e076 "3 - 4i"]

Deep Dive

Complex numbers were formalized by mathematicians like Gauss and Euler in the 18th century. Though initially met with skepticism, they’ve since become crucial in modern science and engineering. Clojure doesn’t have a native complex number type like some languages (e.g., Python), but the included Java interop can handle the necessary operations via the clojure.lang.Numbers class.

Java’s java.lang.Complex is a robust alternative, providing more features and potential optimizations. Clojure’s host interoperability makes it easy to work with Java libraries.

Under the hood, complex number arithmetic involves adding and multiplying the real and imaginary parts, with the key rule that i^2 = -1. Complex number division can be more convoluted, typically requiring the conjugate to avoid division by complex numbers.

See Also