11import 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.
62
7- # Note that power measures are only well-defined for integer powers.
3+ export PowerMeasure
84
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
127
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.
1410
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+ """
1515struct PowerMeasure{M,A} <: AbstractProductMeasure
1616 parent:: M
1717 axes:: A
1818end
1919
20+ dslength (μ:: PowerMeasure ) = prod (dssize (μ))
21+ dssize (μ:: PowerMeasure ) = map (dslength, μ. axes)
22+
2023function Pretty. tile (μ:: PowerMeasure )
2124 sz = length .(μ. axes)
2225 arg1 = Pretty. tile (μ. parent)
2326 arg2 = Pretty. tile (length (sz) == 1 ? only (sz) : sz)
2427 return Pretty. pair_layout (arg1, arg2; sep = " ^ " )
2528end
2629
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+
2734function Base. rand (
2835 rng:: AbstractRNG ,
2936 :: Type{T} ,
3037 d:: PowerMeasure{M} ,
3138) where {T,M<: AbstractMeasure }
32- map (CartesianIndices (d. axes)) do _
39+ map (_cartidxs (d. axes)) do _
3340 rand (rng, T, d. parent)
3441 end
3542end
3643
3744function Base. rand (rng:: AbstractRNG , :: Type{T} , d:: PowerMeasure ) where {T}
38- map (CartesianIndices (d. axes)) do _
45+ map (_cartidxs (d. axes)) do _
3946 rand (rng, d. parent)
4047 end
4148end
4249
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+
4353@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))
4755end
4856
49- marginals (d:: PowerMeasure ) = Fill (d. parent, d. axes)
57+
58+ marginals (d:: PowerMeasure ) = fill_with (d. parent, d. axes)
5059
5160function Base.:^ (μ:: AbstractMeasure , dims:: Tuple{Vararg{<:AbstractArray,N}} ) where {N}
5261 powermeasure (μ, dims)
5362end
5463
55- Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, Base . OneTo .(dims))
64+ Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, one_to .(dims))
5665Base.:^ (μ:: AbstractMeasure , n) = powermeasure (μ, (n,))
5766
5867# Base.show(io::IO, d::PowerMeasure) = print(io, d.parent, " ^ ", size(d.xs))
7685end
7786
7887@inline function logdensity_def (
79- d:: PowerMeasure {M,Tuple{Base . OneTo{StaticInt{N} }}},
88+ d:: PowerMeasure{M,Tuple{StaticOneTo{N }}} ,
8089 x,
8190) where {M,N}
8291 parent = d. parent
8695end
8796
8897@inline function logdensity_def (
89- d:: PowerMeasure {M,NTuple{N,Base . OneTo{StaticInt{ 0 } }}},
98+ d:: PowerMeasure{M,NTuple{N,StaticOneTo{0 }}} ,
9099 x,
91100) where {M,N}
92101 static (0.0 )
110119
111120@inline getdof (μ:: PowerMeasure ) = getdof (μ. parent) * prod (map (length, μ. axes))
112121
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}
114123 static (0 )
115124end
116125
@@ -135,7 +144,7 @@ logdensity_def(::PowerMeasure{P}, x) where {P<:PrimitiveMeasure} = static(0.0)
135144
136145# To avoid ambiguities
137146function logdensity_def (
138- :: PowerMeasure {P,Tuple{Vararg{Base . OneTo{Static . StaticInt{ 0 } },N}}},
147+ :: PowerMeasure {P,Tuple{Vararg{StaticOneTo{ 0 },N}}},
139148 x,
140149) where {P<: PrimitiveMeasure ,N}
141150 static (0.0 )
0 commit comments