Skip to content

Commit 5e182be

Browse files
Merge pull request #119 from 00krishna-opensource/add_docstrings
Building out LabelArrays docstrings.
2 parents 2ced3cd + 2c6d984 commit 5e182be

File tree

7 files changed

+296
-161
lines changed

7 files changed

+296
-161
lines changed

docs/pages.jl

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

33
pages=[
44
"Home" => "index.md",
5+
"Example: Nice DiffEq Syntax Without A DSL" => "Example_dsl.md",
56
"SLArrays" => "SLArrays.md",
67
"LArrays" => "LArrays.md",
7-
"Example: Nice DiffEq Syntax Without A DSL" => "Example_dsl.md",
88
"Relation to NamedTuples" => "NamedTuples_relation.md",
99
"Note_labelled_slices.md"
1010
]

docs/src/Example_dsl.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
# Example: Nice DiffEq Syntax Without A DSL
22

3+
Users of the SciML ecosystem are often solving large models with complicated
4+
states and/or hundreds or thousands of parameters. These models are implemented
5+
using arrays, and those arrays have traditionally been indexed by integers,
6+
such as `p[1]` or `p[1:5]`. Numerical indexing is wonderful for small
7+
models, but can quickly cause problems as models become bigger. It is easy to
8+
forget which index corresponds to which reaction rate or which diffusion coeffient.
9+
This confusion can lead to difficult to debug problems in a user's code. `LabelledArrays`
10+
can make an important difference here. It is much easier to build a model using parameter
11+
references such as `p.rate_nacl` or `p.probability_birth`, instead
12+
of `p[26]` or `p[1026]`. Labelled arrays make both the development and debugging of models
13+
much faster.
14+
315
LabelledArrays.jl are a way to get DSL-like syntax without a macro. In this case,
416
we can solve differential equations with labelled components by making use of
517
labelled arrays, and always refer to the components by name instead of index.
618

7-
Let's solve the Lorenz equation. Using `@LVector`s, we can do:
19+
One key caveat is that users do not need to sacrifice performance when using
20+
labelled arrays. Labelled arrays are as performant as traditional numerically
21+
indexed arrays.
22+
23+
Let's solve the Lorenz equation using an `LVector`s. `LVectors` are
24+
mutable, hence we can use the non-allocating form of the `OrdinaryDiffEq`
25+
API.
826

927
```julia
1028
using LabelledArrays, OrdinaryDiffEq
1129

12-
function lorenz_f(du,u,p,t)
30+
function lorenz_f!(du,u,p,t)
1331
du.x = p.σ*(u.y-u.x)
1432
du.y = u.x*(p.ρ-u.z) - u.y
1533
du.z = u.x*u.y - p.β*u.z
@@ -18,13 +36,21 @@ end
1836
u0 = @LArray [1.0,0.0,0.0] (:x,:y,:z)
1937
p = @LArray [10.0, 28.0, 8/3] (,,)
2038
tspan = (0.0,10.0)
21-
prob = ODEProblem(lorenz_f,u0,tspan,p)
39+
prob = ODEProblem(lorenz_f!,u0,tspan,p)
2240
sol = solve(prob,Tsit5())
2341
# Now the solution can be indexed as .x/y/z as well!
2442
sol[10].x
2543
```
2644

