-
Notifications
You must be signed in to change notification settings - Fork 13
/
lib.js
154 lines (140 loc) Β· 4.2 KB
/
lib.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
144
145
146
147
148
149
150
151
152
153
154
module.exports = class Calc {
limRightOf(x, func) {
if (this.canPlugin(x, func)) {
return func(x);
}
if (Math.abs(x) == Infinity) {
return this.toInfinty(x, func);
}
var right1 = func(x + 0.000000000000001);
var right2 = func(x + 0.00000000000001);
var right3 = func(x + 0.0000000000001);
var rightDif1 = right2 - right1;
var rightDif2 = right3 - right2;
if (rightDif1 < rightDif2 && rightDif2 < 0) {
return Infinity;
}
if (rightDif1 > rightDif2 && rightDif2 > 0) {
return -1 * Infinity;
}
return this.round(right1);
}
limLeftOf(x, func) {
if (this.canPlugin(x, func)) {
return func(x);
}
if (Math.abs(x) == Infinity) {
return this.toInfinty(x, func);
}
var left1 = func(x - 0.000000000000001);
var left2 = func(x - 0.00000000000001);
var left3 = func(x - 0.0000000000001);
var leftDif1 = left2 - left3;
var leftDif2 = left1 - left2;
if (leftDif2 > leftDif1 && leftDif2 > 0) {
return Infinity;
}
if (leftDif2 < leftDif1 && leftDif2 < 0) {
return -1 * Infinity;
}
return this.round(left1);
}
limAt(x, func) {
if (this.canPlugin(x, func)) {
return func(x);
}
if (Math.abs(x) == Infinity) {
return this.toInfinty(x, func);
}
var left1 = func(x - 0.000000000000001);
var right1 = func(x + 0.000000000000001);
if (Math.abs(left1 - right1) < 0.00001) {
return this.round((left1 + right1) / 2);
}
return NaN;
}
canPlugin(x, func) {
var at = func(x);
return at === at && Math.abs(at) != Infinity;
}
toInfinty(x, func) {
if (x > 0) {
var pos1 = Number.MAX_VALUE * 0.99999;
var pos2 = Number.MAX_VALUE;
var dif = pos2 - pos1;
if (dif > 0) {
return Infinity;
} else {
return -1 * Infinity;
}
} else {
var pos1 = Number.MIN_VALUE;
var pos2 = Number.MIN_VALUE * 0.99999;
var dif = pos2 - pos1;
if (dif < 0) {
return Infinity;
} else {
return -1 * Infinity;
}
}
}
deriv(x1, func) {
var at = func(x1);
if (Math.abs(at) == Infinity || at !== at) {
return NaN;
}
var y1 = func(x1);
var x0 = x1 - 0.000000000000001;
var y0 = func(x0);
var x2 = x1 + 0.000000000000001;
var y2 = func(x2);
var slope1 = this.slope(x0, y0, x1, y1);
var slope2 = this.slope(x1, y1, x2, y2);
if (Math.abs(slope1 - slope2) > 0.1) {
return NaN;
}
return (slope1 + slope2) / 2;
}
nthDeriv(n, x1, func) {
var vals = [];
var start = -1 * Math.round(n / 2);
for (var i = start; i <= n + start + 1; i++) {
var newX = x1 + i * 0.000000000000001;
var newY = func(newX);
vals.push(newY);
}
for (var i = 0; i < n; i++) {
var diffs = [];
for (var j = 1; j < vals.length; j++) {
diffs.push(vals[j] - vals[j - 1]);
}
vals = diffs;
}
var out = (vals[0] + vals[1]) / 0.000000000000002;
return out;
}
integral(min, max, func, num) {
var sum = 0;
var dx = (max - min) / num;
var currentX = min + dx / 2;
for (var i = 0; i < num; i++) {
var currentY = func(currentX);
sum += dx * currentY;
currentX += dx;
}
return sum;
}
averageValue(min, max, func, num) {
return this.integral(min, max, func, num) / (max - min);
}
distance(x1, y1, x2, y2) {
return Math.sqrt((x1 - x2) * (x1 - x2) - (y1 - y2) * (y1 - y2));
}
slope(x1, y1, x2, y2) {
return (y1 - y2) / (x1 - x2);
}
round(num) {
var factor = 100000000000000;
return Math.round(num * factor) / factor;
}
}