Axes not working on reshaped array?

The problem is that the type of a is a MatrixReshaped, which despite the name is not an ordinary reshaped array, it is defined by Distributions.jl to be a subtype of Distribution, and in particular it is not a subtype of AbstractArray.

axes(a) works because the default axes(a) method in Base works for any type of argument that defines size(a), whereas the default axes(a,d) method requires a::AbstractArray (maybe because it does the special AbstractArray thing where d can be >= ndims(a) in which case it allows indices of 1).

Possible solutions:

  1. add a method Base.axes(a::MatrixReshaped, d::Integer) = axes(a)[d] to Distributions.jl — update, filed add missing axes(::ReshapedDistribution, k) method by stevengj · Pull Request #1892 · JuliaStats/Distributions.jl · GitHub
  2. add a fallback method axes(A, d::Integer) = axes(A)[d] to Base

Option (1) will be quicker to get in the short term, and is a good thing anyway. Basically, if Distributions.jl wants to define its own “array-ish” type which is not an AbstractArray, they are responsible for defining the applicable array methods.

PS. The fact that the type is printed as MatrixReshaped at all is somewhat deceptive — this is a deprecated alias for ReshapedDistribution. Possibly Julia’s type printing chould be improved so that it doesn’t show deprecated names. Update looks like this works if Distributions.jl uses Base.@deprecate_binding, so I’ve filed a PR: mark the MatrixReshaped type as deprecated by stevengj · Pull Request #1893 · JuliaStats/Distributions.jl · GitHub

1 Like