SLArrays

The SLArray and SLVector macros create static LabelledArrays. First the user would create the array type, then use that constructor to generate instances of the labelled array.

@SLArray and @SLVector macros

Macro constructors are convenient for building most SLArray objects. An @SLArray may be of arbitrary dimension while an @SLVector is a one dimensional array.

LabelledArrays.@SLArrayMacro
@SLArray Size Names
@SLArray Eltype Size Names

The macro creates a labelled static vector with element type ElType, names from Names, and size from Size. If no eltype is given, then the eltype is determined from the arguments in the constructor.

For example:

ABCD = @SLArray (2,2) (:a,:b,:c,:d)
x = ABCD(1.0, 2.5, 3.0, 5.0)
x.a == 1.0
x.b == 2.5
x.c == x[3]
x.d == x[2,2]
EFG = @SLArray (2,2) (e=1:3, f=4, g=2:4)
y = EFG(1.0,2.5,3.0,5.0)
EFG = @SLArray (2,2) (e=(2, :), f=4, g=2:4)

Users can also specify the indices directly.

julia> EFG = @SLArray (2,2) (e=1:3, f=4, g=2:4);
julia> y = EFG(1.0,2.5,3.0,5.0)
2×2 SLArray{Tuple{2,2},Float64,2,4,(e = 1:3, f = 4, g = 2:4)}:
 1.0  3.0
 2.5  5.0

julia> y.g
3-element view(reshape(::StaticArrays.SArray{Tuple{2,2},Float64,2,4}, 4), 2:4) with eltype Float64:
 2.5
 3.0
 5.0

julia> Arr = @SLArray (2, 2) (a = (2, :), b = 3);
julia> z = Arr(1, 2, 3, 4);
julia> z.a
2-element view(::StaticArrays.SArray{Tuple{2,2},Int64,2,4}, 2, :) with eltype Int64:
 2
 4
LabelledArrays.@SLVectorMacro
@SLVector Names
@SLVector Eltype Names

The macro creates a labelled static vector with element type ElType, and names from Names. If no eltype is given, then the eltype is determined from the values in the constructor. The array size is found from the input data.

For example:

ABC = @SLVector (:a,:b,:c)
x = ABC(1.0,2.5,3.0)
x.a == 1.0
x.b == 2.5
x.c == x[3]

SLArray and SLVector constructors

Alternatively, users can construct a static labelled array using the SLVector and SLArrays constructors by writing out the entries as keyword arguments:

LabelledArrays.SLArrayType
SLArray{::Tuple}(::NamedTuple)
SLArray{::Tuple}(kwargs)

These are the standard constructors for SLArray. For general N-dimensional labelled arrays, users need to specify the size (Tuple{dim1,dim2,...}) in the type parameter to the SLArray constructor:

julia> SLArray{Tuple{2,2}}((a=1, b=2, c=3, d=4))
2×2 SLArray{Tuple{2, 2}, Int64, 2, 4, (:a, :b, :c, :d)} with indices SOneTo(2)×SOneTo(2):
 :a => 1  :c => 3
 :b => 2  :d => 4

julia> SLArray{Tuple{2,2}}(a=1, b=2, c=3, d=4)
 2×2 SLArray{Tuple{2,2},2,(:a, :b, :c, :d),Int64}:
 1  3
 2  4

Constructing copies with some changed elements is supported by a keyword constructor whose first argument is the source and whose additional keyword arguments indicate the changes.

julia> ABCD = @SLArray (2,2) (:a,:b,:c,:d);
julia> B = ABCD(1,2,3,4);
julia> B2 = SLArray(B; c=30 )
2×2 SLArray{Tuple{2,2},Int64,2,4,(:a, :b, :c, :d)}:
 1  30
 2   4

Additional examples:

SLArray{Tuple{2,2}}((a=1, b=2, c=3, d=4))
SLVector(v1::SLArray; kwargs...)

Creates a copy of v1 with corresponding items in kwargs replaced.

For example:

ABCD = @SLArray (2,2) (:a,:b,:c,:d);
B = ABCD(1,2,3,4);
B2 = SLArray(B; c=30 )
LabelledArrays.SLVectorFunction
SLVector(::NamedTuple)
SLVector(kwargs)

The standard constructors for SLArray.

julia> SLVector(a=1, b=2, c=3)
3-element SLArray{Tuple{3},1,(:a, :b, :c),Int64}:
 1
 2
 3

Constructing copies with some items changed is supported by a keyword constructor whose first argument is the source and whose additional keyword arguments indicate the changes.

julia> v1 = SLVector(a=1.1, b=2.2, c=3.3);
julia> v2 = SLVector(v1; b=20.20, c=30.30 )
3-element SLArray{Tuple{3},Float64,1,3,(:a, :b, :c)}:
  1.1
 20.2
 30.3

Additional examples:

SLVector((a=1, b=2)) 
SLVector(a=1, b=2) 
SLVector(v1::SLArray; kwargs...)

Creates a 1D copy of v1 with corresponding items in kwargs replaced.

For example:

z = SLVector(a=1, b=2, c=3);
z2 = SLVector(z; c=30)

Manipulating SLArrays and SLVectors

Users may want a list of the labels or keys in an SLArray or SLVector. The symbols(::SLArray) function returns a tuple of array labels.

LabelledArrays.symbolsMethod
symbols(::SLArray)

Returns the labels of the SLArray .

For example:

julia> z = SLVector(a=1, b=2, c=3)
3-element SLArray{Tuple{3}, Int64, 1, 3, (:a, :b, :c)} with indices SOneTo(3):
 :a => 1
 :b => 2
 :c => 3

julia> symbols(z)
(:a, :b, :c)
symbols(::LArray)

Returns the labels of the LArray .

For example:

julia julia> z = @LVector Float64 (:a, :b, :c, :d); julia> symbols(z) (:a, :b, :c, :d)`