Array of vectors in type definition

307 views
Skip to first unread message

Ferran Mazzanti

unread,
Jul 18, 2016, 11:13:23 AM7/18/16
to julia-users
Guys,

today I've tried to include a vector of vectors as part of atype definition, something like

type caw
   legs :: Int64
   spots :: Array{Float64}[]
end

but that fails. Shall I understand that it is not possible to define that in a type definition? I just wanted to include a structrure that could grow
by adding more data dynamically...

Any hint about this?

Thanks for your kind help,

Ferran.

Mauro

unread,
Jul 18, 2016, 11:18:05 AM7/18/16
to julia...@googlegroups.com
Array{Float64}[] creates an instance of type Vector{Array{Float64}}.
You need the type, probably Vector{Vector{Float64}}.

Ferran Mazzanti

unread,
Jul 18, 2016, 6:12:36 PM7/18/16
to julia-users
Hi Mauro,
your solution seems to work... though I do not understand exactly why :)
Even Vector{Array{Float64}} works.
Thanks for your kind help :)
Ferran.

Greg Plowman

unread,
Jul 18, 2016, 8:01:00 PM7/18/16
to julia-users
Although Vector{Array{Float64}} works, be aware that it is specifying a vector of the abstract type Array{Float64} (any dimension allowed).
In general this is not recommended. It is better to specify a concrete type where possible.
In your case, this probably means Vector{Vector{Float64}}.

Patrick Kofod Mogensen

unread,
Jul 19, 2016, 2:50:01 AM7/19/16
to julia-users
Vector{T} means a vector where the elements are of type T. If T = Vector{S} then every element in the original vector is itself a vector with elements of type S. In your case, S = Float64 and T = Vector{S} = Vector{Float64}. I think it's a pretty good idea to make sure you understand this, as the type system is important to understand in order to fully exploit multiple dispatch, and to get good performance out of Julia programs.

Tom Breloff

unread,
Jul 19, 2016, 9:04:29 AM7/19/16
to julia...@googlegroups.com
I think the important thing to know is that Array is an abstract type unless all parameters have a concrete value. Vector{T} is just a typealias for Array{T,1}, so Vector{Float64} is a concrete type because Array{Float64,1} has concrete parameters. However Array{Float64} is NOT fully specified... It is implicitly the abstract type Array{Float64,N}, and so you're creating a vector of abstract types, which will hurt performance in general. 

Ferran Mazzanti

unread,
Jul 20, 2016, 11:57:13 AM7/20/16
to julia-users
Sorry guys again,

I said it works as I tried it out and seemed to work. Now I'm not even sure it does work, don't ask me why. I try now this

type caw
   legs :: Int64
   spots :: Vector{Vector{Float64}}
   caw() = new()
end

