Reputation: 636
I have a Fortran type I defined as mean_covar like below,
type :: mean_covar
real(kind=r8), allocatable :: mu(:,:)
real(kind=r8), allocatable :: sigma(:,:)
real(kind=r8) :: w
end type
So that I can set some variable as type Mean_covar.
I want to define the same thing in Julia, I know Julia has something called struct, with mutable property its fields can be changed. So I did a very simply thing like,
mutable struct Mean_covar
mu::Array{Float64,2}
end
Then I define,
musigma = Mean_covar(Array{Float64,2}(undef,2,2))
so I can then set musigma.mu as any 2d array I want.
But I just wonder, does it has to be so cumbersome when defining musigma (need to set the value of each fields)? Can I just simply do
musigma::Mean_covar
But this give me an error,
UndefVarError: musigma not defined
Upvotes: 1
Views: 105
Reputation: 42244
I am not exactly sure how you want to structure your data but you could do:
struct MeanCovar
mu::Matrix{Float64}
MeanCovar(n) = new(zeros(n,n))
end
This could be used as simple as:
julia> m = MeanCovar(2)
MeanCovar([0.0 0.0; 0.0 0.0])
Note that when you know the size of MeanCovar it can be not mutable since you can still mutate the values in mu
, as mu
hold only reference to the array.
julia> m.mu[1,1] = 66
66
julia> m.mu
2×2 Matrix{Float64}:
66.0 0.0
0.0 0.0
Upvotes: 3