Trapezoidal Integration is not accurate enough in Clojure - clojure

So currently, I wrote a Clojure code to do Trapezoidal integration of a polynomial function in HackerRank.com:
https://www.hackerrank.com/challenges/area-under-curves-and-volume-of-revolving-a-curv
(defn abs[x]
(max x (- 0 x))
)
(defn exp[x n]
(if (> n 0)
(* x (exp x (- n 1)))
1
)
)
(defn fact[x]
(if (> x 0)
(* x (fact (- x 1)))
1)
)
(defn func[x lst1 lst2]
((fn step [sum lst1 lst2]
(if (> (.size lst1) 0)
(step (+ sum (* (last lst1) (exp x (last lst2)))) (drop-last lst1) (drop-last lst2))
sum
)
)
0 lst1 lst2
)
)
(defn integrate[f a b]
(def h 0.001)
(def n (/ (abs (- b a)) h))
((fn step[i sum]
(if (< i n)
(step (+ i 1) (+ sum (f (+ (* i h) a))))
(* h (+ (/(+ (f a) (f b)) 2) sum))
)
) 0 0)
)
(defn volumeIntegral[f a b]
(defn area[r]
(* 3.14159265359 (* r r)))
(def h 0.001)
(def n (/ (abs (- b a)) h))
((fn step[i sum]
(if (< i n)
(step (+ i 1) (+ sum (area (f (+ (* i h) a)))))
(* h (+ (/ (+ (f a) (f b)) 2) sum))
)
) 0 0)
)
(defn lineToVec[line_str] (clojure.string/split line_str #"\s+"))
(defn strToDouble [x] (Double/parseDouble (apply str (filter #(Character/isDigit %) x))))
(defn readline[vec]
((fn step[list vec]
(if (> (.size vec) 0)
(step (conj list (last vec)) (drop-last vec))
list
)
) '() vec)
)
(integrate (fn [x] (func x '(1 2 3 4 5 6 7 8) '(-1 -2 -3 -4 1 2 3 4))) 1 2)
(volumeIntegral (fn [x] (func x '(1 2 3 4 5 6 7 8) '(-1 -2 -3 -4 1 2 3 4))) 1 2)
However, the output I have is:
107.38602491666647
45611.95754801859
While is supposed to be around:
101.4
41193.0
My code passed the first two test cases, but didn't manage to pass the rest. I assume is because of the issue accuracy. I looked through my code several times but couldn't seem to make it better. What am I doing wrong here ? Thank you.

Your exp function isn't quite right -- it doesn't handle negative exponents correctly. Probably best just to use Math/pow.
The other thing you could do is adjust your h value in volumeIntegral but to avoid stack issues, use recur (which gives you tail recursion), e.g. here's a slightly modified version:
(defn volume-integral [f a b]
(defn area[r]
(* Math/PI (* r r)))
(def h 0.000001)
(def n (/ (abs (- b a)) h))
((fn [i sum]
(if (not (< i n))
(* h (+ (/ (+ (f a) (f b)) 2) sum))
(recur (+ i 1) (+ sum (area (f (+ (* i h) a)))))))
0 0))
(I did the something similar with integral.) All in all, I wasn't able to quite hit the second figure, but this should get you on the right track:
101.33517384995224
41119.11576557253

Related

Why is this lazy-sequence not printing?

I cannot figure out why this lazy-sequence is not printing. I've looked at other posts on here (such as this) and none seem to be exactly what I am looking for. Here is the code segment...
(defn exp [x n]
(reduce * (repeat n x))
)
(defn primes
([] (primes 1 1))
([n m] (if (= n 1) (lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (+ m 1) (1))))
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (- n 1) (+ m 1)))))
)
)
(take 4 (primes))
Any help is greatly appreciated. Thank you!
The three comments really give the answer. But always good to actually see the code, so here are two ways that give the printed output.
(defn exp [x n]
(reduce * (repeat n x)))
(defn primes
([] (primes 1 1))
([n m] (if (= n 1)
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (+ m 1) 1)))
(lazy-seq (cons (* (exp 7 n) (exp 11 m)) (primes (- n 1) (+ m 1)))))))
(defn x-1 []
(doseq [prime (take 4 (primes 2 2))]
(println prime)))
(defn x-2 []
(->> (primes 2 2)
(take 4)
(apply println)))

NullPointerException using Clojure

