Advertisement
Guest User

Untitled

a guest
Nov 28th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns main
  2.   (:gen-class)
  3.   (:require [clojure.test :refer :all]))
  4.  
  5.  
  6. (defn get_trapezoid [func]
  7.   (letfn [(step [] 0.5)
  8.           (next [x] (+ x (step)))
  9.           (trapezoid [x] (* (/ (+ (func x) (func (- x (step)))) 2.0) (step)))
  10.           (integrate [mesh_x acc x] (if (> mesh_x x) acc (recur (next mesh_x) (+ acc (trapezoid mesh_x)) x)))]
  11.     (partial integrate (step) 0)
  12.     )
  13.   )
  14.  
  15.  
  16. (defn get_trapezoid_memo [func]
  17.   (let [step 0.1
  18.         to_point (fn [x] (* x step))
  19.         trapezoid (fn [x] (* (/ (+ (func (to_point x)) (func (- (to_point x) step))) 2.0) step))
  20.         integrate (memoize (fn [recfunc mesh_x] (println mesh_x) (if (< (dec mesh_x) 0) 0.0 (+ (trapezoid mesh_x) (recfunc recfunc (dec mesh_x))))))]
  21.     (fn [x] (integrate integrate (Math/floor (/ x step))))))
  22.  
  23.  
  24. (defn get_trapezoid_memo_with_part [func]
  25.   (let [step 0.1
  26.         to_point (fn [x] (* x step))
  27.         trapezoid (fn [x delta] (* (/ (+ (func (to_point x)) (func (- (to_point x) delta))) 2.0) delta))
  28.         integrate (memoize (fn [recfunc mesh_x] (if (< (dec mesh_x) 0) 0.0 (+ (trapezoid mesh_x step) (recfunc recfunc (dec mesh_x))))))]
  29.     (fn [x] (+ (integrate integrate (Math/floor (/ x step))) (trapezoid x (- x (to_point (Math/floor (/ x step)))))))))
  30.  
  31.  
  32. (defn get_trapezoid_stream [func]
  33.   (letfn [(step [] 0.5)
  34.           (trapezoid [x] (* (/ (+ (func x) (func (+ x (step)))) 2.0) (step)))
  35.           (to_point [x] (* x (step)))
  36.           ]
  37.     (fn [x] (let [limit (quot x (step))]
  38.               (->> (range)
  39.                    (map to_point)
  40.                    (map trapezoid)
  41.                    (take limit)
  42.                    (reduce +))))
  43.     ))
  44.  
  45.  
  46. (defn get_trapezoid_stream_2 [func]
  47.   (letfn [(step [] 0.5)
  48.           (trapezoid [x] (* (/ (+ (func x) (func (+ x (step)))) 2.0) (step)))
  49.           (next_point [x] (+ x (step)))
  50.           (next_step [[x, integral]] (println x) [(next_point x) (+ (trapezoid x) integral)])]
  51.     (let
  52.       [seq_pair (iterate next_step [0 0])]
  53.       (fn [x] (nth (map last seq_pair) (quot x (step)))))
  54.     ))
  55.  
  56.  
  57. (let [linear #(* % 2)
  58.       integral_tail (get_trapezoid linear)
  59.       integral_stream (get_trapezoid_stream linear)
  60.       integral_stream_2 (get_trapezoid_stream_2 linear)
  61.       integral_memo_part (get_trapezoid_memo_with_part linear)
  62.       integral_memo (get_trapezoid_memo linear)]
  63.   ;(println (time (integral_memo 17)))
  64.   ;(println (time (integral_memo 15)))
  65.   ;(println (time (integral_memo 15.1)))
  66.   ;(println (time (integral_memo 20)))
  67.   ;(println (time (integral_memo 100)))
  68.   ;(println (time (integral_memo 100)))
  69.   ;(println "------------------")
  70.   ;(println (time (integral_tail 17)))
  71.   ;(println (time (integral_tail 15)))
  72.   ;(println (time (integral_tail 10)))
  73.   ;(println (time (integral_tail 20)))
  74.   ;(println (time (integral_tail 25)))
  75.   ;(println (time (integral_tail 2500)))
  76.   ;(println "------------------")
  77.   ;(println (time (integral_stream 17)))
  78.   ;(println (time (integral_stream 15)))
  79.   ;(println (time (integral_stream 10)))
  80.   ;(println (time (integral_stream 20)))
  81.   ;(println (time (integral_stream 25)))
  82.   ;(println (time (integral_stream 2500)))
  83.   (println "------------------")
  84.   (println (time (integral_stream_2 15)))
  85.   (println (time (integral_stream_2 16)))
  86.   ;(println (time (integral_stream_2 17)))
  87.   ;(println (time (integral_stream_2 18)))
  88.   ;(println (time (integral_stream_2 19)))
  89.   ;(println (time (integral_stream_2 2500)))
  90.   (println "------------------")
  91.   ;(println (time (integral_memo_part 15)))
  92.   ;(println (time (integral_memo_part 15.25)))
  93.   ;(println (time (integral_memo_part 16)))
  94.   ;(println (time (integral_memo_part 17)))
  95.   ;(println (time (integral_memo_part 18)))
  96.   ;(println (time (integral_memo_part 19)))
  97.   )
  98.  
  99. ((deftest generator_tests
  100.    (testing "3*x^2 => x^3"
  101.      (let [quad #(* (Math/pow % 2) 3)
  102.            integral (get_trapezoid quad)]
  103.        (is (< (Math/abs (- (integral 4) 64)) 1))
  104.        (is (< (Math/abs (- (integral 5) 125)) 1))
  105.        (is (< (Math/abs (- (integral 6) 216)) 1))
  106.        )
  107.      )
  108.    (testing "2*x => x^2"
  109.      (let [linear #(* % 2)
  110.            integral (get_trapezoid linear)]
  111.        (is (< (- (integral 3) 9) 1))
  112.        (is (< (- (integral 25) 625) 1))
  113.        (is (= (integral -1) 0))
  114.        )
  115.      )
  116.    ))
  117.  
  118. ;(run-tests 'main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement