Skip to content

Commit 5076373

Browse files
authored
Implement isapprox for Composites (#75)
* Implement isapprox for Composites * Bump version number * Add a test
1 parent 6f5cd3f commit 5076373

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ChainRulesTestUtils"
22
uuid = "cdddcdb0-9152-4a09-a978-84456f9df70a"
3-
version = "0.5.3"
3+
version = "0.5.4"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/isapprox.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ function Base.isapprox(
1515
) where {P <: Tuple}
1616
return isapprox_vec(d_ad, d_fd; kwargs...)
1717
end
18+
19+
function Base.isapprox(
20+
d_ad::Composite{P, <:NamedTuple{T}}, d_fd::Composite{P, <:NamedTuple{T}}; kwargs...,
21+
) where {P, T}
22+
return isapprox_vec(d_ad, d_fd; kwargs...)
23+
end
24+
25+
1826
# Must be for same primal
1927
Base.isapprox(d_ad::Composite{P}, d_fd::Composite{Q}; kwargs...) where {P, Q} = false
20-

test/isapprox.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
# both of these are reasonable diffentials for the `Tuple{Int, Int}` primal
1515
@test Composite{Tuple{Int, Int}}(1f0, 2f0) Composite{Tuple{Int, Int}}(1.0, 2.0)
1616

17+
D = Diagonal(randn(5))
18+
@test Composite{typeof(D)}(diag=D.diag) Composite{typeof(D)}(diag=D.diag)
19+
1720
# But these have different primals so should not be equal
1821
@test !(Composite{Tuple{Int, Int}}(1.0, 2.0) Composite{Tuple{Float64, Float64}}(1.0, 2.0))
1922
end

0 commit comments

Comments
 (0)