-
Notifications
You must be signed in to change notification settings - Fork 3
Add realdot
#2
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
Merged
Merged
Add realdot
#2
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
537fbad
Add missing complex tests and rules (#216)
sethaxen 4b08ee2
rename differentials (#413)
mzgubic 15ae11e
Rename to `realconjtimes` and `imagconjtimes` and export them
devmotion 262f9a9
Add tests
devmotion 8a750f2
Fix tests with Julia 1.0
devmotion b91d03d
Rename to `realdot` and `imagdot`
devmotion 360dcb9
Add dispatch for real arrays
devmotion d6ba3be
Update src/utils.jl
devmotion 25bbc2a
Generalize `::Complex` to `::Number`
devmotion 638a6f5
Rename `utils.jl` to `complex_math.jl`
devmotion 96b44e9
Remove `imagdot`
devmotion 073001f
Merge remote-tracking branch 'tmp/dw/realdot' into dw/realdot
devmotion ce9c71e
Add `realdot`
devmotion 5de6e9b
Update README
devmotion e48caee
Apply suggestions from code review
devmotion 5618631
Update README.md
devmotion 7194ee9
Update src/RealDot.jl
devmotion 1ecb63d
Add test with quaternions
devmotion c6d1099
Fix quaternion multiplication
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
module RealDot | ||
|
||
# Write your package code here. | ||
using LinearAlgebra: LinearAlgebra | ||
|
||
export realdot | ||
|
||
""" | ||
realdot(x, y) | ||
|
||
Compute `real(dot(x, y))` while avoiding computing the imaginary part if possible. | ||
|
||
This function can be useful if you work with derivatives of functions on complex | ||
numbers. In particular, this computation shows up in pullbacks for non-holomorphic | ||
functions. | ||
""" | ||
@inline realdot(x, y) = real(LinearAlgebra.dot(x, y)) | ||
@inline realdot(x::Complex, y::Complex) = 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 | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,63 @@ | ||
using RealDot | ||
using LinearAlgebra | ||
using Test | ||
|
||
# struct need to be defined outside of tests for julia 1.0 compat | ||
# custom complex number (tests fallback definition) | ||
struct CustomComplex{T} <: Number | ||
re::T | ||
im::T | ||
end | ||
|
||
Base.real(x::CustomComplex) = x.re | ||
Base.imag(x::CustomComplex) = x.im | ||
|
||
Base.conj(x::CustomComplex) = CustomComplex(x.re, -x.im) | ||
|
||
function Base.:*(x::CustomComplex, y::Union{Real,Complex}) | ||
return CustomComplex(reim(Complex(reim(x)...) * y)...) | ||
end | ||
Base.:*(x::Union{Real,Complex}, y::CustomComplex) = y * x | ||
function Base.:*(x::CustomComplex, y::CustomComplex) | ||
return CustomComplex(reim(Complex(reim(x)...) * Complex(reim(y)...))...) | ||
end | ||
|
||
# custom quaternion to test definition for hypercomplex numbers | ||
# adapted from Quaternions.jl | ||
struct Quaternion{T<:Real} <: Number | ||
s::T | ||
v1::T | ||
v2::T | ||
v3::T | ||
end | ||
|
||
Base.real(q::Quaternion) = q.s | ||
Base.conj(q::Quaternion) = Quaternion(q.s, -q.v1, -q.v2, -q.v3) | ||
|
||
function Base.:*(q::Quaternion, w::Quaternion) | ||
return Quaternion( | ||
q.s * w.s - q.v1 * w.v1 - q.v2 * w.v2 - q.v3 * w.v3, | ||
q.s * w.v1 + q.v1 * w.s + q.v2 * w.v3 - q.v3 * w.v2, | ||
q.s * w.v2 - q.v1 * w.v3 + q.v2 * w.s + q.v3 * w.v1, | ||
q.s * w.v3 + q.v1 * w.v2 - q.v2 * w.v1 + q.v3 * w.s, | ||
) | ||
end | ||
|
||
function Base.:*(q::Quaternion, w::Union{Real,Complex,CustomComplex}) | ||
a, b = reim(w) | ||
return q * Quaternion(a, b, zero(a), zero(a)) | ||
end | ||
Base.:*(w::Union{Real,Complex,CustomComplex}, q::Quaternion) = conj(conj(q) * conj(w)) | ||
|
||
@testset "RealDot.jl" begin | ||
# Write your tests here. | ||
scalars = ( | ||
randn(), randn(ComplexF64), CustomComplex(randn(2)...), Quaternion(randn(4)...) | ||
) | ||
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)) | ||
end | ||
end | ||
end |
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.
Uh oh!
There was an error while loading. Please reload this page.