Skip to content

Instantly share code, notes, and snippets.

@sunilnandihalli
Created October 28, 2012 14:40
Show Gist options
  • Save sunilnandihalli/3968777 to your computer and use it in GitHub Desktop.
Save sunilnandihalli/3968777 to your computer and use it in GitHub Desktop.
(defprotocol polynomial
(eval-poly [this t])
(derivative [this])
(derivative [this n])
(coeffs [this]))
(deftype poly [coeffs]
polynomial
(eval-poly [this t]
(reduce #(+ (* %1 t) %2) 0.0 coeffs))
(derivative [this]
(->poly (map #(* %1 %2) coeffs (range (count coeffs) 0 -1))))
(derivative [this n]
(loop [c-poly this n-left n]
(if-not (> n 0) c-poly
(recur (derivative c-poly) (dec n)))))
(coeffs [this] coeffs))
@kumarshantanu
Copy link

(defprotocol polynomial
    (eval-poly [this t])
    (derivative [this])
    (derivative [this n])
    (coeffs [this]))

(deftype poly [coeffs]
    polynomial
    (eval-poly [this t]
          (reduce #(+ (* %1 t) %2) 0.0 coeffs))
    (derivative
      ([this]
        (->poly (map #(* %1 %2) coeffs (range (count coeffs) 0 -1))))
      ([this n]
          (loop [c-poly this n-left n]
                  (if-not (> n 0) c-poly
                            (recur (derivative c-poly) (dec n))))))
    (coeffs [this] coeffs))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment