-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathLib.js
143 lines (138 loc) · 4.48 KB
/
MathLib.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function extend(target, source) {
target = target || {};
for (var prop in source) {
if (typeof source[prop] === 'object') {
target[prop] = extend(target[prop], source[prop]);
} else {
target[prop] = source[prop];
}
}
return target;
}
var MathLib = (function() {
var log10Constant = Math.log(10.0);
return {
niceNumber: function(x, round) {
var exp;
var frac;
var niceFrac;
exp = Math.floor(this.log10(x));
frac = x / Math.pow(10.0, exp);
if (round) {
if (frac < 1.5) {
niceFrac = 1.0;
} else if (frac < 3.0) {
niceFrac = 2.0;
} else if (frac < 7.0) {
niceFrac = 5.0;
} else {
niceFrac = 10.0;
}
} else {
if (frac <= 1.0001) {
niceFrac = 1.0;
} else if (frac <= 2.0001) {
niceFrac = 2.0;
} else if (frac <= 5.0001) {
niceFrac = 5.0;
} else {
niceFrac = 10.0;
}
}
return niceFrac * Math.pow(10.0, exp);
},
log10: function(x) {
return Math.log(x) / log10Constant;
},
niceLabels: function(nLabel, minLabel, maxLabel) {
var label = new Array(nLabel);
var nfrac;
var d;
var graphmin, graphmax;
var range, x;
range = niceNumber(maxLabel - minLabel, true);
d = niceNumber(range / (nLabel - 1), true);
graphmin = Math.floor(minLabel / d) * d;
graphmax = Math.ceil(maxLabel / d) * d;
nfrac = Math.max(Math.floor(this.log10(d)), 0);
for (var i = 0; i < nLabel; i++) {
x = graphmin + i * d;
label[i] = x;
}
return label;
},
niceLogLabels: function(nLabel, minLabel, maxLabel) {
var minLogLabel = this.log10(minLabel);
var maxLogLabel = this.log10(maxLabel);
var niceLogLabel = this.niceLabels(nLabel, minLogLabel, maxLogLabel);
for (var i = 0; i < nLabel; i++) {
niceLogLabel[i] = MathLib.niceNumber(
Math.pow(10.0, niceLogLabel[i]), false);
}
return niceLogLabel;
},
cartesianToR: function(x, y, z) {
return Math.sqrt(x * x + y * y + z * z);
},
cartesianToPhi: function(x, y, z) {
if (arguments.length == 2) {
if (y >= 0.0) {
return Math.acos(y / x);
} else {
return -Math.acos(y / x);
}
} else {
if (z >= 0.0) {
return Math.acos(z / this.cartesianToR(x, y, z));
} else {
return -Math.acos(z / this.cartesianToR(x, y, z));
}
}
},
cartesianToTheta: function(x, y) {
var pi = 4.0 * Math.atan(1.0);
if (x == 0.0) {
if (y >= 0.0)
return pi / 2.0;
else
return -pi / 2.0;
} else if (y == 0.0) {
if (x >= 0.0)
return 0.0;
else
return pi;
} else {
if (x >= 0.0)
return Math.atan(y / x);
else
return Math.atan(y / x) + pi;
}
}
, linearGrid: function(n, min, max) {
var diff = (max - min) / (n - 1);
var retval = new Array(n);
retval[0] = min;
retval[n - 1] = max;
for (var i = 1; i < n - 1; i++) {
retval[i] = retval[0] + diff * i;
}
return retval;
},
factorial: function(n) {
var retval = 1;
for (var i = 2; i <= n; i++) {
retval *= i;
}
return retval;
},
doubleFactorial: function(l) {
var retval = 1;
for (var i = l; i >= 2; i -= 2)
retval *= i;
return retval;
},
combination: function(n, k) {
return this.factorial(n) / (this.factorial(k) * this.factorial(n - k));
}
};
})();