My task is to compare memoized integral and regular one. But I can't see effects of…
- User
My task is to compare memoized integral and regular one. But I can't see effects of memoization in output. Maybe I did something wrong? Or maybe I shchould use another function for integral, not sin or cos but something heavy with thread/sleep or something Here's my code: (ns test0.core (:gen-class))
(def h 0.01)
(defn trapezoid [f h x1 x2] (* h (/ (+ (f x1) (f x2)) 2)))
;; Мемоизированная версия интеграции (defn integrate-memoized [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (if (zero? n) summ (recur f h (+ summ (trapezoid f h x1 x2)) (dec n)))))
(defn integral-memoized [f h] (let [memo-f (memoize f)] (fn [x] (integrate-memoized memo-f h 0 (int (/ x h))))))
;; Обычная версия интеграции без мемоизации (defn integrate-plain [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (if (zero? n) summ (recur f h (+ summ (trapezoid f h x1 x2)) (dec n)))))
(defn integral-plain [f h] (fn [x] (integrate-plain f h 0 (int (/ x h)))))
;; Версия с ленивыми последовательностями (defn integrate-lazy [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (cons summ (lazy-seq (integrate-lazy f h (+ summ (trapezoid f h x1 x2)) (inc n))))))
(defn integral-lazy [f h] (let [sequence (integrate-lazy f h 0 0)] (fn [x] (nth sequence (dec (int (/ x h)))))))
;; Интегралы для cos и sin (def integral-cosx-memoized (integral-memoized (fn [x] (Math/cos x)) h)) (def integral-sinx-memoized (integral-memoized (fn [x] (Math/sin x)) h))
(def integral-cosx-plain (integral-plain (fn [x] (Math/cos x)) h)) (def integral-sinx-plain (integral-plain (fn [x] (Math/sin x)) h))
(def integral-cosx-lazy (integral-lazy (fn [x] (Math/cos x)) h)) (def integral-sinx-lazy (integral-lazy (fn [x] (Math/sin x)) h))
(println "Сравнение производительности интегрирования:")
;; Сравнение для интеграла cosx (println "\nmem cos(x):") (time (integral-cosx-memoized 0.5)) (time (integral-cosx-memoized 1)) (time (integral-cosx-memoized 2)) (time (integral-cosx-memoized 4)) (time (integral-cosx-memoized 5))
(println "\nreg cos(x):") (time (integral-cosx-plain 0.5)) (time (integral-cosx-plain 1)) (time (integral-cosx-plain 2)) (time (integral-cosx-plain 4)) (time (integral-cosx-plain 5))
(println "\nlazy cos(x):") (time (integral-cosx-lazy 0.5)) (time (integral-cosx-lazy 1)) (time (integral-cosx-lazy 2)) (time (integral-cosx-lazy 4)) (time (integral-cosx-lazy 5))
;; Сравнение для интеграла sinx (println "\nmem sin(x):") (time (integral-sinx-memoized 0.5)) (time (integral-sinx-memoized 1)) (time (integral-sinx-memoized 2)) (time (integral-sinx-memoized 4)) (time (integral-sinx-memoized 5))
(println "\nreg sin(x):") (time (integral-sinx-plain 0.5)) (time (integral-sinx-plain 1)) (time (integral-sinx-plain 2)) (time (integral-sinx-plain 4)) (time (integral-sinx-plain 5))
(println "\nlazy sin(x):") (time (integral-sinx-lazy 0.5)) (time (integral-sinx-lazy 1)) (time (integral-sinx-lazy 2)) (time (integral-sinx-lazy 4)) (time (integral-sinx-lazy 5))
- Assistant
From your code and output, it seems like memoization isn't providing a significant performance improvement as one might expect under different circumstances. The primary issue is that memoization is effective when you repeatedly call a function with the same input, allowing previously computed results to be reused. However, in your integration setup, each input (or argument) to the integral function is unique for each provided to the function since you're accumulating over a range and not frequently repeating calculations for the same subinterval.
- User
So, lets use not sin/cos but a polynom with thread-sleep. And let's make output clear with repeated Inputs
- Assistant
- User
Why does my output look weird? I only have two or three strings for regular version Memoized Polynomial Integrals: "Elapsed time: 1032.548823 msecs" "Elapsed time: 1023.058789 msecs" "Elapsed time: 1017.114785 msecs" "Elapsed time: 0.191829 msecs" "Elapsed time: 0.336423 msecs" "Elapsed time: 0.518074 msecs"
Plain Polynomial Integrals: "Elapsed time: 2026.312967 msecs" "Elapsed time: 4050.139898 msecs" [Execution timed out after 15 seconds]
- Assistant
- User
Great! Now I want same code but in which I will compare regular version and version with lazy
- Assistant
- User
Now please help me to add comments to my code. I will send one function or fragment of code: (defn trapezoid [f h x1 x2] (* h (/ (+ (f x1) (f x2)) 2)))
- Assistant
Certainly! Adding comments to your code will help clarify the purpose and functionality of each part. Here’s an explanation with comments for the
trapezoidfunction:(defn trapezoid [f h x1 x2] ;; Calculate the area of a trapezoid under the curve defined by function f ;; from x1 to x2 using the trapezoidal rule. ;; f : Function representing the curve. ;; h : Width of the interval (distance between x1 and x2). ;; x1: Starting point of the interval. ;; x2: Ending point of the interval. ;; The area of a trapezoid is calculated as width * average of the two heights. ;; Here, the heights are the function values at x1 and x2, i.e., (f x1) and (f x2). ;; Therefore, the formula used is h * ((f(x1) + f(x2)) / 2). (* h (/ (+ (f x1) (f x2)) 2)))Explanation:
-
Trapezoidal Rule: This numerical integration technique estimates the area under a curve. For a small segment between and , it assumes the curve is approximately linear and calculates the area of the trapezoid formed.
-
- User
(defn integrate-memoized [f h summ n] (let [x1 (* h n), x2 (* h (inc n))] (if (zero? n) summ (recur f h (+ summ (trapezoid f h x1 x2)) (dec n)))))
- Assistant
Here's a detailed explanation with comments for your
integrate-memoizedfunction: - User
(defn integral-memoized [f h] (let [memo-f (memoize f)] (fn [x] (integrate-memoized memo-f h 0 (int (/ x h))))))
- Assistant
Here's a detailed explanation with comments for your
integral-memoizedfunction: - User
(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))))))
- Assistant
- User
(defn integral-lazy [f h] (let [sequence (integrate-lazy f h 0 0)] (fn [x] (nth sequence (int (/ x h))))))
- Assistant
Here's a detailed explanation with comments for the
integral-lazyfunction, which utilizes the previously defined function: