JuliaArrays / JuliaArrays/AxisArrays.jl

Unexpected type returned from multiplication of adjoint AxisArray

Open
#189 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
207
Forks
41
PR merge metrics
No merged PRs in 30d

Description

Minimal example (using AxisArrays 0.4.3 and julia 1.5):

julia> v = rand(3);

julia> v_ax = AxisArray(v);

julia> typeof(v')
LinearAlgebra.Adjoint{Float64,Array{Float64,1}}

julia> typeof(v_ax')
AxisArray{Float64,2,LinearAlgebra.Adjoint{Float64,Array{Float64,1}},Tuple{Axis{:transpose,Base.OneTo{Int64}},Axis{:row,Base.OneTo{Int64}}}}

The expected return of the following operation would be a scalar.

julia> typeof(v' * rand(3, 3) * v)
Float64

julia> typeof(v_ax' * rand(3, 3) * v_ax)
Array{Float64,1}

But as shown the operation using AxisArray returns an Array. Looking into the methods involved in these operations:

julia> y = rand(3, 3) * v_ax;

julia> @which v_ax' * y
*(A::AbstractArray{T,2}, x::AbstractArray{S,1}) where {T, S} in LinearAlgebra at /Applications/Julia-1.5.app/Contents/Resources/julia/share/julia/stdlib/v1.5/LinearAlgebra/src/matmul.jl:49

The method called by the multiplication of v_ax' and y is:

function (*)(A::AbstractMatrix{T}, x::AbstractVector{S}) where {T,S}
    TS = promote_op(matprod, T, S)
    mul!(similar(x,TS,axes(A,1)),A,x)
end

which shows why the return is an Array. A new method might be needed here so that v_ax' * y returns the expected scalar.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with AxisArrays 0.4.3 and Julia 1.5, then inspect the multiplication dispatch shown from LinearAlgebra/src/matmul.jl, especially the AbstractMatrix–AbstractVector method used by v_ax' * y. Done means the AxisArray expression returns the expected scalar rather than an Array, with the behavior verified against the provided example.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.