# UndefVarError: linspace not defined

**URL:** <https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269>\
**Category:** New to Julia\
**Created:** [August 11, 2018, 9:05pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269 "2018-08-11T21:05:11Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Richard\_Thornton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richard_thornton/32/6450_2.png) [@Richard\_Thornton](https://discourse.julialang.org/u/Richard_Thornton)\
**Post date:** [December 16, 2018, 5:39pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/21 "2018-12-16T17:39:31Z")

</div>

> [@Tamas\_Papp](#):
>
> range(0; stop = 5, step = 1)

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 16, 2018, 5:44pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/22 "2018-12-16T17:44:05Z")

</div>

Probably because of

```julia
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

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

```

---

<div class="post-metadata">

**Author:** ![Richard\_Thornton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/richard_thornton/32/6450_2.png) [@Richard\_Thornton](https://discourse.julialang.org/u/Richard_Thornton)\
**Post date:** [December 16, 2018, 7:24pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/23 "2018-12-16T19:24:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [December 16, 2018, 7:26pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/24 "2018-12-16T19:26:16Z")

</div>

You also can just replace `linspace` by `LinRange`

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 12:41pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/25 "2019-12-21T12:41:34Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 21, 2019, 12:44pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/26 "2019-12-21T12:44:17Z")

</div>

> [@PSA: use Julia 0.7 if you are upgrading](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321/):
>
> A lot of people are posting issues where their old code doesn’t work anymore on 1.0 because some function has been renamed or removed. The 0.7 release exists exactly for this reason: your old code will work on 0.7 and give you a specific warning about how to fix your code. If you have any old code, you should use 0.7 to get these warnings before switching to 1.0; even if you just have work habits that may need changing, you should probably use 0.7 for a while. If someone posts an issue like thi…

( **EDIT** corrected link)

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 12:48pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/27 "2019-12-21T12:48:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 21, 2019, 1:00pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/28 "2019-12-21T13:00:00Z")

</div>

> [@ea42gh](#):
>
> Not sure I agree:

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

> [@PSA: use Julia 0.7 if you are upgrading](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321):
>
> A lot of people are posting issues where their old code doesn’t work anymore on 1.0 because some function has been renamed or removed. The 0.7 release exists exactly for this reason: your old code will work on 0.7 and give you a specific warning about how to fix your code. If you have any old code, you should use 0.7 to get these warnings before switching to 1.0; even if you just have work habits that may need changing, you should probably use 0.7 for a while. If someone posts an issue like thi…

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

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 1:21pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/29 "2019-12-21T13:21:03Z")

</div>

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

```julia
ξ = 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/](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`?

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [December 21, 2019, 1:33pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/30 "2019-12-21T13:33:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 1:35pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/31 "2019-12-21T13:35:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 21, 2019, 1:46pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/32 "2019-12-21T13:46:04Z")

</div>

> [@ea42gh](#):
>
> So what is the difference between `range` and `LinRange` ?

Please kindly read the docs, eg

[https://docs.julialang.org/en/v1/base/collections/#Base.LinRange](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.

> [@Syx\_Pek](#):
>
> The function `LinRange` is old and shouldn’t be used.

I am not sure about this, eg

> <https://github.com/JuliaLang/julia/issues/27537>
>
> At the moment \`linspace\` suggests using \`range\`. It is a bit more natural to use… \`LinRange\` as a replacement.
> 
> However, \`LinRange\` does not have any documentation.
> 
> 
> \`\`\`julia
> julia\> linspace(1,5,10)
> ┌ Warning: \`linspace(start, stop, length::Integer)\` is deprecated, use \`range(start, stop=stop, length=length)\` instead.
> │ caller = top-level scope
> └ @ Core :0
> 1.0:0.4444444444444444:5.0
> 
> julia\> LinRange(1,5,10)
> 10-element LinRange{Float64}:
> 1.0,1.44444,1.88889,2.33333,2.77778,3.22222,3.66667,4.11111,4.55556,5.0
> 
> help?\> LinRange
> search: LinRange
> 
> No documentation found.
> 
> Base.LinRange is of type UnionAll.
> 
> Summary
> ≡≡≡≡≡≡≡≡≡
> 
> struct UnionAll \<: Type{T}
> 
> Fields
> ≡≡≡≡≡≡≡≡
> 
> var :: TypeVar
> body :: Any
> 
> Supertype Hierarchy
> ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
> 
> UnionAll \<: Type{T} \<: Any
> \`\`\`

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [December 21, 2019, 1:48pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/33 "2019-12-21T13:48:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 1:55pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/34 "2019-12-21T13:55:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2019, 2:45pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/35 "2019-12-21T14:45:38Z")

</div>

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](https://en.wikipedia.org/wiki/Camel_case) 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
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
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
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](https://github.com/JuliaLang/julia/pull/18777)).

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 21, 2019, 3:16pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/36 "2019-12-21T15:16:53Z")

</div>

> [@ea42gh](#):
>
> `LinRange` is documented as a function, as is `range` :  
> no value judgement, no recommendation, just apparently duplicated functionality

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:

> <https://github.com/JuliaLang/julia/blob/master/CONTRIBUTING.md#improving-documentation>

---

<div class="post-metadata">

**Author:** ![ea42gh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ea42gh/32/12000_2.png) [@ea42gh](https://discourse.julialang.org/u/ea42gh)\
**Post date:** [December 21, 2019, 8:45pm UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/37 "2019-12-21T20:45:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Peeter\_Joot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peeter_joot/32/16693_2.png) [@Peeter\_Joot](https://discourse.julialang.org/u/Peeter_Joot)\
**Post date:** [July 30, 2020, 3:17am UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/38 "2020-07-30T03:17:27Z")

</div>

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

```julia
> /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> 

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 30, 2020, 3:34am UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/39 "2020-07-30T03:34:31Z")

</div>

it’s not `linrange`, it’s `LinRange`, [Collections and Data Structures · The Julia Language](https://docs.julialang.org/en/v0.7/base/collections/#Base.LinRange) ;

and you would see deprecation in the section of v0.7 as well: [Julia v0.7.0 Release Notes · The Julia Language](https://docs.julialang.org/en/v0.7/NEWS/#Deprecated-or-removed-1)

---

<div class="post-metadata">

**Author:** ![Peeter\_Joot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peeter_joot/32/16693_2.png) [@Peeter\_Joot](https://discourse.julialang.org/u/Peeter_Joot)\
**Post date:** [July 30, 2020, 3:38am UTC](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269/40 "2020-07-30T03:38:53Z")

</div>

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.

[Previous page](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269.md?page=1)

[Next page](https://discourse.julialang.org/t/undefvarerror-linspace-not-defined/13269.md?page=3)
