-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigonometry.d
More file actions
558 lines (472 loc) · 14.1 KB
/
Copy pathtrigonometry.d
File metadata and controls
558 lines (472 loc) · 14.1 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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
* This extends the `std.math.trigonometry` Phobos module to support `GDN` objects.
*/
module ad.math.trigonometry;
static import core.math;
static import std.math.trigonometry;
import std.math : abs, isFinite, pow, sqrt;
import std.meta : allSatisfy, anySatisfy;
import std.traits : Select;
static import ad.core.math;
import ad;
import ad.internal : asGDN, CommonGDN, isConvertibleToGDN, isFinite, isGDN, isInfinity, isNaN, pow;
import ad.core.math : sqrt;
/**
* This function computes the sine of its argument.
*
* If $(MATH f(x) = sin(g(x))), then $(MATH f' = cos(g)g').
*
* Params:
* Deg = the degree of `g`
* g = the `GDN` to compute the sine of
*
* Returns:
* the sine expressed as a `GDN`
*/
pragma(inline, true) pure nothrow @nogc @safe GDN!Deg sin(ulong Deg)(in GDN!Deg g)
do {
return ad.core.math.sin(g);
}
/***/ unittest {
assert(sin(GDN!1(0)) is GDN!1(0, 1));
}
/**
* This function computes the cosine of the argument.
*
* If $(MATH f(x) = cos(g(x))), then $(MATH f' = -sin(g)g').
*
* Params:
* Deg = the degree of g
* g = the `GDN` to compute the cosine of
*
* Returns:
* the cosine of g
*/
pure nothrow @nogc @safe GDN!Deg cos(ulong Deg)(in GDN!Deg g)
do {
return ad.core.math.cos(g);
}
/***/ unittest {
const f = cos(GDN!1(0));
assert(f == 1 && f.d == 0);
}
/**
* This function computes the tangent of its argument.
*
* If $(MATH f(x) = tan(g(x))), then $(MATH f' = g'sec(g)$(SUP 2)).
*
* Params:
* Deg = the degree of g
* g = the `GDN` to take the tangent of
*
* Returns:
* the tangent of g
*/
pure nothrow @nogc @safe GDN!Deg tan(ulong Deg)(in GDN!Deg g)
do {
alias cosine = Select!(Deg == 1, core.math.cos, ad.core.math.cos);
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.tan(g.val), g.d/pow(cosine(g.reduce()), 2));
}
/***/ unittest {
import std.math : isClose, PI_4;
const f = tan(GDN!1(-PI_4));
assert(f == -1 && isClose(f.d, 2));
}
unittest {
import std.math : isClose, NaN, PI_4;
assert(tan(GDN!1(NaN(1), NaN(2))) is GDN!1(NaN(1), NaN(2)));
const w = tan(GDN!1(PI_4));
assert(w.val == 1 && isClose(w.d, 2.));
assert(isNaN(tan(GDN!1(real.nan))));
assert(isNaN(tan(GDN!1(real.infinity))));
assert(tan(GDN!2(0)) is GDN!2(0, 1, 0));
// f = 0
// <f',f"> = <1,0>/cos(<0,1>)^2
// = <1,0>/<1,-0>^2
// = <1,0>/<1,2*1*-0>
// = <1,0>/<1,-0>
// = <1,0>
}
/**
* This function computes the arcsine (inverse sine) of its argument g.
*
* If $(MATH f(x) = sin$(SUP -1)g(x)), then $(MATH f' = g'/√(1 - g$(SUP 2)), |g| ≤ 1).
*
* Params:
* Deg = the degree of g
* g = the `GDN` to compute the arcsine of
*
* Returns:
* the arcsine of g
*/
pure nothrow @nogc @safe GDN!Deg asin(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.asin(g.val), g.d/sqrt(1.0L - pow(g.reduce(), 2)));
}
/***/ unittest {
import std.math : PI_2;
assert(asin(GDN!1(0)) is GDN!1(0, 1));
assert(asin(GDN!1(1)) is GDN!1(PI_2, real.infinity));
}
unittest {
import std.format : format;
import std.math : NaN, PI_2;
assert(asin(GDN!1(NaN(1), NaN(2))) is GDN!1(NaN(1), NaN(2)));
assert(asin(GDN!1(-1)) is GDN!1(-PI_2, real.infinity));
assert(isNaN(asin(GDN!1(2))));
const g = GDN!2(0);
const f = asin(g);
assert(f is GDN!2(0, 1, 0), format("asin(%s) != %s", g, f));
// f = 0
// <f',f"> = <1,0>/sqrt(1 - <0,1>^^2)
// = <1,0>/sqrt(1 - <0,0>)
// = <1,0>/sqrt(<1,0>)
// = <1,0>/<1,0>
// = <1,0>
}
/**
* This function computes the arccosine (inverse cosine) of its argument g.
*
* If $(MATH f(x) = cos$(SUP -1)g(x)), then $(MATH f' = -g'/√(1 - g$(SUP 2)), |g| ≤ 1).
*
* Params:
* Deg = the degree of g
* g = the `GDN` to compute the arccosine of
*
* Returns:
* the arccosine of g
*/
pure nothrow @nogc @safe GDN!Deg acos(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.acos(g.val), -g.d/sqrt(1.0L - pow(g.reduce(), 2)));
}
/***/ unittest {
import std.math : PI_2;
assert(acos(GDN!1(0)) is GDN!1(PI_2, -1));
assert(acos(GDN!1(1)) is GDN!1(0, -real.infinity));
}
unittest {
import std.math : NaN, PI, PI_2;
assert(acos(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(acos(GDN!1(-1)) is GDN!1(PI, -real.infinity));
assert(isNaN(acos(GDN!1(2))));
assert(acos(GDN!2(0)) is GDN!2(PI_2, -1, 0));
}
/**
* This function computes the arctangent (inverse tangent) of its argument g.
*
* If $(MATH f(x) = tan$(SUP -1)g(x)), then $(MATH f' = g'/(1 + g$(SUP 2))).
*
* Params:
* Deg = the degree of g
* g = the `GDN` to compute the arctangent of
*
* Returns:
* the arctangent of g
*/
pure nothrow @nogc @safe GDN!Deg atan(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.atan(g.val), g.d/(1.0L + pow(g.reduce(), 2)));
}
/***/ unittest {
assert(atan(GDN!1(0)) is GDN!1(0, 1));
}
unittest {
import std.math : NaN, PI_2;
assert(atan(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(atan(GDN!1(real.infinity)) is GDN!1(PI_2, +0.));
assert(atan(GDN!1(-real.infinity)) is GDN!1(-PI_2, +0.));
assert(atan(GDN!2(0)) is GDN!2(0, 1, 0));
}
/**
* This function computes the arctangent (inverse tangent) of $(MATH g/h).
*
* If $(MATH f(x) = tan$(SUP -1)(g(x)/h(x))), then $(MATH f' = (g'h - gh')/(h$(SUP 2) + g$(SUP 2))).
*
* If either g or h has type `real`, it is converted to a constant generalized dual number with the
* same degree as the other parameter. If g and h are `GDN` objects with different degrees, the one
* with the greater degree is converted to have the same degree as the lesser.
*
* Params:
* G = the type of g
* H = the type of h
* g = the numerator of the arctangent argument
* h = the denominator of the arctangent argument
*
* Returns:
* It returns the angle resulting from the $(MATH tan$(SUP -1)(g/h)).
*/
pure nothrow @nogc @safe
CommonGDN!(G, H) atan2(G, H)(in G g, in H h)
if (anySatisfy!(isGDN, G, H) && allSatisfy!(isConvertibleToGDN, G, H))
do {
alias RGDN = typeof(return);
const gg = asGDN!(RGDN.DEGREE)(g);
const hh = asGDN!(RGDN.DEGREE)(h);
const gg_red = gg.reduce();
const hh_red = hh.reduce();
if (isNaN(gg) || isNaN(hh)) return nanCombine(gg, hh);
RGDN.DerivType!1 df;
if (isFinite(gg) && isInfinity(hh)) {
if (isFinite(gg.d) && isFinite(hh.d)) {
df = gg.d / hh_red;
}
} else if (isInfinity(gg) && isFinite(hh)) {
if (isFinite(gg.d) && isFinite(hh.d)) {
df = -hh.d / gg_red;
}
} else {
df = (gg.d*hh_red - gg_red*hh.d) / (pow(hh_red, 2) + pow(gg_red, 2));
}
return RGDN(std.math.trigonometry.atan2(gg.val, hh.val), df);
}
/***/ unittest {
import std.math : PI_4;
assert(atan2(GDN!1(1), GDN!1(1)) is GDN!1(PI_4, 0));
assert(atan2(GDN!1(1), -1) is GDN!1(3*PI_4, -0.5));
}
unittest {
import std.format : format;
import std.math : isNaN, NaN, PI, PI_2, PI_4;
assert(atan2(GDN!1(-NaN(1), NaN(2)), GDN!1(NaN(1), NaN(3))) is GDN!1(-NaN(1), NaN(3)));
assert(atan2(GDN!2(-1), GDN!2(1)) is GDN!2(-PI_4, 1, 0));
// <f',f"> = (<1,0><1,1> - <-1,1><1,0>) / (<1,1>^2 + <-1,1>^2)
// = (<1,1> - <-1,1>) / (<1,2> + <1,-2>)
// = <2,0> / <2,0>
// = <1,0>
assert(atan2(GDN!1.nan, GDN!1(2)) is GDN!1.nan);
assert(atan2(GDN!1(-1), GDN!1.nan) is GDN!1.nan);
const nz = GDN!1(-0.);
const pz = GDN!1(+0.);
assert(atan2(pz, GDN!1(1)) is GDN!1(+0., 1));
assert(atan2(nz, GDN!1(3)) is GDN!1(-0., 1./3));
const q = atan2(pz, pz);
assert(q.val is +0. && isNaN(q.d), format("atan2(+0, +0) != %s", q));
const w = atan2(nz, pz);
assert(w.val is -0. && isNaN(w.d), format("atan2(-0, +0) != %s", w));
assert(atan2(pz, GDN!1(-1)) is GDN!1(PI, -1));
assert(atan2(nz, GDN!1(-1)) is GDN!1(-PI, -1));
const e = atan2(pz, nz);
assert(e == PI && isNaN(e.d));
const r = atan2(nz, nz);
assert(r == -PI && isNaN(r.d));
assert(atan2(GDN!1(1), pz) is GDN!1(PI_2, -1));
assert(atan2(GDN!1(1), nz) is GDN!1(PI_2, -1));
assert(atan2(GDN!1(-1), pz) is GDN!1(-PI_2, 1));
assert(atan2(GDN!1(-1), nz) is GDN!1(-PI_2, 1));
const ni = GDN!1(-real.infinity);
const pi = GDN!1(real.infinity);
assert(atan2(GDN!1(1), pi) is GDN!1(+0., +0.));
assert(atan2(GDN!1(-1), pi) is GDN!1(-0., +0.));
const t = atan2(ni, GDN!1(0));
assert(t is GDN!1(-PI_2, +0.), format("atan2(-inf, 0) != %s", t));
assert(atan2(pi, GDN!1(0)) is GDN!1(PI_2,-0.));
const y = atan2(GDN!1(1), ni);
assert(y is GDN!1(PI, -0.), format("atan(1, -inf) != %s", y));
const u = atan2(ni, pi);
assert(u == -PI_4 && isNaN(u.d));
const i = atan2(pi, pi);
assert(i == PI_4 && isNaN(i.d));
const o = atan2(ni, ni);
assert(o == -3*PI_4 && isNaN(o.d));
const p = atan2(pi, ni);
assert(p == 3*PI_4 && isNaN(p.d));
}
/**
* This function calculates the hyperbolic sine of its argument g.
*
* If $(MATH f(x) = sinh(g(x))), then $(MATH f' = g'cosh(g)).
*
* Params:
* Deg = the degree of g
* g = the argument of the hyperbolic sine
*
* Returns:
* It returns the hyperbolic sine of g.
*/
pure nothrow @nogc @safe GDN!Deg sinh(ulong Deg)(in GDN!Deg g)
do {
alias ch = Select!(Deg == 1, std.math.trigonometry.cosh, cosh);
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.sinh(g.val), ch(g.reduce())*g.d);
}
/***/ unittest {
const f = sinh(GDN!1(0));
assert(f is GDN!1(0, 1));
}
unittest {
import std.math : E, NaN;
assert(sinh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(sinh(GDN!2(1)) is GDN!2((E - 1/E)/2, (E + 1/E)/2, (E - 1/E)/2));
// <f',f"> = cosh(<1,1>)<1,0> = <cosh(1),sinh(1)1> = <(E + 1/E)/2,(E - 1/E)/2>
}
/**
* This function calculates the hyperbolic cosine of its argument g.
*
* If $(MATH f(x) = cosh(g(x))), then $(MATH f' = g'sinh(g)).
*
* Params:
* Deg = the degree of g
* g = the argument of the hyperbolic cosine
*
* Returns:
* It returns the hyperbolic cosine of g.
*/
pure nothrow @nogc @safe GDN!Deg cosh(ulong Deg)(in GDN!Deg g)
do {
alias sh = Select!(Deg == 1, std.math.trigonometry.sinh, sinh);
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.cosh(g.val), sh(g.reduce())*g.d);
}
/***/ unittest {
assert(cosh(GDN!1(0)) is GDN!1(1, 0));
}
unittest {
import std.math : E, NaN;
assert(cosh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(cosh(GDN!2(1)) is GDN!2((E + 1/E)/2, (E - 1/E)/2, (E + 1/E)/2));
}
/**
* This function calculates the hyperbolic tangent of its argument g.
*
* If $(MATH f(x) = tanh(g(x))), then $(MATH f' = g'/cosh$(SUP 2)(g)).
*
* Params:
* Deg = the degree of g
* g = the argument of the hyperbolic tangent
*
* Returns:
* It returns the hyperbolic tangent of g.
*/
pure nothrow @nogc @safe GDN!Deg tanh(ulong Deg)(in GDN!Deg g)
do {
alias ch = Select!(Deg == 1, std.math.trigonometry.cosh, cosh);
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.tanh(g.val), g.d/pow(ch(g.reduce()), 2));
}
/***/ unittest {
assert(tanh(GDN!1(0)) is GDN!1(0, 1));
}
unittest {
import std.math : isClose, NaN;
assert(tanh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
const g = GDN!2(1);
const f1 = tanh(g);
const f2 = sinh(g) / cosh(g);
assert(f1.val.isClose(f2.val));
assert(f1.d.val.isClose(f2.d.val));
assert(f1.d!2.isClose(f2.d!2));
}
/**
* This function calculates the inverse hyperbolic sine of its argument g.
*
* If $(MATH f(x) = sinh$(SUP -1)g(x)), then $(MATH f' = g'/√(1 + g$(SUP 2))).
*
* Params:
* Deg = the degree of g
* g = the argument of the inverse hyperbolic sine
*
* Returns:
* It returns the inverse hyperbolic sine of g.
*/
pure nothrow @nogc @safe GDN!Deg asinh(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.asinh(g.val), g.d/sqrt(pow(g.reduce(), 2) + 1.0L));
}
/***/ unittest {
assert(asinh(GDN!1(0)) is GDN!1(0, 1));
}
unittest {
import std.math : NaN;
assert(asinh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(asinh(GDN!2(-0.)) is GDN!2(-0., 1, 0));
// <f',f"> = <1,0> / sqrt(1 + <-0,1>^2)
// = <1,0> / sqrt(1 + <+0,-0>)
// = <1,0> / sqrt(<1,-0>)
// = <1,0> / <1,-0>
// = <1,0>
}
/**
* This function calculates the inverse hyperbolic cosine of its argument g.
*
* If $(MATH f(x) = cosh$(SUP -1)g(x)), then $(MATH f' = g'/√(g$(SUP 2) - 1)).
*
* Params:
* Deg = the degree of g
* g = the argument of the inverse hyperbolic cosine
*
* Returns:
* It returns the inverse hyperbolic cosine of g.
*/
pure nothrow @nogc @safe GDN!Deg acosh(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
return GDN!Deg(std.math.trigonometry.acosh(g.val), g.d / sqrt(pow(g.reduce(), 2) - 1.0L));
}
/***/ unittest {
assert(isNaN(acosh(GDN!1(0.9))));
assert(acosh(GDN!1(1)) is GDN!1(0, real.infinity));
}
unittest {
import std.math : NaN;
assert(acosh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
assert(
acosh(GDN!2(2, 3, 1))
is GDN!2(std.math.trigonometry.acosh(2.0L), sqrt(3.0L), -5*sqrt(3.0L)/3));
// f = acosh(2)
// <f',f"> = <3,1> / sqrt(<2,3>^2 - 1)
// = <3,1> / sqrt(<4,12> - 1)
// = <3,1> / sqrt(<3,12>)
// = <3,1> / <sqrt(3), 6/sqrt(3)>
// = <3/sqrt(3), (sqrt(3) - 18/sqrt(3))/3>
// = <sqrt(3),sqrt(3)/3 - 6/sqrt(3)>
// = <sqrt(3),sqrt(3)/3 - 2sqrt(3)>
// = <sqrt(3),-5sqrt(3)/3?
}
/**
* This function calculates the inverse hyperbolic tangent of its argument g.
*
* If $(MATH f(x) = tanh$(SUP -1)g(x)), then $(MATH f' = g'/(1 - g$(SUP 2)), |g| < 1).
*
* Params:
* Deg = the degree of g
* g = the argument of the inverse hyperbolic tangent
*
* Returns:
* It returns the inverse hyperbolic tangent of g.
*/
pure nothrow @nogc @safe GDN!Deg atanh(ulong Deg)(in GDN!Deg g)
do {
if (isNaN(g)) return g;
GDN!Deg.DerivType!1 df;
if (abs(g) < 1.0L) {
df = g.d/(1.0L - pow(g.reduce(), 2));
}
return GDN!Deg(std.math.trigonometry.atanh(g.val), df);
}
/***/ unittest {
import std.math : isNaN;
import ad.math.traits : sgn;
assert(atanh(GDN!1(0)) is GDN!1(0, 1));
const f = atanh(GDN!1(1));
assert(sgn(f) == 1 && isInfinity(f) && isNaN(f.d));
}
unittest {
import std.math : isNaN, NaN;
import ad.math.traits : sgn;
assert(atanh(GDN!1(NaN(1), -NaN(2))) is GDN!1(NaN(1), -NaN(2)));
const q = atanh(GDN!1(-1));
assert(sgn(q) == -1 && isInfinity(q) && isNaN(q.d));
assert(atanh(GDN!2(-0., 2, 1)) is GDN!2(-0., 2, 1));
// f = -0
// <f',f"> = <2,1> / (1 - <-0.,2>^2)
// = <2,1> / (1 - <-0.,-0.>)
// = <2,1> / <1,-0.>
// = <2,(1 - -0/1>
// = <2,1>
}