forked from JuliaDiff/ForwardDiff.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartialsTest.jl
142 lines (107 loc) · 4.67 KB
/
PartialsTest.jl
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
module PartialsTest
using Compat
using Compat.Test
using ForwardDiff
using ForwardDiff: Partials
samerng() = MersenneTwister(1)
for N in (0, 3), T in (Int, Float32, Float64)
println(" ...testing Partials{$N,$T}")
VALUES = (rand(T,N)...,)
PARTIALS = Partials{N,T}(VALUES)
VALUES2 = (rand(T,N)...,)
PARTIALS2 = Partials{N,T}(VALUES2)
##############################
# Utility/Accessor Functions #
##############################
@test PARTIALS.values == VALUES
@test ForwardDiff.valtype(PARTIALS) == T
@test ForwardDiff.valtype(typeof(PARTIALS)) == T
@test ForwardDiff.npartials(PARTIALS) == N
@test ForwardDiff.npartials(typeof(PARTIALS)) == N
@test ndims(PARTIALS) == ndims(PARTIALS2) == 1
@test length(PARTIALS) == N
@test length(VALUES) == N
for i in 1:N
@test PARTIALS[i] == VALUES[i]
end
@test start(PARTIALS) == start(VALUES)
@test N == 0 || (next(PARTIALS, start(PARTIALS)) == next(VALUES, start(VALUES)))
@test done(PARTIALS, start(PARTIALS)) == done(VALUES, start(VALUES))
i = 1
for p in PARTIALS
@test p == VALUES[i]
i += 1
end
#####################
# Generic Functions #
#####################
@test zero(PARTIALS) == zero(typeof(PARTIALS))
@test zero(PARTIALS).values == map(zero, VALUES)
@test one(PARTIALS) == one(typeof(PARTIALS))
@test one(PARTIALS).values == map(one, VALUES)
@test rand(samerng(), PARTIALS) == rand(samerng(), typeof(PARTIALS))
@test ForwardDiff.iszero(PARTIALS) == (N == 0)
@test ForwardDiff.iszero(zero(PARTIALS))
@test PARTIALS == copy(PARTIALS)
@test (PARTIALS == PARTIALS2) == (N == 0)
@test isequal(PARTIALS, copy(PARTIALS))
@test isequal(PARTIALS, PARTIALS2) == (N == 0)
@test hash(PARTIALS) == hash(VALUES, ForwardDiff.PARTIALS_HASH)
@test hash(PARTIALS) == hash(copy(PARTIALS))
@test hash(PARTIALS, hash(1)) == hash(copy(PARTIALS), hash(1))
@test hash(PARTIALS, hash(1)) == hash(copy(PARTIALS), hash(1))
TMPIO = IOBuffer()
write(TMPIO, PARTIALS)
seekstart(TMPIO)
@test read(TMPIO, typeof(PARTIALS)) == PARTIALS
seekstart(TMPIO)
write(TMPIO, PARTIALS2)
seekstart(TMPIO)
@test read(TMPIO, typeof(PARTIALS2)) == PARTIALS2
close(TMPIO)
########################
# Conversion/Promotion #
########################
WIDE_T = widen(T)
WIDE_PARTIALS = convert(Partials{N,WIDE_T}, PARTIALS)
@test typeof(WIDE_PARTIALS) == Partials{N,WIDE_T}
@test WIDE_PARTIALS == PARTIALS
@test convert(Partials{N,T}, PARTIALS) === PARTIALS
@test promote_type(Partials{N,T}, Partials{N,T}) == Partials{N,T}
@test promote_type(Partials{N,T}, Partials{N,WIDE_T}) == Partials{N,WIDE_T}
########################
# Arithmetic Functions #
########################
ZERO_PARTIALS = Partials{0,T}(())
@test (PARTIALS + PARTIALS).values == map(v -> v + v, VALUES)
@test (PARTIALS + ZERO_PARTIALS) === PARTIALS
@test (ZERO_PARTIALS + PARTIALS) === PARTIALS
@test (PARTIALS - PARTIALS).values == map(v -> v - v, VALUES)
@test (PARTIALS - ZERO_PARTIALS) === PARTIALS
@test (ZERO_PARTIALS - PARTIALS) === -PARTIALS
@test getfield(-(PARTIALS), :values) == map(-, VALUES)
X = rand()
Y = rand()
@test X * PARTIALS == PARTIALS * X
@test (X * PARTIALS).values == map(v -> X * v, VALUES)
@test (PARTIALS / X).values == map(v -> v / X, VALUES)
if N > 0
@test ForwardDiff._div_partials(PARTIALS, PARTIALS2, X, Y) == ForwardDiff._mul_partials(PARTIALS, PARTIALS2, inv(Y), -X/(Y^2))
@test ForwardDiff._mul_partials(PARTIALS, PARTIALS2, X, Y).values == map((a, b) -> (X * a) + (Y * b), VALUES, VALUES2)
@test ForwardDiff._mul_partials(ZERO_PARTIALS, PARTIALS, X, Y) == Y * PARTIALS
@test ForwardDiff._mul_partials(PARTIALS, ZERO_PARTIALS, X, Y) == X * PARTIALS
if ForwardDiff.NANSAFE_MODE_ENABLED
ZEROS = Partials((fill(zero(T), N)...,))
@test (NaN * ZEROS).values == ZEROS.values
@test (Inf * ZEROS).values == ZEROS.values
@test (ZEROS / 0).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, X, NaN).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, NaN, X).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, X, Inf).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, Inf, X).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, Inf, NaN).values == ZEROS.values
@test ForwardDiff._mul_partials(ZEROS, ZEROS, NaN, Inf).values == ZEROS.values
end
end
end
end # module