Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: What is undef in Array in Julia

Tags:

julia

  1. This is a constructor for arrays:

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?

  1. What is a constructor in Julia, in what situation do we use a constructor?

If we don't type constructor, Julia will automatically create a constructor. Then, Why we use constructor?

like image 999
Anil Mathews Avatar asked Oct 17 '19 10:10

Anil Mathews


People also ask

What is array {T} (UNDEF) in Julia REPL?

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?

What is an an array in Julia?

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.

What is array {T} (UNDEF)?

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 ().

How do I find the ElType of an array in Julia?

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.


1 Answers

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.

like image 195
Thomas Jalabert Avatar answered Oct 10 '22 12:10

Thomas Jalabert