Skip to content

Commit a286861

Browse files
Merge pull request #118 from 00krishna-opensource/add_docs_labelledarrays
Add Documenter docs for LabelledArrays
2 parents 032452e + 9afeb58 commit a286861

File tree

12 files changed

+335
-0
lines changed

12 files changed

+335
-0
lines changed

.github/workflows/Documentation.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Documentation
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- 'release-'
8+
tags: '*'
9+
pull_request:
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: julia-actions/setup-julia@latest
17+
with:
18+
version: '1'
19+
- name: Install dependencies
20+
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
21+
- name: Build and deploy
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # For authentication with GitHub Actions token
24+
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # For authentication with SSH deploy key
25+
run: julia --project=docs/ docs/make.jl

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.jl.*.cov
33
*.jl.mem
44
Manifest.toml
5+
.vscode/

docs/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build/
2+
site/

docs/Project.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[deps]
2+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
LabelledArrays = "2ee39098-c373-598a-b85f-a56591580800"
4+
5+
[compat]
6+
Documenter = "0.27"

docs/make.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Documenter, LabelledArrays
2+
3+
include("pages.jl")
4+
5+
makedocs(
6+
sitename="LabelledArrays.jl",
7+
authors="Chris Rackauckas",
8+
modules=[LabelledArrays],
9+
clean=true,doctest=false,
10+
format = Documenter.HTML(analytics = "UA-90474609-3",
11+
assets = ["assets/favicon.ico"],
12+
canonical="https://labelledarrays.sciml.ai/stable/"),
13+
pages=pages
14+
)
15+
16+
deploydocs(
17+
repo = "github.com/SciML/LabelledArrays.jl.git";
18+
push_preview = true
19+
)

docs/pages.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Put in a separate page so it can be used by SciMLDocs.jl
2+
3+
pages=[
4+
"Home" => "index.md",
5+
"SLArrays" => "SLArrays.md",
6+
"LArrays" => "LArrays.md",
7+
"Example: Nice DiffEq Syntax Without A DSL" => "Example_dsl.md",
8+
"Relation to NamedTuples" => "NamedTuples_relation.md",
9+
"Note_labelled_slices.md"
10+
]

docs/src/Example_dsl.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Example: Nice DiffEq Syntax Without A DSL
2+
3+
LabelledArrays.jl are a way to get DSL-like syntax without a macro. In this case,
4+
we can solve differential equations with labelled components by making use of
5+
labelled arrays, and always refer to the components by name instead of index.
6+
7+
Let's solve the Lorenz equation. Using `@LVector`s, we can do:
8+
9+
```julia
10+
using LabelledArrays, OrdinaryDiffEq
11+
12+
function lorenz_f(du,u,p,t)
13+
du.x = p.σ*(u.y-u.x)
14+
du.y = u.x*(p.ρ-u.z) - u.y
15+
du.z = u.x*u.y - p.β*u.z
16+
end
17+
18+
u0 = @LArray [1.0,0.0,0.0] (:x,:y,:z)
19+
p = @LArray [10.0, 28.0, 8/3] (,,)
20+
tspan = (0.0,10.0)
21+
prob = ODEProblem(lorenz_f,u0,tspan,p)
22+
sol = solve(prob,Tsit5())
23+
# Now the solution can be indexed as .x/y/z as well!
24+
sol[10].x
25+
```
26+
27+
We can also make use of `@SLVector`:
28+
29+
```julia
30+
LorenzVector = @SLVector (:x,:y,:z)
31+
LorenzParameterVector = @SLVector (,,)
32+
33+
function f(u,p,t)
34+
x = p.σ*(u.y-u.x)
35+
y = u.x*(p.ρ-u.z) - u.y
36+
z = u.x*u.y - p.β*u.z
37+
LorenzVector(x,y,z)
38+
end
39+
40+
u0 = LorenzVector(1.0,0.0,0.0)
41+
p = LorenzParameterVector(10.0,28.0,8/3)
42+
tspan = (0.0,10.0)
43+
prob = ODEProblem(f,u0,tspan,p)
44+
sol = solve(prob,Tsit5())
45+
```

