How to define an array whose elements are manually defined type?

I have a manually defined type, or called struct in Julia,

mutable struct Mean_covar
    mu::Array{Float64,2}
    sigma::Array{Float64,2}
    w::Float64 
end

Now I want to define an const array but its content can change (I mean, just lock the type of the array), so I know I can use Ref.
Now the question is, how to define such an array? Let us just call it B, and say, the length of B array is 10.
I was thinking doing things like,

global const B = Ref{Array{Mean_covar,1}(undef,10)}

But obviously it gives me an error.

So,

  1. how do I define such an array B, whose length is 10, and each element is type Mean_covar?

  2. If I were able to define such an array B, how to set the value to its element? Like, If I want to set values to its 1st value B[1], can I do

     B[1] = Mean_covar(zeros(2,2),zeros(2,2),0.0)
    

    However the above command does not seem to work for something with a Ref property. With Ref property, it seems we need a bra [ and ket ] somewhere?

Thanks!

const B = Array{Mean_covar,1}(undef, 10) works well because Array’s are mutable.

Input:

mutable struct Mean_covar
    mu::Array{Float64,2}
    sigma::Array{Float64,2}
    w::Float64 
end

const B = Array{Mean_covar,1}(undef, 5)
display(B)

Output:

5-element Vector{Mean_covar}:
 #undef
 #undef
 #undef
 #undef
 #undef

Input:

B[1] = Mean_covar(zeros(2,2), zeros(2,2), 0.0)
display(B)

Output:

5-element Vector{Mean_covar}:
    Mean_covar([0.0 0.0; 0.0 0.0], [0.0 0.0; 0.0 0.0], 0.0)
 #undef
 #undef
 #undef
 #undef
1 Like

Thank you very much!
In this case, do I need to add a Ref to B?
like

const B = Ref{Array{Mean_covar,1}(undef,10)}

or just as you said, no need to add Ref?

When to use Ref and when not to use Ref?

Thanks in advance!

const c = Ref(ImmutableObject) or const c = MutableObject.

Ref(x), Vector{T}(undef, n), and Array{T, N}(undef, k_1, ..., k_N) are mutable, so no need to add Ref.

You almost never have to use Ref in julia. It’s like a single element array but most importantly arrays and Refs in julia are not interchangeable (unlike in C, where an array is just fancy syntax for pointeroffset + deref).

Assuming B is an array, this should always work. What you’ve probably been doing is a Ref of/to an array (i.e. Ref{Array{Mean_covar, 1}}, which you’d have to index like B[][1] to first get the array out of the Ref and then index into the array).

If you just have a Array{Mean_covar, 1}, B[1] is sufficient for indexing into it.


You’re probably going to be interested in the following section of the docs:

https://docs.julialang.org/en/v1/manual/noteworthy-differences/#Noteworthy-differences-from-C/C

1 Like

Thank you! Like you said, for const array there is no need to add Ref, since array type is mutable. The Ref trick is most likely for a variable.

Furthermore, unless you plan to mutate the w inside your struct, you don’t need Mean_covar to be mutable. Even if Mean_covar is immutable, you can still mutate mu and sigma.

1 Like