|
1 |
| -function test_scalar(f, f′, xs...) |
2 |
| - for r = (rrule, frule) |
3 |
| - rr = r(f, xs...) |
4 |
| - @test rr !== nothing |
5 |
| - fx, ∂x = rr |
6 |
| - @test fx == f(xs...) |
7 |
| - @test ∂x(1) ≈ f′(xs...) atol=1e-5 |
8 |
| - end |
9 |
| -end |
10 |
| - |
11 | 1 | @testset "base" begin
|
12 | 2 | @testset "Trig" begin
|
13 |
| - @testset "Basics" for x = (Float64(π), Complex(π, π/2)) |
14 |
| - test_scalar(sin, cos, x) |
15 |
| - test_scalar(cos, x -> -sin(x), x) |
16 |
| - test_scalar(tan, x -> 1 + tan(x)^2, x) |
17 |
| - test_scalar(sec, x -> sec(x) * tan(x), x) |
18 |
| - test_scalar(csc, x -> -csc(x) * cot(x), x) |
19 |
| - test_scalar(cot, x -> -1 - cot(x)^2, x) |
20 |
| - test_scalar(sinpi, x -> π * cospi(x), x) |
21 |
| - test_scalar(cospi, x -> -π * sinpi(x), x) |
| 3 | + @testset "Basics" for x = (Float64(π)-0.01, Complex(π, π/2)) |
| 4 | + test_scalar(sin, x) |
| 5 | + test_scalar(cos, x) |
| 6 | + test_scalar(tan, x) |
| 7 | + test_scalar(sec, x) |
| 8 | + test_scalar(csc, x) |
| 9 | + test_scalar(cot, x) |
| 10 | + test_scalar(sinpi, x) |
| 11 | + test_scalar(cospi, x) |
22 | 12 | end
|
23 |
| - @testset "Hyperbolic" for x = (Float64(π), Complex(π, π/2)) |
24 |
| - test_scalar(sinh, cosh, x) |
25 |
| - test_scalar(cosh, sinh, x) |
26 |
| - test_scalar(tanh, x -> sech(x)^2, x) |
27 |
| - test_scalar(sech, x -> -tanh(x) * sech(x), x) |
28 |
| - test_scalar(csch, x -> -coth(x) * csch(x), x) |
29 |
| - test_scalar(coth, x -> -csch(x)^2, x) |
| 13 | + @testset "Hyperbolic" for x = (Float64(π)-0.01, Complex(π-0.01, π/2)) |
| 14 | + test_scalar(sinh, x) |
| 15 | + test_scalar(cosh, x) |
| 16 | + test_scalar(tanh, x) |
| 17 | + test_scalar(sech, x) |
| 18 | + test_scalar(csch, x) |
| 19 | + test_scalar(coth, x) |
30 | 20 | end
|
31 | 21 | @testset "Degrees" begin
|
32 | 22 | x = 45.0
|
33 |
| - test_scalar(sind, x -> (π / 180) * cosd(x), x) |
34 |
| - test_scalar(cosd, x -> (-π / 180) * sind(x), x) |
35 |
| - test_scalar(tand, x -> (π / 180) * (1 + tand(x)^2), x) |
36 |
| - test_scalar(secd, x -> (π / 180) * secd(x) * tand(x), x) |
37 |
| - test_scalar(cscd, x -> (-π / 180) * cscd(x) * cotd(x), x) |
38 |
| - test_scalar(cotd, x -> (-π / 180) * (1 + cotd(x)^2), x) |
| 23 | + test_scalar(sind, x) |
| 24 | + test_scalar(cosd, x) |
| 25 | + test_scalar(tand, x) |
| 26 | + test_scalar(secd, x) |
| 27 | + test_scalar(cscd, x) |
| 28 | + test_scalar(cotd, x) |
39 | 29 | end
|
40 |
| - @testset "Inverses" for x = (1.0, Complex(1.0, 0.25)) |
41 |
| - test_scalar(asin, x -> 1 / sqrt(1 - x^2), x) |
42 |
| - test_scalar(acos, x -> -1 / sqrt(1 - x^2), x) |
43 |
| - test_scalar(atan, x -> 1 / (1 + x^2), x) |
44 |
| - test_scalar(asec, x -> 1 / (abs(x) * sqrt(x^2 - 1)), x) |
45 |
| - test_scalar(acsc, x -> -1 / (abs(x) * sqrt(x^2 - 1)), x) |
46 |
| - test_scalar(acot, x -> -1 / (1 + x^2), x) |
| 30 | + @testset "Inverses" for x = (0.5, Complex(0.5, 0.25)) |
| 31 | + test_scalar(asin, x) |
| 32 | + test_scalar(acos, x) |
| 33 | + test_scalar(atan, x) |
| 34 | + test_scalar(asec, 1/x) |
| 35 | + test_scalar(acsc, 1/x) |
| 36 | + test_scalar(acot, 1/x) |
47 | 37 | end
|
48 |
| - @testset "Inverse hyperbolic" for x = (0.0, Complex(0.0, 0.25)) |
49 |
| - test_scalar(asinh, x -> 1 / sqrt(x^2 + 1), x) |
50 |
| - test_scalar(acosh, x -> 1 / sqrt(x^2 - 1), x + 1) # +1 accounts for domain |
51 |
| - test_scalar(atanh, x -> 1 / (1 - x^2), x) |
52 |
| - test_scalar(asech, x -> -1 / x / sqrt(1 - x^2), x) |
53 |
| - test_scalar(acsch, x -> -1 / abs(x) / sqrt(1 + x^2), x) |
54 |
| - test_scalar(acoth, x -> 1 / (1 - x^2), x + 1) |
| 38 | + @testset "Inverse hyperbolic" for x = (0.5, Complex(0.5, 0.25)) |
| 39 | + test_scalar(asinh, x) |
| 40 | + test_scalar(acosh, x + 1) # +1 accounts for domain |
| 41 | + test_scalar(atanh, x) |
| 42 | + test_scalar(asech, x) |
| 43 | + test_scalar(acsch, x) |
| 44 | + test_scalar(acoth, x + 1) |
55 | 45 | end
|
56 |
| - @testset "Inverse degrees" begin |
57 |
| - x = 1.0 |
58 |
| - test_scalar(asind, x -> 180 / π / sqrt(1 - x^2), x) |
59 |
| - test_scalar(acosd, x -> -180 / π / sqrt(1 - x^2), x) |
60 |
| - test_scalar(atand, x -> 180 / π / (1 + x^2), x) |
61 |
| - test_scalar(asecd, x -> 180 / π / abs(x) / sqrt(x^2 - 1), x) |
62 |
| - test_scalar(acscd, x -> -180 / π / abs(x) / sqrt(x^2 - 1), x) |
63 |
| - test_scalar(acotd, x -> -180 / π / (1 + x^2), x) |
| 46 | + @testset "Inverse degrees" for x = (0.5, Complex(0.5, 0.25)) |
| 47 | + test_scalar(asind, x) |
| 48 | + test_scalar(acosd, x) |
| 49 | + test_scalar(atand, x) |
| 50 | + test_scalar(asecd, 1/x) |
| 51 | + test_scalar(acscd, 1/x) |
| 52 | + test_scalar(acotd, 1/x) |
64 | 53 | end
|
65 | 54 | @testset "Multivariate" begin
|
66 | 55 | x, y = rand(2)
|
|
83 | 72 | @test r === rsincos
|
84 | 73 | @test df(1, 2) === dsincos
|
85 | 74 | end
|
86 |
| - end |
87 |
| - @testset "Misc. Tests" begin |
88 |
| - @testset "*(x, y)" begin |
89 |
| - x, y = rand(3, 2), rand(2, 5) |
90 |
| - z, (dx, dy) = rrule(*, x, y) |
| 75 | + end # Trig |
91 | 76 |
|
92 |
| - @test z == x * y |
| 77 | + @testset "math" begin |
| 78 | + for x in (-0.1, 6.4, 1.0+0.5im, -10.0+0im) |
| 79 | + test_scalar(deg2rad, x) |
| 80 | + test_scalar(rad2deg, x) |
93 | 81 |
|
94 |
| - z̄ = rand(3, 5) |
| 82 | + test_scalar(inv, x) |
95 | 83 |
|
96 |
| - @test dx(z̄) == extern(accumulate(zeros(3, 2), dx, z̄)) |
97 |
| - @test dy(z̄) == extern(accumulate(zeros(2, 5), dy, z̄)) |
| 84 | + test_scalar(exp, x) |
| 85 | + test_scalar(exp2, x) |
| 86 | + test_scalar(exp10, x) |
98 | 87 |
|
99 |
| - test_accumulation(rand(3, 2), dx, z̄, z̄ * y') |
100 |
| - test_accumulation(rand(2, 5), dy, z̄, x' * z̄) |
| 88 | + x isa Real && test_scalar(cbrt, x) |
| 89 | + if (x isa Real && x >= 0) || x isa Complex |
| 90 | + test_scalar(sqrt, x) |
| 91 | + test_scalar(log, x) |
| 92 | + test_scalar(log2, x) |
| 93 | + test_scalar(log10, x) |
| 94 | + test_scalar(log1p, x) |
| 95 | + end |
101 | 96 | end
|
102 |
| - @testset "hypot(x, y)" begin |
103 |
| - x, y = rand(2) |
104 |
| - h, dxy = frule(hypot, x, y) |
| 97 | + end |
105 | 98 |
|
106 |
| - @test extern(dxy(One(), Zero())) === x / h |
107 |
| - @test extern(dxy(Zero(), One())) === y / h |
| 99 | + @testset "*(x, y)" begin |
| 100 | + x, y = rand(3, 2), rand(2, 5) |
| 101 | + z, (dx, dy) = rrule(*, x, y) |
108 | 102 |
|
109 |
| - cx, cy = cast((One(), Zero())), cast((Zero(), One())) |
110 |
| - dx, dy = extern(dxy(cx, cy)) |
111 |
| - @test dx === x / h |
112 |
| - @test dy === y / h |
| 103 | + @test z == x * y |
113 | 104 |
|
114 |
| - cx, cy = cast((rand(), Zero())), cast((Zero(), rand())) |
115 |
| - dx, dy = extern(dxy(cx, cy)) |
116 |
| - @test dx === x / h * cx.value[1] |
117 |
| - @test dy === y / h * cy.value[2] |
118 |
| - end |
| 105 | + z̄ = rand(3, 5) |
| 106 | + |
| 107 | + @test dx(z̄) == extern(accumulate(zeros(3, 2), dx, z̄)) |
| 108 | + @test dy(z̄) == extern(accumulate(zeros(2, 5), dy, z̄)) |
| 109 | + |
| 110 | + test_accumulation(rand(3, 2), dx, z̄, z̄ * y') |
| 111 | + test_accumulation(rand(2, 5), dy, z̄, x' * z̄) |
119 | 112 | end
|
| 113 | + |
| 114 | + @testset "hypot(x, y)" begin |
| 115 | + x, y = rand(2) |
| 116 | + h, dxy = frule(hypot, x, y) |
| 117 | + |
| 118 | + @test extern(dxy(One(), Zero())) === x / h |
| 119 | + @test extern(dxy(Zero(), One())) === y / h |
| 120 | + |
| 121 | + cx, cy = cast((One(), Zero())), cast((Zero(), One())) |
| 122 | + dx, dy = extern(dxy(cx, cy)) |
| 123 | + @test dx === x / h |
| 124 | + @test dy === y / h |
| 125 | + |
| 126 | + cx, cy = cast((rand(), Zero())), cast((Zero(), rand())) |
| 127 | + dx, dy = extern(dxy(cx, cy)) |
| 128 | + @test dx === x / h * cx.value[1] |
| 129 | + @test dy === y / h * cy.value[2] |
| 130 | + end |
| 131 | + |
120 | 132 | @testset "identity" begin
|
121 | 133 | rng = MersenneTwister(1)
|
122 | 134 | n = 4
|
|
0 commit comments