`eltype` of a `ReshapedDistribution` ignores the wrapped distribution

Open Beginner friendly
#2,093 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
92/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
julia
Domain
data

Research direction

Start in src/reshaped.jl by comparing the ReshapedDistribution eltype method with the related partype method mentioned in the issue, then reproduce the Float32 example. Done means reshaped Float32 distributions report the wrapped element type and rand returns Float32 rather than Float64.

Written by the indexing model from the issue text.

Description

reshapeing a Float32 distribution reports — and samples — Float64:

julia> using Distributions, LinearAlgebra

julia> dist = MvNormal(zeros(Float32, 6), Diagonal(ones(Float32, 6)));

julia> eltype(dist), eltype(rand(dist))
(Float32, Float32)

julia> d = reshape(dist, 2, 3);

julia> eltype(d), eltype(rand(d))
(Float64, Float64)

src/reshaped.jl defines

Base.eltype(::Type{ReshapedDistribution{<:Any,<:ValueSupport,D}}) where {D} = eltype(D)

ReshapedDistribution{<:Any,<:ValueSupport,D} is a UnionAll, so Type{...} matches only that type itself and never a concrete ReshapedDistribution{2,Continuous,...}. Dispatch falls through to the generic eltype(::Type{<:Sampleable{F,Continuous}}) = Float64 in src/common.jl, and rand then allocates with that.

Spelling the method with <: fixes both:

Base.eltype(::Type{<:ReshapedDistribution{<:Any,<:ValueSupport,D}}) where {D} = eltype(D)

Found while reviewing #2072, whose new partype method for ReshapedDistribution uses the <: form.

Distributions v0.25.131, Julia 1.12.7.

Dominant language
Julia
Stars
1.2k
Forks
447
PR merge metrics
No merged PRs in 30d

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.

More from JuliaStats/Distributions.jl

All issues in JuliaStats/Distributions.jl

Similar issues

More Julia issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.