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

MethodError on initializing DiscreteNonParametric with @view #1941

Closed
Uroc327 opened this issue Jan 21, 2025 · 1 comment
Closed

MethodError on initializing DiscreteNonParametric with @view #1941

Uroc327 opened this issue Jan 21, 2025 · 1 comment

Comments

@Uroc327
Copy link

Uroc327 commented Jan 21, 2025

I would like to use DiscreteNonParametric to wrap existing arrays of symbols and probabilities in a type-safe manner, e.g., to pass it to a function taking ::DiscreteUnivariateDistribution. This does not compile, though. The code

using Distributions
a = [1, 2, 3]
p = [0.5, 0.5]
DiscreteNonParametric((@view a[2:end]), p)

yields

ERROR: MethodError: Cannot `convert` an object of type Vector{Int64} to an object of type SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}
The function `convert` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  SubArray{T, N, P, I, L}(::Any, ::Any, ::Any, ::Any) where {T, N, P, I, L}
   @ Base subarray.jl:19
  convert(::Type{T}, ::T) where T
   @ Base Base.jl:126
  convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray
   @ LinearAlgebra ~/.julia/juliaup/julia-1.11.2+0.x64.linux.gnu/share/julia/stdlib/v1.11/LinearAlgebra/src/factorization.jl:104
  ...

Stacktrace:
 [1] DiscreteNonParametric{Int64, Float64, SubArray{Int64, 1, Vector{…}, Tuple{…}, true}, Vector{Float64}}(xs::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{…}}, true}, ps::Vector{Float64}; check_args::Bool)
   @ Distributions ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:34
 [2] DiscreteNonParametric
   @ ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:24 [inlined]
 [3] DiscreteNonParametric(vs::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, ps::Vector{Float64})
   @ Distributions ~/.julia/packages/Distributions/cWeit/src/univariate/discrete/discretenonparametric.jl:38
 [4] top-level scope
   @ REPL[4]:1
Some type information was truncated. Use `show(err)` to see complete types.

If the probability distribution is stored and processed, I can easily use DiscreteNonParametric(a[2:end], p). However, the extra allocation is inefficient if the type is only used as a wrapper.

@devmotion
Copy link
Member

This issue is fixed by #1908. I'll close the issue as a duplicate of #1832.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants