Array{T}(undef, dims)
I am new to Julia, and don't have a good background in programming. In this syntax, why is undef
used for creating the array?
If we don't type constructor, Julia will automatically create a constructor. Then, Why we use constructor?
When I write it on the Julia REPL: Now Array {T} (undef, dims) is the generalization of that. "Construct an array of a specific type T with a specific number of dimensions dims " So far, I didn't explain what is undef. undef is a shortcut for UndefInitializer (). In this example, we wanted an uninitialized array. What does it mean?
An Array is an ordered set of elements which are often specified with squared brackets having comma-separated items. We can create arrays that are − In Julia, arrays are actually mutable type collections which are used for lists, vectors, tables, and matrices.
So Julia will ask for some memory space. When I write it on the Julia REPL: Now Array {T} (undef, dims) is the generalization of that. "Construct an array of a specific type T with a specific number of dimensions dims " So far, I didn't explain what is undef. undef is a shortcut for UndefInitializer ().
In the Julia Manual section on array literals you can read that: containing the comma-separated arguments as its elements. by the types of the arguments inside the braces. If all the arguments are the same type, then that is its eltype. using convert and that type is the array’s eltype.
First, you want to understand what is a constructor: For that, I suggest you the Julia doc: Constructors in Julia
Now that you have the theory, let's break apart this expression:
a = Array{Int}(undef, (2, 2))
What this expression is saying is "I want a
to be an Array
of dimension (2, 2)". So Julia will ask for some memory space. When I write it on the Julia REPL:
julia> a = Array{Int}(undef, (2, 2))
2×2 Array{Int64,2}:
0 0
0 0
Now Array{T}(undef, dims)
is the generalization of that. "Construct an array of a specific type T
with a specific number of dimensions dims
"
So far, I didn't explain what is undef
. undef
is a shortcut for UndefInitializer()
. In this example, we wanted an uninitialized array. What does it mean? For that, you have to understand that variables are not created ex nihilo on your terminal. They are occupying a specific place in the memory of your computer. And sometimes, the same memory space was occupied by another variable. So the space my new variable can take might not be empty:
julia> a = Array{Float64}(undef, (2, 2))
2×2 Array{Float64,2}:
6.94339e-310 6.94339e-310
6.94339e-310 0.0
Here, I never asked for these values to be there. I could erase it to work with a clean variable. But that would mean to erase the value for each cell, and it's much more expensive for the computer to replace each value rather than declaring "here is the new variable".
So basically, undef
and uninitialized arrays are used for performance purposes. If you want an array well initialized, you can use fill.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With