-
Notifications
You must be signed in to change notification settings - Fork 56
/
Manifolds.jl
562 lines (448 loc) · 16 KB
/
Manifolds.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
module Manifolds
import Base: isapprox,
exp,
log,
angle,
eltype,
similar,
getindex,
setindex!,
size,
copy,
convert,
dataids,
+,
-,
*
import LinearAlgebra: dot,
norm,
det,
cross,
I,
UniformScaling,
Diagonal
using StaticArrays
import Markdown: @doc_str
import Distributions: _rand!, support
import Random: rand
using LinearAlgebra
using Random: AbstractRNG
using SimpleTraits
using ForwardDiff
using UnsafeArrays
import Einsum: @einsum
import OrdinaryDiffEq: ODEProblem,
AutoVern9,
Rodas5,
solve
"""
Manifold
A manifold type. The `Manifold` is used to dispatch to different exponential
and logarithmic maps as well as other function on manifold.
"""
abstract type Manifold end
"""
MPoint
Type for a point on a manifold. While a [`Manifold`](@ref) not necessarily
requires this type, for example when it is implemented for `Vector`s or
`Matrix` type elements, this type can be used for more complicated
representations, semantic verification or even dispatch for different
representations of points on a manifold.
"""
abstract type MPoint end
"""
TVector
Type for a tangent vector of a manifold. While a [`Manifold`](@ref) not
necessarily requires this type, for example when it is implemented for `Vector`s
or `Matrix` type elements, this type can be used for more complicated
representations, semantic verification or even dispatch for different
representations of tangent vectors and their types on a manifold.
"""
abstract type TVector end
"""
CoTVector
Type for a cotangent vector of a manifold. While a [`Manifold`](@ref) not
necessarily requires this type, for example when it is implemented for `Vector`s
or `Matrix` type elements, this type can be used for more complicated
representations, semantic verification or even dispatch for different
representations of cotangent vectors and their types on a manifold.
"""
abstract type CoTVector end
"""
IsDecoratorManifold
A `Trait` to mark a manifold as a decorator type. For any function that is only
implemented for a decorator (i.e. a Manifold with `@traitimpl
IsDecoratorManifold{M}`), a specific function should be implemented as a
`@traitfn`, that transparently passes down through decorators, i.e.
```
@traitfn my_feature(M::MT, k...) where {MT; IsDecoratorManifold{MT}} = my_feature(M.manifold, k...)
```
or the shorter version
```
@traitfn my_feature(M::::IsDecoratorManifold, k...) = my_feature(M.manifold, k...)
```
such that decorators act just as pass throughs for other decorator functions and
```
my_feature(M::MyManifold, k...) = #... my explicit implementation
```
then implements the feature itself.
"""
@traitdef IsDecoratorManifold{M}
"""
base_manifold(M::Manifold)
Strip all decorators on `M`, returning the underlying topological manifold.
"""
function base_manifold end
@traitfn function base_manifold(M::MT) where {MT<:Manifold;IsDecoratorManifold{MT}}
return base_manifold(M.manifold)
end
@traitfn base_manifold(M::MT) where {MT<:Manifold;!IsDecoratorManifold{MT}} = M
@doc doc"""
manifold_dimension(M::Manifold)
The dimension $n$ of real space $\mathbb R^n$ to which the neighborhood
of each point of the manifold is homeomorphic.
"""
function manifold_dimension end
@doc doc"""
representation_size(M::Manifold, ::Type{T}) where {T}
The size of array representing an object of type `T` on manifold `M`,
for example point, tangent vector or cotangent vector.
The second argument should in these cases be equal to, respectively,
`MPoint`, `TVector` and `CoTVector`, regardless of the type used to represent
said objects.
"""
function representation_size end
@traitfn function representation_size(M::MT, ::Type{T}) where {MT<:Manifold,T;!IsDecoratorManifold{MT}}
error("representation_size not implemented for manifold $(typeof(M)) and type $(T).")
end
@traitfn function representation_size(M::MT, ::Type{T}) where {MT<:Manifold,T;IsDecoratorManifold{MT}}
return representation_size(base_manifold(M), T)
end
@traitfn function manifold_dimension(M::MT) where {MT<:Manifold;!IsDecoratorManifold{MT}}
error("manifold_dimension not implemented for a $(typeof(M)).")
end
@traitfn function manifold_dimension(M::MT) where {MT<:Manifold;IsDecoratorManifold{MT}}
manifold_dimension(base_manifold(M))
end
"""
isapprox(M::Manifold, x, y; kwargs...)
Check if points `x` and `y` from manifold `M` are approximately equal.
Keyword arguments can be used to specify tolerances.
"""
isapprox(M::Manifold, x, y; kwargs...) = isapprox(x, y; kwargs...)
"""
isapprox(M::Manifold, x, v, w; kwargs...)
Check if vectors `v` and `w` tangent at `x` from manifold `M` are
approximately equal.
Keyword arguments can be used to specify tolerances.
"""
isapprox(M::Manifold, x, v, w; kwargs...) = isapprox(v, w; kwargs...)
"""
OutOfInjectivityRadiusError
An error thrown when a function (for example logarithmic map or inverse
retraction) is given arguments outside of its injectivity radius.
"""
struct OutOfInjectivityRadiusError <: Exception end
abstract type AbstractRetractionMethod end
"""
ExponentialRetraction
Retraction using the exponential map.
"""
struct ExponentialRetraction <: AbstractRetractionMethod end
"""
retract!(M::Manifold, y, x, v, [t=1], [method::AbstractRetractionMethod=ExponentialRetraction()])
Retraction (cheaper, approximate version of exponential map) of tangent
vector `t*v` at point `x` from manifold `M`.
Result is saved to `y`.
Retraction method can be specified by the last argument. Please look at the
documentation of respective manifolds for available methods.
"""
retract!(M::Manifold, y, x, v, method::ExponentialRetraction) = exp!(M, y, x, v)
retract!(M::Manifold, y, x, v) = retract!(M, y, x, v, ExponentialRetraction())
retract!(M::Manifold, y, x, v, t::Real) = retract!(M, y, x, t*v)
retract!(M::Manifold, y, x, v, t::Real, method::AbstractRetractionMethod) = retract!(M, y, x, t*v, method)
"""
retract(M::Manifold, x, v, [t=1], [method::AbstractRetractionMethod])
Retraction (cheaper, approximate version of exponential map) of tangent
vector `t*v` at point `x` from manifold `M`.
"""
function retract(M::Manifold, x, v, method::AbstractRetractionMethod)
xr = similar_result(M, retract, x, v)
retract!(M, xr, x, v, method)
return xr
end
function retract(M::Manifold, x, v)
xr = similar_result(M, retract, x, v)
retract!(M, xr, x, v)
return xr
end
retract(M::Manifold, x, v, t::Real) = retract(M, x, t*v)
retract(M::Manifold, x, v, t::Real, method::AbstractRetractionMethod) = retract(M, x, t*v, method)
abstract type AbstractInverseRetractionMethod end
"""
LogarithmicInverseRetraction
Inverse retraction using the logarithmic map.
"""
struct LogarithmicInverseRetraction <: AbstractInverseRetractionMethod end
"""
inverse_retract!(M::Manifold, v, x, y, [method::AbstractInverseRetractionMethod=LogarithmicInverseRetraction()])
Inverse retraction (cheaper, approximate version of logarithmic map) of points
`x` and `y`.
Result is saved to `y`.
Inverse retraction method can be specified by the last argument. Please look
at the documentation of respective manifolds for available methods.
"""
inverse_retract!(M::Manifold, v, x, y, method::LogarithmicInverseRetraction) = log!(M, v, x, y)
inverse_retract!(M::Manifold, v, x, y) = inverse_retract!(M, v, x, y, LogarithmicInverseRetraction())
"""
inverse_retract(M::Manifold, x, y, [method::AbstractInverseRetractionMethod])
Inverse retraction (cheaper, approximate version of logarithmic map) of points
`x` and `y` from manifold `M`.
Inverse retraction method can be specified by the last argument. Please look
at the documentation of respective manifolds for available methods.
"""
function inverse_retract(M::Manifold, x, y, method::AbstractInverseRetractionMethod)
vr = similar_result(M, inverse_retract, x, y)
inverse_retract!(M, vr, x, y, method)
return vr
end
function inverse_retract(M::Manifold, x, y)
vr = similar_result(M, inverse_retract, x, y)
inverse_retract!(M, vr, x, y)
return vr
end
project_point!(M::Manifold, x) = error("project onto tangent space not implemented for a $(typeof(M)) and point $(typeof(x)).")
function project_point(M::Manifold, x)
y = similar_result(M, project_point, x)
project_tangent!(M, y, x)
return y
end
project_tangent!(M::Manifold, w, x, v) = error("project onto tangent space not implemented for a $(typeof(M)) and point $(typeof(x)) with input $(typeof(v)).")
function project_tangent(M::Manifold, x, v)
vt = similar_result(M, project_tangent, v, x)
project_tangent!(M, vt, x, v)
return vt
end
"""
inner(M::Manifold, x, v, w)
Inner product of tangent vectors `v` and `w` at point `x` from manifold `M`.
"""
inner(M::Manifold, x, v, w) = error("inner: Inner product not implemented on a $(typeof(M)) for input point $(typeof(x)) and tangent vectors $(typeof(v)) and $(typeof(w)).")
"""
norm(M::Manifold, x, v)
Norm of tangent vector `v` at point `x` from manifold `M`.
"""
norm(M::Manifold, x, v) = sqrt(inner(M, x, v, v))
"""
distance(M::Manifold, x, y)
Shortest distance between the points `x` and `y` on manifold `M`.
"""
distance(M::Manifold, x, y) = norm(M, x, log(M, x, y))
"""
angle(M::Manifold, x, v, w)
Angle between tangent vectors `v` and `w` at point `x` from manifold `M`.
"""
angle(M::Manifold, x, v, w) = acos(inner(M, x, v, w) / norm(M, x, v) / norm(M, x, w))
"""
exp!(M::Manifold, y, x, v, t=1)
Exponential map of tangent vector `t*v` at point `x` from manifold `M`.
Result is saved to `y`.
"""
exp!(M::Manifold, y, x, v, t::Real) = exp!(M, y, x, t*v)
exp!(M::Manifold, y, x, v) = error("Exponential map not implemented on a $(typeof(M)) for input point $(x) and tangent vector $(v).")
"""
exp(M::Manifold, x, v, t=1)
Exponential map of tangent vector `t*v` at point `x` from manifold `M`.
"""
function exp(M::Manifold, x, v)
x2 = similar_result(M, x, v)
exp!(M, x2, x, v)
return x2
end
exp(M::Manifold, x, v, t::Real) = exp(M, x, t*v)
"""
exp(M::Manifold, x, v, T::AbstractVector)
Exponential map of tangent vector `t*v` at point `x` from manifold `M` for
each `t` in `T`.
"""
exp(M::Manifold, x, v, T::AbstractVector) = map(geodesic(M, x, v), T)
log!(M::Manifold, v, x, y) = error("Logarithmic map not implemented on $(typeof(M)) for points $(typeof(x)) and $(typeof(y))")
function log(M::Manifold, x, y)
v = similar_result(M, log, x, y)
log!(M, v, x, y)
return v
end
"""
geodesic(M::Manifold, x, v)
Get the geodesic with initial point `x` and velocity `v`. The geodesic
is the curve of constant velocity that is locally distance-minimizing. This
function returns a function of time, which may be a `Real` or an
`AbstractVector`.
"""
geodesic(M::Manifold, x, v) = t -> exp(M, x, v, t)
"""
geodesic(M::Manifold, x, v, t)
Get the point at time `t` traveling from `x` along the geodesic with initial
point `x` and velocity `v`.
"""
geodesic(M::Manifold, x, v, t::Real) = exp(M, x, v, t)
"""
geodesic(M::Manifold, x, v, T::AbstractVector)
Get the points for each `t` in `T` traveling from `x` along the geodesic with
initial point `x` and velocity `v`.
"""
geodesic(M::Manifold, x, v, T::AbstractVector) = exp(M, x, v, T)
"""
shortest_geodesic(M::Manifold, x, y)
Get a geodesic with initial point `x` and point `y` at `t=1` whose length is
the shortest path between the two points. When there are multiple shortest
geodesics, there is no guarantee which will be returned. This function returns
a function of time, which may be a `Real` or an `AbstractVector`.
"""
shortest_geodesic(M::Manifold, x, y) = geodesic(M, x, log(M, x, y))
"""
shortest_geodesic(M::Manifold, x, y, t)
Get the point at time `t` traveling from `x` along a shortest geodesic
connecting `x` and `y`, where `y` is reached at `t=1`.
"""
shortest_geodesic(M::Manifold, x, y, t::Real) = geodesic(M, x, log(M, x, y), t)
"""
shortest_geodesic(M::Manifold, x, y, T::AbstractVector)
Get the points for each `t` in `T` traveling from `x` along a shortest geodesic
connecting `x` and `y`, where `y` is reached at `t=1`.
"""
function shortest_geodesic(M::Manifold, x, y, T::AbstractVector)
return geodesic(M, x, log(M, x, y), T)
end
vector_transport!(M::Manifold, vto, x, v, y) = project_tangent!(M, vto, x, v)
function vector_transport(M::Manifold, x, v, y)
vto = similar_result(M, vector_transport, v, x, y)
vector_transport!(M, vto, x, y, v)
return vto
end
@doc doc"""
injectivity_radius(M::Manifold, x)
Distance $d$ such that `exp(M, x, v)` is injective for all tangent
vectors shorter than $d$ (has a left inverse).
"""
injectivity_radius(M::Manifold, x) = Inf
@doc doc"""
injectivity_radius(M::Manifold, x, R::AbstractRetractionMethod)
Distance $d$ such that `retract(M, x, v, R)` is injective for all tangent
vectors shorter than $d$ (has a left inverse).
"""
injectivity_radius(M::Manifold, x, ::AbstractRetractionMethod) = injectivity_radius(M, x)
"""
injectivity_radius(M::Manifold, x)
Infimum of the injectivity radii of all manifold points.
"""
injectivity_radius(M::Manifold) = Inf
function zero_tangent_vector(M::Manifold, x)
v = similar_result(M, zero_tangent_vector, x)
zero_tangent_vector!(M, v, x)
return v
end
zero_tangent_vector!(M::Manifold, v, x) = log!(M, v, x, x)
"""
similar_result_type(M::Manifold, f, args::NTuple{N,Any}) where N
Returns type of element of the array that will represent the result of
function `f` for manifold `M` on given arguments (passed at a tuple)
"""
function similar_result_type(M::Manifold, f, args::NTuple{N,Any}) where N
T = typeof(reduce(+, one(eltype(eti)) for eti ∈ args))
return T
end
"""
similar_result(M::Manifold, f, x...)
Allocates an array for the result of function `f` on manifold `M`
and arguments `x...` for implementing the non-modifying operation
using the modifying operation.
"""
function similar_result(M::Manifold, f, x...)
T = similar_result_type(M, f, x)
return similar(x[1], T)
end
"""
is_manifold_point(M,x)
check, whether `x` is a valid point on the [`Manifold`](@ref) `M`. If it is not,
an error is thrown.
The default is to return `true`, i.e. if no checks are implmented,
the assumption is to be optimistic.
"""
is_manifold_point(M::Manifold, x; kwargs...) = true
is_manifold_point(M::Manifold, x::MPoint) = error("A validation for a $(typeof(x)) on $(typeof(M)) not implemented.")
"""
is_tangent_vector(M,x,v)
check, whether `v` is a valid tangnt vector in the tangent plane of `x` on the
[`Manifold`](@ref) `M`. An implementation should first check
[`is_manifold_point`](@ref)`(M,x)` and then validate `v`. If it is not a tangent
vector an error should be thrown.
The default is to return `true`, i.e. if no checks are implmented,
the assumption is to be optimistic.
"""
is_tangent_vector(M::Manifold, x, v; kwargs...) = true
is_tangent_vector(M::Manifold, x::MPoint, v::TVector) = error("A validation for a $(typeof(v)) in the tangent space of a $(typeof(x)) on $(typeof(M)) not implemented.")
include("utils.jl")
include("ArrayManifold.jl")
include("DistributionsBase.jl")
include("Metric.jl")
include("Euclidean.jl")
include("ProductManifold.jl")
include("Rotations.jl")
include("Sphere.jl")
include("ProjectedDistribution.jl")
export Manifold,
IsDecoratorManifold,
Euclidean,
Sphere,
ProductManifold,
ProductMPoint,
ProductTVector,
ProductCoTVector
export ×,
base_manifold,
distance,
exp,
exp!,
geodesic,
shortest_geodesic,
injectivity_radius,
inner,
inverse_retract,
inverse_retract!,
isapprox,
is_manifold_point,
is_tangent_vector,
log,
log!,
manifold_dimension,
norm,
project_point,
project_point!,
project_tangent,
project_tangent!,
retract,
retract!,
submanifold,
submanifold_component,
zero_tangent_vector,
zero_tangent_vector!
export Metric,
RiemannianMetric,
LorentzMetric,
EuclideanMetric,
MetricManifold,
HasMetric,
metric,
local_metric,
inverse_local_metric,
det_local_metric,
log_local_metric_density,
christoffel_symbols_first,
christoffel_symbols_second,
riemann_tensor,
ricci_tensor,
einstein_tensor,
ricci_curvature,
gaussian_curvature
end # module