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

Wrong type of output? #39900

Open
andreasvarga opened this issue Mar 3, 2021 · 3 comments
Open

Wrong type of output? #39900

andreasvarga opened this issue Mar 3, 2021 · 3 comments
Labels
linear algebra Linear algebra

Comments

@andreasvarga
Copy link
Contributor

The following horizontal concatenation produces the expected result:

julia> [1 rand(3)' rand(1,2) 2]
1×7 Array{Float64,2}:
 1.0  0.277368  0.788148  0.388695  0.495806  0.293525  2.0

However, I wonder if the following one has the correct output type:

julia> [1 rand(3)' 2]
1×5 Adjoint{Float64,Array{Float64,2}}:
 1.0  0.551156  0.578134  0.821385  2.0

I see no reason why the output should be of type Adjoint !

@mgkuhn
Copy link
Contributor

mgkuhn commented Mar 3, 2021

Note that rand(3)' is also of type Adjoint. If you want Julia to actually execute the transpose (i.e., copying the matrix data in memory), rather than just putting a “lazy wrapper” type around rand(3) to remember to permute the dimensions here, then you can collect the result:

julia> collect([1 rand(3)' 2])
1×5 Array{Float64,2}:
 1.0  0.441081  0.621996  0.349487  2.0
julia> [1 collect(rand(3)') 2]
1×5 Array{Float64,2}:
 1.0  0.346152  0.274838  0.194685  2.0

(But I would only do so after having confirmed that this has a performance advantage.)

@mbauman
Copy link
Sponsor Member

mbauman commented Mar 3, 2021

It's not a performance thing in this particular case; it's a behavior thing. Adjoint vectors have some special behaviors like rand(3)' * rand(3) evaluating to a scalar instead of a 1-vector or 1x1 matrix. So it makes sense to preserve that in some cases. Note that we don't preserve the wrapper in the case of a adjoint matrix column:

julia> [1 rand(3, 1)' 2]
1×5 Matrix{Float64}:
 1.0  0.866621  0.70354  0.0792592  2.0

I had no idea that cat had these smarts, but it makes sense to me (and may just fall out of similar or some such).

@KristofferC
Copy link
Sponsor Member

I had no idea that cat had these smarts, but it makes sense to me (and may just fall out of similar or some such).

I think it is explicitly handled:

hcat(avs::Union{Number,AdjointAbsVec}...) = _adjoint_hcat(avs...)

_adjoint_hcat(avs::Union{Number,AdjointAbsVec}...) = adjoint(vcat(map(adjoint, avs)...))

@mbauman mbauman added the linear algebra Linear algebra label Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear algebra Linear algebra
Projects
None yet
Development

No branches or pull requests

4 participants