Browse files

Another shortcut: build-term-list adds coefficients of terms of the s…

…ame order, so to add two polynomials we just need to concatenate together their term list and construct a new one
  • Loading branch information...
ray1729 committed Jun 12, 2010
1 parent 2878f2a commit dc65a65c3e7bae6141667deab57bd8f619f9dc03
Showing with 6 additions and 3 deletions.
  1. +6 −3 src/ray1729/clojure/polynomials.clj
@@ -101,19 +101,22 @@
; Arithmetic operations
+(defn- add-terms [tp tq]
+ (reduce concat (concat tp tq)))
(defmethod ga/+ [::polynomial ::polynomial]
[p q]
(when (not= (variable p) (variable q))
(throw (IllegalArgumentException. "addition of polynomials in different variables not supported")))
- (polynomial (variable p) (merge-with ga/+ (terms p) (terms q))))
+ (polynomial (variable p) (add-terms (terms p) (terms q))))
(defmethod ga/+ [root-type ::polynomial]
[c p]
- (ga/+ (polynomial (variable p) (merge-with ga/+ {0 c} (terms p)))))
+ (polynomial (variable p) (add-terms {0 c} (terms p))))
(defmethod ga/+ [::polynomial root-type]
[p c]
- (ga/+ (polynomial (variable p) (merge-with ga/+ (terms p) {0 c}))))
+ (polynomial (variable p) (add-terms (terms p) {0 c})))
(defmethod ga/- ::polynomial

0 comments on commit dc65a65

Please sign in to comment.