UndefVarError: linspace not defined

using PyPlot
x=range(0; stop = 5, step = 1)
y=x.^3 + x.^2 + x - 400
plot(x,y,linewidth=2.0,linestyle=“-”)
title(“Cubic Plot”)
xlabel(“X”)
ylabel(“Y”)

will not compile

Probably because of

julia> y=x.^3 + x.^2 + x - 400
ERROR: MethodError: no method matching -(::Array{Int64,1}, ::Int64)
Closest candidates are:
  -(::Complex{Bool}, ::Real) at complex.jl:298
  -(::Missing, ::Number) at missing.jl:97
  -(::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:107
  ...
Stacktrace:
 [1] top-level scope at none:0

Use eg

y= @. x^3 + x^2 + x - 400

Yes changing the function declaration allows it to compile, but the plot is not coming up;

nfo: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]
WARNING: using PyPlot.axes in module Main conflicts with an existing identifier.

By the way, the original program worked fine in version .64 of Julia.

You also can just replace linspace by LinRange

Hmm: linspace -> range, ok.
The next problem is to figure out how to use the range:
how would I have to change the following codesnippet to work:

ξ = linspace(0,0.6,100)
plot(ξ, zeros(ξ), "k--")

For that matter, how should I think about the difference between range and LinRange?

(EDIT corrected link)

Not sure I agree:
I am trying to prepare some notebooks for next semester.
I expect students will install the latest version (1.3 as of today),
and rightfully expect the notebooks to work.

So how would I change that code snippet so ones( \xi ) will not error?

Sorry, I linked a specific comment, not the topic,

Just upgrade the notebooks using 0.7, and they will keep working on 1.3.

OK. I installed julia .7 in addition to 1.3 :frowning:
and figured out I need to use

ξ = LinRange(0,0.6,100)
plot(ξ, zero(ξ), "k--")

or possibly ξ = range(0,0.6,length=100)

The statement
Users updating code written on older versions to work with 1.0 may be interested in using Julia 0.7, available on the old releases page, during the upgrade process. on
https://julialang.org/downloads/ does not make the statement strongly enough:
being new to julia, I had just ignored it…

So what is the difference between range and LinRange?

If you’re using any Julia ≥0.7, you would want to use range, else use LinRange.

If you have code that is Julia ≤0.6 and you want to upgrade it to Julia 1.0, then using Julia 0.7 have warnings that tell you if functions got deprecated.

Basically, just use range. The function LinRange is old and shouldn’t be used.

So does LinRange serve any purpose,or will it be deprecated as well?

Please kindly read the docs, eg

https://docs.julialang.org/en/v1/base/collections/#Base.LinRange

Also, it is preferred to open a new topic if you have questions, instead of reviving old ones. Some of the original discussion above is now outdated.

I am not sure about this, eg

Ah I was mistaken about LinRange I always thought it was deprecated in favor of range but I was wrong.

OK, great, thank you! Will leave this thread alone henceforth!
One last comment though: I did read the doc: LinRange is documented as a function, as is range:
no value judgement, no recommendation, just apparently duplicated functionality.

In general, the convention in Julia is that a lower-case function like range may produce a variety of different types depending on its arguments, while CamelCase in Julia is used for specific types and constructors thereof.

Both LinRange and StepRangeLen are examples of AbstractRange types. Although you can call their constructors directly in rare cases where you need a specific range type, it is generally preferable to call the range function and let it determine the appropriate type from the types of the arguments.

For typical range of floating-point values, range returns a StepRangeLen:

julia> range(0,3,length=4)
0.0:1.0:3.0

julia> typeof(ans)
StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}

For an integer step, it returns a StepRange:

julia> range(0,4,step=2)
0:2:4

julia> typeof(ans)
StepRange{Int64,Int64}

For non-numeric types, however, e.g. a range between two vectors, it returns a LinRange:

julia> range([1,2], [3,4], length=10)
10-element LinRange{Array{Int64,1}}:
 [1.0, 2.0],[1.22222, 2.22222],…,[2.77778, 3.77778],[3.0, 4.0]

because LinRange is more generic (https://github.com/JuliaLang/julia/pull/18777).

The documentation of the range function needs work, however (https://github.com/JuliaLang/julia/issues/33798).

5 Likes

Looking at it, I agree that it could be improved. Since now you are in the position of remembering why you found it confusing, please consider making a pull request:

I am too new to julia to feel confident making a contribution for now:
what I did need was the explanation of the significance of CamelCase versus lower case functions.
Thank you, that really cleared things up for me!

1 Like

If I use julia 0.7, I don’t see any warning about linrange deprecation, just an error:

> /Applications/Julia-0.7.app/Contents/Resources/julia/bin/julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.7.0 (2018-08-08 06:46 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-apple-darwin14.5.0

julia> theta = linrange( 0, 1 * pi, 500 ) ;
ERROR: UndefVarError: linrange not defined
Stacktrace:
 [1] top-level scope at none:0

julia> 

it’s not linrange, it’s LinRange, Collections and Data Structures · The Julia Language ;

and you would see deprecation in the section of v0.7 as well: Julia v0.7.0 Release Notes · The Julia Language

1 Like

It was linrange when I wrote the code in ~2015. I thought that this thread was about linrange, but I see now it was about linspace – so I guess linrange was depreciated sometime before 0.7.