Explain, please, how does the memoization work here: (def h 0.01) ;; trapezoid method…
- User
Explain, please, how does the memoization work here: (def h 0.01)
;; trapezoid method (defn trapezoid [f h x1 x2] ; h * ((f(x1) + f(x2)) / 2) (* h (/ (+ (f x1) (f x2)) 2)))
;; integrate function (defn integrate [f h accum n] (let [start-point (* h n), end-point (* h (inc n))] (if (zero? n) accum (recur f h (+ accum (trapezoid f h start-point end-point)) (dec n)))))
;; memoized version (defn integral-memoized [f h] (let [memo-f (memoize f)] (fn [x] (integrate memo-f h 0 (int (/ x h))))))
;; regular version (defn integral-regular [f h] (fn [x] (integrate f h 0 (int (/ x h)))))
;; simulated heavy computation with a polynomial (defn expensive-poly [x] (Thread/sleep 4)
(+ (* x x) (* 2 x) 3)) ; x^2 + 2x + 3(def integral-poly-memoized (integral-memoized expensive-poly h)) (def integral-poly-regular (integral-regular expensive-poly h))
(println "Memoized Polynomial Integrals:") (time (integral-poly-memoized 1)) (time (integral-poly-memoized 2)) (time (integral-poly-memoized 3)) (time (integral-poly-memoized 1)) (time (integral-poly-memoized 2)) (time (integral-poly-memoized 3))
(println "\nRegular Polynomial Integrals:") (time (integral-poly-regular 1)) (time (integral-poly-regular 2)) (time (integral-poly-regular 3)) (time (integral-poly-regular 1)) (time (integral-poly-regular 2)) (time (integral-poly-regular 3))
- Assistant
- User
and how does lazy-seq work here: (def h 0.01)
;; trapezoid method (defn trapezoid [f h x1 x2] ; h * ((f(x1) + f(x2)) / 2) (* h (/ (+ (f x1) (f x2)) 2)))
;; integrate function (defn integrate [f h accum n] (let [start-point (* h n), end-point (* h (inc n))] (if (zero? n) accum (recur f h (+ accum (trapezoid f h start-point end-point)) (dec n)))))
;; regular version (defn integral-regular [f h] (fn [x] (integrate f h 0 (int (/ x h)))))
;; integral using lazy sequences (defn integrate-lazy [f h accum n] (let [start-point (* h n), end-point (* h (inc n))] (cons accum (lazy-seq (integrate-lazy f h (+ accum (trapezoid f h start-point end-point)) (inc n))))))
;; lazy integral from 0 to x (defn integral-lazy [f h] (let [sequence (integrate-lazy f h 0 0)] (fn [x] (nth sequence (int (/ x h))))))
;; simulated heavy computation with a polynomial (defn expensive-poly [x] (Thread/sleep 2)
(+ (* x x) (* 2 x) 3)) ; x^2 + 2x + 3(def integral-poly-regular (integral-regular expensive-poly h)) (def integral-poly-lazy (integral-lazy expensive-poly h))
(println "Regular Polynomial Integrals:") (time (integral-poly-regular 1)) (time (integral-poly-regular 1)) (time (integral-poly-regular 2)) (time (integral-poly-regular 2)) (time (integral-poly-regular 3)) (time (integral-poly-regular 3))
- Assistant
- User
What is the difference between lazy-seq and memoize?
- Assistant
- User
Memoize based on cashing and lazy-seq based on hashing, right?
- Assistant
It seems like there might be some confusion in your question regarding the mechanisms underlying
memoizeandlazy-seq. Let's clarify how they work:Memoization ()