-
Notifications
You must be signed in to change notification settings - Fork 56
/
ProductManifold.jl
256 lines (229 loc) · 8.04 KB
/
ProductManifold.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
function active_traits(f, ::ProductManifold, args...)
return merge_traits(IsDefaultMetric(ProductMetric()))
end
function allocate_coordinates(::ProductManifold, p, T, n::Int)
return allocate(submanifold_component(p, 1), T, n)
end
"""
ProductFVectorDistribution([type::VectorSpaceFiber], [x], distrs...)
Generates a random vector at point `x` from vector space (a fiber of a tangent
bundle) of type `type` using the product distribution of given distributions.
Vector space type and `x` can be automatically inferred from distributions `distrs`.
"""
struct ProductFVectorDistribution{
TSpace<:VectorSpaceFiber{<:Any,<:ProductManifold},
TD<:(NTuple{N,Distribution} where {N}),
} <: FVectorDistribution{TSpace}
type::TSpace
distributions::TD
end
"""
ProductPointDistribution(M::ProductManifold, distributions)
Product distribution on manifold `M`, combined from `distributions`.
"""
struct ProductPointDistribution{
TM<:ProductManifold,
TD<:(NTuple{N,Distribution} where {N}),
} <: MPointDistribution{TM}
manifold::TM
distributions::TD
end
function adjoint_Jacobi_field(
M::ProductManifold,
p::ArrayPartition,
q::ArrayPartition,
t,
X::ArrayPartition,
β::Tβ,
) where {Tβ}
return ArrayPartition(
map(
adjoint_Jacobi_field,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
ntuple(_ -> t, length(M.manifolds)),
submanifold_components(M, X),
ntuple(_ -> β, length(M.manifolds)),
)...,
)
end
function adjoint_Jacobi_field!(M::ProductManifold, Y, p, q, t, X, β::Tβ) where {Tβ}
map(
adjoint_Jacobi_field!,
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, q),
ntuple(_ -> t, length(M.manifolds)),
submanifold_components(M, X),
ntuple(_ -> β, length(M.manifolds)),
)
return Y
end
@doc raw"""
flat(M::ProductManifold, p, X::FVector{TangentSpaceType})
use the musical isomorphism to transform the tangent vector `X` from the tangent space at
`p` on the [`ProductManifold`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/metamanifolds/#ManifoldsBase.ProductManifold)
`M` to a cotangent vector. This can be done elementwise for every entry of `X` (with respect
to the corresponding entry in `p`) separately.
"""
flat(::ProductManifold, ::Any...)
function jacobi_field(
M::ProductManifold,
p::ArrayPartition,
q::ArrayPartition,
t,
X::ArrayPartition,
β::Tβ,
) where {Tβ}
return ArrayPartition(
map(
jacobi_field,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
ntuple(_ -> t, length(M.manifolds)),
submanifold_components(M, X),
ntuple(_ -> β, length(M.manifolds)),
)...,
)
end
function jacobi_field!(M::ProductManifold, Y, p, q, t, X, β::Tβ) where {Tβ}
map(
jacobi_field!,
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, q),
ntuple(_ -> t, length(M.manifolds)),
submanifold_components(M, X),
ntuple(_ -> β, length(M.manifolds)),
)
return Y
end
"""
manifold_volume(M::ProductManifold)
Return the volume of [`ProductManifold`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/metamanifolds/#ManifoldsBase.ProductManifold)
`M`, i.e. product of volumes of the manifolds `M` is constructed from.
"""
manifold_volume(M::ProductManifold) = mapreduce(manifold_volume, *, M.manifolds)
function ProductFVectorDistribution(distributions::FVectorDistribution...)
M = ProductManifold(map(d -> support(d).space.manifold, distributions)...)
fiber_type = support(distributions[1]).space.fiber_type
if !all(d -> support(d).space.fiber_type == fiber_type, distributions)
error(
"Not all distributions have support in vector spaces of the same type, which is currently not supported",
)
end
# Probably worth considering sum spaces in the future?
p = ArrayPartition(map(d -> support(d).space.point, distributions)...)
return ProductFVectorDistribution(Fiber(M, p, fiber_type), distributions)
end
function ProductPointDistribution(M::ProductManifold, distributions::MPointDistribution...)
return ProductPointDistribution{typeof(M),typeof(distributions)}(M, distributions)
end
function ProductPointDistribution(distributions::MPointDistribution...)
M = ProductManifold(map(d -> support(d).manifold, distributions)...)
return ProductPointDistribution(M, distributions...)
end
function Random.rand(rng::AbstractRNG, d::ProductPointDistribution)
return ArrayPartition(map(d -> rand(rng, d), d.distributions)...)
end
function Random.rand(rng::AbstractRNG, d::ProductFVectorDistribution)
return ArrayPartition(map(d -> rand(rng, d), d.distributions)...)
end
function Distributions._rand!(
rng::AbstractRNG,
d::ProductPointDistribution,
x::AbstractArray{<:Number},
)
return copyto!(x, rand(rng, d))
end
function Distributions._rand!(
rng::AbstractRNG,
d::ProductPointDistribution,
p::ArrayPartition,
)
map(
(t1, t2) -> Distributions._rand!(rng, t1, t2),
d.distributions,
submanifold_components(d.manifold, p),
)
return p
end
function Distributions._rand!(
rng::AbstractRNG,
d::ProductFVectorDistribution,
v::AbstractArray{<:Number},
)
return copyto!(v, rand(rng, d))
end
function Distributions._rand!(
rng::AbstractRNG,
d::ProductFVectorDistribution,
X::ArrayPartition,
)
map(
t -> Distributions._rand!(rng, t[1], t[2]),
d.distributions,
submanifold_components(d.space.manifold, X),
)
return X
end
@doc raw"""
Y = riemannian_Hessian(M::ProductManifold, p, G, H, X)
riemannian_Hessian!(M::ProductManifold, Y, p, G, H, X)
Compute the Riemannian Hessian ``\operatorname{Hess} f(p)[X]`` given the
Euclidean gradient ``∇ f(\tilde p)`` in `G` and the Euclidean Hessian ``∇^2 f(\tilde p)[\tilde X]`` in `H`,
where ``\tilde p, \tilde X`` are the representations of ``p,X`` in the embedding,.
On a product manifold, this decouples and can be computed elementwise.
"""
riemannian_Hessian(M::ProductManifold, p, G, H, X)
function riemannian_Hessian!(M::ProductManifold, Y, p, G, H, X)
map(
riemannian_Hessian!,
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, G),
submanifold_components(M, H),
submanifold_components(M, X),
)
return Y
end
@doc raw"""
sharp(M::ProductManifold, p, ξ::FVector{CotangentSpaceType})
Use the musical isomorphism to transform the cotangent vector `ξ` from the tangent space at
`p` on the [`ProductManifold`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/metamanifolds/#ManifoldsBase.ProductManifold)
`M` to a tangent vector. This can be done elementwise for every entry of `ξ` (and `p`)
separately
"""
sharp(::ProductManifold, ::Any...)
Distributions.support(d::ProductPointDistribution) = MPointSupport(d.manifold)
function Distributions.support(tvd::ProductFVectorDistribution)
return FVectorSupport(tvd.type)
end
function uniform_distribution(M::ProductManifold)
return ProductPointDistribution(M, map(uniform_distribution, M.manifolds))
end
function uniform_distribution(M::ProductManifold, p)
return ProductPointDistribution(
M,
map(uniform_distribution, M.manifolds, submanifold_components(M, p)),
)
end
@doc raw"""
volume_density(M::ProductManifold, p, X)
Return volume density on the [`ProductManifold`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/metamanifolds/#ManifoldsBase.ProductManifold)
`M`, i.e. product of constituent volume densities.
"""
function volume_density(M::ProductManifold, p, X)
dens = map(
volume_density,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)
return prod(dens)
end