Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns main
- (:gen-class)
- (:require [clojure.test :refer :all]))
- (defn get_trapezoid [func]
- (letfn [(step [] 0.5)
- (next [x] (+ x (step)))
- (trapezoid [x] (* (/ (+ (func x) (func (- x (step)))) 2.0) (step)))
- (integrate [mesh_x acc x] (if (> mesh_x x) acc (recur (next mesh_x) (+ acc (trapezoid mesh_x)) x)))]
- (partial integrate (step) 0)
- )
- )
- (defn get_trapezoid_memo [func]
- (let [step 0.1
- to_point (fn [x] (* x step))
- trapezoid (fn [x] (* (/ (+ (func (to_point x)) (func (- (to_point x) step))) 2.0) step))
- integrate (memoize (fn [recfunc mesh_x] (println mesh_x) (if (< (dec mesh_x) 0) 0.0 (+ (trapezoid mesh_x) (recfunc recfunc (dec mesh_x))))))]
- (fn [x] (integrate integrate (Math/floor (/ x step))))))
- (defn get_trapezoid_memo_with_part [func]
- (let [step 0.1
- to_point (fn [x] (* x step))
- trapezoid (fn [x delta] (* (/ (+ (func (to_point x)) (func (- (to_point x) delta))) 2.0) delta))
- integrate (memoize (fn [recfunc mesh_x] (if (< (dec mesh_x) 0) 0.0 (+ (trapezoid mesh_x step) (recfunc recfunc (dec mesh_x))))))]
- (fn [x] (+ (integrate integrate (Math/floor (/ x step))) (trapezoid x (- x (to_point (Math/floor (/ x step)))))))))
- (defn get_trapezoid_stream [func]
- (letfn [(step [] 0.5)
- (trapezoid [x] (* (/ (+ (func x) (func (+ x (step)))) 2.0) (step)))
- (to_point [x] (* x (step)))
- ]
- (fn [x] (let [limit (quot x (step))]
- (->> (range)
- (map to_point)
- (map trapezoid)
- (take limit)
- (reduce +))))
- ))
- (defn get_trapezoid_stream_2 [func]
- (letfn [(step [] 0.5)
- (trapezoid [x] (* (/ (+ (func x) (func (+ x (step)))) 2.0) (step)))
- (next_point [x] (+ x (step)))
- (next_step [[x, integral]] (println x) [(next_point x) (+ (trapezoid x) integral)])]
- (let
- [seq_pair (iterate next_step [0 0])]
- (fn [x] (nth (map last seq_pair) (quot x (step)))))
- ))
- (let [linear #(* % 2)
- integral_tail (get_trapezoid linear)
- integral_stream (get_trapezoid_stream linear)
- integral_stream_2 (get_trapezoid_stream_2 linear)
- integral_memo_part (get_trapezoid_memo_with_part linear)
- integral_memo (get_trapezoid_memo linear)]
- ;(println (time (integral_memo 17)))
- ;(println (time (integral_memo 15)))
- ;(println (time (integral_memo 15.1)))
- ;(println (time (integral_memo 20)))
- ;(println (time (integral_memo 100)))
- ;(println (time (integral_memo 100)))
- ;(println "------------------")
- ;(println (time (integral_tail 17)))
- ;(println (time (integral_tail 15)))
- ;(println (time (integral_tail 10)))
- ;(println (time (integral_tail 20)))
- ;(println (time (integral_tail 25)))
- ;(println (time (integral_tail 2500)))
- ;(println "------------------")
- ;(println (time (integral_stream 17)))
- ;(println (time (integral_stream 15)))
- ;(println (time (integral_stream 10)))
- ;(println (time (integral_stream 20)))
- ;(println (time (integral_stream 25)))
- ;(println (time (integral_stream 2500)))
- (println "------------------")
- (println (time (integral_stream_2 15)))
- (println (time (integral_stream_2 16)))
- ;(println (time (integral_stream_2 17)))
- ;(println (time (integral_stream_2 18)))
- ;(println (time (integral_stream_2 19)))
- ;(println (time (integral_stream_2 2500)))
- (println "------------------")
- ;(println (time (integral_memo_part 15)))
- ;(println (time (integral_memo_part 15.25)))
- ;(println (time (integral_memo_part 16)))
- ;(println (time (integral_memo_part 17)))
- ;(println (time (integral_memo_part 18)))
- ;(println (time (integral_memo_part 19)))
- )
- ((deftest generator_tests
- (testing "3*x^2 => x^3"
- (let [quad #(* (Math/pow % 2) 3)
- integral (get_trapezoid quad)]
- (is (< (Math/abs (- (integral 4) 64)) 1))
- (is (< (Math/abs (- (integral 5) 125)) 1))
- (is (< (Math/abs (- (integral 6) 216)) 1))
- )
- )
- (testing "2*x => x^2"
- (let [linear #(* % 2)
- integral (get_trapezoid linear)]
- (is (< (- (integral 3) 9) 1))
- (is (< (- (integral 25) 625) 1))
- (is (= (integral -1) 0))
- )
- )
- ))
- ;(run-tests 'main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement