Skip to content

Commit

Permalink
Implement isapprox for Composites (#75)
Browse files Browse the repository at this point in the history
* Implement isapprox for Composites

* Bump version number

* Add a test
  • Loading branch information
willtebbutt authored Nov 30, 2020
1 parent 6f5cd3f commit 5076373
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesTestUtils"
uuid = "cdddcdb0-9152-4a09-a978-84456f9df70a"
version = "0.5.3"
version = "0.5.4"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
9 changes: 8 additions & 1 deletion src/isapprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ function Base.isapprox(
) where {P <: Tuple}
return isapprox_vec(d_ad, d_fd; kwargs...)
end

function Base.isapprox(
d_ad::Composite{P, <:NamedTuple{T}}, d_fd::Composite{P, <:NamedTuple{T}}; kwargs...,
) where {P, T}
return isapprox_vec(d_ad, d_fd; kwargs...)
end


# Must be for same primal
Base.isapprox(d_ad::Composite{P}, d_fd::Composite{Q}; kwargs...) where {P, Q} = false

3 changes: 3 additions & 0 deletions test/isapprox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
# both of these are reasonable diffentials for the `Tuple{Int, Int}` primal
@test Composite{Tuple{Int, Int}}(1f0, 2f0) Composite{Tuple{Int, Int}}(1.0, 2.0)

D = Diagonal(randn(5))
@test Composite{typeof(D)}(diag=D.diag) Composite{typeof(D)}(diag=D.diag)

# But these have different primals so should not be equal
@test !(Composite{Tuple{Int, Int}}(1.0, 2.0) Composite{Tuple{Float64, Float64}}(1.0, 2.0))
end
Expand Down

2 comments on commit 5076373

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/25556

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.4 -m "<description of version>" 507637312fe85e0acd32a8172c25a80b7530b6b9
git push origin v0.5.4

Please sign in to comment.