@@ -206,6 +206,49 @@ julia> expand_units(x^2)
206
206
8.987551787368176e16 m² s⁻⁴
207
207
```
208
208
209
+ ### Arrays
210
+
211
+ For working with an array of quantities that have the same dimensions,
212
+ you can use a ` QuantityArray ` :
213
+
214
+ ``` julia
215
+ julia> ar = QuantityArray (rand (3 ), u " m/s" )
216
+ 3 - element QuantityArray (:: Vector{Float64} , :: Quantity{Float64, Dimensions{DynamicQuantities.FixedRational{Int32, 25200}}} ):
217
+ 0.2729202669351497 m s⁻¹
218
+ 0.992546340360901 m s⁻¹
219
+ 0.16863543422972482 m s⁻¹
220
+ ```
221
+
222
+ This ` QuantityArray ` is a subtype ` <:AbstractArray{Quantity{Float64,Dimensions{...}},1} ` ,
223
+ meaning that indexing a specific element will return a ` Quantity ` :
224
+
225
+ ``` julia
226
+ julia> ar[2 ]
227
+ 0.992546340360901 m s⁻¹
228
+
229
+ julia> ar[2 ] *= 2
230
+ 1.985092680721802 m s⁻¹
231
+
232
+ julia> ar[2 ] += 0.5 u " m/s"
233
+ 2.485092680721802 m s⁻¹
234
+ ```
235
+
236
+ This also has a custom broadcasting interface which
237
+ allows the compiler to avoid redundant dimension calculations,
238
+ relative to if you had simply used an array of quantities:
239
+
240
+ ``` julia
241
+ julia> f (v) = v^ 2 * 1.5 ;
242
+
243
+ julia> @btime $ f .(xa) setup= (xa = randn (100000 ) .* u " km/s" );
244
+ 109.500 μs (2 allocations: 3.81 MiB)
245
+
246
+ julia> @btime $ f .(qa) setup= (xa = randn (100000 ) .* u " km/s" ; qa = QuantityArray (xa));
247
+ 50.917 μs (3 allocations: 781.34 KiB)
248
+ ```
249
+
250
+ So we can see the ` QuantityArray ` version saves on both time and memory.
251
+
209
252
### Unitful
210
253
211
254
DynamicQuantities allows you to convert back and forth from Unitful.jl:
@@ -281,30 +324,3 @@ julia> @btime f($q8);
281
324
julia> @btime f ($ q32);
282
325
8.417 μs (2 allocations: 39.11 KiB)
283
326
```
284
-
285
- ## Vectors
286
-
287
- There is not (yet) a separate class for vectors, but you can create units
288
- like so:
289
-
290
- ``` julia
291
- julia> randn (5 ) .* u " m/s"
292
- 5 - element Vector{Quantity{Float64, Dimensions{FixedRational{Int32, 25200 }}}}:
293
- 1.1762086954956399 m s⁻¹
294
- 1.320811324040591 m s⁻¹
295
- 0.6519033652437799 m s⁻¹
296
- 0.7424822374423569 m s⁻¹
297
- 0.33536928068133726 m s⁻¹
298
- ```
299
-
300
- Because it is type stable, you can have mixed units in a vector too:
301
-
302
- ``` julia
303
- julia> v = [Quantity (randn (), mass= rand (0 : 5 ), length= rand (0 : 5 )) for _= 1 : 5 ]
304
- 5 - element Vector{Quantity{Float64, Dimensions{FixedRational{Int32, 25200 }}}}:
305
- 0.4309293892461158 kg⁵
306
- 1.415520139801276
307
- 1.2179414706524276 m³ kg⁴
308
- - 0.18804207255117408 m³ kg⁵
309
- 0.52123911329638 m³ kg²
310
- ```
0 commit comments