Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement addition/multiplication by constant directly, rather than r…

…edispatching via the multimethod
  • Loading branch information...
commit 2878f2af2efe4ff2494677f52f2c8e4a1370bcaf 1 parent 2635b40
@ray1729 authored
Showing with 16 additions and 11 deletions.
  1. +16 −11 src/ray1729/clojure/polynomials.clj
View
27 src/ray1729/clojure/polynomials.clj
@@ -15,7 +15,7 @@
; Polynomials are represented as struct maps with a variable and a
; sorted hash of terms: the key in the hash is the order, and the
; value the corresponding coefficient
-
+;
(defstruct polynomial-struct :variable :terms)
;
@@ -109,25 +109,30 @@
(defmethod ga/+ [root-type ::polynomial]
[c p]
- (ga/+ (polynomial (variable p) 0 c) p))
+ (ga/+ (polynomial (variable p) (merge-with ga/+ {0 c} (terms p)))))
-(defmethod ga/+ [::polynomial root-type] [p c] (ga/+ c p))
+(defmethod ga/+ [::polynomial root-type]
+ [p c]
+ (ga/+ (polynomial (variable p) (merge-with ga/+ (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 ::polynomial]
[p q]
(when (not= (variable p) (variable q))
- (throw (IllegalArgumentException. "multiplication of polynomials in different variables not supported")))
- (let [tq (terms q)]
- (reduce ga/+ (for [[order coeff] (terms p)]
- (polynomial (variable p)
- (interleave (map #(+ order %) (keys tq)) (map #(ga/* coeff %) (vals tq))))))))
-
+ (throw (IllegalArgumentException. "multiplication of polynomials in different variables not supported")))
+ (polynomial (variable p) (multiply-terms (terms p) (terms q))))
+
(defmethod ga/* [root-type ::polynomial]
[c p]
- (ga/* (polynomial (variable p) 0 c) p))
+ (polynomial (variable p) (multiply-terms {0 c} (terms p))))
-(defmethod ga/* [::polynomial root-type] [p c] (ga/* c p))
+(defmethod ga/* [::polynomial root-type]
+ [p c]
+ (polynomial (variable p) (multiply-terms (terms p) {0 c})))
Please sign in to comment.
Something went wrong with that request. Please try again.