Arrays

We start by understanding the abstract type of all arrays and array like types. This will include vectors and matrices. The abstract type is AbstractArray{T,N}. The curly bracket notation specifies the type of the elements and the number of dimensions.

As an example of the specialisation we have AbstractVector{T} which is an alias for AbstractArray{T,1}. Another example is AbstractMatrix{T} which is an alias for AbstractArray{T,2}.

Concrete Types

Corresponding concrete types exist and can be created as shown in the code below.


                days = Array{String, 1}(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])
                println("The array days = $days")
                println("This is a one dimensional array.")
                println("The first element is $(days[begin]) and the last element $(days[end]).")
                println("Usings days[begin] and days[end] is the same as using days[1]) and days[5] because indexing starts from 1.")
                
                println("""Arrays are not immutable. Entries can be added to the end: e.g. push!(days, "Friday" )""")
                push!(days, "Friday")
                println("The array days = $days")
                println("The first element is $(days[begin]) and the last element $(days[end]).")
                
                println("Elements can also be removed from the end of an array using: lastDay = pop!(days)")
                lastDay = pop!(days)
                println(lastDay)
                println(days)
                
                println("A list of items can be added to the end of a list using append!")
                append!(days, ["Friday", "Saturday"])
                println(days)
            

As this is a one dimensional array we could have used Vector instead: days = Vector{String}(["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"])

The explicit constructor syntax can be further shortened so that this is the same declaration as above: days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]

Matrices

Matrices follow the above pattern and can be initialised by using spaces between items in each row and semi-colons for the next row.


                m1 = Matrix{Int8}([1 2; 3 4])
                2×2 Matrix{Int8}:
                1  2
                3  4
            

Matrices don't have to be square. Here is one with three rows and 2 columns. The valid index ranges are given by the function axes.


                m2 = Matrix{Int8}([1 2; 3 4; 5 6])
                3×2 Matrix{Int8}:
                 1  2
                 3  4
                 5  6
                
                axes(m2) => (Base.OneTo(3), Base.OneTo(2))
            

Some other useful functions are ndims, size, length:


                print("ndims(m2): $(ndims(m2))\tsize(m2): $(size(m2))\tlength(m2): $(length(m2))")
                ndims(m2): 2	size(m2): (3, 2)	length(m2): 6
            

There is a way to divide an array into its head and tail. Note the ellipsis (3 dots) after the t in the function body.

                function headAndTail( a )
                    (h, t...) = a
                    print("Head is $h and t is $t")
                end
     
                days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
            
                headAndTail(days)
                # Output is => Head is Sunday and t is ["Monday", "Tuesday", "Wednesday", "Thursday"]
            

Interestingly, the assignment to head and tail can be done in the parameter list as shown below. However, note that the internal brackets are vital. If they are omitted all of the input array is assigned to 'h' and then an empty list is assigned to 't'.

                function headAndTail( (h, t...) )
                    print("Head is $h and t is $t")
                end
         
                days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"]
                
                headAndTail(days)
                # Output is => Head is Sunday and t is ["Monday", "Tuesday", "Wednesday", "Thursday"]
            

There is some stuff to learn about populating arrays and matrixes. Basically range creates a thumk of code that can produce e.g. a set of numbers and then collect actually computes them.


                xs = range(-1, 1, length=9)
                # xs is shown in REPL as: -1.0:0.25:1.0
                # i.e. it has computed (2nd param - 1st param) / (length - 1)
                # It also does some more subtle stuff to arrange to get to the last value.
                
                collect(xs)
                # Output is as follows...
                9-element Vector{Float64}:
                 -1.0
                 -0.75
                 -0.5
                 -0.25
                  0.0
                  0.25
                  0.5
                  0.75
                  1.0