Skip to content

Commit 7d05ac2

Browse files
author
Pietro Vertechi
authored
Export fieldarrays (#50)
1 parent f8dc712 commit 7d05ac2

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ julia> s.re
2828
2×2 Array{Float64,2}:
2929
0.680079 0.92407
3030
0.874437 0.929336
31+
32+
julia> fieldarrays(s) # obtain all field arrays as a named tuple
33+
(re = [0.680079 0.92407; 0.874437 0.929336], im = [0.625239 0.267358; 0.737254 0.804478])
3134
```
3235

3336
Note that the same approach can be used directly from an `Array` of complex numbers:

src/StructArrays.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module StructArrays
22

33
import Requires
44
export StructArray, StructVector
5-
export collect_structarray
5+
export collect_structarray, fieldarrays
66

77
include("interface.jl")
88
include("structarray.jl")

src/structarray.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ function Base.similar(s::StructArray{T}, sz::Tuple) where {T}
8585
StructArray{T}(map(typ -> similar(typ, sz), fieldarrays(s)))
8686
end
8787

88+
"""
89+
`fieldarrays(s::StructArray)`
90+
91+
Return the field arrays corresponding to the various entry of the struct as a named tuple.
92+
If the struct has no names (e.g. a tuple) automatic names are assigned (`:x1, :x2, ...`).
93+
94+
## Examples
95+
96+
```julia
97+
julia> s = StructArray(rand(ComplexF64, 4));
98+
99+
julia> fieldarrays(s)
100+
(re = [0.396526, 0.486036, 0.459595, 0.0323561], im = [0.147702, 0.81043, 0.00993469, 0.487091])
101+
```
102+
"""
88103
fieldarrays(s::StructArray) = getfield(s, :fieldarrays)
89104

90105
Base.getproperty(s::StructArray, key::Symbol) = getfield(fieldarrays(s), key)

0 commit comments

Comments
 (0)