Skip to content

Commit

Permalink
Export fieldarrays (JuliaArrays#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Vertechi authored Feb 17, 2019
1 parent f8dc712 commit 7d05ac2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/StructArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module StructArrays

import Requires
export StructArray, StructVector
export collect_structarray
export collect_structarray, fieldarrays

include("interface.jl")
include("structarray.jl")
Expand Down
15 changes: 15 additions & 0 deletions src/structarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7d05ac2

Please sign in to comment.