Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why [1:2] != Array[1:2]

I am learning Julia following the Wikibook, but I don't understand why the following two commands give different results:

julia> [1:2]
1-element Array{UnitRange{Int64},1}:
 1:2

julia> Array[1:2]
1-element Array{Array,1}:
 [1,2]

Apologies if there is an explanation I haven't seen in the Wikibook, I have looked briefly but didn't find one.

like image 845
Jonathan H Avatar asked Oct 29 '16 19:10

Jonathan H


People also ask

What does mean a [- 1 in array?

array[-1] is the last element of the array; array[-2] is the next to last. So this is a slice of the array omitting the first and last elements.

How do you know if 2 numbers in an array are equal?

Check if two arrays are equal or not using SortingSort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.

Can 2 arrays be equal?

Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

Can an array be uneven?

Each row of a 2D array can have a different number of cells, so each row has its own length : uneven[0] refers to row 0 of the array, uneven[1] refers to row 1, and so on.


1 Answers

Type[a] runs convert on the elements, and there is a simple conversion between a Range to an Array (collect). So Array[1:2] converts 1:2 to an array, and then makes an array of objects like that. This is the same thing as why Float64[1;2;3] is an array of Float64.


These previous parts answer answered the wrong thing. Oops...

a:b is not an array, it's a UnitRange. Why would you create an array for A = a:b? It only takes two numbers to store it, and you can calculate A[i] basically for free for any i. Using an array would take an amount of memory which is proportional to the b-a, and thus for larger arrays would take a lot of time to allocate, whereas allocation for UnitRange is essentially free.

These kinds of types in Julia are known as lazy iterators. LinSpace is another. Another interesting set of types are the special matrix types: why use more than an array to store a Diagonal? The UniformScaling operator acts as the identity matrix while only storing one value (it's scale) to make A-kI efficient.

Since Julia has a robust type system, there is no reason to make all of these things arrays. Instead, you can make them a specialized type which will act (*, +, etc.) and index like an array, but actually aren't. This will make them take less memory and be faster. If you ever need the array, just call collect(A) or full(A).


I realized that you posted something a little more specific. The reason here is that Array[1:2] calls the getindex function for an array. This getindex function has a special dispatch on a Range so that way it "acts like it's indexed by an array" (see the discussion from earlier). So that's "special-cased", but in actuality it just has dispatches to act like an array just like it does with every other function. [A] gives an array of typeof(A) no matter what A is, so there's no magic here.


like image 67
Chris Rackauckas Avatar answered Oct 04 '22 03:10

Chris Rackauckas