【问题标题】:Using Julia's pmap() with comprehensions将 Julia 的 pmap() 与推导一起使用
【发布时间】:2018-08-05 00:00:54
【问题描述】:

用一个简单的函数比如

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 pmap


    【解决方案1】:

    您使用的是哪个版本的 Julia?请更新到最新的稳定版本(0.6.x),因为这在当前版本中运行良好,这个 esample 是在 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)
    

    如果您不打算转换或过滤范围内收集的元素,那么您也可以简单地调用collect(4:-.1:2) 而不是[y for y = 4:-.1:2]

    如果您只需要迭代范围的值,那么您甚至不需要收集值,只需按原样使用范围即可,即:

    pmap(fun, 0:.1:2, 4:-.1:2)
    

    【讨论】:

    • 我也在使用 0.6.2,但是 x86_64-w64-mingw32。
    • 好吧,由于未知原因,理解和范围无法完成这项工作,但 collect() 确实有效。也许我的 Julia 安装有问题。感谢您的帮助。
    猜你喜欢
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多