在 Julia 中从 python 替代 np.meshgrid()?

Moh*_*aad 2 numpy mesh julia

我想知道,np.meshgrid()Julia 中的 NumPy-Python是否有替代品?

#Python 参考:https ://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html

jul*_*ohm 5

请记住,在 Julia 中您通常可以避免meshgrid使用智能广播操作。例如。给定一个f(x, y)对标量值和两个“向量” xsand 进行运算的函数ys,您可以编写f.(xs, ys')生成几乎任何结果meshgrid都会给您和更多:

julia> xs = 0:0.5:1
0.0:0.5:1.0

julia> ys = 0.0:1.0
0.0:1.0:1.0

julia> f(x, y) = (x, y)                # equivalent to meshgrid
f (generic function with 1 method)

julia> f.(xs, ys')
3×2 Matrix{Tuple{Float64, Float64}}:
 (0.0, 0.0)  (0.0, 1.0)
 (0.5, 0.0)  (0.5, 1.0)
 (1.0, 0.0)  (1.0, 1.0)

julia> g(x, y) = x*y                   # more efficient than meshgrid + product
g (generic function with 1 method)

julia> g.(xs, ys')
3×2 Matrix{Float64}:
 0.0  0.0
 0.0  0.5
 0.0  1.0
Run Code Online (Sandbox Code Playgroud)