diff --git a/README.md b/README.md index 51449f83..dae1307b 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,9 @@ julia> s.re 2×2 Array{Float64,2}: 0.680079 0.92407 0.874437 0.929336 + +julia> fieldarrays(s) # obtain all field arrays as a named tuple +(re = [0.680079 0.92407; 0.874437 0.929336], im = [0.625239 0.267358; 0.737254 0.804478]) ``` Note that the same approach can be used directly from an `Array` of complex numbers: diff --git a/src/StructArrays.jl b/src/StructArrays.jl index 882fde70..ab698a02 100644 --- a/src/StructArrays.jl +++ b/src/StructArrays.jl @@ -2,7 +2,7 @@ module StructArrays import Requires export StructArray, StructVector -export collect_structarray +export collect_structarray, fieldarrays include("interface.jl") include("structarray.jl") diff --git a/src/structarray.jl b/src/structarray.jl index f2b6f728..8d7376de 100644 --- a/src/structarray.jl +++ b/src/structarray.jl @@ -85,6 +85,21 @@ function Base.similar(s::StructArray{T}, sz::Tuple) where {T} StructArray{T}(map(typ -> similar(typ, sz), fieldarrays(s))) end +""" +`fieldarrays(s::StructArray)` + +Return the field arrays corresponding to the various entry of the struct as a named tuple. +If the struct has no names (e.g. a tuple) automatic names are assigned (`:x1, :x2, ...`). + +## Examples + +```julia +julia> s = StructArray(rand(ComplexF64, 4)); + +julia> fieldarrays(s) +(re = [0.396526, 0.486036, 0.459595, 0.0323561], im = [0.147702, 0.81043, 0.00993469, 0.487091]) +``` +""" fieldarrays(s::StructArray) = getfield(s, :fieldarrays) Base.getproperty(s::StructArray, key::Symbol) = getfield(fieldarrays(s), key)