|
1 | 1 | export HierarchicalMeasure
|
2 | 2 |
|
| 3 | +""" |
| 4 | + struct HierarchicalMeasure{F,M<:AbstractMeasure,G} <: AbstractMeasure |
3 | 5 |
|
4 |
| -# TODO: Document and use FlattenMode |
5 |
| -abstract type FlattenMode end |
6 |
| -struct NoFlatten <: FlattenMode end |
7 |
| -struct AutoFlatten <: FlattenMode end |
| 6 | +Represents a hierarchical measure. |
8 | 7 |
|
9 |
| - |
10 |
| -struct HierarchicalMeasure{F,M<:AbstractMeasure,FM<:FlattenMode} <: AbstractMeasure |
| 8 | +User code should not instantiate `HierarchicalMeasure` directly, use |
| 9 | +[`hierarchical_measure`](@ref) instead. |
| 10 | +""" |
| 11 | +struct HierarchicalMeasure{F,M<:AbstractMeasure,G} <: AbstractMeasure |
11 | 12 | f::F
|
12 | 13 | m::M
|
13 |
| - flatten_mode::FM |
| 14 | + flatten::G |
14 | 15 | end
|
15 | 16 |
|
16 |
| -# TODO: Document |
17 |
| -const HierarchicalProductMeasure{F,M<:AbstractMeasure} = HierarchicalMeasure{F,M,NoFlatten} |
18 |
| -export HierarchicalProductMeasure |
19 |
| - |
20 |
| -HierarchicalProductMeasure(f, m::AbstractMeasure) = HierarchicalMeasure(f, m, NoFlatten()) |
| 17 | +""" |
| 18 | + hierarchical_measure(f, m::AbstractMeasure, flatten) |
21 | 19 |
|
22 |
| -# TODO: Document |
23 |
| -const FlatHierarchicalMeasure{F,M<:AbstractMeasure} = HierarchicalMeasure{F,M,AutoFlatten} |
24 |
| -export FlatHierarchicalMeasure |
| 20 | +Construct a hierarchical measure from a function `f`, measure `m` and |
| 21 | +""" |
| 22 | +@inline function hierarchical_measure(f, m::AbstractMeasure, flatten) |
| 23 | + F, M, G = Core.Typeof(f), Core.Typeof(m), Core.Typeof(flatten) |
| 24 | + HierarchicalProductMeasure{F,M,G}(f, m, flatten) |
| 25 | +end |
25 | 26 |
|
26 |
| -FlatHierarchicalMeasure(f, m::AbstractMeasure) = HierarchicalMeasure(f, m, AutoFlatten()) |
27 | 27 |
|
28 |
| -HierarchicalMeasure(f, m::AbstractMeasure) = FlatHierarchicalMeasure(f, m) |
| 28 | +#!!!!!! |
| 29 | +const HierarchicalProductMeasure{F,M<:AbstractMeasure} = HierarchicalMeasure{F,M,::typeof(=>)} |
| 30 | +const FlatHierarchicalMeasure{F,M<:AbstractMeasure} = HierarchicalMeasure{F,M,::typeof(vcat)} |
29 | 31 |
|
30 | 32 |
|
31 | 33 |
|
32 |
| -function _split_variate_after(::NoFlatten, μ::AbstractMeasure, x::Tuple{2}) |
33 |
| - @assert x isa Tuple{2} |
34 |
| - return x[1], x[2] |
| 34 | +function _split_variate(::typeof(=>), ::AbstractMeasure, x::Pair) |
| 35 | + return x.first, x.second |
35 | 36 | end
|
36 | 37 |
|
| 38 | +function _split_variate(flatten::F, μ_primary::AbstractMeasure, x) where F |
| 39 | + test_primary = testvalue(μ_primary) |
| 40 | + return _split_variate_byvalue(flatten, test_primary, x) |
| 41 | +end |
37 | 42 |
|
38 |
| -function _split_variate_after(::AutoFlatten, μ::AbstractMeasure, x) |
39 |
| - a_test = testvalue(μ) |
40 |
| - return _autosplit_variate_after_testvalue(a_test, x) |
| 43 | +function _split_variate(::Type{F}, μ::AbstractMeasure, x) where F |
| 44 | + test_primary = testvalue(μ) |
| 45 | + return _split_variate_byvalue(F, test_primary, x) |
41 | 46 | end
|
42 | 47 |
|
43 |
| -function _autosplit_variate_after_testvalue(::Any, x) |
| 48 | + |
| 49 | +function _split_variate_byvalue(::Any, x) |
44 | 50 | @assert x isa Tuple{2}
|
45 | 51 | return x[1], x[2]
|
46 | 52 | end
|
47 | 53 |
|
48 |
| -function _autosplit_variate_after_testvalue(a_test::AbstractVector, x::AbstractVector) |
49 |
| - n, m = length(eachindex(a_test)), length(eachindex(x)) |
| 54 | +function _split_variate_byvalue(test_primary::AbstractVector, x::AbstractVector) |
| 55 | + n, m = length(eachindex(test_primary)), length(eachindex(x)) |
50 | 56 | # TODO: Use getindex or view?
|
51 | 57 | return x[begin:n], x[begin+n:m]
|
52 | 58 | end
|
53 | 59 |
|
54 |
| -function _autosplit_variate_after_testvalue(::Tuple{N}, x::Tuple{M}) where {N,M} |
| 60 | +function _split_variate_byvalue(::Tuple{N}, x::Tuple{M}) where {N,M} |
55 | 61 | return ntuple(i -> x[i], Val(1:N)), ntuple(i -> x[i], Val(N+1:M))
|
56 | 62 | end
|
57 | 63 |
|
58 |
| -@generated function _autosplit_variate_after_testvalue(::NamedTuple{names_a}, x::NamedTuple{names}) where {names_a,names} |
| 64 | +@generated function _split_variate_byvalue(::NamedTuple{names_a}, x::NamedTuple{names}) where {names_a,names} |
59 | 65 | # TODO: implement
|
60 | 66 | @assert false
|
61 | 67 | end
|
|
147 | 153 |
|
148 | 154 | function _to_std_with_rest(flatten_mode::FlattenMode, ν_inner::StdMeasure, μ::AbstractMeasure, x)
|
149 | 155 | dof_μ = getdof(μ)
|
150 |
| - x_μ, x_rest = _split_variate_after(flatten_mode, μ, x) |
| 156 | + x_μ, x_rest = _split_variate(flatten_mode, μ, x) |
151 | 157 | y = transport_to(ν_inner^dof_μ, μ, x_μ)
|
152 | 158 | return y, x_rest
|
153 | 159 | end
|
|
0 commit comments