docs/src/LArrays.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# LArrays
2+
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:
6+
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:
14+
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+
```
21+
22+
or using an `@LVector` shorthand:
23+
24+
```julia
25+
A = @LVector Float64 (:a,:b,:c,:d)
26+
A .= rand(4)
27+
```
28+
29+
As with `SLArray`, alternative constructors exist that use the keyword argument
30+
form:
31+
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
38+
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
43+
```
44+
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+
```
58+
59+
The labels of LArray and SLArray can be accessed
60+
by function `symbols`, which returns a tuple of symbols.

docs/src/NamedTuples_relation.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Relation to NamedTuples
2+
3+
Julia's Base has NamedTuples in v0.7+. They are constructed as:
4+
5+
```julia
6+
p == 10.0= 28.0= 8/3)
7+
```
8+
9+
and they support `p[1]` and `p.σ` as well. The `LVector`, `SLVector`, `LArray`
10+
and `SLArray` constructors also support named tuples as their arguments:
11+
12+
```julia
13+
julia> LVector((a=1, b=2))
14+
2-element LArray{Int64,1,(:a, :b)}:
15+
1
16+
2
17+
18+
julia> SLVector((a=1, b=2))
19+
2-element SLArray{Tuple{2},1,(:a, :b),Int64}:
20+
1
21+
2
22+
23+
julia> LArray((2,2), (a=1, b=2, c=3, d=4))
24+
2×2 LArray{Int64,2,(:a, :b, :c, :d)}:
25+
1 3
26+
2 4
27+
28+
julia> SLArray{Tuple{2,2}}((a=1, b=2, c=3, d=4))
29+
2×2 SLArray{Tuple{2,2},2,(:a, :b, :c, :d),Int64}:
30+
1 3
31+
2 4
32+
```
33+
34+
Converting to a named tuple from a labelled array x is available
35+
using `convert(NamedTuple, x)`. Furthermore, `pairs(x)`
36+
creates an iterator that is functionally the same as
37+
`pairs(convert(NamedTuple, x))`, yielding `:label => x.label`
38+
for each label of the array.
39+
40+
There are some crucial differences between a labelled array and
41+
a named tuple. Labelled arrays can have any dimensions while
42+
named tuples are always 1D. A named tuple can have different types
43+
on each element, while an `SLArray` can only have one element
44+
type and furthermore it has the actions of a static vector.
45+
As a result `SLArray` has less element type information, which
46+
improves compilation speed while giving more vector functionality
47+
than a NamedTuple. `LArray` also only has a single element type and,
48+
unlike a named tuple, is mutable.

docs/src/Note_labelled_slices.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Note: Labelled slices
2+
3+
This functionality has been removed from LabelledArrays.jl, but can
4+
replicated with the same compile-time performance and indexing syntax
5+
using [DimensionalData.jl](https://github.com/rafaqz/DimensionalData.jl).

docs/src/SLArrays.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SLArrays
2+
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
5+
instances of the labelled array.
6+
7+
```julia
8+
ABC = @SLVector (:a,:b,:c)
9+
A = ABC(1,2,3)
10+
A.a == 1
11+
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:
27+
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
34+
```
35+
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:
38+
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+
```
58+
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
66+
```
67+
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
75+
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
81+
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
88+
```

docs/src/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# LabelledArrays.jl: Arrays with Label Goodness
2+
3+
LabelledArrays.jl is a package which provides arrays with labels, i.e. they are
4+
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
6+
`:second` and retrieve it with `A.second`.
7+
8+
## Installation
9+
10+
To install LabelledArrays.jl, use the Julia package manager:
11+
12+
```julia
13+
using Pkg
14+
Pkg.add("LabelledArrays")
15+
```
16+
17+
## Contributing
18+
19+
- Please refer to the
20+
[SciML ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://github.com/SciML/ColPrac/blob/master/README.md)
21+
for guidance on PRs, issues, and other matters relating to contributing to SciML.
22+
- There are a few community forums:
23+
- the #diffeq-bridged channel in the [Julia Slack](https://julialang.org/slack/)
24+
- [JuliaDiffEq](https://gitter.im/JuliaDiffEq/Lobby) on Gitter
25+
- on the [Julia Discourse forums](https://discourse.julialang.org)
26+
- see also [SciML Community page](https://sciml.ai/community/)

0 commit comments

Comments
 (0)