The following code does not work as expected:
>using StructArrays, LabelledArrays
>SLV2 = @SLVector Int (:x, :y)
>vf=StructArray([rand(SLV2) for _ in 1:5])
>vf.x
ERROR: type Tuple has no field x
The same code with SLVector
replaced by <:FieldVector
actually works.
Is there a way to make this work with LabelledArrays
?
Thanks in advance.
What is the goal you are trying to achieve? Can you maybe provide the code that works and what it works for?
Your current vf
is a tuple, so sure it does not have a vf.x
, but every single entry of said tuple is an SLArray
and hence has an .x
, for example vf[1].x
does work.
Do you maybe want to have a vector of all these x
es? That would be something like
[e.x for e in vf]
Here the goal is to use StructArray
to represent Array of field vectors as vector of arrays. vf
is supposed to be such a “vector field” where each component is stored as a whole array, and can be accessed by vf.x
, etc., and vf[1]
can be used to access the 1st field vector, e.g., the vector formed by 1st elements of vf.x
and vf.y
, resp… All these work for FieldVector
, but not for SLVector
, i.e., labelled vector with names for each component.
The reason to want to use SLVector
instead of FieldVector
is that the latter does not offer an easy constructor as the former, and one must define a type explicitly as a subtype <:FieldVector{N,T}
. This is difficult to do if one does not know a-priori what the names of the vector components are, (e.g., :x, :y, …).
Hm, then the data structure you chose (StructArray
) is just not want you describe you want, It stores a vector of the vectors, and not each component separately as a vector internally (that you could access with .x
).
So [e.x for e in vf]
extracts this vector of first components (but allocates), simply because the data structure is completely different.
So if you want an easy constructor, you loose the vf.x
easy access. The code above does give you the same result, but it allocates.
Maybe a comparison here is, you exchanged your bike with a car, but that is at the cost of the car making more noise, but is more comfortable/faster. You can not have both.
PS: Just saw at the top, this is your first post – welcome to the Julia forum ![:wave: :wave:](https://emoji.discourse-cdn.com/twitter/wave.png?v=12)