Skip to content

ArrayInterface.size vs StaticArrays.Size #182

@cscherrer

Description

@cscherrer

Hi,

I'd like to get a better understanding of the intending idioms for working with arrays, especially the case where we want different methods for the static vs dynamic case.

StaticArrays.Size solves the problem like this:

julia> Size(zeros(SMatrix{3, 4}))
Size(3, 4)

julia> Size(zeros(3, 4))
Size(StaticArrays.Dynamic(), StaticArrays.Dynamic())

Though we might have some missing results, it's nice that the size computation itself is always static. Working with types is also convenient:

julia> Size(typeof(zeros(SMatrix{3, 4})))
Size(3, 4)

julia> Size(typeof(zeros(3, 4)))
Size(StaticArrays.Dynamic(), StaticArrays.Dynamic())

By contrast, here's ArrayInterface.size:

julia> ArrayInterface.size(zeros(SMatrix{3, 4}))
(static(3), static(4))

julia> ArrayInterface.size(zeros(3, 4))
(3, 4)

That last result might seem convenient, but computing this requires dynamic dispatch, which seems to defeat the purpose. And it fails on types:

julia> ArrayInterface.size(typeof(zeros(3, 4)))
ERROR: MethodError: no method matching size(::Type{Matrix{Float64}})
Closest candidates are:
  size(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:524
  size(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}, ::Integer) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:523
  size(::Union{LinearAlgebra.Cholesky, LinearAlgebra.CholeskyPivoted}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/cholesky.jl:442
  ...
Stacktrace:
 [1] axes
   @ ./abstractarray.jl:89 [inlined]
 [2] axes
   @ ~/.julia/packages/ArrayInterface/CmPZc/src/axes.jl:166 [inlined]
 [3] size(a::Type{Matrix{Float64}})
   @ ArrayInterface ~/.julia/packages/ArrayInterface/CmPZc/src/size.jl:18
 [4] top-level scope
   @ REPL[95]:1

julia> ArrayInterface.size(typeof(zeros(3, 4)))
ERROR: MethodError: no method matching size(::Type{Matrix{Float64}})
Closest candidates are:
  size(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:524
  size(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}, ::Integer) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/qr.jl:523
  size(::Union{LinearAlgebra.Cholesky, LinearAlgebra.CholeskyPivoted}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/LinearAlgebra/src/cholesky.jl:442
  ...
Stacktrace:
 [1] axes
   @ ./abstractarray.jl:89 [inlined]
 [2] axes
   @ ~/.julia/packages/ArrayInterface/CmPZc/src/axes.jl:166 [inlined]
 [3] size(a::Type{Matrix{Float64}})
   @ ArrayInterface ~/.julia/packages/ArrayInterface/CmPZc/src/size.jl:18
 [4] top-level scope
   @ REPL[95]:1

Is ArrayInterface.size intended to be used for building generated functions or for something else? In what cases does it have benefits over StaticArrays.Size?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions