Skip to content

Commit 77691e7

Browse files
committed
test more math
1 parent cb59302 commit 77691e7

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

src/rulesets/Base/base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
@scalar_rule(\(x, y), (-(y / x / x), inv(x)))
6262
@scalar_rule(^(x, y), (y * x^(y - 1), Ω * log(x)))
6363

64-
@scalar_rule(inv(x), -abs2(Ω))
64+
@scalar_rule(inv(x), -Ω^2)
6565
@scalar_rule(sqrt(x), inv(2 * Ω))
6666
@scalar_rule(cbrt(x), inv(3 * Ω^2))
6767
@scalar_rule(exp(x), Ω)

test/rulesets/Base/base.jl

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@
4343
test_scalar(acsch, x)
4444
test_scalar(acoth, x + 1)
4545
end
46-
@testset "Inverse degrees" begin
47-
x = 0.5
46+
@testset "Inverse degrees" for x = (0.5, Complex(0.5, 0.25))
4847
test_scalar(asind, x)
4948
test_scalar(acosd, x)
5049
test_scalar(atand, x)
@@ -73,40 +72,63 @@
7372
@test r === rsincos
7473
@test df(1, 2) === dsincos
7574
end
76-
end
77-
@testset "Misc. Tests" begin
78-
@testset "*(x, y)" begin
79-
x, y = rand(3, 2), rand(2, 5)
80-
z, (dx, dy) = rrule(*, x, y)
75+
end # Trig
8176

82-
@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)
8381

84-
= rand(3, 5)
82+
test_scalar(inv, x)
8583

86-
@test dx(z̄) == extern(accumulate(zeros(3, 2), dx, z̄))
87-
@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)
8887

89-
test_accumulation(rand(3, 2), dx, z̄, z̄ * y')
90-
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
9196
end
92-
@testset "hypot(x, y)" begin
93-
x, y = rand(2)
94-
h, dxy = frule(hypot, x, y)
97+
end
9598

96-
@test extern(dxy(One(), Zero())) === x / h
97-
@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)
98102

99-
cx, cy = cast((One(), Zero())), cast((Zero(), One()))
100-
dx, dy = extern(dxy(cx, cy))
101-
@test dx === x / h
102-
@test dy === y / h
103+
@test z == x * y
103104

104-
cx, cy = cast((rand(), Zero())), cast((Zero(), rand()))
105-
dx, dy = extern(dxy(cx, cy))
106-
@test dx === x / h * cx.value[1]
107-
@test dy === y / h * cy.value[2]
108-
end
105+
= 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̄)
109112
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+
110132
@testset "identity" begin
111133
rng = MersenneTwister(1)
112134
n = 4

0 commit comments

Comments
 (0)