Skip to content

Commit c727c73

Browse files
authored
README + docs enhancement (#188)
* Documenter.jl compatible ToC links * julia-repl highlighting * typo * add explicit import for titlecase * Update make.jl
1 parent 3e2752f commit c727c73

File tree

2 files changed

+36
-33
lines changed

2 files changed

+36
-33
lines changed

README.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This can greatly improve both runtime performance, by avoiding type instabilitie
2929
DynamicQuantities can greatly outperform Unitful
3030
when the compiler cannot infer dimensions in a function:
3131

32-
```julia
32+
```julia-repl
3333
julia> using BenchmarkTools, DynamicQuantities; import Unitful
3434
3535
julia> dyn_uni = 0.2u"m/s"
@@ -54,7 +54,7 @@ while the Unitful quantity object, which stores its dimensions in the type, requ
5454
However, if the dimensions in your function *can* be inferred by the compiler,
5555
then you can get better speeds with Unitful:
5656

57-
```julia
57+
```julia-repl
5858
julia> g(x) = x ^ 2 * 0.3;
5959
6060
julia> @btime g($dyn_uni);
@@ -73,7 +73,7 @@ to units and the compiler can optimize away units from the code.
7373
You can create a `Quantity` object
7474
by using the convenience macro `u"..."`:
7575

76-
```julia
76+
```julia-repl
7777
julia> x = 0.3u"km/s"
7878
300.0 m s⁻¹
7979
@@ -83,7 +83,7 @@ julia> y = 42 * u"kg"
8383

8484
or by importing explicitly:
8585

86-
```julia
86+
```julia-repl
8787
julia> using DynamicQuantities: kPa
8888
8989
julia> room_temp = 100kPa
@@ -93,7 +93,7 @@ julia> room_temp = 100kPa
9393
Note that `Units` is an exported submodule, so you can
9494
also access this as `Units.kPa`. You may like to define
9595

96-
```julia
96+
```julia-repl
9797
julia> const U = Units
9898
```
9999

@@ -106,14 +106,14 @@ You can also construct values explicitly with the `Quantity` type,
106106
with 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
110110
julia> x = Quantity(300.0, length=1, time=-1)
111111
300.0 m s⁻¹
112112
```
113113

114114
Elementary calculations with `+, -, *, /, ^, sqrt, cbrt, abs` are supported:
115115

116-
```julia
116+
```julia-repl
117117
julia> x * y
118118
12600.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

138138
Furthermore, we can do dimensional analysis by detecting `DimensionError`:
139139

140-
```julia
140+
```julia-repl
141141
julia> x + 3 * x
142142
1.2 m¹ᐟ² kg
143143
@@ -147,14 +147,14 @@ ERROR: DimensionError: 0.3 m¹ᐟ² kg and 10.2 kg² s⁻² have incompatible di
147147

148148
The dimensions of a `Quantity` can be accessed either with `dimension(quantity)` for the entire `Dimensions` object:
149149

150-
```julia
150+
```julia-repl
151151
julia> dimension(x)
152152
m¹ᐟ² kg
153153
```
154154

155155
or with `umass`, `ulength`, etc., for the various dimensions:
156156

157-
```julia
157+
```julia-repl
158158
julia> umass(x)
159159
1//1
160160
@@ -164,15 +164,15 @@ julia> ulength(x)
164164

165165
Finally, you can strip units with `ustrip`:
166166

167-
```julia
167+
```julia-repl
168168
julia> ustrip(x)
169169
0.2
170170
```
171171

172172
You can also convert a quantity to a desired unit and *then* strip the units
173173
using a two-argument version of `ustrip`:
174174

175-
```julia
175+
```julia-repl
176176
julia> ustrip(u"km", 1000u"m")
177177
1.0
178178
@@ -187,27 +187,27 @@ This is equivalent to `ustrip(quantity / unit)` but performs dimension checks fi
187187
There are a variety of physical constants accessible
188188
via the `Constants` submodule:
189189

190-
```julia
190+
```julia-repl
191191
julia> Constants.c
192192
2.99792458e8 m s⁻¹
193193
```
194194

195195
which you may like to define as
196196

197-
```julia
197+
```julia-repl
198198
julia> const C = Constants
199199
```
200200

201201
These can also be used inside the `u"..."` macro:
202202

203-
```julia
203+
```julia-repl
204204
julia> u"Constants.c * Hz"
205205
2.99792458e8 m s⁻²
206206
```
207207

208208
Similarly, you can just import each individual constant:
209209

210-
```julia
210+
```julia-repl
211211
julia> using DynamicQuantities.Constants: h
212212
```
213213

@@ -220,7 +220,7 @@ You can also choose to not eagerly convert to SI base units,
220220
instead leaving the units as the user had written them.
221221
For example:
222222

223-
```julia
223+
```julia-repl
224224
julia> q = 100us"cm * kPa"
225225
100.0 cm kPa
226226
@@ -231,14 +231,14 @@ julia> q^2
231231
You can convert to regular SI base units with
232232
`uexpand`:
233233

234-
```julia
234+
```julia-repl
235235
julia> uexpand(q^2)
236236
1.0e6 kg² s⁻⁴
237237
```
238238

239239
This also works with constants:
240240

241-
```julia
241+
```julia-repl
242242
julia> x = us"Constants.c * Hz"
243243
1.0 Hz c
244244
@@ -250,7 +250,7 @@ julia> uexpand(x^2)
250250
```
251251

252252
You can also convert a quantity in regular base SI units to symbolic units with the `|>` infix operator
253-
```julia
253+
```julia-repl
254254
julia> 5e-9u"m" |> us"nm"
255255
5.0 nm
256256
```
@@ -262,13 +262,13 @@ with `uconvert(us"nm", 5e-9u"m")`.)
262262

263263
Finally, you can also import these directly:
264264

265-
```julia
265+
```julia-repl
266266
julia> using DynamicQuantities.SymbolicUnits: cm
267267
```
268268

269269
or constants:
270270

271-
```julia
271+
```julia-repl
272272
julia> using DynamicQuantities.SymbolicConstants: h
273273
```
274274

@@ -281,13 +281,13 @@ respectively.
281281

282282
You can create custom units with the `@register_unit` macro:
283283

284-
```julia
284+
```julia-repl
285285
julia> @register_unit OneFiveV 1.5u"V"
286286
```
287287

288288
and then use it in calculations normally:
289289

290-
```julia
290+
```julia-repl
291291
julia> x = us"OneFiveV"
292292
1.0 OneFiveV
293293
@@ -303,7 +303,7 @@ julia> 3us"V" |> us"OneFiveV"
303303
You can also use "*affine*" units such as Celsius or Fahrenheit,
304304
using the `ua"..."` string macro:
305305

306-
```julia
306+
```julia-repl
307307
julia> room_temp = 22ua"degC"
308308
295.15 K
309309
@@ -316,7 +316,7 @@ can use them in the same way as regular quantities, including taking differences
316316

317317
To convert back, you can use the two-argument `ustrip` with the particular affine unit:
318318

319-
```julia
319+
```julia-repl
320320
julia> ustrip(ua"degC", 295.15u"K")
321321
22.0
322322
```
@@ -337,7 +337,7 @@ julia> ar = rand(3)u"m/s"
337337
This `QuantityArray` is a subtype `<:AbstractArray{Quantity{Float64,Dimensions{...}},1}`,
338338
meaning that indexing a specific element will return a `Quantity`:
339339

340-
```julia
340+
```julia-repl
341341
julia> ar[2]
342342
0.992546340360901 m s⁻¹
343343
@@ -378,7 +378,7 @@ julia> x = (1:3)us"km/h"
378378

379379
DynamicQuantities allows you to convert back and forth from Unitful.jl:
380380

381-
```julia
381+
```julia-repl
382382
julia> using Unitful: Unitful, @u_str; import DynamicQuantities
383383
384384
julia> x = 0.5u"km/s"
@@ -407,7 +407,7 @@ for all known unit and constant symbols, using a sparse array.
407407
You can create custom spaces dimension spaces by simply creating
408408
a Julia struct subtyped to `AbstractDimensions`:
409409

410-
```julia
410+
```julia-repl
411411
julia> 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
427427
which represents rational numbers with an integer numerator and fixed denominator.
428428
This is much faster than `Rational`.
429429

430-
```julia
430+
```julia-repl
431431
julia> typeof(0.5u"kg")
432432
Quantity{Float64, Dimensions{FRInt32}}
433433
```
434434

435435
You can change the type of the value field by initializing with a value
436436
explicitly of the desired type.
437437

438-
```julia
438+
```julia-repl
439439
julia> typeof(Quantity(Float16(0.5), mass=1, length=1))
440440
Quantity{Float16, Dimensions{FRInt32}}
441441
```
442442

443443
or by conversion:
444444

445-
```julia
445+
```julia-repl
446446
julia> typeof(convert(Quantity{Float16}, 0.5u"m/s"))
447447
Quantity{Float16, Dimensions{FRInt32}}
448448
```
@@ -454,7 +454,7 @@ struct will fit into 64 bits.
454454
You can change the type of the dimensions field by passing
455455
the type you wish to use as the second argument to `Quantity`:
456456

457-
```julia
457+
```julia-repl
458458
julia> using DynamicQuantities
459459
460460
julia> R8 = Dimensions{FRInt8};

docs/make.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ readme = replace(readme, r"<img src=\"([^\"]+)\"[^>]+>.*" => s"![](\1)")
1616
# Then, we remove any line with "<div" on it:
1717
readme = replace(readme, r"<[/]?div.*" => s"")
1818

19+
# Then, we titlecase the ToC entries to be compatible with Documenter.jl
20+
readme = replace(readme, r"\[(.*?)\]\(#(.*?)\)" => titlecase)
21+
1922
# Finally, we read in file docs/src/index_base.md:
2023
index_base = open(dirname(@__FILE__) * "/src/index_base.md") do io
2124
read(io, String)

0 commit comments

Comments
 (0)