Using linspace in Julia 0.7
Solution 1
You should use LinRange
, as documented here.
A range with len linearly spaced elements between its start and stop. The size of the spacing is controlled by len, which must be an Int.
julia> LinRange(1.5, 5.5, 9)
9-element LinRange{Float64}:
1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5
Edit 2021: As of version 1.7 you can use the range
function for this:
jl> range(1.5, 5.5, 9)
1.5:0.5:5.5
For version 1.6 you have to write: range(1.5, 5.5, length=9)
.
Solution 2
Following the deprecations, it is now:
julia> range(0.1, stop = 1.1, length = 6) |> collect
6-element Array{Float64,1}:
0.1
0.3
0.5
0.7
0.9
1.1
In your example, the second argument is a step
, not the stop
, notice this method is also deprecated, you have to use keyword arguments now:
julia> @which range(0.1, 1.1, 6)
range(start, step, length) in Base at deprecated.jl:53
Admin
Updated on July 22, 2022Comments
-
Admin almost 2 years
I am confused about using linspace in Julia 0.7. Here is the what I entered in the REPL and the result:
julia> a = linspace(0.1,1.1,6) ┌ Warning: `linspace(start, stop, length::Integer)` is deprecated, use `range(start, stop=stop, length=length)` instead. │ caller = top-level scope └ @ Core :0 0.1:0.2:1.1
My question is about the deprecated warning and the suggested use of range. The range statement doesn't do the same thing as the linspace command.
If you enter the a = linspace(0.1,1.1,6) and collect(a), you get the following:
julia> collect(a) 6-element Array{Float64,1}: 0.1 0.3 0.5 0.7 0.9 1.1
If you enter b = range(0.1,1.1,6) and collect(b), you get:
julia> collect(b) 6-element Array{Float64,1}: 0.1 1.2 2.3 3.4 4.5 5.6
This is obviously not the same.
Why is linspace deprecated (perhaps a different question) and a non-equivalent range command suggested?
My actual question is: Is it safe to keep using linspace for the desired results it provides, and, if not, what should I be using instead?
-
DNF almost 6 yearsI'm confused by the downvotes. How is this not helpful?
-
HarmonicaMuse almost 6 yearsI guess, it must be because it's not documented.
-
Liso almost 6 years@SalchiPapa I think it is awkward to decrease DNFs reputation because Julia is in flux. Maybe we could try to balance this answer at zero? ;)
-
HarmonicaMuse almost 6 years@Liso, I don't really mind reputation points myself, but there you go, back at 0 again. :) Still, an example (specially because is undocumented) to reproduce the same output with
LinRange
, would be nice. Linking to a discussion is helpful, but doesn't answer the question. -
Paul Jurczak over 5 yearsIt is documented now: docs.julialang.org/en/v1.0.0/base/collections/#Base.LinRange
-
carstenbauer about 5 yearsAs a remark, from Julia 1.1 on the stop keyword can be omitted, that is the second positional argument is interpreted as stop automatically.