-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
88 lines (79 loc) · 1.83 KB
/
utils.js
File metadata and controls
88 lines (79 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// https://stackoverflow.com/questions/15313418/what-is-assert-in-javascript
function assert (condition, message) {
if (!condition) {
message = message || 'Assertion failed'
if (typeof Error !== 'undefined') {
throw new Error(message)
}
throw message
}
}
// Random numbers utils
var returnValue = false
var vValue = 0.0
function gaussRandom () {
if (returnValue) {
returnValue = false
return vValue
}
var u = 2 * Math.random() - 1
var v = 2 * Math.random() - 1
var r = u * u + v * v
if (r === 0 || r > 1) return gaussRandom()
var c = Math.sqrt(-2 * Math.log(r) / r)
vValue = v * c // cache this
returnValue = true
return u * c
}
function randf (a, b) {
return Math.random() * (b - a) + a
}
function randi (a, b) {
return Math.floor(Math.random() * (b - a) + a)
}
function randn (mu, std) {
return mu + gaussRandom() * std
}
// helper function returns array of zeros of length n
// and uses typed arrays if available
function zeros (n) {
if (typeof (n) === 'undefined' || isNaN(n)) { return [] }
if (typeof ArrayBuffer === 'undefined') {
// lacking browser support
var arr = new Array(n)
for (var i = 0; i < n; i++) { arr[i] = 0 }
return arr
} else {
return new Float64Array(n)
}
}
var sampleWeighted = function (p) {
var r = Math.random()
var c = 0.0
for (var i = 0, n = p.length; i < n; i++) {
c += p[i]
if (c >= r) { return i }
}
assert(false, 'wtf')
}
var setConst = function (arr, c) {
for (var i = 0, n = arr.length; i < n; i++) {
arr[i] = c
}
}
function getopt (opt, fieldName, defaultValue) {
if (typeof opt === 'undefined') {
return defaultValue
}
return (typeof opt[fieldName] !== 'undefined') ? opt[fieldName] : defaultValue
}
module.exports = {
assert,
randf,
randi,
randn,
zeros,
setConst,
getopt,
sampleWeighted
}