Why `sprand` does not accept a Distribution arg?

WalterMadelim
Sat, 22 Mar 2025 07:38:30 +0000
General Usage

Hi there,
I wonder why this cannot work out (What is the available method currently then?).

julia> import SparseArrays.sprand as sprand

julia> import Distributions.Uniform as UD

julia> sprand(5, 6, .7)
5×6 SparseArrays.SparseMatrixCSC{Float64, Int64} with 23 stored entries:
 0.213111  0.611876  0.592097   ⋅         ⋅         ⋅
 0.50981   0.326668  0.792674  0.227358  0.322273  0.616509
 0.9323    0.136838  0.56782   0.843999  0.718162  0.475489
 0.222931  0.582418   ⋅        0.412739   ⋅        0.942747
  ⋅        0.290387   ⋅        0.876276  0.449718  0.408883

julia> rand(UD(-5, 4), 5, 6)
5×6 Matrix{Float64}:
 -3.24126  -0.896462  -2.59361    0.11748   0.917439  -0.742394
  3.61575   3.95655   -4.05713   -2.69737  -0.846111  -3.27312
 -2.39694  -0.194496   0.946839  -3.9091    2.11152   -4.85219
  2.77711   0.618885  -1.53174   -4.30585   2.2522     1.27031
 -1.65799  -1.4638    -4.91182   -1.24282   2.79811    3.06926

julia> sprand(UD(-5, 4), 5, 6, .7)
ERROR: MethodError: no method matching sprand(::Distributions.Uniform{Float64}, ::Int64, ::Int64, ::Float64)
The function `sprand` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  sprand(::Type{T}, ::Integer, ::Integer, ::AbstractFloat) where T
   @ SparseArrays K:\julia-1.11.4\share\julia\stdlib\v1.11\SparseArrays\src\sparsematrix.jl:2048
  sprand(::Random.AbstractRNG, ::Integer, ::Integer, ::AbstractFloat)
   @ SparseArrays K:\julia-1.11.4\share\julia\stdlib\v1.11\SparseArrays\src\sparsematrix.jl:2042
  sprand(::Random.AbstractRNG, ::Integer, ::Integer, ::AbstractFloat, ::Function, ::Type{T}) where T
   @ SparseArrays K:\julia-1.11.4\share\julia\stdlib\v1.11\SparseArrays\src\sparsematrix.jl:2027
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

11 posts - 4 participants