Tuesday, April 13, 2021

Ranges

We saw ranges in the blog post about loops. The purpose of this post is to provide a fuller explanation of ranges.

As mentioned in that earlier blog post, a range is a compact, memory efficient, notation for a sequence of numbers. We also declared a range of numbers representing the integers between 1 and 10, inclusive:

In [1]:
Out[1]:
1:10

That doesn't look much like a sequence of numbers! The reason why is that the range does not store all the integers between 1 and 10, it just stores the start and stop:

In [2]:
First value of range is 1
Last value of range is 10

In what way are ranges memory efficient? Well, here's some more information about r:

In [3]:
type of r = UnitRange{Int64}

type of r.start = Int64
size of r.start = 8 bytes

type of r.stop = Int64
size of r.stop = 8 bytes

size of r = 16 bytes

Notice that the number of bytes used by the whole range (16) is the same as the number of bytes used by the start and stop, 8 bytes each. The size of a larger range is the same as this little range r:

In [4]:
Out[4]:
16

That's the memory efficiency of ranges!

By the way, we'll get to the "UnitRange{Int64}" part in a second.

Testing Whether a Value is in a Range

We can test whether a value falls within a range by using the in keyword like this:

In [5]:
true
false
false

Further, this range r contains only integers:

In [6]:
false

As mentioned earlier, Julia allows characters to be entered in a LaTeX-like format. In the case of two of these characters, Julia treats them as representing in and "not in":

In [7]:
true
false

Getting all the Values in a Range

Useful for debugging as well as just plain underatanding a range is the collect function:

In [8]:
Out[8]:
10-element Vector{Int64}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

Other Types of Ranges

We've seen how to make a range of integers going from 1 to 10. How do me make a range for only the even numbers between 1 and 10? What if we wanted to count downward from 10 to 1?

Here's how to make a range where we skip numbers - counting by 2 - which will give us the even numbers if we start at an even number:

In [9]:
Out[9]:
2:2:10
In [10]:
type of e = StepRange{Int64, Int64}

First value of range is 2
Last value of range is 10
Step size of range is 2

size of e = 24 bytes
In [11]:
Out[11]:
5-element Vector{Int64}:
  2
  4
  6
  8
 10

This is a different type of range: it is a StepRange instead of a UnitRange like we saw earlier. The syntax for creating a StepRange is start:step:stop, and this can be used for counting backwards:

In [12]:
Out[12]:
10:-1:1
In [13]:
Out[13]:
10-element Vector{Int64}:
 10
  9
  8
  7
  6
  5
  4
  3
  2
  1
In [14]:
In [15]:
Out[15]:
1.0:0.5:10.0
In [16]:
Out[16]:
19-element Vector{Float64}:
  1.0
  1.5
  2.0
  2.5
  3.0
  3.5
  4.0
  4.5
  5.0
  5.5
  6.0
  6.5
  7.0
  7.5
  8.0
  8.5
  9.0
  9.5
 10.0

Ranges with Non-Integer Bounds

In [17]:
Out[17]:
1.0:1.0:10.0
In [18]:
Out[18]:
10-element Vector{Float64}:
  1.0
  2.0
  3.0
  4.0
  5.0
  6.0
  7.0
  8.0
  9.0
 10.0
In [19]:
Out[19]:
4.5:1.0:8.5
In [20]:
Out[20]:
5-element Vector{Float64}:
 4.5
 5.5
 6.5
 7.5
 8.5
In [21]:
Out[21]:
4.5:0.25:8.75
In [22]:
Out[22]:
18-element Vector{Float64}:
 4.5
 4.75
 5.0
 5.25
 5.5
 5.75
 6.0
 6.25
 6.5
 6.75
 7.0
 7.25
 7.5
 7.75
 8.0
 8.25
 8.5
 8.75

For more information on ranges, see: https://docs.julialang.org/en/v1/base/math/

No comments:

Post a Comment