Skip to content
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

Add realdot #2

Merged
merged 19 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ uuid = "c1ae055f-0cd5-4b69-90a6-9a35b1a98df9"
authors = ["David Widmann"]
version = "0.1.0"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[compat]
julia = "1"

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@
[![Coverage](https://codecov.io/gh/devmotion/RealDot.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/devmotion/RealDot.jl)
[![Coverage](https://coveralls.io/repos/github/devmotion/RealDot.jl/badge.svg?branch=main)](https://coveralls.io/github/devmotion/RealDot.jl?branch=main)
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)

This package only contains and exports a single function `realdot(x, y)`. It computes
`real(LinearAlgebra.dot(x, y))` while avoiding computing the imaginary part of
`LinearAlgebra.dot((x, y)` if possible.
devmotion marked this conversation as resolved.
Show resolved Hide resolved

devmotion marked this conversation as resolved.
Show resolved Hide resolved
This function can be useful e.g. if you define pullbacks for non-holomorphic functions
(see e.g. [this discussion in the ChainRulesCore.jl repo](https://github.com/JuliaDiff/ChainRulesCore.jl/pull/474)). It was implemented initially in [ChainRules.jl](https://github.com/JuliaDiff/ChainRules.jl) in [this PR](https://github.com/JuliaDiff/ChainRules.jl/pull/216) as `_realconjtimes`.
19 changes: 18 additions & 1 deletion src/RealDot.jl
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::Number, y::Number) = muladd(real(x), real(y), imag(x) * imag(y))
devmotion marked this conversation as resolved.
Show resolved Hide resolved
@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
30 changes: 29 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
using RealDot
using LinearAlgebra
using Test

# 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 "RealDot.jl" begin
# Write your tests here.
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))
end
end
end