本文介绍了用数组eltype实现一个采样器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,使用rand更加容易...我想我遵循了中添加了simple-sampler-without-pre-compute-data-1"rel =" nofollow noreferrer>,但它似乎不像是采样器返回数组:

Hooking into rand has been easier in the old days... I think I followed the description in the docs, but it doesn't seem to like a sampler returning an array:

using Random

struct Shell{N}
    r0::Float64
    r1::Float64
end

Base.eltype(::Type{Shell{N}}) where {N} = Array{Float64, N}

function Random.rand(rng::Random.AbstractRNG, d::Random.SamplerTrivial{Shell{N}}) where {N}
    # ignore the correctness of the sampling algorithm for now :)
    shell = d[]
    Δ = shell.r1 - shell.r0
    θ = Δ .* randn(N)
    r = shell.r0 .+ θ .* .√rand(N) 
end

测试:

julia> rand(Shell{2}(0, 1))
2-element Array{Float64,1}:
 0.5165139561555491  
 0.035180151872393726

julia> rand(Shell{2}(0, 1), 2)
ERROR: MethodError: no method matching Array{Float64,2}(::Array{Float64,1})
Closest candidates are:
  Array{Float64,2}(::AbstractArray{S,N}) where {T, N, S} at array.jl:498
  Array{Float64,2}(::UndefInitializer, ::Int64, ::Int64) where T at boot.jl:406
  Array{Float64,2}(::UndefInitializer, ::Int64...) where {T, N} at boot.jl:410
  ...
Stacktrace:
 [1] convert(::Type{Array{Float64,2}}, ::Array{Float64,1}) at ./array.jl:490
 [2] setindex!(::Array{Array{Float64,2},1}, ::Array{Float64,1}, ::Int64) at ./array.jl:782
 [3] rand! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Random/src/Random.jl:271 [inlined]
 [4] rand! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Random/src/Random.jl:266 [inlined]
 [5] rand at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Random/src/Random.jl:279 [inlined]
 [6] rand at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Random/src/Random.jl:280 [inlined]
 [7] rand(::Shell{2}, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/Random/src/Random.jl:283
 [8] top-level scope at REPL[14]:1

我想念什么?

推荐答案

您对rand的定义始终返回Array{Float64,1},但是您声明了它返回Array{Float,N}(通过您对eltype的定义).这就是错误告诉您的内容:将新生成的Vector{Float64}eltype Array{Float64,2}放入结果数组中,必须进行转换,但没有定义此类转换,因此会出现错误.

Your definition of rand always returns an Array{Float64,1}, yet you declared that it returns Array{Float,N} (via your definition of eltype). This is what the error tells you: to put a freshly produced Vector{Float64} into the result array with eltype Array{Float64,2}, a conversion has to be made, but no such conversion is defined, hence the error.

在这种情况下,将eltype定义更改为Base.eltype(::Type{<:Shell}} = Array{Float64, 1}似乎可以解决您的问题.

In this case, it seems that changing the eltype definition to Base.eltype(::Type{<:Shell}} = Array{Float64, 1} would solve your problem.

这篇关于用数组eltype实现一个采样器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!