|
1 | 1 | """
|
2 |
| -``` |
3 |
| -struct VarName{sym, T<:Tuple} |
4 |
| - indexing::T |
5 |
| -end |
6 |
| -``` |
| 2 | + VarName(sym[, indexing=()]) |
| 3 | +
|
| 4 | +A variable identifier for a symbol `sym` and indices `indexing` in the format |
| 5 | +returned by [`@vinds`](@ref). |
| 6 | +
|
| 7 | +The Julia variable in the model corresponding to `sym` can refer to a single value or to a |
| 8 | +hierarchical array structure of univariate, multivariate or matrix variables. The field `indexing` |
| 9 | +stores the indices requires to access the random variable from the Julia variable indicated by `sym` |
| 10 | +as a tuple of tuples. Each element of the tuple thereby contains the indices of one indexing |
| 11 | +operation. |
| 12 | +
|
| 13 | +`VarName`s can be manually constructed using the `VarName(sym, indexing)` constructor, or from an |
| 14 | +indexing expression through the [`@varname`](@ref) convenience macro. |
| 15 | +
|
| 16 | +# Examples |
7 | 17 |
|
8 |
| -A variable identifier. Every variable has a symbol `sym` and indices `indexing` in the format |
9 |
| -returned by [`@vinds`](@ref). The Julia variable in the model corresponding to `sym` can refer to a |
10 |
| -single value or to a hierarchical array structure of univariate, multivariate or matrix |
11 |
| -variables. `indexing` stores the indices that can access the random variable from the Julia |
12 |
| -variable. |
| 18 | +```jldoctest |
| 19 | +julia> vn = VarName(:x, ((Colon(), 1), (2,))) |
| 20 | +x[Colon(),1][2] |
13 | 21 |
|
14 |
| -Examples: |
| 22 | +julia> vn.indexing |
| 23 | +((Colon(), 1), (2,)) |
15 | 24 |
|
16 |
| -- `x[1] ~ Normal()` will generate a `VarName` with `sym == :x` and `indexing == "((1,))"`. |
17 |
| -- `x[:,1] ~ MvNormal(zeros(2))` will generate a `VarName` with `sym == :x` and |
18 |
| - `indexing == ((Colon(), 1))"`. |
19 |
| -- `x[:,1][2] ~ Normal()` will generate a `VarName` with `sym == :x` and |
20 |
| - `indexing == ((Colon(), 1), (2,))`. |
| 25 | +julia> VarName(DynamicPPL.@vsym(x[:, 1][1+1]), DynamicPPL.@vinds(x[:, 1][1+1])) |
| 26 | +x[Colon(),1][2] |
| 27 | +``` |
21 | 28 | """
|
22 | 29 | struct VarName{sym, T<:Tuple}
|
23 | 30 | indexing::T
|
|
26 | 33 | VarName(sym::Symbol, indexing::Tuple = ()) = VarName{sym, typeof(indexing)}(indexing)
|
27 | 34 |
|
28 | 35 | """
|
29 |
| - VarName(vn::VarName, indexing) |
| 36 | + VarName(vn::VarName[, indexing=()]) |
30 | 37 |
|
31 | 38 | Return a copy of `vn` with a new index `indexing`.
|
32 | 39 | """
|
@@ -131,8 +138,29 @@ _issubrange(i::Colon, j::ConcreteIndex) = true
|
131 | 138 | """
|
132 | 139 | @varname(expr)
|
133 | 140 |
|
134 |
| -A macro that returns an instance of `VarName` given the symbol or expression of a Julia variable, |
135 |
| -e.g. `@varname x[1,2][1+5][45][3]` returns `VarName{:x}(((1, 2), (6,), (45,), (3,)))`. |
| 141 | +A macro that returns an instance of [`VarName`](@ref) given a symbol or indexing expression `expr`. |
| 142 | +
|
| 143 | +The `sym` value is taken from the actual variable name, and the index values are put appropriately |
| 144 | +into the constructor (and resolved at runtime). |
| 145 | +
|
| 146 | +# Examples |
| 147 | +
|
| 148 | +```jldoctest |
| 149 | +julia> @varname(x).indexing |
| 150 | +() |
| 151 | +
|
| 152 | +julia> @varname(x[1]).indexing |
| 153 | +((1,),) |
| 154 | +
|
| 155 | +julia> @varname(x[:, 1]).indexing |
| 156 | +((Colon(), 1),) |
| 157 | +
|
| 158 | +julia> @varname(x[:, 1][2]).indexing |
| 159 | +((Colon(), 1), (2,)) |
| 160 | +
|
| 161 | +julia> @varname(x[1,2][1+5][45][3]).indexing |
| 162 | +((1, 2), (6,), (45,), (3,)) |
| 163 | +``` |
136 | 164 |
|
137 | 165 | !!! compat "Julia 1.5"
|
138 | 166 | Using `begin` in an indexing expression to refer to the first index requires at least
|
|
0 commit comments