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.
10+
11+ Note that power measures are only well-defined for integer powers.
1412
13+ The nth power of a measure μ can be written μ^n.
14+ """
1515struct PowerMeasure{M,A} <: AbstractProductMeasure
1616 parent:: M
1717 axes:: A
1818end
1919
20+ maybestatic_length (μ:: PowerMeasure ) = prod (maybestatic_size (μ))
21+ maybestatic_size (μ:: PowerMeasure ) = map (maybestatic_length, μ. 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+ function _cartidxs (axs:: Tuple{Vararg{<:AbstractUnitRange,N}} ) where {N}
33+ CartesianIndices (map (_dynamic, axs))
34+ end
35+
2736function Base. rand (
2837 rng:: AbstractRNG ,
2938 :: Type{T} ,
3039 d:: PowerMeasure{M} ,
3140) where {T,M<: AbstractMeasure }
32- map (CartesianIndices (d. axes)) do _
41+ map (_cartidxs (d. axes)) do _
3342 rand (rng, T, d. parent)
3443 end
3544end
3645
3746function Base. rand (rng:: AbstractRNG , :: Type{T} , d:: PowerMeasure ) where {T}
38- map (CartesianIndices (d. axes)) do _
47+ map (_cartidxs (d. axes)) do _
3948 rand (rng, d. parent)
4049 end
4150end
4251
52+ @inline _pm_axes (sz:: Tuple{Vararg{<:IntegerLike,N}} ) where {N} = map (one_to, sz)
53+ @inline _pm_axes (axs:: Tuple{Vararg{<:AbstractUnitRange,N}} ) where {N} = axs
54+
4355@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)
56+ PowerMeasure (x, _pm_axes (sz))
4757end
4858
49- marginals (d:: PowerMeasure ) = Fill (d. parent, d. axes)
59+ marginals (d:: PowerMeasure ) = fill_with (d. parent, d. axes)
5060
5161function Base.:^ (μ:: AbstractMeasure , dims:: Tuple{Vararg{<:AbstractArray,N}} ) where {N}
5262 powermeasure (μ, dims)
5363end
5464
55- Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, Base . OneTo .(dims))
65+ Base.:^ (μ:: AbstractMeasure , dims:: Tuple ) = powermeasure (μ, one_to .(dims))
5666Base.:^ (μ:: AbstractMeasure , n) = powermeasure (μ, (n,))
5767
5868# Base.show(io::IO, d::PowerMeasure) = print(io, d.parent, " ^ ", size(d.xs))
7585 end
7686end
7787
78- @inline function logdensity_def (
79- d:: PowerMeasure {M,Tuple{Base. OneTo{StaticInt{N}}}},
80- x,
81- ) where {M,N}
88+ @inline function logdensity_def (d:: PowerMeasure{M,Tuple{Static.SOneTo{N}}} , x) where {M,N}
8289 parent = d. parent
8390 sum (1 : N) do j
8491 @inbounds logdensity_def (parent, x[j])
8592 end
8693end
8794
8895@inline function logdensity_def (
89- d:: PowerMeasure {M,NTuple{N,Base . OneTo{StaticInt{ 0 } }}},
96+ d:: PowerMeasure{M,NTuple{N,Static.SOneTo{0 }}} ,
9097 x,
9198) where {M,N}
9299 static (0.0 )
110117
111118@inline getdof (μ:: PowerMeasure ) = getdof (μ. parent) * prod (map (length, μ. axes))
112119
113- @inline function getdof (:: PowerMeasure {<: Any ,NTuple{N,Base . OneTo{StaticInt{ 0 } }}}) where {N}
120+ @inline function getdof (:: PowerMeasure{<:Any,NTuple{N,Static.SOneTo{0 }}} ) where {N}
114121 static (0 )
115122end
116123
@@ -135,7 +142,7 @@ logdensity_def(::PowerMeasure{P}, x) where {P<:PrimitiveMeasure} = static(0.0)
135142
136143# To avoid ambiguities
137144function logdensity_def (
138- :: PowerMeasure {P,Tuple{Vararg{Base . OneTo{ Static. StaticInt{ 0 } },N}}},
145+ :: PowerMeasure {P,Tuple{Vararg{Static. SOneTo{ 0 },N}}},
139146 x,
140147) where {P<: PrimitiveMeasure ,N}
141148 static (0.0 )
0 commit comments