Amro
September 17, 2021, 11:07pm
1
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
Amro
September 17, 2021, 11:14pm
4
@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
Amro:
mutable struct R
Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]
end
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
Amro
September 17, 2021, 11:29pm
6
@PetrKryslUCSD
It is possible from the below reference. However, it works well with vector, but not Array{Float64, 2}
. Do you have any idea?
This seems like a great use case for a macro. You might take inspiration from https://github.com/mauro3/Parameters.jl which does something similar:
julia> using Parameters
julia> @with_kw struct Foo
x::Int32 = 1
y::Float64
end
Foo
julia> Foo(y = 2)
Foo
x: Int32 1 …
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
Amro:
Base.@kwdef mutable struct R
Ls::Array{Float64, 2} = [0,0,0; 0,0,0; 0,0,0]
end
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
Amro
September 17, 2021, 11:45pm
8
@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