Last active
December 26, 2020 13:36
-
-
Save ryanivandsouza/52c6b05a3fcc608ac85cbf9a0eef439b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
next' n x = (x + n / x) / 2 | |
-- [a0, f a0, f(f a0), f(f(f a0)), …] | |
repeat' f a = a : repeat' f (f a) | |
-- get approximations for square root of n by repeat' (next' n) a0 | |
within' (eps a1:a2:rest) | |
| abs(a1 – a2) < eps = a2 | |
| otherwise = within' eps (a2:rest) | |
sqrt' n a0 eps = within' eps $ repeat' (next' n) a0 | |
relative' eps a1:a2:rest | |
| abs(a1 – a2) < eps*(abs a2) = a2 | |
| otherwise = relative' eps a2:rest | |
relativeSqrt' n a0 eps = relative' eps $ repeat' (next' n) a0 | |
easydiff' f x h = ((f (x+h)) - (f x)) / h | |
halve' x = x / 2 | |
differentiate' h0 f x = map (easydiff' f x) (repeat' halve h0) | |
within' eps $ differentiate' h0 f x | |
elimerror' n (a1:a2:rest) = | |
((a2*(2**n) – a1) / (2**n - 1)) : elimerror' n (a2:rest) | |
order' a1:a2:a3:rest = round $ logBase 2 ((a1-a3)/(a2-a3) - 1) | |
improve' s = elimerror' (order' s) s | |
within' eps $ improve' $ differentiate' h0 f x | |
--or better | |
within' eps $ improve' $ improve' $ improve' $ differentiate' h0 f x | |
second' a1:a2:rest = a2 | |
super' s = map second' (repeat' improve' s) | |
within' eps $ super' $ differentiate' h0 f x | |
easyintegrate' f a b = ((f a) + (f b))*(b-a) / 2 | |
addpair' (a b) = a + b | |
zip' (a:as) (b:bs) = (a b) : (zip' as bs) | |
integrate' f a b = (easyintegrate' f a b) : | |
(map addpair (zip' (integrate' f a mid) | |
(integrate' f mid b))) | |
where mid = (a + b) / 2 | |
within' eps $ integrate' f a b | |
relative' eps $ integrate' f a b | |
integrate' f a b = (easyintegrate' f a b) : | |
(map addpair (zip' (integrate' f a mid) | |
(integrate' f mid b))) | |
where mid = (a + b) / 2 | |
integrate'' f a b = integ' f a b (f a) (f b) | |
integ' f a b fa fb = ((fa+fb) * (b-a) / 2) : | |
(map addpair (zip' (integrate'' f a m fa fm) | |
(integrate'' f m b fm fb))) | |
where mid = (a + b) / 2 | |
fm = f mid | |
super' (integrate sin 0 4) | |
improve' (integrate f 0 1) where f x = 1/(1 + x*x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nil = undefined | |
var cons = x => xs => s => s(x)(xs) | |
var head = p => p(x => xs => x) | |
var tail = p => p(x => xs => xs) | |
var compose = f => g => x => f(g(x)) | |
var shead = head | |
var stail = stream => tail(stream)() | |
var sfold = f => stream => f(shead(stream))(() => sfold(f)(stail(stream))) | |
var smap = f => sfold(compose(cons)(f)) | |
sput = n => xs => { | |
if(n > 0) { | |
console.log(shead(xs)) | |
sput(n - 1)(stail(xs)) | |
} | |
} | |
var repeat = n => cons(n)(() => repeat(n)) | |
sput(4)(repeat(1)) | |
var nexta = n => x => (x + n/x)/2 | |
var repeata = f => a => cons(a)(() => repeata(f)(f(a))) | |
var abs = x => x < 0 ? -x : x | |
var closeenough = delta => eps => guesses => { | |
return delta(shead(guesses))(shead(stail(guesses))) <= eps | |
? shead(stail(guesses)) | |
: closeenough(delta)(eps)(stail(guesses)) | |
} | |
var within = closeenough(x => y => abs(x - y)) | |
var sqrt = eps => n => a0 => within(eps)(repeata(nexta(n))(a0)) | |
console.log(sqrt(0.001)(2)(1)) | |
var relative = closeenough(x => y => abs(x/y - 1)) | |
var sqrtRelative = eps => n => a0 => relative(eps)(repeata(nexta(n))(a0)) | |
console.log(sqrtRelative(0.001)(2)(1)) | |
var square = x => x * x | |
var easydiff = f => x => h => (f(x + h) - f(x))/h | |
var halve = x => x / 2 | |
var differentiate = h0 => f => x => smap(easydiff(f)(x))(repeata(halve)(h0)) | |
var derivative = within(0.000001)(differentiate(1)(square)(3)) | |
console.log(derivative) | |
var eliminateError = n => xs => cons((shead(stail(xs)) * Math.pow(2, n) - shead(xs))/(Math.pow(2, n) - 1)) | |
(() => eliminateError(n)(stail(xs))) | |
var order = xs => { | |
var a = shead(xs) | |
var b = shead(stail(xs)) | |
var c = shead(stail(stail(xs))) | |
return Math.round(Math.log2((a - c)/(b - c) - 1)) || 1 | |
} | |
var improve = xs => eliminateError(order(xs))(xs) | |
var derivativeImproved = within(0.0001)(improve(differentiate(1)(x => x * x * x)(4))) | |
console.log(derivativeImproved) | |
var second = compose(shead)(stail) | |
console.log(second(repeata(x => x + 1)(0))) | |
var superb = xs => smap(second)(repeata(improve)(xs)) | |
var derivativeSuperb = within(0.0001)(superb(differentiate(1)(x => x * x * x)(4))) | |
console.log(derivativeSuperb) | |
var easyintegrate = f => a => b => (f(a) + f(b))*(b - a)/2 | |
console.log(easyintegrate(x => x)(1)(2)) | |
var addPair = p => head(p) + tail(p) | |
console.log(addPair(cons(2)(5))) | |
var showPair = p => "(" + head(p) + "," + tail(p) + ")" | |
var zip = xs => ys => cons(cons(shead(xs))(shead(ys))) | |
(() => zip(stail(xs))(stail(ys))) | |
// sput(3)(smap(addPair)(zip(repeat(1))(repeat(2)))) | |
var mid = a => b => (a + b) / 2 | |
var integrate = f => a => b => cons(easyintegrate(f)(a)(b)) | |
(() => smap(addPair)(zip(integrate(f)(a)(mid(a)(b))) | |
(integrate(f)(mid(a)(b))(b)))) | |
//sput(4)(integrate(x => x*x)(1)(2)) | |
var integration = within(0.0001)(superb(integrate(x => x*x)(1)(2))) | |
console.log(integration) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment