Skip to content

Commit 934653d

Browse files
committed
tests: add vec -> vec and mtx -> mtx tests
1 parent a8a1cf5 commit 934653d

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

test/JacobianTest.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,50 @@ end
104104

105105
@testset "Comparison vs Calculus.jl" begin
106106

107+
@testset "$f(x::Vector)::Vector" for f in DiffTests.VECTOR_TO_VECTOR_FUNCS
108+
v = f(X)
109+
j = ForwardDiff.jacobian(f, X)
110+
@test isapprox(j, Calculus.jacobian(f, X, :forward), atol=1.3FINITEDIFF_ERROR)
111+
112+
@testset "chunk size = $c and tag = $(repr(tag))" for c in CHUNK_SIZES, tag in (nothing, Tag(f, eltype(X)))
113+
cfg = JacobianConfig(f, X, ForwardDiff.Chunk{c}(), tag)
114+
115+
out = ForwardDiff.jacobian(f, X, cfg)
116+
@test isapprox(out, j)
117+
118+
out = similar(X, length(v), length(X))
119+
ForwardDiff.jacobian!(out, f, X, cfg)
120+
@test isapprox(out, j)
121+
122+
out = DiffResults.DiffResult(similar(v, length(v)), similar(v, length(v), length(X)))
123+
ForwardDiff.jacobian!(out, f, X, cfg)
124+
@test isapprox(DiffResults.value(out), v)
125+
@test isapprox(DiffResults.jacobian(out), j)
126+
end
127+
end
128+
129+
@testset "$f(x::Matrix)::Matrix" for f in DiffTests.MATRIX_TO_MATRIX_FUNCS
130+
v = f(XX)
131+
j = ForwardDiff.jacobian(f, XX)
132+
@test isapprox(j, Calculus.jacobian(x -> vec(f(reshape(x, size(XX)))), vec(XX), :forward), atol=1.3FINITEDIFF_ERROR)
133+
134+
@testset "chunk size = $c and tag = $(repr(tag))" for c in CHUNK_SIZES, tag in (nothing, Tag(f, eltype(XX)))
135+
cfg = JacobianConfig(f, XX, ForwardDiff.Chunk{c}(), tag)
136+
137+
out = ForwardDiff.jacobian(f, XX, cfg)
138+
@test isapprox(out, j)
139+
140+
out = similar(XX, length(v), length(XX))
141+
ForwardDiff.jacobian!(out, f, XX, cfg)
142+
@test isapprox(out, j)
143+
144+
out = DiffResults.DiffResult(similar(v, length(v)), similar(v, length(v), length(XX)))
145+
ForwardDiff.jacobian!(out, f, XX, cfg)
146+
@test isapprox(DiffResults.value(out), vec(v))
147+
@test isapprox(DiffResults.jacobian(out), j)
148+
end
149+
end
150+
107151
@testset "$f(x::Array)::Array" for f in DiffTests.ARRAY_TO_ARRAY_FUNCS
108152
v = f(X)
109153
j = ForwardDiff.jacobian(f, X)

test/utils.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Random.seed!(1)
1010
const XLEN = DEFAULT_CHUNK_THRESHOLD + 1
1111
const YLEN = div(DEFAULT_CHUNK_THRESHOLD, 2) + 1
1212
const X, Y = rand(XLEN), rand(YLEN)
13+
const XX, YY = rand(XLEN, XLEN), rand(YLEN, YLEN)
1314
const CHUNK_SIZES = (1, div(DEFAULT_CHUNK_THRESHOLD, 3), div(DEFAULT_CHUNK_THRESHOLD, 2), DEFAULT_CHUNK_THRESHOLD, DEFAULT_CHUNK_THRESHOLD + 1)
1415
const HESSIAN_CHUNK_SIZES = (1, 2, 3)
1516
const FINITEDIFF_ERROR = 3e-5

0 commit comments

Comments
 (0)