-
Notifications
You must be signed in to change notification settings - Fork 56
/
ConnectionManifold.jl
401 lines (353 loc) · 12.3 KB
/
ConnectionManifold.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
@doc raw"""
AbstractAffineConnection
Abstract type for affine connections on a manifold.
"""
abstract type AbstractAffineConnection end
"""
LeviCivitaConnection
The [Levi-Civita connection](https://en.wikipedia.org/wiki/Levi-Civita_connection) of a Riemannian manifold.
"""
struct LeviCivitaConnection <: AbstractAffineConnection end
struct MetricDecoratorType <: AbstractDecoratorType end
"""
AbstractConnectionManifold{𝔽,M<:AbstractManifold{𝔽},G<:AbstractAffineConnection} <: AbstractDecoratorManifold{𝔽}
Equip an [`AbstractManifold`](@ref) explicitly with an [`AbstractAffineConnection`](@ref) `G`.
`AbstractConnectionManifold` is defined by values of [`christoffel_symbols_second`](@ref),
which is used for a default implementation of [`exp`](@ref), [`log`](@ref) and
[`vector_transport_to`](@ref). Closed-form formulae for particular connection manifolds may
be explicitly implemented when available.
An overview of basic properties of affine connection manifolds can be found in [^Pennec2020].
[^Pennec2020]:
> X. Pennec and M. Lorenzi, “5 - Beyond Riemannian geometry: The affine connection
> setting for transformation groups,” in Riemannian Geometric Statistics in Medical Image
> Analysis, X. Pennec, S. Sommer, and T. Fletcher, Eds. Academic Press, 2020, pp. 169–229.
> doi: 10.1016/B978-0-12-814725-2.00012-1.
"""
abstract type AbstractConnectionManifold{𝔽} <:
AbstractDecoratorManifold{𝔽,MetricDecoratorType} end
"""
connection(M::AbstractManifold)
Get the connection (an object of a subtype of [`AbstractAffineConnection`](@ref))
of [`AbstractManifold`](@ref) `M`.
"""
connection(::AbstractManifold)
"""
ConnectionManifold(M, C)
Decorate the [`AbstractManifold`](@ref) `M` with [`AbstractAffineConnection`](@ref) `C`.
"""
struct ConnectionManifold{𝔽,M<:AbstractManifold{𝔽},C<:AbstractAffineConnection} <:
AbstractConnectionManifold{𝔽}
manifold::M
connection::C
end
@doc raw"""
christoffel_symbols_second(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend = default_differential_backend(),
)
Compute the Christoffel symbols of the second kind in local coordinates of basis `B`.
For affine connection manifold the Christoffel symbols need to be explicitly implemented
while, for a [`MetricManifold`](@ref) they are computed as (in Einstein summation convention)
````math
Γ^{l}_{ij} = g^{kl} Γ_{ijk},
````
where ``Γ_{ijk}`` are the Christoffel symbols of the first kind
(see [`christoffel_symbols_first`](@ref)), and ``g^{kl}`` is the inverse of the local
representation of the metric tensor. The dimensions of the resulting multi-dimensional array
are ordered ``(l,i,j)``.
"""
christoffel_symbols_second(::AbstractManifold, ::Any, ::AbstractBasis)
@decorator_transparent_signature christoffel_symbols_second(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
christoffel_symbols_second_jacobian(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend = default_differential_backend(),
)
Get partial derivatives of the Christoffel symbols of the second kind
for manifold `M` at `p` with respect to the coordinates of `B`, i.e.
```math
\frac{∂}{∂ p^l} Γ^{k}_{ij} = Γ^{k}_{ij,l}.
```
The dimensions of the resulting multi-dimensional array are ordered ``(i,j,k,l)``.
"""
christoffel_symbols_second_jacobian(::AbstractManifold, ::Any, B::AbstractBasis)
function christoffel_symbols_second_jacobian(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend=default_differential_backend(),
retraction::AbstractRetractionMethod=ManifoldsBase.default_retraction_method(M),
)
d = manifold_dimension(M)
∂Γ = reshape(
_jacobian(
c -> christoffel_symbols_second(
M,
retract(M, p, get_vector(M, p, c, B), retraction),
B;
backend=backend,
),
p,
backend,
),
d,
d,
d,
d,
)
return ∂Γ
end
@decorator_transparent_signature christoffel_symbols_second_jacobian(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
"""
connection(M::ConnectionManifold)
Return the connection associated with [`ConnectionManifold`](@ref) `M`.
"""
connection(M::ConnectionManifold) = M.connection
Base.copyto!(M::AbstractConnectionManifold, q, p) = copyto!(M.manifold, q, p)
Base.copyto!(M::AbstractConnectionManifold, Y, p, X) = copyto!(M.manifold, Y, p, X)
@doc raw"""
exp(M::AbstractConnectionManifold, p, X)
Compute the exponential map on the [`AbstractConnectionManifold`](@ref) `M` equipped with
corresponding affine connection.
If `M` is a [`MetricManifold`](@ref) with a metric that was declared the default metric
using [`is_default_metric`](@ref), this method falls back to `exp(M, p, X)`.
Otherwise it numerically integrates the underlying ODE, see [`solve_exp_ode`](@ref).
Currently, the numerical integration is only accurate when using a single
coordinate chart that covers the entire manifold. This excludes coordinates
in an embedded space.
"""
exp(::AbstractConnectionManifold, ::Any...)
@decorator_transparent_fallback function exp!(M::AbstractConnectionManifold, q, p, X)
tspan = (0.0, 1.0)
A = get_default_atlas(M)
i = get_chart_index(M, A, p)
B = induced_basis(M, A, i, TangentSpace)
sol = solve_exp_ode(M, p, X, tspan, B; dense=false, saveat=[1.0])
n = length(p)
return copyto!(q, sol.u[1][(n + 1):end])
end
"""
gaussian_curvature(M::AbstractManifold, p, B::AbstractBasis; backend::AbstractDiffBackend = default_differential_backend())
Compute the Gaussian curvature of the manifold `M` at the point `p` using basis `B`.
This is equal to half of the scalar Ricci curvature, see [`ricci_curvature`](@ref).
"""
gaussian_curvature(::AbstractManifold, ::Any, ::AbstractBasis)
function gaussian_curvature(M::AbstractManifold, p, B::AbstractBasis; kwargs...)
return ricci_curvature(M, p, B; kwargs...) / 2
end
@decorator_transparent_signature gaussian_curvature(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
function injectivity_radius(M::AbstractConnectionManifold, p)
return injectivity_radius(base_manifold(M), p)
end
function injectivity_radius(M::AbstractConnectionManifold, m::AbstractRetractionMethod)
return injectivity_radius(base_manifold(M), m)
end
function injectivity_radius(M::AbstractConnectionManifold, m::ExponentialRetraction)
return injectivity_radius(base_manifold(M), m)
end
function injectivity_radius(M::AbstractConnectionManifold, p, m::AbstractRetractionMethod)
return injectivity_radius(base_manifold(M), p, m)
end
function injectivity_radius(M::AbstractConnectionManifold, p, m::ExponentialRetraction)
return injectivity_radius(base_manifold(M), p, m)
end
"""
ricci_tensor(M::AbstractManifold, p, B::AbstractBasis; backend::AbstractDiffBackend = default_differential_backend())
Compute the Ricci tensor, also known as the Ricci curvature tensor,
of the manifold `M` at the point `p` using basis `B`,
see [`https://en.wikipedia.org/wiki/Ricci_curvature#Introduction_and_local_definition`](https://en.wikipedia.org/wiki/Ricci_curvature#Introduction_and_local_definition).
"""
ricci_tensor(::AbstractManifold, ::Any, ::AbstractBasis)
function ricci_tensor(M::AbstractManifold, p, B::AbstractBasis; kwargs...)
R = riemann_tensor(M, p, B; kwargs...)
n = size(R, 1)
Ric = allocate(R, Size(n, n))
@einsum Ric[i, j] = R[l, i, l, j]
return Ric
end
@decorator_transparent_signature ricci_tensor(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
riemann_tensor(M::AbstractManifold, p, B::AbstractBasis; backend::AbstractDiffBackend=default_differential_backend())
Compute the Riemann tensor ``R^l_{ijk}``, also known as the Riemann curvature
tensor, at the point `p` in local coordinates defined by `B`. The dimensions of the
resulting multi-dimensional array are ordered ``(l,i,j,k)``.
The function uses the coordinate expression involving the second Christoffel symbol,
see [`https://en.wikipedia.org/wiki/Riemann_curvature_tensor#Coordinate_expression`](https://en.wikipedia.org/wiki/Riemann_curvature_tensor#Coordinate_expression)
for details.
# See also
[`christoffel_symbols_second`](@ref), [`christoffel_symbols_second_jacobian`](@ref)
"""
riemann_tensor(::AbstractManifold, ::Any, ::AbstractBasis)
function riemann_tensor(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend=default_differential_backend(),
)
n = size(p, 1)
Γ = christoffel_symbols_second(M, p, B; backend=backend)
∂Γ = christoffel_symbols_second_jacobian(M, p, B; backend=backend) ./ n
R = allocate(∂Γ, Size(n, n, n, n))
@einsum R[l, i, j, k] =
∂Γ[l, i, k, j] - ∂Γ[l, i, j, k] + Γ[s, i, k] * Γ[l, s, j] - Γ[s, i, j] * Γ[l, s, k]
return R
end
@decorator_transparent_signature riemann_tensor(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
solve_exp_ode(
M::AbstractConnectionManifold,
p,
X,
tspan,
B::AbstractBasis;
backend::AbstractDiffBackend = default_differential_backend(),
solver = AutoVern9(Rodas5()),
kwargs...,
)
Approximate the exponential map on the manifold over the provided timespan
assuming the default connection of the given manifold by solving the ordinary differential
equation
```math
\frac{d^2}{dt^2} p^k + Γ^k_{ij} \frac{d}{dt} p_i \frac{d}{dt} p_j = 0,
```
where ``Γ^k_{ij}`` are the Christoffel symbols of the second kind, and
the Einstein summation convention is assumed. The arguments `tspan` and
`solver` follow the `OrdinaryDiffEq` conventions. `kwargs...` specify keyword
arguments that will be passed to `OrdinaryDiffEq.solve`.
Currently, the numerical integration is only accurate when using a single
coordinate chart that covers the entire manifold. This excludes coordinates
in an embedded space.
!!! note
This function only works when
[OrdinaryDiffEq.jl](https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl) is loaded with
```julia
using OrdinaryDiffEq
```
"""
function solve_exp_ode(M, p, X, tspan, B::AbstractBasis; kwargs...)
return error(
"solve_exp_ode not implemented on $(typeof(M)) for point $(typeof(p)), vector $(typeof(X)), and timespan $(typeof(tspan)). For a suitable default, enter `using OrdinaryDiffEq` on Julia 1.1 or greater.",
)
end
#
# Introduce transparency
# (a) new functions & other parents
for f in [
christoffel_symbols_second_jacobian,
exp,
gaussian_curvature,
get_coordinates,
get_vector,
log,
mean,
median,
project,
ricci_tensor,
riemann_tensor,
vector_transport_along,
vector_transport_direction,
vector_transport_direction!, #since it has a default using _to!
vector_transport_to,
]
eval(
quote
function decorator_transparent_dispatch(
::typeof($f),
::AbstractConnectionManifold,
args...,
)
return Val(:parent)
end
end,
)
end
# (b) changes / intransparencies.
for f in [
christoffel_symbols_second, # this is basic for connection manifolds but not for metric manifolds
exp!,
get_coordinates!,
get_vector!,
get_basis,
inner,
inverse_retract!,
log!,
mean!,
median!,
norm,
project!,
retract!,
vector_transport_along!,
vector_transport_to!,
]
eval(
quote
function decorator_transparent_dispatch(
::typeof($f),
::AbstractConnectionManifold,
args...,
)
return Val(:intransparent)
end
end,
)
end
# (c) special cases
function decorator_transparent_dispatch(
::typeof(exp!),
M::AbstractConnectionManifold,
q,
p,
X,
t,
)
return Val(:parent)
end
function decorator_transparent_dispatch(
::typeof(inverse_retract!),
M::AbstractConnectionManifold,
X,
p,
q,
m::LogarithmicInverseRetraction,
)
return Val(:parent)
end
function decorator_transparent_dispatch(
::typeof(retract!),
M::AbstractConnectionManifold,
q,
p,
X,
m::ExponentialRetraction,
)
return Val(:parent)
end