结合理解使用 Julia 的 pmap()
Using Julia's pmap() with comprehensions
用简单的函数如
function fun(x::Real, y::Real)
x, y
end
我想通过
使用 pmap() 调用
pmap(fun, [x for x=0:.1:2], [y for y=4:-.1:2])
Julia 给出了这个错误
ERROR: LoadError: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of type AbstractFloat
This may have arisen from a call to the constructor AbstractFloat(...),
since type constructors fall back to convert methods.
我不太明白这里发生了什么。
根据我所做的一些研究:
It's well-established that to call map on an N-argument function, you pass N lists (or whatever collection) to map:
julia> map(+, (1,2), (3,4))
(4,6)
那怎么了?
您使用的是哪个版本的 Julia?请更新到最新的稳定版本 (0.6.x),因为这在当前版本中工作正常,这个示例是 运行 在 JuliaBox:
jrun@notebook-0hnhf:/home/jrun$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _' | |
| | |_| | | | (_| | | Version 0.6.2 (2017-12-13 18:08 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu'
julia> function fun(x::Real, y::Real)
x, y
end
fun (generic function with 1 method)
julia> pmap(fun, [x for x = 0:.1:2], [y for y = 4:-.1:2])
21-element Array{Tuple{Float64,Float64},1}:
(0.0, 4.0)
(0.1, 3.9)
(0.2, 3.8)
(0.3, 3.7)
(0.4, 3.6)
(0.5, 3.5)
(0.6, 3.4)
(0.7, 3.3)
(0.8, 3.2)
(0.9, 3.1)
(1.0, 3.0)
(1.1, 2.9)
(1.2, 2.8)
(1.3, 2.7)
(1.4, 2.6)
(1.5, 2.5)
(1.6, 2.4)
(1.7, 2.3)
(1.8, 2.2)
(1.9, 2.1)
(2.0, 2.0)
如果您不打算对 运行ge 的收集元素进行 t运行sform 或过滤,那么您也可以简单地调用 collect(4:-.1:2)
而不是 [y for y = 4:-.1:2]
.
如果您只需要遍历 运行ge 的值,那么您甚至不需要收集这些值,只需按原样使用 运行ge 即可,即:
pmap(fun, 0:.1:2, 4:-.1:2)
用简单的函数如
function fun(x::Real, y::Real)
x, y
end
我想通过
使用 pmap() 调用pmap(fun, [x for x=0:.1:2], [y for y=4:-.1:2])
Julia 给出了这个错误
ERROR: LoadError: MethodError: Cannot `convert` an object of type Tuple{Float64,Float64} to an object of type AbstractFloat
This may have arisen from a call to the constructor AbstractFloat(...),
since type constructors fall back to convert methods.
我不太明白这里发生了什么。
根据我所做的一些研究:
It's well-established that to call map on an N-argument function, you pass N lists (or whatever collection) to map:
julia> map(+, (1,2), (3,4))
(4,6)
那怎么了?
您使用的是哪个版本的 Julia?请更新到最新的稳定版本 (0.6.x),因为这在当前版本中工作正常,这个示例是 运行 在 JuliaBox:
jrun@notebook-0hnhf:/home/jrun$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _' | |
| | |_| | | | (_| | | Version 0.6.2 (2017-12-13 18:08 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu'
julia> function fun(x::Real, y::Real)
x, y
end
fun (generic function with 1 method)
julia> pmap(fun, [x for x = 0:.1:2], [y for y = 4:-.1:2])
21-element Array{Tuple{Float64,Float64},1}:
(0.0, 4.0)
(0.1, 3.9)
(0.2, 3.8)
(0.3, 3.7)
(0.4, 3.6)
(0.5, 3.5)
(0.6, 3.4)
(0.7, 3.3)
(0.8, 3.2)
(0.9, 3.1)
(1.0, 3.0)
(1.1, 2.9)
(1.2, 2.8)
(1.3, 2.7)
(1.4, 2.6)
(1.5, 2.5)
(1.6, 2.4)
(1.7, 2.3)
(1.8, 2.2)
(1.9, 2.1)
(2.0, 2.0)
如果您不打算对 运行ge 的收集元素进行 t运行sform 或过滤,那么您也可以简单地调用 collect(4:-.1:2)
而不是 [y for y = 4:-.1:2]
.
如果您只需要遍历 运行ge 的值,那么您甚至不需要收集这些值,只需按原样使用 运行ge 即可,即:
pmap(fun, 0:.1:2, 4:-.1:2)