Arithmetic/Complex: Difference between revisions

Content deleted Content added
Line 622:
(defrecord Complex [r i]
Object
(toString [this] (apply str (:rinterleave (vals this) ["+" (:i this) "i"]))))
 
(defmethod ari/+ [Complex Complex]
[x y] (map->Complex (merge-with + (:r x) (:r y)))
(+ (:i x) (:i y))))
 
(defmethod ari/+ [Complex java.lang.Number]
[x{:keys [r i]} y] (->Complex x(+ :r (+ y) (->Complex (:i x))))
 
(defmethod ari/- Complex
[x] (->Complex> (-x vals (:rmap x)-) (apply - (:i x)))>Complex)
 
(defmethod ari/* [Complex Complex]
[x y] (map->Complex (merge-with * (:r x) (:r y)))
(* (:i x) (:i y))))
 
(defmethod ari/* [Complex java.lang.Number]
[x{:keys [r i]} y] (->Complex x(* :r (* y) (->Complex (:i x))))
 
(ari/defmethod* ari / Complex
[x] (->Complex> (/x vals (:rmap x)/) (/apply (:i x)->Complex)))
 
(defn conj [^Complex x{:keys [r i]}]
(->Complex (:r x) (- (:i x))))
 
(defn inv [^Complex x{:keys [r i]}]
(let [rm (:+ (* r xr), i (:* i xi)),]
m (+ (* r r) (* i i))]
(->Complex (/ r m) (- (/ i m)))))
</lang>