Last active
February 21, 2017 13:07
-
-
Save paudirac/a629229e5207f35f148063cae2c95314 to your computer and use it in GitHub Desktop.
Eval and derive polynomials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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