Skip to content

Commit 9d18825

Browse files
committed
- implemented simplex sampling in Nim and WOW it works! such a nice language!
1 parent 01a36a7 commit 9d18825

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

nimplex.nim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyrigth (C) 2023 Adam M. Krajewski
2+
3+
import std/math
4+
import arraymancer
5+
import strutils
6+
7+
proc simplex_grid*(m: int,
8+
n: int): Tensor[int] =
9+
let L: int = binom(n+m-1, m-1)
10+
result = newTensor[int]([L, m])
11+
var x = zeros[int](m)
12+
x[m-1] = n
13+
for j in 0..m-1:
14+
result[0, j] = x[j]
15+
var h = m
16+
for i in 1..L-1:
17+
h -= 1
18+
let val = x[h]
19+
x[h] = 0
20+
x[m-1] = val - 1
21+
x[h-1] += 1
22+
for j in 0..m-1:
23+
result[i, j] = x[j]
24+
if val != 1:
25+
h = m
26+
return result
27+
28+
echo "Simplex dimensions:"
29+
let m = readLine(stdin).parseInt()
30+
31+
echo "N divisions:"
32+
let n = readLine(stdin).parseInt()
33+
34+
echo simplex_grid(m, n)

0 commit comments

Comments
 (0)