How to define an element inside a structure with type 3x3 Matrix{Float64}

I need to define Ls to be of 3x3 Matrix{Float64}. I could write as below but it is not working, any idea to define this?

mutable struct R
    Ls::Array{Float64, 3x3} = [0,0,0; 0,0,0; 0,0,0] 
    Ls::Vector{Vector{Float64}} = [0,0,0; 0,0,0; 0,0,0]
end

Array{Float64, 2} is a two-dimensional array. The size of the array is not part of the type.

1 Like

@PetrKryslUCSD Thank you! I have did that, but I get the below error:

mutable struct R
    Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]  
end
ERROR: LoadError: syntax: unexpected semicolon in array expression around util.jl:450
Stacktrace:
 [1] top-level scope

I am doubtful one can have an initializer in the type. I would do

julia> mutable struct R
           Ls::Array{Float64, 2}
       end

julia> a = R(zeros(3,3))
R([0.0 0.0 0.0; 0.0 0.0 0.0; 0.0 0.0 0.0])

julia> a.Ls
3×3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0
 0.0  0.0  0.0

julia>
2 Likes

@PetrKryslUCSD
It is possible from the below reference. However, it works well with vector, but not Array{Float64, 2}. Do you have any idea?

julia> using Parameters, Base;
julia> Base.@kwdef mutable struct R
    Br::Vector{Float64} = [0,0,0]
    #Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0] 
end
julia> u = R()
R([0.0, 0.0, 0.0])
julia> Base.@kwdef mutable struct R
           Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]
       end
ERROR: syntax: unexpected semicolon in array expression around util.jl:450
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

Try

julia> Base.@kwdef mutable struct R
                  Ls::Array{Float64, 2} = [0 0 0; 0 0 0; 0 0 0]
              end
R

julia>
3 Likes

@PetrKryslUCSD Thank you very much, yes it is working now.
As I though previously that , is similar to space as in Matlab language but it seems in Julia matters.

1 Like