@@ -17,7 +17,7 @@ julia> using StructArrays, Random
1717julia> Random. seed! (4 );
1818
1919julia> s = StructArray {ComplexF64} ((rand (2 ,2 ), rand (2 ,2 )))
20- 2 × 2 StructArray{Complex{Float64}, 2 ,NamedTuple{( :re , :im ),Tuple{ Array{Float64,2 },Array{Float64,2 }}} }:
20+ 2 × 2 StructArray ( :: Array{Float64,2} , :: Array{Float64,2} ) with eltype Complex{Float64 }:
2121 0.680079 + 0.625239im 0.92407 + 0.267358im
2222 0.874437 + 0.737254im 0.929336 + 0.804478im
2323
@@ -37,7 +37,7 @@ Note that the same approach can be used directly from an `Array` of complex numb
3737
3838``` julia
3939julia> StructArray ([1 + im, 3 - 2im ])
40- 2 - element StructArray{Complex{Int64}, 1 ,NamedTuple{( :re , :im ),Tuple{ Array{Int64,1 },Array{Int64,1 }}} }:
40+ 2 - element StructArray ( :: Array{Int64,1} , :: Array{Int64,1} ) with eltype Complex{Int64 }:
4141 1 + 1im
4242 3 - 2im
4343```
@@ -48,7 +48,7 @@ One can also create a `StructArrray` from an iterable of structs without creatin
4848
4949``` julia
5050julia> StructArray (log (j+ 2.0 * im) for j in 1 : 10 )
51- 10 - element StructArray{Complex{Float64}, 1 ,NamedTuple{( :re , :im ),Tuple{ Array{Float64,1 },Array{Float64,1 }}} }:
51+ 10 - element StructArray ( :: Array{Float64,1} , :: Array{Float64,1} ) with eltype Complex{Float64 }:
5252 0.8047189562170501 + 1.1071487177940904im
5353 1.0397207708399179 + 0.7853981633974483im
5454 1.2824746787307684 + 0.5880026035475675im
@@ -65,14 +65,14 @@ Another option is to create an uninitialized `StructArray` and then fill it with
6565
6666``` julia
6767julia> s = StructArray {ComplexF64} (undef, 2 , 2 )
68- 2 × 2 StructArray{Complex{Float64}, 2 ,NamedTuple{( :re , :im ),Tuple{ Array{Float64,2 },Array{Float64,2 }}} }:
68+ 2 × 2 StructArray ( :: Array{Float64,2} , :: Array{Float64,2} ) with eltype Complex{Float64 }:
6969 6.91646e-310 + 6.91646e-310im 6.91646e-310 + 6.91646e-310im
7070 6.91646e-310 + 6.91646e-310im 6.91646e-310 + 6.91646e-310im
7171
7272julia> rand! (s)
73- 2 × 2 StructArray{Complex{Float64}, 2 ,NamedTuple{( :re , :im ),Tuple{ Array{Float64,2 },Array{Float64,2 }}} }:
74- 0.446415 + 0.671453im 0.0797964 + 0.675723im
75- 0.0340059 + 0.420472im 0.907252 + 0.808263im
73+ 2 × 2 StructArray ( :: Array{Float64,2} , :: Array{Float64,2} ) with eltype Complex{Float64 }:
74+ 0.680079 + 0.874437im 0.625239 + 0.737254im
75+ 0.92407 + 0.929336im 0.267358 + 0.804478im
7676```
7777
7878### Using custom array types
@@ -87,39 +87,53 @@ julia> a = CuArray(rand(Float32, 10));
8787julia> b = CuArray (rand (Float32, 10 ));
8888
8989julia> StructArray {ComplexF32} ((a, b))
90- 10 - element StructArray{Complex{Float32}, 1 ,NamedTuple{( :re , :im ),Tuple{ CuArray{Float32,1 },CuArray{Float32,1 }}} }:
91- 0.7539003f0 + 0.5406891f0im
92- 0.2818451f0 + 0.60345674f0im
93- 0.3271774f0 + 0.56674314f0im
94- 0.6943406f0 + 0.8360009f0im
95- 0.9609026f0 + 0.27519035f0im
96- 0.051933408f0 + 0.93443274f0im
97- 0.51335454f0 + 0.90320504f0im
98- 0.6588727f0 + 0.16270757f0im
99- 0.20075476f0 + 0.6591008f0im
100- 0.58832633f0 + 0.45309567f0im
90+ 10 - element StructArray ( :: CuArray{Float32,1} , :: CuArray{Float32,1} ) with eltype Complex{Float32 }:
91+ 0.19555175f0 + 0.9604322f0im
92+ 0.68348145f0 + 0.5778245f0im
93+ 0.69664395f0 + 0.79825306f0im
94+ 0.118531585f0 + 0.3031248f0im
95+ 0.80057466f0 + 0.8964418f0im
96+ 0.63772964f0 + 0.2923274f0im
97+ 0.65374136f0 + 0.7932533f0im
98+ 0.6043732f0 + 0.65964353f0im
99+ 0.1106627f0 + 0.090207934f0im
100+ 0.707458f0 + 0.1700114f0im
101101
102102julia> c = CuArray (rand (ComplexF32, 10 ));
103103
104104julia> StructArray (c)
105- 10 - element StructArray{Complex{Float32},1 ,NamedTuple{(:re , :im ),Tuple{CuArray{Float32,1 },CuArray{Float32,1 }}}}:
106- 0.76695776f0 + 0.31588173f0im
107- 0.9804857f0 + 0.15740407f0im
108- 0.85849273f0 + 0.51903546f0im
109- 0.106796384f0 + 0.9493377f0im
110- 0.38152885f0 + 0.8419838f0im
111- 0.8892112f0 + 0.5276251f0im
112- 0.11579132f0 + 0.79168653f0im
113- 0.16804445f0 + 0.40545344f0im
114- 0.42822742f0 + 0.61150527f0im
115- 0.29996157f0 + 0.94151044f0im
105+ 10 - element StructArray (:: Array{Float32,1} , :: Array{Float32,1} ) with eltype Complex{Float32}:
106+ 0.7176411f0 + 0.864058f0im
107+ 0.252609f0 + 0.14824867f0im
108+ 0.26842773f0 + 0.9084332f0im
109+ 0.33128333f0 + 0.5106474f0im
110+ 0.6509278f0 + 0.87059164f0im
111+ 0.9522146f0 + 0.053706646f0im
112+ 0.899577f0 + 0.63242567f0im
113+ 0.325814f0 + 0.59225655f0im
114+ 0.56267905f0 + 0.21927536f0im
115+ 0.49719965f0 + 0.754143f0im
116+ ```
117+
118+ If you already have your data in a ` StructArray ` with field arrays of a given format (say plain ` Array ` ) you can change them with ` replace_storage ` :
119+
120+ ``` julia
121+ julia> s = StructArray ([1.0 + im, 2.0 - im])
122+ 2 - element StructArray (:: Array{Float64,1} , :: Array{Float64,1} ) with eltype Complex{Float64}:
123+ 1.0 + 1.0im
124+ 2.0 - 1.0im
125+
126+ julia> replace_storage (CuArray, s)
127+ 2 - element StructArray (:: CuArray{Float64,1} , :: CuArray{Float64,1} ) with eltype Complex{Float64}:
128+ 1.0 + 1.0im
129+ 2.0 - 1.0im
116130```
117131
118132## Example usage to store a data table
119133
120134``` julia
121135julia> t = StructArray ((a = [1 , 2 ], b = [" x" , " y" ]))
122- 2 - element StructArray{NamedTuple{( :a , :b ),Tuple {Int64,String}}, 1 , NamedTuple{(:a , :b ),Tuple{Array{ Int64,1 },Array{ String, 1 }} }}:
136+ 2 - element StructArray ( :: Array {Int64,1} , :: Array{String,1} ) with eltype NamedTuple{(:a , :b ),Tuple{Int64,String}}:
123137 (a = 1 , b = " x" )
124138 (a = 2 , b = " y" )
125139
@@ -132,7 +146,7 @@ julia> t.a
132146 2
133147
134148julia> push! (t, (a = 3 , b = " z" ))
135- 3 - element StructArray{NamedTuple{( :a , :b ),Tuple {Int64,String}}, 1 , NamedTuple{(:a , :b ),Tuple{Array{ Int64,1 },Array{ String, 1 }} }}:
149+ 3 - element StructArray ( :: Array {Int64,1} , :: Array{String,1} ) with eltype NamedTuple{(:a , :b ),Tuple{Int64,String}}:
136150 (a = 1 , b = " x" )
137151 (a = 2 , b = " y" )
138152 (a = 3 , b = " z" )
@@ -143,15 +157,16 @@ julia> push!(t, (a = 3, b = "z"))
143157StructArrays also provides a ` LazyRow ` wrapper for lazy row iteration. ` LazyRow(t, i) ` does not materialize the i-th row but returns a lazy wrapper around it on which ` getproperty ` does the correct thing. This is useful when the row has many fields only some of which are necessary. It also allows changing columns in place.
144158
145159``` julia
160+ julia> t = StructArray ((a = [1 , 2 ], b = [" x" , " y" ]));
161+
146162julia> LazyRow (t, 2 ). a
1471632
148164
149165julia> LazyRow (t, 2 ). a = 123
150166123
151167
152168julia> t
153- 2 - element StructArray{NamedTuple{(:a , :b ),Tuple{Int64,String}},1 ,NamedTuple{(:a , :b ),Tu
154- ple{Array{Int64,1 },Array{String,1 }}}}:
169+ 2 - element StructArray (:: Array{Int64,1} , :: Array{String,1} ) with eltype NamedTuple{(:a , :b ),Tuple{Int64,String}}:
155170 (a = 1 , b = " x" )
156171 (a = 123 , b = " y" )
157172```
0 commit comments