Julia's Extended Array Conversions Interface
The following ArrayInterface functions extend Julia's Array interface for how arrays can be converted to different forms.
Conversion Functions
ArrayInterface.aos_to_soa
— Functionaos_to_soa(x)
Converts an array of structs formulation to a struct of array.
ArrayInterface.promote_eltype
— Functionpromote_eltype(::Type{<:AbstractArray{T,N}}, ::Type{T2})
Computes the type of the AbstractArray
that results from the element type changing to promote_type(T,T2)
.
Note that no generic fallback is given.
ArrayInterface.restructure
— Functionrestructure(x,y)
Restructures the object y
into a shape of x
, keeping its values intact. For simple objects like an Array
, this simply amounts to a reshape. However, for more complex objects such as an ArrayPartition
, not all of the structural information is adequately contained in the type for standard tools to work. In these cases, restructure
gives a way to convert for example an Array
into a matching ArrayPartition
.
ArrayInterface.safevec
— Functionsafevec(v)
It is a form of vec
which is safe for all values in vector spaces, i.e., if it is already a vector, like an AbstractVector or Number, it will return said AbstractVector or Number.
ArrayInterface.has_trivial_array_constructor
— Functionhas_trivial_array_constructor(T::Type, args...) -> Bool
Returns true
if an object of type T
can be constructed using the collection of args
Note: This checks if a compatible convert
methood exists between T
and args
Examples:
julia> ca = ComponentVector((x = rand(3), y = rand(4),))
ComponentVector{Float64}(x = [0.6549137106381634, 0.37555505280294565, 0.8521039568665254], y = [0.40314196291239024, 0.35484725607638834, 0.6580528978034597, 0.10055508457632167])
julia> ArrayInterface.has_trivial_array_constructor(typeof(ca), ones(6))
true
julia> ArrayInterface.has_trivial_array_constructor(typeof(cv), (x = rand(6),))
false