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

Transpose/Adjoint vectors need a broadcast style #32289

Open
mbauman opened this issue Jun 10, 2019 · 2 comments
Open

Transpose/Adjoint vectors need a broadcast style #32289

mbauman opened this issue Jun 10, 2019 · 2 comments
Labels
broadcast Applying a function over a collection

Comments

@mbauman
Copy link
Member

mbauman commented Jun 10, 2019

LinearAlgebra implements broadcasting for transposed/adjoint rows by specializing the broadcast method itself for a handful of combinations of types instead of working within the Broadcast system directly. This leads to the following behaviors:

julia> (1:3)' * 3
1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 3  6  9

julia> (1:3)' .* 3
1×3 Array{Int64,2}:
 3  6  9

julia> broadcast(*, (1:3)', 3)
1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 3  6  9

julia> broadcast(*, (1:3)', Ref(3))
1×3 Array{Int64,2}:
 3  6  9

julia> broadcast(*, (1:3)', (1:3)')
1×3 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:
 1  4  9

julia> broadcast(*, (1:3)', transpose(1:3))
1×3 Array{Int64,2}:
 1  4  9

julia> broadcast(*, transpose(1:3), transpose(1:3))
1×3 LinearAlgebra.Transpose{Int64,Array{Int64,1}}:
 1  4  9

Implementing the style will allow us to more consistently do what we want here — preserve the transposed row — but of course it'll be a minor change in the returned array types. We'll need to decide if, e.g., identity.(sparse(1:3)') should be a sparse matrix or an adjoint sparse vector.

(This is marked with a TODO in the code but I cannot find a corresponding issue.)

@vchuravy
Copy link
Member

One of the challenging aspects is that the broadcast style of parent should be honoured as well. Since Transpose can be used to wrap arbitrary subtypes of AbstractArray, it can be used with types that override broadcast behaviour such as CuArray.

For DistributedArrays I solved this by using nested BroadcastStyles.

@mbauman
Copy link
Member Author

mbauman commented Jun 11, 2019

Yeah, I imagine this working the way the current ad-hoc broadcast methods do — just unwrap all the arguments, re-call broadcast on the unwrapped arrays (which will compute the "nested" style) and then appropriately re-wrap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
broadcast Applying a function over a collection
Projects
None yet
Development

No branches or pull requests

2 participants