diff --git a/wip/nim/makefile b/wip/nim/makefile new file mode 100644 index 00000000..236cffdd --- /dev/null +++ b/wip/nim/makefile @@ -0,0 +1,2 @@ +build: samples.nim + nim c samples.nim diff --git a/wip/nim/samples b/wip/nim/samples index 724e5cc0..bf7b6bf9 100755 Binary files a/wip/nim/samples and b/wip/nim/samples differ diff --git a/wip/nim/samples.nim b/wip/nim/samples.nim index 68aba385..1fc5d3b3 100644 --- a/wip/nim/samples.nim +++ b/wip/nim/samples.nim @@ -1,32 +1,64 @@ -import std/random import std/math +import std/random + # randomize() -proc pow2(x:float, y:int): float = - return pow(x, float(y)) +## Basic math functions +proc factorial(n: int): int = + if n == 0 or n < 0: + return 1 + else: + return n * factorial(n - 1) proc sine(x: float): float = - let n = 100 + let n = 8 + # ^ Taylor will converge really quickly + # notice that the factorial of 17 is + # already pretty gigantic var acc = 0.0 for i in 0..n: - let k = 2*n + 1 - let taylor = pow2(x, k) / float(k) + var k = 2*i + 1 + var taylor = pow(-1, i.float) * pow(x, k.float) / factorial(k).float acc = acc + taylor - return acc + return acc + +# Helpers for calculating the log function + +## Arithmetic-geomtric mean +proc ag(x: float, y: float): float = + let n = 100 + var a = (x + y)/2.0 + var b = sqrt(x * y) + for i in 0..n: + let temp = a + a = (a+b)/2.0 + b = sqrt(b*temp) + return a + +## Find m such that x * 2^m > 2^100 + +proc log_slow(x: float): float = + # See: + var y = x - 1 + let n = 100000000 + var acc = 0.0 + for i in 1..n: + let taylor = pow(-1.0, float(i+1)) * pow(y, i.float) / i.float + acc = acc + taylor + return acc proc log(x: float): float = - var y = x - 1 - let n = 1000 - for i in 1..n: - let taylor = pow2(-1.0, n+1) * pow2(y, n) / float(n) - result = result + taylor - return result + return 1 +## Test these functions +echo factorial(5) +echo sine(1.0) +echo log(1.0) +echo log(2.0) + +## Distribution functions proc normal(): float = let u1 = rand(1.0) let u2 = rand(1.0) let z = 1 # see https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform#Basic_form - - -echo sine(0.1) diff --git a/wip/nim/sums/sums.nim b/wip/nim/sums/sums.nim index a49fe312..aac34608 100644 --- a/wip/nim/sums/sums.nim +++ b/wip/nim/sums/sums.nim @@ -1,5 +1,4 @@ import std/math -# randomize() proc factorial(n: int): int = if n == 0 or n < 0: @@ -19,5 +18,5 @@ proc sine(x: float): float = acc = acc + taylor return acc -# echo factorial(17) +echo factorial(17) echo sine(1.0)