Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linspace() not giving any proper result in Julia

Tags:

julia

I want to create an linearly spaced array of 10 elements between 0 and 1 in Julia. I tried the linspace command.

    julia> linspace(0.0,1.0,10)

This is the output I got.

    linspace(0.0,1.0,10)

I thought I was supposed to get an array as the output. I can't figure out what I'm doing wrong. I use Julia v0.4.3 from the command line. I tried the same thing from Juno IDE and it worked fine there.

like image 509
Haritha Elango Avatar asked Jan 29 '26 01:01

Haritha Elango


1 Answers

Actually, that is an array-like object! It just displays itself a little strangely because it generates its values on-the-fly as you ask for them. This is similar to ranges, wherein 1:1000000 will simply spit 1:1000000 right back at you without allocating and computing all million elements.

julia> v = linspace(0,1,10)
linspace(0.0,1.0,10)

julia> for elt in v
           println(elt)
       end
0.0
0.1111111111111111
0.2222222222222222
0.3333333333333333
0.4444444444444444
0.5555555555555556
0.6666666666666666
0.7777777777777778
0.8888888888888888
1.0

julia> v[3]
0.2222222222222222

The display of linspace objects has changed in the developmental version 0.5 precisely because others have had this same reaction, too. It now shows you a preview of the elements it will generate:

julia-0.5> linspace(0,1,10)
10-element LinSpace{Float64}:
 0.0,0.111111,0.222222,0.333333,0.444444,0.555556,0.666667,0.777778,0.888889,1.0

julia-0.5> linspace(0,1,101)
101-element LinSpace{Float64}:
 0.0,0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,…,0.91,0.92,0.93,0.94,0.95,0.96,0.97,0.98,0.99,1.0
like image 117
mbauman Avatar answered Jan 30 '26 18:01

mbauman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!