Permalink
Browse files

Implement negation by multiplying by -1 (removes some code duplication)

  • Loading branch information...
1 parent 26f8aa3 commit 9862da3970da478b8965aff8e9587011f63cf444 @ray1729 committed Jun 12, 2010
Showing with 4 additions and 4 deletions.
  1. +4 −4 src/ray1729/clojure/polynomials.clj
@@ -119,14 +119,14 @@
[p c]
(polynomial (variable p) (add-terms (terms p) {0 c})))
-(defmethod ga/- ::polynomial
- [p]
- (polynomial (variable p) (interleave (keys (terms p)) (map ga/- (vals (terms p))))))
-
(defn- multiply-terms [tp tq]
(reduce concat (for [[order coeff] tp]
(interleave (map #(+ order %) (keys tq)) (map #(ga/* coeff %) (vals tq))))))
+(defmethod ga/- ::polynomial
+ [p]
+ (polynomial (variable p) (multiply-terms {0 -1} (terms p))))
+
(defmethod ga/* [::polynomial ::polynomial]
[p q]
(when (not= (variable p) (variable q))

0 comments on commit 9862da3

Please sign in to comment.