This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,8 @@
(defn ps+ [ps0 ps1]
(letfn [(+zs [ps] (concat ps (repeat :z)))
(notz? [a] (not= :z a))
(nval [a] (if (notz? a) a 0))
(z+ [a0 a1] (if (= :z a0 a1) :z (+ (nval a0) (nval a1))))]
(take-while notz? (map z+ (+zs ps0) (+zs ps1)))))
(defn ps- [ps0 ps1] (ps+ ps0 (map - ps1)))

View file

@ -0,0 +1,9 @@
(defn ps*
([ps0 ps1] (ps* [0] ps0 ps1))
([[a0 & resta] [p0 & rest0] [p1 & rest1 :as ps1]]
(lazy-seq
(cons
(+ a0 (* p0 p1))
(let [mrest1 (if (or (nil? rest1) (zero? p0)) nil, (map #(* p0 %) rest1))
accum (cond (nil? resta) mrest1, (nil? mrest1) resta, :else (ps+ resta mrest1))]
(if (nil? rest0) accum, (ps* (or accum [0]) rest0 ps1)))))))

View file

@ -0,0 +1,7 @@
(defn indexed [ps] (map vector (iterate inc 0) ps))
(defn differentiate [ps]
(drop 1 (for [[n a] (indexed ps)] (* n a))))
(defn integrate [ps]
(cons 0 (for [[n a] (indexed ps)] (/ a (inc n)))))

View file

@ -0,0 +1,5 @@
(println (ps+ [1 2] [3 4 5]))
; (4 6 5)
(println (ps* [1 2] [3 4 5]))
; (3 10 13 10)

View file

@ -0,0 +1,14 @@
(def nfacts (iterate (fn [[f n]] [(* f n) (inc n)]) [1 1]))
(def facts (map first nfacts))
(def sin (map / (cycle [0 1 0 -1]) facts))
(def cos (map / (cycle [1 0 -1 0]) facts))
(println (take 10 sin))
; (0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880)
(println (take 10 (integrate cos)))
; (0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880)
(println (take 20 (ps+ (ps* sin sin) (ps* cos cos))))
; (1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

View file

@ -0,0 +1,7 @@
(letfn [(fsin [] (lazy-seq (integrate (fcos))))
(fcos [] (ps- [1] (integrate (fsin))))]
(def sinx (fsin))
(def cosx (fcos)))
(println (take 10 sinx))
; (0 1 0 -1/6 0 1/120 0 -1/5040 0 1/362880)