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

(*)(::Adjoint{Vector}, ::Vector) should use a non-recursive dot product #35174

Closed
MasonProtter opened this issue Mar 19, 2020 · 2 comments · Fixed by #35257
Closed

(*)(::Adjoint{Vector}, ::Vector) should use a non-recursive dot product #35174

MasonProtter opened this issue Mar 19, 2020 · 2 comments · Fixed by #35257

Comments

@MasonProtter
Copy link
Contributor

MasonProtter commented Mar 19, 2020

This is somewhat tightly related to #22220, but I think a bit different:

julia> σ = [[0  1
             1  0],
            [0 -im
             im 0],
            [1  0
             0  -1]]
3-element Array{Array{Complex{Int64},2},1}:
 [0 + 0im 1 + 0im; 1 + 0im 0 + 0im]
 [0 + 0im 0 - 1im; 0 + 1im 0 + 0im]
 [1 + 0im 0 + 0im; 0 + 0im -1 + 0im]

julia> [1 2 3] * σ
1-element Array{Array{Complex{Int64},2},1}:
 [3 + 0im 1 - 2im; 1 + 2im -3 + 0im]

julia> [1,2,3]'* σ
ERROR: DimensionMismatch("x and y are of different lengths!")
Stacktrace:
 [1] dot at /Users/mason/julia/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/generic.jl:879 [inlined]
 [2] dot(::Array{Int64,1}, ::Array{Array{Complex{Int64},2},1}) at /Users/mason/julia/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/generic.jl:894
 [3] *(::Adjoint{Int64,Array{Int64,1}}, ::Array{Array{Complex{Int64},2},1}) at /Users/mason/julia/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/adjtrans.jl:245
 [4] top-level scope at REPL[46]:1

Here we see that [1 2 3] * σ did something quite different from [1,2,3]'* σ: it essentially did what one should expect given the definitions you see in a linear algebra class, but [1,2,3]'* σ tries to recursively apply dot down to each level of the arrays which produces the above error.

To make matters worse, if I try to 'lift' my vector [1, 2, 3] to the same eltype as σ, I get a disastrously 'wrong' answer:

julia> 𝟙 = [1 0; 0 1];

julia> [1*𝟙, 2*𝟙, 3*𝟙]' * σ
0 + 0im

This seems to come down to the fact that dot is recursive, but I'd argue that even if we want dot to be recursive, * should not be recursive.

Perhaps the solution is a non-recursive dot that * would lower to instead of the recursive form?

This would be unfortunately breaking.


Thanks @ericphanson for making clear to me that the source of my issue is the recursive part.

@MasonProtter
Copy link
Contributor Author

MasonProtter commented Mar 19, 2020

Perhaps even more strange is that transpose actually does the right thing here, but not adjoint, so maybe there is a path to arguing that this change would be a bug fix?

julia> transpose([1,2,3])*σ
2×2 Array{Complex{Int64},2}:
 3+0im   1-2im
 1+2im  -3+0im

(credit to Syx Pek on Slack for noticing this)

@MasonProtter
Copy link
Contributor Author

#27677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant