I learned from all of you that Julia discourage global variable. Thank you very much indeed!
I am translating my Fortran code to Julia. It is a Monte Carlo parametric expectation maximization code. Basically using gaussian mixture model.
Here I show a module call Mixture, as a small example. My purpose is simple.
I define a type Mean_covar, then I need an array called musigma. Each element of the musigma array is of the type Mean_covar.
The size of array musigma, and the value of its each element will be initialized by calling the function mean_covar_init in the main program which will use this βMixtureβ module.
I also wish to export this array musigma, so that I can directly use it without do Mixture.musigma all the time.
Here is the module:
module Mixture
export musigma
mutable struct Mean_covar
mu::Array{Float64,2}
sigma::Array{Float64,2}
w::Float64
end
global const musigma = Array{Mean_covar,1}()
function mean_covar_init(kmix::Int64,dim_p::Int64
,weight::Array{Float64,1}
,sigma::Array{Float64,3}
,mu::Array{Float64,2})
@assert length(weight) == kmix
@assert size(sigma) == (kmix,dim_p,dim_p)
@assert size(mu) == (kmix,dim_p)
resize!(musigma, kmix)
for k in 1:kmix
musigma[k] = Mean_covar(zeros(dim_p,1),zeros(dim_p,dim_p),0.0)
musigma[k].mu[1,1] = mu[k,1]
musigma[k].mu[2,1] = mu[k,2]
musigma[k].sigma[1,1] = sigma[k,1,1]
musigma[k].sigma[2,2] = sigma[k,2,2]
musigma[k].w = weight[k]
end
return nothing
end
end
Note that I added const for musigma array just to lock the type of the musigma array.
Now my question is, in order to export musigma array, do I have to declare it as a global array?