朱莉娅相当于 Python numpy "arange"
Julia equivalent of Python numpy "arange"
在 Python 中,我可以使用
创建一个间隔均匀的值数组
xi2 = np.arange(0, np.sqrt(6), 1e-3)
如何在 Julia 中编写此代码?我试过了,
xi2 = range(0,sqrt(6),step=1e-3)
但是这个returns0.0:0.001:2.449
结果 0.0:0.001:2.449
确实是一系列均匀分布的值,可以像其他 AbstractArray
一样进行索引、切片、广播和一般使用。例如:
julia> xi2 = range(0,sqrt(6),step=1e-3)
0.0:0.001:2.449
julia> xi2[1]
0.0
julia> xi2[100]
0.099
julia> length(xi2)
2450
julia> isa(xi2, AbstractArray)
true
julia> sin.(xi2)
2450-element Vector{Float64}:
0.0
0.0009999998333333417
0.0019999986666669333
0.002999995500002025
⋮
0.6408405168240852
0.6400725224915994
0.6393038880866445
0.6385346143778549
如果出于任何原因你想抢先将 xi2
变成完整的 Array
,你可以使用 collect
julia> collect(xi2)
2450-element Vector{Float64}:
0.0
0.001
0.002
0.003
⋮
2.446
2.447
2.448
2.449
可以说,这将“具体化”范围。不过,这比 Range
使用更多的内存,而且比您预期的要少。
在 Python 中,我可以使用
创建一个间隔均匀的值数组xi2 = np.arange(0, np.sqrt(6), 1e-3)
如何在 Julia 中编写此代码?我试过了,
xi2 = range(0,sqrt(6),step=1e-3)
但是这个returns0.0:0.001:2.449
结果 0.0:0.001:2.449
确实是一系列均匀分布的值,可以像其他 AbstractArray
一样进行索引、切片、广播和一般使用。例如:
julia> xi2 = range(0,sqrt(6),step=1e-3)
0.0:0.001:2.449
julia> xi2[1]
0.0
julia> xi2[100]
0.099
julia> length(xi2)
2450
julia> isa(xi2, AbstractArray)
true
julia> sin.(xi2)
2450-element Vector{Float64}:
0.0
0.0009999998333333417
0.0019999986666669333
0.002999995500002025
⋮
0.6408405168240852
0.6400725224915994
0.6393038880866445
0.6385346143778549
如果出于任何原因你想抢先将 xi2
变成完整的 Array
,你可以使用 collect
julia> collect(xi2)
2450-element Vector{Float64}:
0.0
0.001
0.002
0.003
⋮
2.446
2.447
2.448
2.449
可以说,这将“具体化”范围。不过,这比 Range
使用更多的内存,而且比您预期的要少。