Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
style = "sciml"
style = "sciml"
format_markdown = true
37 changes: 15 additions & 22 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
The LabelledArrays.jl package is licensed under the MIT "Expat" License:

> Copyright (c) 2017: Christopher Rackauckas.
>
>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
>
>
> of this software and associated documentation files (the "Software"), to deal
>
>
> in the Software without restriction, including without limitation the rights
>
>
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
>
>
> copies of the Software, and to permit persons to whom the Software is
>
>
> furnished to do so, subject to the following conditions:
>
>
>
>
> The above copyright notice and this permission notice shall be included in all
>
>
> copies or substantial portions of the Software.
>
>
>
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
>
>
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
>
>
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
>
>
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
>
>
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
>
>
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
>
>
> SOFTWARE.
>
>
133 changes: 71 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![codecov](https://codecov.io/gh/SciML/LabelledArrays.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/SciML/LabelledArrays.jl)
[![Build Status](https://github.com/SciML/LabelledArrays.jl/workflows/CI/badge.svg)](https://github.com/SciML/LabelledArrays.jl/actions?query=workflow%3ACI)

[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor%27s%20Guide-blueviolet)](https://github.com/SciML/ColPrac)
[![SciML Code Style](https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826)](https://github.com/SciML/SciMLStyle)

# About
Expand All @@ -19,10 +19,12 @@ are labelled. Thus for instance you can set that the second component is named
## How to install

The package can be installed from this repository by

```julia
> using Pkg
> Pkg.add("LabelledArrays")
using Pkg
Pkg.add("LabelledArrays")
```

## Tutorials and Documentation

For information on using the package,
Expand All @@ -37,28 +39,28 @@ First you create the type and then you can use that constructor to generate
instances of the labelled array.

```julia
ABC = @SLVector (:a,:b,:c)
A = ABC(1,2,3)
ABC = @SLVector (:a, :b, :c)
A = ABC(1, 2, 3)
A.a == 1

ABCD = @SLArray (2,2) (:a,:b,:c,:d)
B = ABCD(1,2,3,4)
ABCD = @SLArray (2, 2) (:a, :b, :c, :d)
B = ABCD(1, 2, 3, 4)
B.c == 3
B[2,2] == B.d
B[2, 2] == B.d
```

Here we have that `A == [1,2,3]` and for example `A.b == 2`. We can create a
typed `SLArray` via:

```julia
SVType = @SLVector Float64 (:a,:b,:c)
SVType = @SLVector Float64 (:a, :b, :c)
```

Alternatively, you can also construct a static labelled array using the
`SLVector` constructor by writing out the entries as keyword arguments:

```julia
julia> SLVector(a=1, b=2, c=3)
julia> SLVector(a = 1, b = 2, c = 3)
3-element SLArray{Tuple{3},1,(:a, :b, :c),Int64}:
1
2
Expand All @@ -69,7 +71,7 @@ For general N-dimensional labelled arrays, you need to specify the size
(`Tuple{dim1,dim2,...}`) as the type parameter to the `SLArray` constructor:

```julia
julia> SLArray{Tuple{2,2}}(a=1, b=2, c=3, d=4)
julia> SLArray{Tuple{2, 2}}(a = 1, b = 2, c = 3, d = 4)
2×2 SLArray{Tuple{2,2},2,(:a, :b, :c, :d),Int64}:
1 3
2 4
Expand All @@ -80,27 +82,32 @@ a keyword constructor whose first argument is the source and
additonal keyword arguments change several entries.

```julia
julia> v1 = SLVector(a=1.1, b=2.2, c=3.3);
julia> v2 = SLVector(v1; b=20.20, c=30.30 )
julia> v1 = SLVector(a = 1.1, b = 2.2, c = 3.3);

julia> v2 = SLVector(v1; b = 20.20, c = 30.30)
3-element SLArray{Tuple{3},Float64,1,3,(:a, :b, :c)}:
1.1
20.2
30.3
```

```julia
julia> ABCD = @SLArray (2,2) (:a,:b,:c,:d);
julia> B = ABCD(1,2,3,4);
julia> B2 = SLArray(B; c=30 )
julia> ABCD = @SLArray (2, 2) (:a, :b, :c, :d);

julia> B = ABCD(1, 2, 3, 4);

julia> B2 = SLArray(B; c = 30)
2×2 SLArray{Tuple{2,2},Int64,2,4,(:a, :b, :c, :d)}:
1 30
2 4
```

One can also specify the indices directly.

```julia
julia> EFG = @SLArray (2,2) (e=1:3, f=4, g=2:4);
julia> y = EFG(1.0,2.5,3.0,5.0)
julia> EFG = @SLArray (2, 2) (e = 1:3, f = 4, g = 2:4);

julia> y = EFG(1.0, 2.5, 3.0, 5.0)
2×2 SLArray{Tuple{2,2},Float64,2,4,(e = 1:3, f = 4, g = 2:4)}:
1.0 3.0
2.5 5.0
Expand All @@ -112,7 +119,9 @@ julia> y.g
5.0

julia> Arr = @SLArray (2, 2) (a = (2, :), b = 3);

julia> z = Arr(1, 2, 3, 4);

julia> z.a
2-element view(::StaticArrays.SArray{Tuple{2,2},Int64,2,4}, 2, :) with eltype Int64:
2
Expand All @@ -126,63 +135,64 @@ loss by using the labelled instead of indexing. Using the macro with values
and labels generates the labelled array with the given values:

```julia
A = @LArray [1,2,3] (:a,:b,:c)
A = @LArray [1, 2, 3] (:a, :b, :c)
A.a == 1
```

One can generate a labelled array with undefined values by instead giving
the dimensions:

```julia
A = @LArray Float64 (2,2) (:a,:b,:c,:d)
W = rand(2,2)
A = @LArray Float64 (2, 2) (:a, :b, :c, :d)
W = rand(2, 2)
A .= W
A.d == W[2,2]
A.d == W[2, 2]
```

or using an `@LVector` shorthand:

```julia
A = @LVector Float64 (:a,:b,:c,:d)
A = @LVector Float64 (:a, :b, :c, :d)
A .= rand(4)
```

As with `SLArray`, alternative constructors exist that use the keyword argument
form:

```julia
julia> LVector(a=1, b=2, c=3)
julia> LVector(a = 1, b = 2, c = 3)
3-element LArray{Int64,1,(:a, :b, :c)}:
1
2
3

julia> LArray((2,2); a=1, b=2, c=3, d=4) # need to specify size as first argument
julia> LArray((2, 2); a = 1, b = 2, c = 3, d = 4) # need to specify size as first argument
2×2 LArray{Int64,2,(:a, :b, :c, :d)}:
1 3
2 4
```

One can also specify the indices directly.

```julia
julia> z = @LArray [1.,2.,3.] (a = 1:2, b = 2:3);
julia> z = @LArray [1.0, 2.0, 3.0] (a = 1:2, b = 2:3);

julia> z.b
2-element view(::Array{Float64,1}, 2:3) with eltype Float64:
2.0
3.0

julia> z = @LArray [1 2; 3 4] (a = (2, :), b = 2:3);

julia> z.a
2-element view(::Array{Int64,2}, 2, :) with eltype Int64:
3
4
```

The labels of LArray and SLArray can be accessed
The labels of LArray and SLArray can be accessed
by function `symbols`, which returns a tuple of symbols.




## Example: Nice DiffEq Syntax Without A DSL

LabelledArrays.jl are a way to get DSL-like syntax without a macro. In this case,
Expand All @@ -194,69 +204,69 @@ Let's solve the Lorenz equation. Using `@LVector`s, we can do:
```julia
using LabelledArrays, OrdinaryDiffEq

function lorenz_f(du,u,p,t)
du.x = p.σ*(u.y-u.x)
du.y = u.x*(p.ρ-u.z) - u.y
du.z = u.x*u.y - p.β*u.z
function lorenz_f(du, u, p, t)
du.x = p.σ * (u.y - u.x)
du.y = u.x * (p.ρ - u.z) - u.y
du.z = u.x * u.y - p.β * u.z
end

u0 = @LArray [1.0,0.0,0.0] (:x,:y,:z)
p = @LArray [10.0, 28.0, 8/3] (:σ,:ρ,:β)
tspan = (0.0,10.0)
prob = ODEProblem(lorenz_f,u0,tspan,p)
sol = solve(prob,Tsit5())
u0 = @LArray [1.0, 0.0, 0.0] (:x, :y, :z)
p = @LArray [10.0, 28.0, 8 / 3] (:σ, :ρ, :β)
tspan = (0.0, 10.0)
prob = ODEProblem(lorenz_f, u0, tspan, p)
sol = solve(prob, Tsit5())
# Now the solution can be indexed as .x/y/z as well!
sol[10].x
```

We can also make use of `@SLVector`:

```julia
LorenzVector = @SLVector (:x,:y,:z)
LorenzParameterVector = @SLVector (:σ,:ρ,:β)

function f(u,p,t)
x = p.σ*(u.y-u.x)
y = u.x*(p.ρ-u.z) - u.y
z = u.x*u.y - p.β*u.z
LorenzVector(x,y,z)
LorenzVector = @SLVector (:x, :y, :z)
LorenzParameterVector = @SLVector (:σ, :ρ, :β)

function f(u, p, t)
x = p.σ * (u.y - u.x)
y = u.x * (p.ρ - u.z) - u.y
z = u.x * u.y - p.β * u.z
LorenzVector(x, y, z)
end

u0 = LorenzVector(1.0,0.0,0.0)
p = LorenzParameterVector(10.0,28.0,8/3)
tspan = (0.0,10.0)
prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob,Tsit5())
u0 = LorenzVector(1.0, 0.0, 0.0)
p = LorenzParameterVector(10.0, 28.0, 8 / 3)
tspan = (0.0, 10.0)
prob = ODEProblem(f, u0, tspan, p)
sol = solve(prob, Tsit5())
```

## Relation to NamedTuples

Julia's Base has NamedTuples in v0.7+. They are constructed as:

```julia
p = (σ = 10.0,ρ = 28.0,β = 8/3)
p = (σ = 10.0, ρ = 28.0, β = 8 / 3)
```

and they support `p[1]` and `p.σ` as well. The `LVector`, `SLVector`, `LArray`
and `SLArray` constructors also support named tuples as their arguments:

```julia
julia> LVector((a=1, b=2))
julia> LVector((a = 1, b = 2))
2-element LArray{Int64,1,(:a, :b)}:
1
2

julia> SLVector((a=1, b=2))
julia> SLVector((a = 1, b = 2))
2-element SLArray{Tuple{2},1,(:a, :b),Int64}:
1
2

julia> LArray((2,2), (a=1, b=2, c=3, d=4))
julia> LArray((2, 2), (a = 1, b = 2, c = 3, d = 4))
2×2 LArray{Int64,2,(:a, :b, :c, :d)}:
1 3
2 4

julia> SLArray{Tuple{2,2}}((a=1, b=2, c=3, d=4))
julia> SLArray{Tuple{2, 2}}((a = 1, b = 2, c = 3, d = 4))
2×2 SLArray{Tuple{2,2},2,(:a, :b, :c, :d),Int64}:
1 3
2 4
Expand All @@ -269,18 +279,17 @@ creates an iterator that is functionally the same as
for each label of the array.

There are some crucial differences between a labelled array and
a named tuple. Labelled arrays can have any dimensions while
a named tuple. Labelled arrays can have any dimensions while
named tuples are always 1D. A named tuple can have different types
on each element, while an `SLArray` can only have one element
type and furthermore it has the actions of a static vector.
As a result `SLArray` has less element type information, which
As a result `SLArray` has less element type information, which
improves compilation speed while giving more vector functionality
than a NamedTuple. `LArray` also only has a single element type and,
unlike a named tuple, is mutable.


## Note: Labelled slices

This functionality has been removed from LabelledArrays.jl, but can
replicated with the same compile-time performance and indexing syntax
This functionality has been removed from LabelledArrays.jl, but can
replicated with the same compile-time performance and indexing syntax
using [DimensionalData.jl](https://rafaqz.github.io/DimensionalData.jl/stable/).
10 changes: 9 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ include("pages.jl")
makedocs(sitename = "LabelledArrays.jl",
authors = "Chris Rackauckas",
modules = [LabelledArrays],
clean = true, doctest = false,
clean = true, doctest = false, linkcheck = true,
strict = [
:doctest,
:linkcheck,
:parse_error,
:example_block,
# Other available options are
# :autodocs_block, :cross_references, :docs_block, :eval_block, :example_block, :footnote, :meta_block, :missing_docs, :setup_block
],
format = Documenter.HTML(analytics = "UA-90474609-3",
assets = ["assets/favicon.ico"],
canonical = "https://docs.sciml.ai/LabelledArrays/stable/"),
Expand Down
Loading