@@ -29,7 +29,7 @@ This can greatly improve both runtime performance, by avoiding type instabilitie
2929DynamicQuantities can greatly outperform Unitful
3030when the compiler cannot infer dimensions in a function:
3131
32- ``` julia
32+ ``` julia-repl
3333julia> using BenchmarkTools, DynamicQuantities; import Unitful
3434
3535julia> dyn_uni = 0.2u"m/s"
@@ -54,7 +54,7 @@ while the Unitful quantity object, which stores its dimensions in the type, requ
5454However, if the dimensions in your function * can* be inferred by the compiler,
5555then you can get better speeds with Unitful:
5656
57- ``` julia
57+ ``` julia-repl
5858julia> g(x) = x ^ 2 * 0.3;
5959
6060julia> @btime g($dyn_uni);
@@ -73,7 +73,7 @@ to units and the compiler can optimize away units from the code.
7373You can create a ` Quantity ` object
7474by using the convenience macro ` u"..." ` :
7575
76- ``` julia
76+ ``` julia-repl
7777julia> x = 0.3u"km/s"
7878300.0 m s⁻¹
7979
@@ -83,7 +83,7 @@ julia> y = 42 * u"kg"
8383
8484or by importing explicitly:
8585
86- ``` julia
86+ ``` julia-repl
8787julia> using DynamicQuantities: kPa
8888
8989julia> room_temp = 100kPa
@@ -93,7 +93,7 @@ julia> room_temp = 100kPa
9393Note that ` Units ` is an exported submodule, so you can
9494also access this as ` Units.kPa ` . You may like to define
9595
96- ``` julia
96+ ``` julia-repl
9797julia> const U = Units
9898```
9999
@@ -106,14 +106,14 @@ You can also construct values explicitly with the `Quantity` type,
106106with a value and keyword arguments for the powers of the physical dimensions
107107(` mass ` , ` length ` , ` time ` , ` current ` , ` temperature ` , ` luminosity ` , ` amount ` ):
108108
109- ``` julia
109+ ``` julia-repl
110110julia> x = Quantity(300.0, length=1, time=-1)
111111300.0 m s⁻¹
112112```
113113
114114Elementary calculations with ` +, -, *, /, ^, sqrt, cbrt, abs ` are supported:
115115
116- ``` julia
116+ ``` julia-repl
117117julia> x * y
11811812600.0 m kg s⁻¹
119119
@@ -137,7 +137,7 @@ Each of these values has the same type, which means we don't need to perform typ
137137
138138Furthermore, we can do dimensional analysis by detecting ` DimensionError ` :
139139
140- ``` julia
140+ ``` julia-repl
141141julia> x + 3 * x
1421421.2 m¹ᐟ² kg
143143
@@ -147,14 +147,14 @@ ERROR: DimensionError: 0.3 m¹ᐟ² kg and 10.2 kg² s⁻² have incompatible di
147147
148148The dimensions of a ` Quantity ` can be accessed either with ` dimension(quantity) ` for the entire ` Dimensions ` object:
149149
150- ``` julia
150+ ``` julia-repl
151151julia> dimension(x)
152152m¹ᐟ² kg
153153```
154154
155155or with ` umass ` , ` ulength ` , etc., for the various dimensions:
156156
157- ``` julia
157+ ``` julia-repl
158158julia> umass(x)
1591591//1
160160
@@ -164,15 +164,15 @@ julia> ulength(x)
164164
165165Finally, you can strip units with ` ustrip ` :
166166
167- ``` julia
167+ ``` julia-repl
168168julia> ustrip(x)
1691690.2
170170```
171171
172172You can also convert a quantity to a desired unit and * then* strip the units
173173using a two-argument version of ` ustrip ` :
174174
175- ``` julia
175+ ``` julia-repl
176176julia> ustrip(u"km", 1000u"m")
1771771.0
178178
@@ -187,27 +187,27 @@ This is equivalent to `ustrip(quantity / unit)` but performs dimension checks fi
187187There are a variety of physical constants accessible
188188via the ` Constants ` submodule:
189189
190- ``` julia
190+ ``` julia-repl
191191julia> Constants.c
1921922.99792458e8 m s⁻¹
193193```
194194
195195which you may like to define as
196196
197- ``` julia
197+ ``` julia-repl
198198julia> const C = Constants
199199```
200200
201201These can also be used inside the ` u"..." ` macro:
202202
203- ``` julia
203+ ``` julia-repl
204204julia> u"Constants.c * Hz"
2052052.99792458e8 m s⁻²
206206```
207207
208208Similarly, you can just import each individual constant:
209209
210- ``` julia
210+ ``` julia-repl
211211julia> using DynamicQuantities.Constants: h
212212```
213213
@@ -220,7 +220,7 @@ You can also choose to not eagerly convert to SI base units,
220220instead leaving the units as the user had written them.
221221For example:
222222
223- ``` julia
223+ ``` julia-repl
224224julia> q = 100us"cm * kPa"
225225100.0 cm kPa
226226
@@ -231,14 +231,14 @@ julia> q^2
231231You can convert to regular SI base units with
232232` uexpand ` :
233233
234- ``` julia
234+ ``` julia-repl
235235julia> uexpand(q^2)
2362361.0e6 kg² s⁻⁴
237237```
238238
239239This also works with constants:
240240
241- ``` julia
241+ ``` julia-repl
242242julia> x = us"Constants.c * Hz"
2432431.0 Hz c
244244
@@ -250,7 +250,7 @@ julia> uexpand(x^2)
250250```
251251
252252You can also convert a quantity in regular base SI units to symbolic units with the ` |> ` infix operator
253- ``` julia
253+ ``` julia-repl
254254julia> 5e-9u"m" |> us"nm"
2552555.0 nm
256256```
@@ -262,13 +262,13 @@ with `uconvert(us"nm", 5e-9u"m")`.)
262262
263263Finally, you can also import these directly:
264264
265- ``` julia
265+ ``` julia-repl
266266julia> using DynamicQuantities.SymbolicUnits: cm
267267```
268268
269269or constants:
270270
271- ``` julia
271+ ``` julia-repl
272272julia> using DynamicQuantities.SymbolicConstants: h
273273```
274274
@@ -281,13 +281,13 @@ respectively.
281281
282282You can create custom units with the ` @register_unit ` macro:
283283
284- ``` julia
284+ ``` julia-repl
285285julia> @register_unit OneFiveV 1.5u"V"
286286```
287287
288288and then use it in calculations normally:
289289
290- ``` julia
290+ ``` julia-repl
291291julia> x = us"OneFiveV"
2922921.0 OneFiveV
293293
@@ -303,7 +303,7 @@ julia> 3us"V" |> us"OneFiveV"
303303You can also use "* affine* " units such as Celsius or Fahrenheit,
304304using the ` ua"..." ` string macro:
305305
306- ``` julia
306+ ``` julia-repl
307307julia> room_temp = 22ua"degC"
308308295.15 K
309309
@@ -316,7 +316,7 @@ can use them in the same way as regular quantities, including taking differences
316316
317317To convert back, you can use the two-argument ` ustrip ` with the particular affine unit:
318318
319- ``` julia
319+ ``` julia-repl
320320julia> ustrip(ua"degC", 295.15u"K")
32132122.0
322322```
@@ -337,7 +337,7 @@ julia> ar = rand(3)u"m/s"
337337This ` QuantityArray ` is a subtype ` <:AbstractArray{Quantity{Float64,Dimensions{...}},1} ` ,
338338meaning that indexing a specific element will return a ` Quantity ` :
339339
340- ``` julia
340+ ``` julia-repl
341341julia> ar[2]
3423420.992546340360901 m s⁻¹
343343
@@ -378,7 +378,7 @@ julia> x = (1:3)us"km/h"
378378
379379DynamicQuantities allows you to convert back and forth from Unitful.jl:
380380
381- ``` julia
381+ ``` julia-repl
382382julia> using Unitful: Unitful, @u_str; import DynamicQuantities
383383
384384julia> x = 0.5u"km/s"
@@ -407,7 +407,7 @@ for all known unit and constant symbols, using a sparse array.
407407You can create custom spaces dimension spaces by simply creating
408408a Julia struct subtyped to ` AbstractDimensions ` :
409409
410- ``` julia
410+ ``` julia-repl
411411julia> struct CookiesAndMilk{R} <: AbstractDimensions{R}
412412 cookies::R
413413 milk::R
@@ -427,22 +427,22 @@ Exponents are tracked by default with the type `FRInt32` (alias for `FixedRation
427427which represents rational numbers with an integer numerator and fixed denominator.
428428This is much faster than ` Rational ` .
429429
430- ``` julia
430+ ``` julia-repl
431431julia> typeof(0.5u"kg")
432432Quantity{Float64, Dimensions{FRInt32}}
433433```
434434
435435You can change the type of the value field by initializing with a value
436436explicitly of the desired type.
437437
438- ``` julia
438+ ``` julia-repl
439439julia> typeof(Quantity(Float16(0.5), mass=1, length=1))
440440Quantity{Float16, Dimensions{FRInt32}}
441441```
442442
443443or by conversion:
444444
445- ``` julia
445+ ``` julia-repl
446446julia> typeof(convert(Quantity{Float16}, 0.5u"m/s"))
447447Quantity{Float16, Dimensions{FRInt32}}
448448```
@@ -454,7 +454,7 @@ struct will fit into 64 bits.
454454You can change the type of the dimensions field by passing
455455the type you wish to use as the second argument to ` Quantity ` :
456456
457- ``` julia
457+ ``` julia-repl
458458julia> using DynamicQuantities
459459
460460julia> R8 = Dimensions{FRInt8};
0 commit comments