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:
xxxxxxxxxx
r = 1:10
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:
xxxxxxxxxx
println("First value of range is ", r.start)
println("Last value of range is ", r.stop)
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
:
xxxxxxxxxx
# Specific type of the range:
println("type of r = ", typeof(r))
println()
# Type and size of the component parts of the range:
println("type of r.start = ", typeof(r.start))
println("size of r.start = ", sizeof(typeof(r.start)), " bytes")
println()
println("type of r.stop = ", typeof(r.stop))
println("size of r.stop = ", sizeof(typeof(r.stop)), " bytes")
println()
# Size of the entire range:
println("size of r = ", sizeof(r), " bytes")
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
:
xxxxxxxxxx
sizeof(1:1000000)
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:
xxxxxxxxxx
println(4 in r)
println(27 in r)
println(-3 in r)
true false false
Further, this range r
contains only integers:
xxxxxxxxxx
println(2.7 in r)
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":
xxxxxxxxxx
println(6 ∈ r) # Entered as \in [TAB]
println(9 ∉ r) # Entered as \notin [TAB]
true false
Getting all the Values in a Range¶
Useful for debugging as well as just plain underatanding a range is the collect function:
xxxxxxxxxx
collect(r)
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:
xxxxxxxxxx
e = 2:2:10
2:2:10
xxxxxxxxxx
println("type of e = ", typeof(e))
println()
println("First value of range is ", e.start)
println("Last value of range is ", e.stop)
println("Step size of range is ", e.step)
println()
println("size of e = ", sizeof(e), " bytes")
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
xxxxxxxxxx
collect(e)
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:
xxxxxxxxxx
b = 10:-1:1
10:-1:1
xxxxxxxxxx
collect(b)
10-element Vector{Int64}: 10 9 8 7 6 5 4 3 2 1
xxxxxxxxxx
## Fractional Step Sizes
xxxxxxxxxx
r1 = 1:0.5:10
1.0:0.5:10.0
xxxxxxxxxx
collect(r1)
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¶
xxxxxxxxxx
r2 = 1:10.5
1.0:1.0:10.0
xxxxxxxxxx
collect(r2)
10-element Vector{Float64}: 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
xxxxxxxxxx
s = 4.5:8.75
4.5:1.0:8.5
xxxxxxxxxx
collect(s)
5-element Vector{Float64}: 4.5 5.5 6.5 7.5 8.5
xxxxxxxxxx
t = 4.5:0.25:8.75
4.5:0.25:8.75
xxxxxxxxxx
collect(t)
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