1
1
import Base
2
- using FillArrays: Fill
3
- # """
4
- # A power measure is a product of a measure with itself. The number of elements in
5
- # the product determines the dimensionality of the resulting support.
6
2
7
- # Note that power measures are only well-defined for integer powers.
3
+ export PowerMeasure
8
4
9
- # The nth power of a measure μ can be written μ^x.
10
- # """
11
- # PowerMeasure{M,N,D} = ProductMeasure{Fill{M,N,D}}
5
+ """
6
+ struct PowerMeasure{M,...} <: AbstractProductMeasure
12
7
13
- export PowerMeasure
8
+ A power measure is a product of a measure with itself. The number of elements in
9
+ the product determines the dimensionality of the resulting support.
14
10
11
+ Note that power measures are only well-defined for integer powers.
12
+
13
+ The nth power of a measure μ can be written μ^x.
14
+ """
15
15
struct PowerMeasure{M,A} <: AbstractProductMeasure
16
16
parent:: M
17
17
axes:: A
18
18
end
19
19
20
+ dslength (μ:: PowerMeasure ) = prod (dssize (μ))
21
+ dssize (μ:: PowerMeasure ) = map (dslength, μ. axes)
22
+
20
23
function Pretty. tile (μ:: PowerMeasure )
21
24
sz = length .(μ. axes)
22
25
arg1 = Pretty. tile (μ. parent)
23
26
arg2 = Pretty. tile (length (sz) == 1 ? only (sz) : sz)
24
27
return Pretty. pair_layout (arg1, arg2; sep = " ^ " )
25
28
end
26
29
30
+ # ToDo: Make rand return static arrays for statically-sized power measures.
31
+
32
+ _cartidxs (axs:: Tuple{Vararg{<:AbstractUnitRange,N}} ) where {N} = CartesianIndices (map (_dynamic, axs))
33
+
27
34
function Base. rand (
28
35
rng:: AbstractRNG ,
29
36
:: Type{T} ,
30
37
d:: PowerMeasure{M} ,
31
38
) where {T,M<: AbstractMeasure }
32
- map (CartesianIndices (d. axes)) do _
39
+ map (_cartidxs (d. axes)) do _
33
40
rand (rng, T, d. parent)
34
41
end
35
42
end
36
43
37
44
function Base. rand (rng:: AbstractRNG , :: Type{T} , d:: PowerMeasure ) where {T}
38
- map (CartesianIndices (d. axes)) do _
45
+ map (_cartidxs (d. axes)) do _
39
46
rand (rng, d. parent)
40
47
end
41
48
end
42
49
50
+ @inline _pm_axes (sz:: Tuple{Vararg{<:IntegerLike,N}} ) where N = map (one_to, sz)
51
+ @inline _pm_axes (axs:: Tuple{Vararg{<:AbstractUnitRange,N}} ) where N = axs
52
+
43
53
@inline function powermeasure (x:: T , sz:: Tuple{Vararg{<:Any,N}} ) where {T,N}
44
- a = axes (Fill {T,N} (x, sz))
45
- A = typeof (a)
46
- PowerMeasure {T,A} (x, a)
54
+ PowerMeasure (x, _pm_axes (sz))
47
55
end
48
56
49
- marginals (d:: PowerMeasure ) = Fill (d. parent, d. axes)
57
+
58
+ marginals (d:: PowerMeasure ) = fill_with (d. parent, d. axes)
50
59
51
60
function Base.:^ (μ:: AbstractMeasure , dims:: Tuple{Vararg{<:AbstractArray,N}} ) where {N}
52
61
powermeasure (μ, dims)
53
62
end
54
63
55
- Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, Base . OneTo .(dims))
64
+ Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, one_to .(dims))
56
65
Base.:^ (μ:: AbstractMeasure , n) = powermeasure (μ, (n,))
57
66
58
67
# Base.show(io::IO, d::PowerMeasure) = print(io, d.parent, " ^ ", size(d.xs))
76
85
end
77
86
78
87
@inline function logdensity_def (
79
- d:: PowerMeasure {M,Tuple{Base . OneTo{StaticInt{N} }}},
88
+ d:: PowerMeasure{M,Tuple{StaticOneTo{N }}} ,
80
89
x,
81
90
) where {M,N}
82
91
parent = d. parent
86
95
end
87
96
88
97
@inline function logdensity_def (
89
- d:: PowerMeasure {M,NTuple{N,Base . OneTo{StaticInt{ 0 } }}},
98
+ d:: PowerMeasure{M,NTuple{N,StaticOneTo{0 }}} ,
90
99
x,
91
100
) where {M,N}
92
101
static (0.0 )
110
119
111
120
@inline getdof (μ:: PowerMeasure ) = getdof (μ. parent) * prod (map (length, μ. axes))
112
121
113
- @inline function getdof (:: PowerMeasure {<: Any ,NTuple{N,Base . OneTo{StaticInt{ 0 } }}}) where {N}
122
+ @inline function getdof (:: PowerMeasure{<:Any,NTuple{N,StaticOneTo{0 }}} ) where {N}
114
123
static (0 )
115
124
end
116
125
@@ -135,7 +144,7 @@ logdensity_def(::PowerMeasure{P}, x) where {P<:PrimitiveMeasure} = static(0.0)
135
144
136
145
# To avoid ambiguities
137
146
function logdensity_def (
138
- :: PowerMeasure {P,Tuple{Vararg{Base . OneTo{Static . StaticInt{ 0 } },N}}},
147
+ :: PowerMeasure {P,Tuple{Vararg{StaticOneTo{ 0 },N}}},
139
148
x,
140
149
) where {P<: PrimitiveMeasure ,N}
141
150
static (0.0 )
0 commit comments