then I can do things like
z = caw()
which produce
caw(4587026064,#undef)

Now the question is: how do I start adding vectors to z.spots?
I've tried things like

push!(z.spots,[1.0])

but then I get 

LoadError: UndefRefError: access to undefined reference
while loading In[30], in expression starting on line 1

Thanks for your help,

Ferran.

and now it does not complain

Stefan Karpinski

unread,
Jul 20, 2016, 12:35:21 PM7/20/16
to Julia Users
You're constructing caw with no spots at all, not an empty vector. You should probably not construct incomplete objects, but rather create objects with reasonable initial values, like new(0, Vector{Vector{Float64}}()). I'm not sure what legs is supposed to be.

Jussi Piitulainen

unread,
Jul 20, 2016, 12:37:25 PM7/20/16
to julia-users

 
julia> type caw
         legs
:: Int64
         spots
:: Vector{Vector{Float64}}
         caw
() = new(4587026064, Vector{Float64}[])
       
end

julia> z = caw()
caw
(4587026064,Array{Float64,1}[])

julia> push!(z.spots, [1.0])
1-element Array{Array{Float64,1},1}:
 
[1.0]

julia
> z
caw
(4587026064,[[1.0]])



Ferran Mazzanti

unread,
Jul 22, 2016, 4:28:58 AM7/22/16
to julia-users
Sorry for being so noob once again, but I'm getting close without getting actually to it.
Could somebody tell me please how should I change the previous definition of caw() as given by Jussi to make room
for a Vector of Arrays (I can fix them to dimension 2) instead of a Vector of Vector?  I'd like to be able to define it simllarly,
so that instances of caw() are initially empty and I push!() 2-dimensional arrays into it...

Thanks and sorry again,

Ferran.

Jussi Piitulainen

unread,
Jul 22, 2016, 5:26:58 AM7/22/16
to julia-users


perjantai 22. heinäkuuta 2016 11.28.58 UTC+3 Ferran Mazzanti kirjoitti:
Sorry for being so noob once again, but I'm getting close without getting actually to it.
Could somebody tell me please how should I change the previous definition of caw() as given by Jussi to make room
for a Vector of Arrays (I can fix them to dimension 2) instead of a Vector of Vector?  I'd like to be able to define it simllarly,
so that instances of caw() are initially empty and I push!() 2-dimensional arrays into it...

 My hobby: empty things.

Array type has two parameters: element type and number of dimensions. Vector{Float64} is shorthand for Array{Float64,1}, and Matrix{Float64} is similar shorthand for Array{Float64,2}. A vector can be made by listing the elements in square brackets after the element type:
julia> Matrix{Float64}[]
0-element Array{Array{Float64,2},1}

Or as Stefan did using the type of the vector:
julia> Vector{Matrix{Float64}}()
0-element Array{Array{Float64,2},1}

There is no Matrix there but you can push! a Matrix there. Or you can start with a non-empty Vector that contains an empty Matrix:
julia> Matrix{Float64}[Matrix{Float64}()]
1-element Array{Array{Float64,2},1}:
 
0x0 Array{Float64,2}

The expression inside the square brackets makes an empty Matrix. I think they are changing this, but in the current release the type name before the square brackets makes a big difference when the element is an Array.

I'm not sure how the square brackets would be used if you wanted a matrix of matrices, but you could at least start with an empty one by writing Matrix{Matrix{Float64}}(). And for an empty vector of matrices, as said, Vector{Matrix{Float64}}. And Julia will show these types as Array{T,1} and Array{T,2} instead of Vector and Matrix.


Jussi Piitulainen

unread,
Jul 22, 2016, 5:30:40 AM7/22/16
to julia-users


perjantai 22. heinäkuuta 2016 12.26.58 UTC+3 Jussi Piitulainen kirjoitti:

I'm not sure how the square brackets would be used if you wanted a matrix of matrices, but you could at least start with an empty one by writing Matrix{Matrix{Float64}}(). And for an empty vector of matrices, as said, Vector{Matrix{Float64}}. And Julia will show these types as Array{T,1} and Array{T,2} instead of Vector and Matrix.

Sorry, Vector{Matrix{Float64}}() to make a Vector{Matrix{Float64}}.

Ferran Mazzanti

unread,
Jul 22, 2016, 6:32:39 AM7/22/16
to julia-users
Thanks Jussi for being so helpful :)
I'll try that and check. Then I'll carefully think about all this to see what can I learn from here :)
Best regards,
Ferran.

Ferran Mazzanti

unread,
Jul 22, 2016, 8:03:25 AM7/22/16
to julia-users
Ok all tests seem to fail, sorry. Jossi doing what you say directly works, but trying to wrap up things in a type deffinition either fails, or fails when I try to push!() things. Could you please be so kind to fill the whole example? Something like:

type croc()
  teeth :: Int64
  legs :: Int64
  sons :: <== here comes the Vector of Arrays definition
  croc() = new(0,0, ....something here to initialize the vector of arrays...)
end

such that I can initialize it like

reptile = croc()

and I can set up things like

reptile.teeth = 80
reptile.legs = 4
push!(reptile.sons,[[1.0 2.0];[3.0 4.0]])

...this is essentially what I'm looking for :)

Best regards and thanks for your kind help again,

Ferran.

Jussi Piitulainen

unread,
Jul 22, 2016, 8:46:11 AM7/22/16
to julia-users
type croc
  teeth
:: Int64
  legs
:: Int64
  sons
:: Vector{Matrix{Float64}}
  croc
() = new(0,0, Vector{Matrix{Float64}}())
  croc
(th, gs) = new(th, gs, Vector{Matrix{Float64}}())
  croc
(th, gs, ns) = new(th, gs, ns)
end

reptile
= croc()
reptile
.teeth = 4
reptile
.legs = 80
push
!(reptile.sons, [[3.0 1.0];[4.0 1.0]])

another
= croc(80, 4)

sons
= Vector{Matrix{Float64}}()
push
!(sons, rand(3,1))
push
!(sons, rand(4,1))
onemore
= croc(80, 4, sons)

@show reptile
@show another
@show onemore

Reply all
Reply to author
Forward
0 new messages