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

dot(::Adjoint, ::Adjoint) incorrect for numbers that are not multiplicatively commutative #908

Closed
sethaxen opened this issue Feb 12, 2022 · 0 comments · Fixed by JuliaLang/julia#44219
Labels
bug Something isn't working

Comments

@sethaxen
Copy link
Contributor

sethaxen commented Feb 12, 2022

The overload dot(x::Adjoint, y::Adjoint) assumes that the number eltypes of the inputs commute under multiplication, so that dot(x', y') == sum(x .* y') == conj(sum(x' .* y)) == conj(dot(x, y))
https://github.com/JuliaLang/julia/blob/1aac07efe44c229abf41b9c50031783f46bed4bf/stdlib/LinearAlgebra/src/generic.jl#L889

For numbers that don't commute under multiplication (like Quaternions), this definition is incorrect. A short demo:

julia> using Quaternions, LinearAlgebra

julia> mydot(x, y) = mapreduce(mydot, +, x, y);  # define our own recursive dot function

julia> mydot(x::Number, y::Number) = dot(x, y)

julia> x = [quatrand() for _ in 1:2, _ in 1:2];

julia> y = [quatrand() for _ in 1:2, _ in 1:2];

julia> dot(x, y)  mydot(x, y)  # fine
true

julia> dot(transpose(x), transpose(y))  mydot(transpose(x), transpose(y))   # fine
true

julia> dot(x', y')  mydot(x', y')   # uh-oh
false

julia> method = @which dot(x', y')
dot(x::Adjoint, y::Adjoint) in LinearAlgebra at /Applications/Julia-1.7.app/Contents/Resources/julia/share/julia/stdlib/v1.7/LinearAlgebra/src/generic.jl:922

julia> Base.delete_method(method)

julia> dot(x', y')  mydot(x', y')   # all good
true

This could be handled by restricting the types to Union{Real,Complex}, e.g.

 dot(x::Adjoint{Union{Real,Complex}}, y::Adjoint{Union{Real,Complex}}) = conj(dot(parent(x), parent(y))) 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants