Skip to content

Instantly share code, notes, and snippets.

@paudirac
Last active February 21, 2017 13:07
Show Gist options
  • Save paudirac/a629229e5207f35f148063cae2c95314 to your computer and use it in GitHub Desktop.
Save paudirac/a629229e5207f35f148063cae2c95314 to your computer and use it in GitHub Desktop.
Eval and derive polynomials
(defn accumulate [op initial sequence]
(if (empty? sequence)
initial
(op (first sequence)
(accumulate op initial (rest sequence)))))
(defn eval-poly [x coefs]
(accumulate (fn [c0 cs]
(+ c0 (* x cs)))
0
coefs))
(defn d-poly [coefs]
(let [pos (range (count coefs))]
(rest (map (fn [[a b]] (* a b)) (map vector pos coefs)))))
(eval-poly 4
(d-poly
(d-poly (list 0 65 (/ 18 (Math/sqrt 4)) (/ 3 (Math/sqrt 36))))))
; # => 30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment