-
Notifications
You must be signed in to change notification settings - Fork 0
/
z.clj
58 lines (46 loc) · 1.13 KB
/
z.clj
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
54
55
56
57
58
(ns com.gfredericks.z
"A simple implementation of complex numbers. The real/imaginary
components can be anything that clojure's core arithmetic functions
handle."
(:refer-clojure :exclude [+ - * /])
(:require [com.gfredericks.lib-4395 :as alg]
[com.gfredericks.z.impl :as z]))
(defn real->z
[r]
(z/complex r 0))
(defn imag->z
[i]
(z/complex 0 i))
(defn z
[r i]
(z/complex r i))
(defn polar->z
[mag arg]
(com.gfredericks.z.impl.PolarComplex. mag arg))
(def ZERO (real->z 0))
(def ONE (real->z 1))
(def I (imag->z 1))
(alg/defs-keys + - * /
(alg/compile-field ZERO
ONE
z/add
z/negate
z/multiply
z/invert))
(defn z->real
[z]
(z/real z))
(defn z->imag
[z]
(z/imag z))
(defn conjugate [z] (z/conjugate z))
(defn magnitude [z] (z/mag z))
(defn abs-squared
"Returns the square of the absolute value of the argument."
[z]
(* z (z/conjugate z)))
(defn separation
"Returns a double representing the absolute value of the difference
between the two given numbers."
[z1 z2]
(z/mag (- z1 z2)))