-
Notifications
You must be signed in to change notification settings - Fork 64
Add realdot
and imagdot
from ChainRules
#474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
537fbad
Add missing complex tests and rules (#216)
sethaxen 4b08ee2
rename differentials (#413)
mzgubic ace93fb
Add src/rulesets/Base/utils.jl from ChainRules.jl
devmotion 0d2269e
Rename to `realconjtimes` and `imagconjtimes` and export them
devmotion b1a89ec
Add tests
devmotion 3d05206
Add documentation
devmotion d6c6c2a
Bump version
devmotion d9d046f
Fix tests with Julia 1.0
devmotion 1747c42
Rename to `realdot` and `imagdot`
devmotion 630355e
Merge branch 'master' into dw/conjtimes
devmotion ff5e71e
Add dispatch for real arrays
devmotion 9c3cab2
Merge branch 'dw/conjtimes' of github.com:JuliaDiff/ChainRulesCore.jl…
devmotion 5155c75
Update src/utils.jl
devmotion 5fb7ff2
Generalize `::Complex` to `::Number`
devmotion a042dd0
Rename `utils.jl` to `complex_math.jl`
devmotion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
""" | ||
realdot(x, y) | ||
|
||
Compute `real(dot(x, y))` while avoiding computing the imaginary part if possible. | ||
|
||
This function can be useful if you implement a `rrule` for a non-holomorphic function | ||
on complex numbers. | ||
|
||
See also: [`imagdot`](@ref) | ||
""" | ||
@inline realdot(x, y) = real(dot(x, y)) | ||
@inline realdot(x::Number, y::Number) = muladd(real(x), real(y), imag(x) * imag(y)) | ||
@inline realdot(x::Real, y::Number) = x * real(y) | ||
@inline realdot(x::Number, y::Real) = real(x) * y | ||
@inline realdot(x::Real, y::Real) = x * y | ||
|
||
""" | ||
imagdot(x, y) | ||
|
||
Compute `imag(dot(x, y))` while avoiding computing the real part if possible. | ||
|
||
This function can be useful if you implement a `rrule` for a non-holomorphic function | ||
on complex numbers. | ||
|
||
See also: [`realdot`](@ref) | ||
""" | ||
@inline imagdot(x, y) = imag(dot(x, y)) | ||
@inline function imagdot(x::Number, y::Number) | ||
return muladd(-imag(x), real(y), real(x) * imag(y)) | ||
end | ||
@inline imagdot(x::Real, y::Number) = x * imag(y) | ||
@inline imagdot(x::Number, y::Real) = -imag(x) * y | ||
@inline imagdot(x::Real, y::Real) = ZeroTangent() | ||
@inline imagdot(x::AbstractArray{<:Real}, y::AbstractArray{<:Real}) = ZeroTangent() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# struct need to be defined outside of tests for julia 1.0 compat | ||
# custom complex number to test fallback definition | ||
struct CustomComplex{T} | ||
re::T | ||
im::T | ||
end | ||
|
||
Base.real(x::CustomComplex) = x.re | ||
Base.imag(x::CustomComplex) = x.im | ||
|
||
function LinearAlgebra.dot(a::CustomComplex, b::Number) | ||
return CustomComplex(reim((a.re - a.im * im) * b)...) | ||
end | ||
function LinearAlgebra.dot(a::Number, b::CustomComplex) | ||
return CustomComplex(reim(conj(a) * (b.re + b.im * im))...) | ||
end | ||
function LinearAlgebra.dot(a::CustomComplex, b::CustomComplex) | ||
return CustomComplex(reim((a.re - a.im * im) * (b.re + b.im * im))...) | ||
end | ||
|
||
@testset "complex_math.jl" begin | ||
@testset "dot" begin | ||
scalars = (randn(), randn(ComplexF64), CustomComplex(reim(randn(ComplexF64))...)) | ||
arrays = (randn(10), randn(ComplexF64, 10)) | ||
for inputs in (scalars, arrays) | ||
for x in inputs, y in inputs | ||
@test realdot(x, y) == real(dot(x, y)) | ||
|
||
if eltype(x) <: Real && eltype(y) <: Real | ||
@test imagdot(x, y) === ZeroTangent() | ||
else | ||
@test imagdot(x, y) == imag(dot(x, y)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe they should be moved to
src/rule_definition_tools.jl
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That file is full of metaprogramming stuff.
It doesn't need this.
Files called
utils.jl
are always a mistake.I think something like
complex_math.jl
as it currently stands,or
additional_mathematical_functions.jl
if we think we will need more of them later.