27-
We can also make use of `@SLVector`:
45+
In the example above, we used an `LArray` to define the
46+
intial state `u0` as well as the parameter vector `p`.
47+
The reminder of the ODE solution steps are are no different
48+
that the original `DifferentialEquations` [tutorials](https://diffeq.sciml.ai/stable/tutorials/ode_example/#Example-2:-Solving-Systems-of-Equations).
49+
50+
Alternatively, we can use an immutable `SLVector` to
51+
implement the same equation. In this case, we need to
52+
use the allocating form of the `OrdinaryDiffEq` API when
53+
defining our model equation.
2854

2955
```julia
3056
LorenzVector = @SLVector (:x,:y,:z)
@@ -42,4 +68,5 @@ p = LorenzParameterVector(10.0,28.0,8/3)
4268
tspan = (0.0,10.0)
4369
prob = ODEProblem(f,u0,tspan,p)
4470
sol = solve(prob,Tsit5())
45-
```
71+
```
72+

docs/src/LArrays.md

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
11
# LArrays
22

3-
The `LArrays`s are fully mutable arrays with labels. There is no performance
4-
loss by using the labelled instead of indexing. Using the macro with values
5-
and labels generates the labelled array with the given values:
3+
`LArrays` are fully mutable arrays with labels. There is no performance
4+
loss by using labelled indexing instead of purely numerical indexing.
5+
Using the macro with values and labels generates the labelled array with
6+
the given values:
67

7-
```julia
8-
A = @LArray [1,2,3] (:a,:b,:c)
9-
A.a == 1
10-
```
11-
12-
One can generate a labelled array with undefined values by instead giving
13-
the dimensions:
8+
Users interested in using labelled elements in their arrays should also
9+
consider `ComponentArrays` from the
10+
[ComponentArrays.jl](https://github.com/jonniedie/ComponentArrays.jl)
11+
library. `ComponentArrays` are well integrated into the SciML ecosystem.
1412

15-
```julia
16-
A = @LArray Float64 (2,2) (:a,:b,:c,:d)
17-
W = rand(2,2)
18-
A .= W
19-
A.d == W[2,2]
20-
```
13+
## `@LArray` and `@LVector` macros
2114

22-
or using an `@LVector` shorthand:
15+
Macro constructors are convenient for building most `LArray` objects. An
16+
`@LArray` may be of arbitrary dimension while an `@LVector` is a
17+
one dimensional array.
2318

24-
```julia
25-
A = @LVector Float64 (:a,:b,:c,:d)
26-
A .= rand(4)
19+
```@docs
20+
@LArray
21+
@LVector
2722
```
2823

29-
As with `SLArray`, alternative constructors exist that use the keyword argument
30-
form:
24+
## `LArray` and `LVector` constructors
3125

32-
```julia
33-
julia> LVector(a=1, b=2, c=3)
34-
3-element LArray{Int64,1,(:a, :b, :c)}:
35-
1
36-
2
37-
3
26+
The original constructors for `LArray`s and `LVector`s are as
27+
follows:
3828

39-
julia> LArray((2,2); a=1, b=2, c=3, d=4) # need to specify size as first argument
40-
2×2 LArray{Int64,2,(:a, :b, :c, :d)}:
41-
1 3
42-
2 4
29+
```@docs
30+
LArray
31+
LVector
4332
```
33+
## Manipulating `LArrays` and `LVectors`
4434

45-
One can also specify the indices directly.
46-
```julia
47-
julia> z = @LArray [1.,2.,3.] (a = 1:2, b = 2:3);
48-
julia> z.b
49-
2-element view(::Array{Float64,1}, 2:3) with eltype Float64:
50-
2.0
51-
3.0
52-
julia> z = @LArray [1 2; 3 4] (a = (2, :), b = 2:3);
53-
julia> z.a
54-
2-element view(::Array{Int64,2}, 2, :) with eltype Int64:
55-
3
56-
4
57-
```
35+
User may want a list of the labels or keys in an `LArray` or `LVector`.
36+
The `symbols(::LArray)` function returns a tuple of array labels.
5837

59-
The labels of LArray and SLArray can be accessed
60-
by function `symbols`, which returns a tuple of symbols.
38+
```@docs
39+
symbols
40+
```

docs/src/SLArrays.md

Lines changed: 20 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,35 @@
11
# SLArrays
22

3-
The `SLArray` and `SLVector` macros are for creating static LabelledArrays.
4-
First you create the type and then you can use that constructor to generate
3+
The `SLArray` and `SLVector` macros create static LabelledArrays.
4+
First the user would create the array type, then use that constructor to generate
55
instances of the labelled array.
66

7-
```julia
8-
ABC = @SLVector (:a,:b,:c)
9-
A = ABC(1,2,3)
10-
A.a == 1
7+
## `@SLArray` and `@SLVector` macros
118

12-
ABCD = @SLArray (2,2) (:a,:b,:c,:d)
13-
B = ABCD(1,2,3,4)
14-
B.c == 3
15-
B[2,2] == B.d
16-
```
17-
18-
Here we have that `A == [1,2,3]` and for example `A.b == 2`. We can create a
19-
typed `SLArray` via:
20-
21-
```julia
22-
SVType = @SLVector Float64 (:a,:b,:c)
23-
```
24-
25-
Alternatively, you can also construct a static labelled array using the
26-
`SLVector` constructor by writing out the entries as keyword arguments:
9+
Macro constructors are convenient for building most `SLArray` objects. An
10+
`@SLArray` may be of arbitrary dimension while an `@SLVector` is a
11+
one dimensional array.
2712

28-
```julia
29-
julia> SLVector(a=1, b=2, c=3)
30-
3-element SLArray{Tuple{3},1,(:a, :b, :c),Int64}:
31-
1
32-
2
33-
3
13+
```@docs
14+
@SLArray
15+
@SLVector
3416
```
3517

36-
For general N-dimensional labelled arrays, you need to specify the size
37-
(`Tuple{dim1,dim2,...}`) as the type parameter to the `SLArray` constructor:
18+
## `SLArray` and `SLVector` constructors
3819

39-
```julia
40-
julia> SLArray{Tuple{2,2}}(a=1, b=2, c=3, d=4)
41-
2×2 SLArray{Tuple{2,2},2,(:a, :b, :c, :d),Int64}:
42-
1 3
43-
2 4
44-
```
45-
46-
Constructing copies with some items changed is supported by
47-
a keyword constructor whose first argument is the source and
48-
additonal keyword arguments change several entries.
49-
50-
```julia
51-
julia> v1 = SLVector(a=1.1, b=2.2, c=3.3);
52-
julia> v2 = SLVector(v1; b=20.20, c=30.30 )
53-
3-element SLArray{Tuple{3},Float64,1,3,(:a, :b, :c)}:
54-
1.1
55-
20.2
56-
30.3
57-
```
20+
Alternatively, users can construct a static labelled array using the
21+
`SLVector` and `SLArrays` constructors by writing out the entries as keyword arguments:
5822

59-
```julia
60-
julia> ABCD = @SLArray (2,2) (:a,:b,:c,:d);
61-
julia> B = ABCD(1,2,3,4);
62-
julia> B2 = SLArray(B; c=30 )
63-
2×2 SLArray{Tuple{2,2},Int64,2,4,(:a, :b, :c, :d)}:
64-
1 30
65-
2 4
23+
```@docs
24+
SLArray
25+
SLVector
6626
```
6727

68-
One can also specify the indices directly.
69-
```julia
70-
julia> EFG = @SLArray (2,2) (e=1:3, f=4, g=2:4);
71-
julia> y = EFG(1.0,2.5,3.0,5.0)
72-
2×2 SLArray{Tuple{2,2},Float64,2,4,(e = 1:3, f = 4, g = 2:4)}:
73-
1.0 3.0
74-
2.5 5.0
28+
## Manipulating `SLArrays` and `SLVectors`
7529

76-
julia> y.g
77-
3-element view(reshape(::StaticArrays.SArray{Tuple{2,2},Float64,2,4}, 4), 2:4) with eltype Float64:
78-
2.5
79-
3.0
80-
5.0
30+
Users may want a list of the labels or keys in an `SLArray` or `SLVector`.
31+
The `symbols(::SLArray)` function returns a tuple of array labels.
8132

82-
julia> Arr = @SLArray (2, 2) (a = (2, :), b = 3);
83-
julia> z = Arr(1, 2, 3, 4);
84-
julia> z.a
85-
2-element view(::StaticArrays.SArray{Tuple{2,2},Int64,2,4}, 2, :) with eltype Int64:
86-
2
87-
4
33+
```@docs
34+
symbols(::SLArray)
8835
```

docs/src/index.md

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

33
LabelledArrays.jl is a package which provides arrays with labels, i.e. they are
44
arrays which `map`, `broadcast`, and all of that good stuff, but their components
5-
are labelled. Thus for instance you can set that the second component is named
5+
are labelled. For instance, users can name the second component of an array to
66
`:second` and retrieve it with `A.second`.
77

88
## Installation

0 commit comments

Comments
 (0)