I'm new to clojure, attempting to write a function (all-bit-seqs n) that generates all bit strings of length n as a list. So if I were to call (all-bit-seqs 2), it would output ((0 0) (0 1) (1 0) (1 1)) in any order. However, I am getting a NullPointerException when I call the helper function rest-bit-seqs, and I can't figure out why. My code is as follows, any help would be appreciated.
(defn not-bit [x]
(* -1 (- x 1))
)
(defn inc-bit-seq [x]
(cond
(= 0 (not-bit (first x))) (cons 0 (inc-bit-seq (rest x)))
:else (cons 1 (rest x))
)
)
(defn pow [x, y]
(cond
(not= y 0) (* x (pow x (- y 1)))
:else 1
)
)
(defn rest-bit-seqs [n, x, lst]
(cond
(not= x (pow 2 n)) (cons lst (rest-bit-seqs n (+ 1 x) (inc-bit-seq lst)))
:else '()
)
)
(defn zero-seq [n]
(cond
(= n 0) '()
:else (cons 0 (zero-seq (- n 1)))
)
)
(defn all-bit-seqs [n]
(rest-bit-seqs n 0 (zero-seq n))
)
that is because you don't handle the case of an empty sequence here:
(defn inc-bit-seq [x]
(cond
(= 0 (not-bit (first x))) (cons 0 (inc-bit-seq (rest x)))
:else (cons 1 (rest x))
)
)
so at one point you pass (first x) (which is nil for an empty seq) to not-bit.
the following fix solves this:
(defn inc-bit-seq [x]
(when (seq x)
(cond
(= 0 (not-bit (first x))) (cons 0 (inc-bit-seq (rest x)))
:else (cons 1 (rest x))
))
)
in repl:
user> (all-bit-seqs 2)
((0 0) (1 0) (0 1) (1 1))
the other things that are totally weird here, is your choice of cond instead of simple if, and uncommon formatting. I would consider rewriting the code this way:
(defn not-bit [x]
(* -1 (- x 1)))
(defn inc-bit-seq [x]
(when (seq x)
(if (zero? (not-bit (first x)))
(cons 0 (inc-bit-seq (rest x)))
(cons 1 (rest x)))))
(defn pow [x, y]
(if-not (zero? y)
(* x (pow x (- y 1)))
1))
(defn rest-bit-seqs [n, x, lst]
(when-not (== x (pow 2 n))
(cons lst (rest-bit-seqs n (+ 1 x) (inc-bit-seq lst)))))
(defn zero-seq [n]
(when-not (zero? n)
(cons 0 (zero-seq (- n 1)))))
(defn all-bit-seqs [n]
(rest-bit-seqs n 0 (zero-seq n)))
(it's about style, haven't looked at overall code correctness)
Your error is here:
(= 0 (not-bit (first x)))
If x is empty, (first x) will return nil, so this will happen:
(= 0 (not-bit nil))
(= 0 (* -1 (- nil 1))
When you try to evaluate (- nil 1), you'll get a NullPointerException.
The quick-and-dirty way to fix the problem is to get rid of not-bit and replace that condition with
(= 1 (first x))
However, there are much shorter/simpler ways to solve this problem. Here's one way:
(defn inc-bit-seq [[head & tail]]
(if (zero? head)
(cons 1 tail)
(cons 0 (inc-bit-seq tail))))
(defn all-bit-seqs [n]
(take (bit-shift-left 1 n)
(iterate inc-bit-seq (repeat n 0))))
Another way would be to use strings:
(defn pad-first [n x xs]
(concat (repeat (- n (count xs)) x) xs))
(defn all-bit-seqs [n]
(map (comp (partial pad-first n 0)
(partial map #(Character/getNumericValue %))
#(Long/toBinaryString %))
(range (bit-shift-left 1 n))))

Adding values to a list in a sort of "overlapped" way

I'll explain in math, here's the transformation I'm struggling to write Scheme code for:
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
Where two letters together like ad means (* a d).
I'm trying to write it in a purely functional manner, but I'm struggling to see how. Any suggestions would be greatly appreciated.
Here are some examples:
(1mul '(0 1) '(0 1)) = '(0 0 1)
(1mul '(1 2 3) '(1 1)) = '(1 3 5 3)
(1mul '(1 2 3) '(1 2)) = '(1 4 7 6)
(1mul '(1 2 3) '(2 1)) = '(2 5 8 3)
(1mul '(1 2 3) '(2 2)) = '(2 6 10 6)
(1mul '(5 5 5) '(1 1)) = '(5 10 10 5)
(1mul '(0 0 1) '(2 5)) = '(0 0 2 5)
(1mul '(1 1 2 3) '(2 5)) = '(2 7 9 16 15)
So, the pattern is like what I posted at the beginning:
Multiply the first number in the list by every number in the second list (ad, ae, af) and then continue along, (bd, be, bf, cd, ce, cf) and arrange the numbers "somehow" to add the corresponding values. The reason I call it overlapping is because you can sort of visualize it like this:
(list
aa'
(+ ba' ab')
(+ ca' bb' ac')
(+ cb' bc')
cc')
Again,
(f '(a b c) '(d e f)) = '(ad (+ bd ae) (+ cd be af) (+ ce bf) cf)
However, not just for 3x3 lists, for any sized lists.
Here's my code. It's in racket
#lang racket
(define (drop n xs)
(cond [(<= n 0) xs]
[(empty? xs) '()]
[else (drop (sub1 n) (rest xs))]))
(define (take n xs)
(cond [(<= n 0) '()]
[(empty? xs) '()]
[else (cons (first xs) (take (sub1 n) (rest xs)))]))
(define (mult as bs)
(define (*- a b)
(list '* a b))
(define degree (length as))
(append
(for/list ([i (in-range 1 (+ 1 degree))])
(cons '+ (map *- (take i as) (reverse (take i bs)))))
(for/list ([i (in-range 1 degree)])
(cons '+ (map *- (drop i as) (reverse (drop i bs)))))))
The for/lists are just ways of mapping over a list of numbers and collecting the result in a list. If you need, I can reformulate it just maps.
Is this a good candidate for recursion? Not sure, but here's a
a direct translation of what you asked for.
(define (f abc def)
(let ((a (car abc)) (b (cadr abc)) (c (caddr abc))
(d (car def)) (e (cadr def)) (f (caddr def)))
(list (* a d)
(+ (* b d) (* a e))
(+ (* c d) (* b e) (* a f))
(+ (* c e) (* b f))
(* c f))))
Is it correct to assume, that you want to do this computation?
(a+b+c)*(d+e+f) = a(d+e+f) + b(d+e+f) + c(d+e+f)
= ad+ae+af + bd+be+bf + cd+ce+cf
If so, this is simple:
(define (f xs ys)
(* (apply + xs) (apply + ys))
If you are interested in the symbolic version:
#lang racket
(define (f xs ys)
(define (fx x)
(define (fxy y)
(list '* x y))
(cons '+ (map fxy ys)))
(cons '+ (map fx xs)))
And here is a test:
> (f '(a b c) '(d e f))
'(+ (+ (* a d) (* a e) (* a f))
(+ (* b d) (* b e) (* b f))
(+ (* c d) (* c e) (* c f)))

putting clojure code in a loop

(def throws 10)
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(loop [hits 0]
(let [x (rand)
y (rand)]
; still inside the let
(if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
(recur (inc hits))
(* 4 (/ hits throws)))))
I got that code working and running until the if condition is true. How can I rewrite it so it takes X as parameter and runs X times?
I basically want to call (r-squared 100) and get how many hits I got as return value.
I think this is what you want, if understend question correctly.
(defn test [n]
(loop [hits 0 n n]
(let [x (rand)
y (rand)]
(if (< n 0)
hits ;// you can put (* 4 (/ hits throws)) here
(if (< (r-squared x y) 0.25)
(recur (inc hits) (dec n))
(recur hits (dec n)))))))
Didn't eval it, so parn might be slightly wrong.
(def throws 10)
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(defn test-r-squared [n]
(loop [hits (int 0) n (int n)]
(let [x (rand)
y (rand)]
; still inside the let
(if (< n 0)
(* 4 (/ hits throws))
(if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
(recur (inc hits) (dec n))
(recur hits (dec n)))))))
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(defn hit[]
(let [x (rand) y (rand)]
(< (r-squared x y) 0.25)))
(frequencies (for [i (range 1000)] (hit))) ; {true 787, false 213}

Project Euler #9 (Pythagorean triplets) in Clojure

My answer to this problem feels too much like these solutions in C.
Does anyone have any advice to make this more lispy?
(use 'clojure.test)
(:import 'java.lang.Math)
(with-test
(defn find-triplet-product
([target] (find-triplet-product 1 1 target))
([a b target]
(let [c (Math/sqrt (+ (* a a) (* b b)))]
(let [sum (+ a b c)]
(cond
(> a target) "ERROR"
(= sum target) (reduce * (list a b (int c)))
(> sum target) (recur (inc a) 1 target)
(< sum target) (recur a (inc b) target))))))
(is (= (find-triplet-product 1000) 31875000)))
The clojure-euluer-project has several programs for you to reference.
I personally used this algorithm(which I found described here):
(defn generate-triple [n]
(loop [m (inc n)]
(let [a (- (* m m) (* n n))
b (* 2 (* m n)) c (+ (* m m) (* n n)) sum (+ a b c)]
(if (>= sum 1000)
[a b c sum]
(recur (inc m))))))
Seems to me much less complicated :-)