Usage
The FixedSizeArray type
Constructors
The basic type provided by this package is FixedSizeArray. Its constructor uses a similar syntax to Base's Array:
julia> using FixedSizeArraysjulia> v = FixedSizeArray{Float64}(undef, 3)3-element FixedSizeArray{Float64, 1, Memory{Float64}}: 6.9419837859114e-310 4.243992035e-314 6.94198375836664e-310julia> M = FixedSizeArray{Float64}(undef, 2, 2)2×2 FixedSizeArray{Float64, 2, Memory{Float64}}: 0.0 5.0e-324 0.0 0.0
You can also construct FixedSizeArrays from other arrays:
julia> using FixedSizeArrays
julia> arr = [10 20; 30 14]
2×2 Matrix{Int64}:
10 20
30 14
julia> FixedSizeArray(arr) # construct from an `AbstractArray` value
2×2 FixedSizeArray{Int64, 2, Memory{Int64}}:
10 20
30 14
julia> FixedSizeArray{Float64}(arr) # construct from an `AbstractArray` value while converting element type
2×2 FixedSizeArray{Float64, 2, Memory{Float64}}:
10.0 20.0
30.0 14.0To "convert" a wrapper type like Adjoint while preserving the wrapper structure, you can use Adapt:
julia> using FixedSizeArrays, Adapt
julia> arr = transpose([10 20; 30 14])
2×2 transpose(::Matrix{Int64}) with eltype Int64:
10 30
20 14
julia> adapt(FixedSizeArray, arr)
2×2 transpose(::FixedSizeArray{Int64, 2, Memory{Int64}}) with eltype Int64:
10 30
20 14For 1- and 2-dimensional arrays you can use the aliases FixedSizeVector and FixedSizeMatrix, respectively:
julia> using FixedSizeArraysjulia> v = FixedSizeVector{Float64}(undef, 2)2-element FixedSizeArray{Float64, 1, Memory{Float64}}: 6.9419837859114e-310 1.25197752027e-312julia> M = FixedSizeMatrix{Float64}(undef, 3, 3)3×3 FixedSizeArray{Float64, 2, Memory{Float64}}: 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
The memory backend
The FixedSizeArray{T,N,Mem} type has three parameters:
T: the type of the elements of the array (e.g.Int64orFloat64);N: the number of dimensions of the array (e.g. 1 for a vector, 2 for a matrix);Mem<:DenseVector{T}: the memory backend.
In Julia v1.11+, the default memory backend is the Memory{T} type. Since Julia v1.10 does not have the Memory type, to make this package usable also on Julia v1.10 Vector{T} is used as memory backend, but many of the memory/performance optimizations enabled by this package will not be available in that version of Julia and in general FixedSizeArrays.jl does not provide significant improvements compared to Base's Array for that specific version.
To make it easier to refer to the concrete type FixedSizeArray{T,N,Mem} with the default memory backend, the following convenient aliases are available:
The collect_as utility
The array literals syntax [A, B, C, ...] to construct arrays is limited to Base's Array and cannot be extended to custom array types. The package Collects.jl provides a convenient function, collect_as, to overcome this limitation and construct FixedSizeArrays out of any iterable:
julia> iter = (i for i ∈ 7:9 if i≠8);
julia> using FixedSizeArrays, Collects
julia> collect_as(FixedSizeArray, iter) # construct from an arbitrary iterator
2-element FixedSizeArray{Int64, 1, Memory{Int64}}:
7
9
julia> collect_as(FixedSizeArray{Float64}, iter) # construct from an arbitrary iterator while converting element type
2-element FixedSizeArray{Float64, 1, Memory{Float64}}:
7.0
9.0
julia> collect_as(FixedSizeVectorDefault, (3.14, -4.2, 2.68)) # construct from a tuple
3-element FixedSizeArray{Float64, 1, Memory{Float64}}:
3.14
-4.2
2.68See the Collects.jl Readme for more information.
BoundsErrorLight exception
To facilitate the escape analysis of FixedSizeArrays, accessing an out-of-bound index of these arrays raises a BoundsErrorLight exception when possible. This exception type does not store the entire array for reporting the error message, thus enabling more performance optimizations compared to arrays which throw BoundsError exceptions.