-
Notifications
You must be signed in to change notification settings - Fork 1
/
circular.ts
362 lines (353 loc) · 10.7 KB
/
circular.ts
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { Component } from "../component";
import { MathContext } from "../context";
import { UndefinedValue } from "../../errors";
import { Numerical } from "../../definitions";
export namespace TrigCyclic {
/**
* Calculates the trigonometric sine with rounding according to the given
* context.
*
* **Method**:
*
* For \\( x < 2\pi \\)
* The Taylor series converges for all \\( x \\).
*
* \\[ \sin x = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n+1}}{(2n+1)!} \\]
*
* For \\( x \geqslant 2\pi \\), range reduction can be performed.
* The \\( \sin x \\) function has a periodicity of \\( 2\pi \\).
*
* \\[ x \equiv f \pmod{2\pi} \\]
* @param x A number.
* @param context The context settings to use.
*/
export function sin(x: Component, context: MathContext) {
const ctx: MathContext = {
precision: 2 * context.precision,
rounding: context.rounding
};
x = x.mod(Component.TWO.mul(Component.PI, ctx), ctx);
if(Component.abs(x, context).equals(Component.PI, context))
return Component.ZERO;
const x_sq = x.mul(x, ctx);
let sum = Component.ZERO;
let term = x;
let n = 0;
while(true) {
sum = sum.add(term, ctx);
const f = Component.create((2 * n + 2) * (2 * n + 3));
const term1 = term.mul(x_sq, ctx).div(f, ctx).neg;
if(term1.equals(Component.ZERO, ctx))
return Component.round(sum, context);
term = term1;
n++;
}
}
/**
* Calculates the trigonometric cosine with rounding according to the given
* context.
*
* **Method**:
*
* For \\( x < 2\pi \\)
* The Taylor series converges for all \\( x \\).
*
* \\[ \cos x = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n}}{(2n)!} \\]
*
* For \\( x \geqslant 2\pi \\), range reduction can be performed.
* The \\( \cos x \\) function has a periodicity of \\( 2\pi \\).
*
* \\[ x \equiv f \pmod{2\pi} \\]
* @param x A number.
* @param context The context settings to use.
*/
export function cos(x: Component, context: MathContext) {
const ctx: MathContext = {
precision: 2 * context.precision,
rounding: context.rounding
};
x = x.mod(Component.TWO.mul(Component.PI, ctx), ctx);
const x_sq = x.mul(x, ctx);
let sum = Component.ZERO;
let term = Component.ONE;
let n = 0;
while(true) {
sum = sum.add(term, ctx);
const f = Component.create((2*n+1) * (2*n+2));
const term1 = term.mul(x_sq, ctx).div(f, ctx).neg;
if(term1.equals(Component.ZERO, ctx))
return Component.round(sum, context);
term = term1;
n++;
}
}
/**
* Calculates the trigonometric tangent with rounding according to the given context.
*
* **Method**:
*
* \\[ \tan x = \frac{\sin x}{\cos x} \\]
*
* @param x A number.
* @param context The context settings to use.
*/
export function tan(x: Component, context: MathContext) {
const ctx: MathContext = {
precision: 2 * context.precision,
rounding: context.rounding
};
const res = sin(x, ctx).div(cos(x, ctx), ctx);
return Component.round(res, context);
}
/**
* Computes the inverse trigonometric sine for \\( x \\) (\\( |x| < 0.5 \\))
* with rounding according to the given context settings.
* @param x A number.
* @param context The context settings to use.
* @ignore
*/
function asin_less(x: Component, context: MathContext) {
const ctx: MathContext = {
precision: 2 * context.precision,
rounding: context.rounding
};
const x_sq = x.mul(x, ctx);
let sum = Component.ZERO;
let term = x;
let temp = x;
let f = Component.ONE;
let n = 0;
while(true) {
sum = sum.add(temp, ctx);
const f1 = Component.create(2*n + 3);
const fac = f.div(Component.create(2*n+2), ctx);
const term1 = term.mul(x_sq, ctx).mul(fac, ctx);
const temp1 = term1.div(f1, ctx);
if(temp1.equals(Component.ZERO, ctx))
return Component.round(sum, context);
f = f1;
term = term1;
temp = temp1;
n++;
}
}
/**
* Calculates the inverse trigonometric sine of a number with rounding
* according to the given context.
*
* **Method**:
*
* If \\( x < 0.5 \\)
* use the definition from integration:
*
* \\[ \sin^{-1} x = \int_0^x \frac{dt}{\sqrt{1-t^2}} \\]
*
* Since \\( x < 1 \\)
*
* \\[ \sin^{-1} = \sum_{n=0}^\infty \frac{(2n-1)!!}{2^n n!} \cdot \frac{x^{2n+1}}{2n+1} \\]
*
* If \\( x \geqslant 0.5 \\)
*
* \\[ \sin^{-1} x = \frac{\pi}{2} - \sin^{-1} \sqrt{\frac{1-x}{2}} \\]
*
* @param x A number.
* @param context The context settings to use.
*/
export function asin(x: Component, context: MathContext): Component {
if(Component.abs(x, context).moreThan(Component.ONE))
throw new UndefinedValue("asin (for reals)", x);
if(x.lessThan(Component.ZERO))
return asin(x.neg, context).neg;
const half = Component.create("0.5");
if(x.lessThan(half))
return asin_less(x, context);
const ctx: MathContext = {
precision: context.precision + 5,
rounding: context.rounding
};
const piBy2 = Component.PI.div(Component.TWO, ctx);
const z = Component.ONE.sub(x, ctx).div(Component.TWO, ctx);
const s = z.pow(half, ctx);
const temp = asin_less(s, ctx);
const res = piBy2.sub(Component.TWO.mul(temp, ctx), ctx);
return Component.round(res, context);
}
/**
* Calculates the inverse trigonometric cosine of a number with rounding
* according to the given context.
*
* **Method**:
*
* If \\( \lvert x \rvert < 0.5 \\),
* \\[ \cos^{-1} x = \frac{\pi}{2} - \sin^{-1} x \\]
*
* otherwise,
* \\[ \cos^{-1} x = 2 \sin^{-1} \sqrt{\frac{1-x}{2}} \\]
*
* @param x A number.
* @param context The context settings to use.
* @see {@link asin}
*/
export function acos(x: Component, context: MathContext) {
if(Component.abs(x, context).moreThan(Component.ONE))
throw new UndefinedValue("acos (for reals)", x);
const ctx: MathContext = {
precision: context.precision + 5,
rounding: context.rounding
};
const half = Component.create("0.5");
if(Component.abs(x, context).lessThan(half)) {
const res = Component.PI.mul(half, ctx).sub(asin_less(x, ctx), ctx);
return Component.round(res, context);
}
const z = Component.ONE.sub(x, ctx).div(Component.TWO, ctx);
const s = z.pow(half, ctx);
const temp = asin(s, ctx);
const res = Component.TWO.mul(temp, ctx);
return Component.round(res, context);
}
/**
* Calculates the inverse trigonometric tangent of a number (\\( x < 1 \\)).
*
* **Method**:
*
* \\[ \tan^{-1} x = \int_0^x \frac{1}{1+t^2} dt \\]
* Since \\( x < 1 \\)
* \\[ \tan^{-1} = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n+1}}{2n+1} \\]
*
* @param x A number.
* @param context The context settings to use.
* @ignore
*/
function atan_less(x: Component, context: MathContext) {
const ctx: MathContext = {
precision: 2 * context.precision,
rounding: context.rounding
};
const x_sq = x.mul(x, ctx);
let sum = Component.ZERO;
let temp = x;
let term = x;
let n = 0;
while(true) {
sum = sum.add(term, ctx);
const temp1 = temp.mul(x_sq, ctx).neg;
const f = Component.create(2*n + 3);
const term1 = temp1.div(f, ctx);
if(term1.equals(Component.ZERO, ctx))
return Component.round(sum, context);
temp = temp1;
term = term1;
n++;
}
}
/**
* Calculates the inverse trigonometric tangent of a number with rounding
* according to the given context.
*
* **Method**:
*
* The input can be divided into 4 regions for fast convergence.
*
* 1. \\( 0 \leqslant x < \sqrt{2}-1 \\):
*
* \\[ \tan^{-1} = \sum_{n=0}^{\infty} (-1)^n \frac{x^{2n+1}}{2n+1} \\]
*
* 2. \\( \sqrt{2}-1 \leqslant x < 1 \\):
*
* \\[ \tan^{-1} x = \frac{\pi}{4} - \tan^{-1} \left( \frac{1-x}{1+x} \right) \\]
*
* 3. \\( 1 \leqslant x < \sqrt{2}+1 \\):
*
* \\[ \tan^{-1} x = \frac{\pi}{4} + \tan^{-1} \left( \frac{x-1}{x+1} \right) \\]
*
* 4. \\( x \geqslant \sqrt{2}+1 \\):
*
* \\[ \tan^{-1} x = \frac{\pi}{4} + \tan^{-1} \left( \frac{1}{x} \right) \\]
*
* @param x A number.
* @param context The context settings to use.
*/
export function atan(x: Component, context: MathContext): Component {
if(x.equals(Component.ZERO, context))
return Component.ZERO;
if(x.lessThan(Component.ZERO))
return atan(x.neg, context).neg;
const ctx: MathContext = {
precision: context.precision + 5,
rounding: context.rounding
};
const limit1 = Component.create("0.414213562373");
const limit2 = Component.ONE;
const limit3 = Component.create("2.414213562373");
let referenceValue: Component;
let less: Component;
let sign: -1 | 1;
if(x.lessThan(limit1)) {
less = x;
referenceValue = Component.ZERO;
sign = 1;
} else if(x.lessThan(limit2)) {
const num = Component.ONE.sub(x, ctx);
const den = Component.ONE.add(x, ctx);
less = num.div(den, ctx);
referenceValue = Component.PI.div(Component.FOUR, ctx);
sign = -1;
} else if(x.lessThan(limit3)) {
const num = x.sub(Component.ONE, ctx);
const den = x.add(Component.ONE, ctx);
less = num.div(den, ctx);
referenceValue = Component.PI.div(Component.FOUR, ctx);
sign = 1;
} else {
less = Component.ONE.div(x, ctx);
referenceValue = Component.PI.div(Component.TWO, ctx);
sign = -1;
}
const res = sign === 1? referenceValue.add(atan_less(less, ctx), ctx):
referenceValue.sub(atan_less(less, ctx), ctx);
return Component.round(res, context);
}
/**
* Calculates the solution for \\( \theta \\) for the set of equations
*
* \\[ \begin{align}
* x &= r \cos \theta \\\\
* y &= r \sin \theta
* \end{align} \\]
*
* Put simply, the above boils down to
* \\[ \operatorname{atan2}(y, x) =
* \begin{cases}
* \arctan(\frac{y}{x}) &\text{if } x > 0, \\\\
* \arctan(\frac{y}{x}) + \pi &\text{if } x < 0 \text{ and } y \geqslant 0, \\\\
* \arctan(\frac{y}{x}) - \pi &\text{if } x < 0 \text{ and } y < 0, \\\\
* +\frac{\pi}{2} &\text{if } x = 0 \text{ and } y > 0, \\\\
* -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y < 0, \\\\
* \text{undefined} &\text{if } x = 0 \text{ and } y = 0
* \end{cases} \\]
*
* @param y The vertical component.
* @param x The horizontal component.
* @param context The context settings to use.
*/
export function atan2(y: Component, x: Component, context: MathContext) {
const yComp = y.compareTo(Component.ZERO);
const xComp = x.compareTo(Component.ZERO);
if(xComp === 0 && yComp === 0)
throw new UndefinedValue("atan2", <Numerical><unknown>[0, 0]);
const pi = Component.PI;
const two = Component.TWO;
if(xComp === 0)
return yComp === -1? pi.div(two, context).neg: pi.div(two, context);
const ctx: MathContext = {
precision: context.precision + 5,
rounding: context.rounding
};
const arg = y.div(x, ctx);
if(xComp === 1) return atan(arg, context);
const value = atan(arg, ctx);
const res = yComp === -1? value.sub(pi, ctx): value.add(pi, ctx);
return Component.round(res, context);
}
}