-
Notifications
You must be signed in to change notification settings - Fork 56
/
MetricManifold.jl
840 lines (756 loc) · 25.8 KB
/
MetricManifold.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
"""
IsMetricManifold <: AbstractTrait
Specify that a certain decorated Manifold is a metric manifold in the sence that it provides
explicit metric properties, extending/changing the default metric properties of a manifold.
"""
struct IsMetricManifold <: AbstractTrait end
"""
IsDefaultMetric{G<:AbstractMetric}
Specify that a certain [`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric)
is the default metric for a manifold.
This way the corresponding [`MetricManifold`](@ref) falls back to the default methods
of the manifold it decorates.
"""
struct IsDefaultMetric{G<:AbstractMetric} <: AbstractTrait
metric::G
end
parent_trait(::IsDefaultMetric) = IsMetricManifold()
# piping syntax for decoration
(metric::AbstractMetric)(M::AbstractManifold) = MetricManifold(M, metric)
(::Type{T})(M::AbstractManifold) where {T<:AbstractMetric} = MetricManifold(M, T())
"""
MetricManifold{𝔽,M<:AbstractManifold{𝔽},G<:AbstractMetric} <: AbstractDecoratorManifold{𝔽}
Equip a [`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) explicitly with an
[`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric) `G`.
For a Metric AbstractManifold, by default, assumes, that you implement the linear form
from [`local_metric`](@ref) in order to evaluate the exponential map.
If the corresponding `AbstractMetric` `G` yields closed form formulae for e.g.
the exponential map and this is implemented directly (without solving the ode),
you can of course still implement that directly.
# Constructor
MetricManifold(M, G)
Generate the [`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` as a manifold with the `AbstractMetric` `G`.
"""
struct MetricManifold{𝔽,M<:AbstractManifold{𝔽},G<:AbstractMetric} <:
AbstractDecoratorManifold{𝔽}
manifold::M
metric::G
end
function _drop_embedding_type(t::TraitList)
return TraitList(t.head, _drop_embedding_type(t.tail))
end
function _drop_embedding_type(t::TraitList{IsIsometricEmbeddedManifold})
return _drop_embedding_type(t.tail)
end
function _drop_embedding_type(t::TraitList{IsEmbeddedSubmanifold})
return _drop_embedding_type(t.tail)
end
_drop_embedding_type(t::EmptyTrait) = t
function active_traits(f, M::MetricManifold, args...)
at = active_traits(f, M.manifold, args...)
imf = is_metric_function(f)
idm = imf && is_default_metric(M.manifold, M.metric)
return merge_traits(
idm ? IsDefaultMetric(M.metric) : EmptyTrait(),
IsMetricManifold(),
# avoid forwarding to the embedding if the metric is not the default one
idm ? at : _drop_embedding_type(at),
imf ? EmptyTrait() : IsExplicitDecorator(),
)
end
# remetricise instead of double-decorating
(metric::AbstractMetric)(M::MetricManifold) = MetricManifold(M.manifold, metric)
(::Type{T})(M::MetricManifold) where {T<:AbstractMetric} = MetricManifold(M.manifold, T())
decorated_manifold(M::MetricManifold) = M.manifold
get_embedding(M::MetricManifold) = get_embedding(M.manifold)
function change_metric!(
::T,
M::AbstractDecoratorManifold,
Y,
::G,
p,
X,
) where {G<:AbstractMetric,T<:TraitList{<:IsDefaultMetric{<:G}}}
return copyto!(M, Y, p, X)
end
function change_metric!(M::MetricManifold, Y, G::AbstractMetric, p, X)
M.metric === G && return copyto!(M, Y, p, X) # no metric change
# TODO: For local metric, inverse_local metric, det_local_metric: Introduce a default basis?
B = DefaultOrthogonalBasis()
G1 = local_metric(M, p, B)
G2 = local_metric(G(M), p, B)
x = get_coordinates(M, p, X, B)
C1 = cholesky(G1).L
C2 = cholesky(G2).L
z = (C1 \ C2)'x
return get_vector!(M, Y, p, z, B)
end
# Default fallback II: Default metric (not yet hit, check subtyping?)
function change_representer!(
::T,
M::AbstractDecoratorManifold,
Y,
::G,
p,
X,
) where {G<:AbstractMetric,T<:TraitList{<:IsDefaultMetric{<:G}}}
return copyto!(M, Y, p, X)
end
# Default fallback II: compute in local metric representations
function change_representer!(M::AbstractManifold, Y, G::AbstractMetric, p, X)
M.metric === G && return copyto!(M, Y, p, X) # no metric change
# TODO: For local metric, inverse_local metric, det_local_metric: Introduce a default basis?
B = DefaultOrthogonalBasis()
G1 = local_metric(M, p, B)
G2 = local_metric(G(M), p, B)
x = get_coordinates(M, p, X, B)
z = (G1 \ G2)'x
return get_vector!(M, Y, p, z, B)
end
"""
connection(::MetricManifold)
Return the [`LeviCivitaConnection`](@ref) for a metric manifold.
"""
connection(::MetricManifold) = LeviCivitaConnection()
default_retraction_method(M::MetricManifold) = default_retraction_method(M.manifold)
function default_retraction_method(M::MetricManifold, t::Type)
return default_retraction_method(M.manifold, t)
end
function default_inverse_retraction_method(M::MetricManifold)
return default_inverse_retraction_method(M.manifold)
end
function default_inverse_retraction_method(M::MetricManifold, t::Type)
return default_inverse_retraction_method(M.manifold, t)
end
function default_vector_transport_method(M::MetricManifold)
return default_vector_transport_method(M.manifold)
end
function default_vector_transport_method(M::MetricManifold, t::Type)
return default_vector_transport_method(M.manifold, t)
end
@doc raw"""
det_local_metric(M::AbstractManifold, p, B::AbstractBasis)
Return the determinant of local matrix representation of the metric tensor ``g``, i.e. of the
matrix ``G(p)`` representing the metric in the tangent space at ``p`` with as a matrix.
See also [`local_metric`](@ref)
"""
function det_local_metric(M::AbstractManifold, p, B::AbstractBasis)
return det(local_metric(M, p, B))
end
@trait_function det_local_metric(M::AbstractDecoratorManifold, p, B::AbstractBasis)
function exp!(::TraitList{IsMetricManifold}, M::AbstractDecoratorManifold, q, p, X)
return retract!(
M,
q,
p,
X,
ODEExponentialRetraction(ManifoldsBase.default_retraction_method(M, typeof(p))),
)
end
"""
einstein_tensor(M::AbstractManifold, p, B::AbstractBasis; backend::AbstractDiffBackend = diff_badefault_differential_backendckend())
Compute the Einstein tensor of the manifold `M` at the point `p`, see [https://en.wikipedia.org/wiki/Einstein_tensor](https://en.wikipedia.org/wiki/Einstein_tensor)
"""
function einstein_tensor(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend=default_differential_backend(),
)
Ric = ricci_tensor(M, p, B; backend=backend)
g = local_metric(M, p, B)
Ginv = inverse_local_metric(M, p, B)
S = sum(Ginv .* Ric)
G = Ric - g .* S / 2
return G
end
@trait_function einstein_tensor(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
flat(N::MetricManifold{M,G}, p, X::TFVector)
Compute the musical isomorphism to transform the tangent vector `X` from the
[`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` equipped with
[`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric) `G` to a cotangent by
computing
````math
X^♭= G_p X,
````
where ``G_p`` is the local matrix representation of `G`, see [`local_metric`](@ref)
"""
flat(::MetricManifold, ::Any, ::TFVector)
function flat!(
::TraitList{IsMetricManifold},
M::AbstractDecoratorManifold,
ξ::CoTFVector,
p,
X::TFVector,
)
g = local_metric(M, p, ξ.basis)
copyto!(ξ.data, g * X.data)
return ξ
end
function flat!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
ξ::CoTFVector,
p,
X::TFVector,
) where {𝔽,TM<:AbstractManifold,G<:AbstractMetric}
flat!(M.manifold, ξ, p, X)
return ξ
end
function get_basis(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return get_basis(M.manifold, p, B)
end
function get_coordinates(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return get_coordinates(M.manifold, p, X, B)
end
function get_coordinates!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return get_coordinates!(M.manifold, Y, p, X, B)
end
function get_vector(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
c,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return get_vector(M.manifold, p, c, B)
end
function get_vector!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
c,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return get_vector!(M.manifold, Y, p, c, B)
end
@doc raw"""
inverse_local_metric(M::AbstractcManifold{𝔽}, p, B::AbstractBasis)
Return the local matrix representation of the inverse metric (cometric) tensor
of the tangent space at `p` on the [`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` with respect
to the [`AbstractBasis`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/bases.html#ManifoldsBase.AbstractBasis) basis `B`.
The metric tensor (see [`local_metric`](@ref)) is usually denoted by ``G = (g_{ij}) ∈ 𝔽^{d×d}``,
where ``d`` is the dimension of the manifold.
Then the inverse local metric is denoted by ``G^{-1} = g^{ij}``.
"""
inverse_local_metric(::AbstractManifold, ::Any, ::AbstractBasis)
function inverse_local_metric(M::AbstractManifold, p, B::AbstractBasis)
return inv(local_metric(M, p, B))
end
@trait_function inverse_local_metric(M::AbstractDecoratorManifold, p, B::AbstractBasis)
function Base.convert(::Type{MetricManifold{𝔽,MT,GT}}, M::MT) where {𝔽,MT,GT}
return _convert_with_default(M, GT, Val(is_default_metric(M, GT())))
end
function _convert_with_default(
M::MT,
T::Type{<:AbstractMetric},
::Val{true},
) where {MT<:AbstractManifold}
return MetricManifold(M, T())
end
function _convert_with_default(
M::MT,
T::Type{<:AbstractMetric},
::Val{false},
) where {MT<:AbstractManifold}
return error(
"Can not convert $(M) to a MetricManifold{$(MT),$(T)}, since $(T) is not the default metric.",
)
end
function exp(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return exp(M.manifold, p, X)
end
function exp(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
t::Number,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return exp(M.manifold, p, X, t)
end
function exp!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
q,
p,
X,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return exp!(M.manifold, q, p, X)
end
function exp!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
q,
p,
X,
t::Number,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return exp!(M.manifold, q, p, X, t)
end
injectivity_radius(M::MetricManifold) = injectivity_radius(M.manifold)
function injectivity_radius(M::MetricManifold, m::AbstractRetractionMethod)
return injectivity_radius(M.manifold, m)
end
@doc raw"""
inner(N::MetricManifold{M,G}, p, X, Y)
Compute the inner product of `X` and `Y` from the tangent space at `p` on the
[`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` using the
[`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric) `G`.
If `M` has `G` as its [`IsDefaultMetric`](@ref) trait,
this is done using `inner(M, p, X, Y)`, otherwise the [`local_metric`](@ref)`(M, p)` is employed as
````math
g_p(X, Y) = ⟨X, G_p Y⟩,
````
where ``G_p`` is the loal matrix representation of the `AbstractMetric` `G`.
"""
inner(::MetricManifold, ::Any, ::Any, ::Any)
function inner(
::TraitList{IsMetricManifold},
M::AbstractDecoratorManifold,
p,
X::TFVector,
Y::TFVector,
)
X.basis === Y.basis ||
error("calculating inner product of vectors from different bases is not supported")
return dot(X.data, local_metric(M, p, X.basis) * Y.data)
end
function inner(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
Y,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return inner(M.manifold, p, X, Y)
end
"""
is_default_metric(M::AbstractManifold, G::AbstractMetric)
returns whether an [`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric)
is the default metric on the manifold `M` or not.
This can be set by defining this function, or setting the [`IsDefaultMetric`](@ref) trait for an
[`AbstractDecoratorManifold`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/decorator.html#ManifoldsBase.AbstractDecoratorManifold).
"""
is_default_metric(M::AbstractManifold, G::AbstractMetric)
@trait_function is_default_metric(M::AbstractDecoratorManifold, G::AbstractMetric)
function is_default_metric(
::TraitList{IsDefaultMetric{G}},
::AbstractDecoratorManifold,
::G,
) where {G<:AbstractMetric}
return true
end
is_default_metric(M::MetricManifold) = is_default_metric(M.manifold, M.metric)
is_default_metric(::AbstractManifold, ::AbstractMetric) = false
function is_point(
::TraitList{IsMetricManifold},
M::MetricManifold{𝔽,TM,G},
p;
kwargs...,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return is_point(M.manifold, p; kwargs...)
end
function is_vector(
::TraitList{IsMetricManifold},
M::MetricManifold{𝔽,TM,G},
p,
X,
cbp::Bool=true;
kwargs...,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return is_vector(M.manifold, p, X, cbp; kwargs...)
end
@doc raw"""
local_metric(M::AbstractManifold{𝔽}, p, B::AbstractBasis)
Return the local matrix representation at the point `p` of the metric tensor ``g`` with
respect to the [`AbstractBasis`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/bases.html#ManifoldsBase.AbstractBasis) `B` on the [`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M`.
Let ``d``denote the dimension of the manifold and $b_1,\ldots,b_d$ the basis vectors.
Then the local matrix representation is a matrix ``G\in 𝔽^{n\times n}`` whose entries are
given by ``g_{ij} = g_p(b_i,b_j), i,j\in\{1,…,d\}``.
This yields the property for two tangent vectors (using Einstein summation convention)
``X = X^ib_i, Y=Y^ib_i \in T_p\mathcal M`` we get ``g_p(X, Y) = g_{ij} X^i Y^j``.
"""
local_metric(::AbstractManifold, ::Any, ::AbstractBasis)
@trait_function local_metric(M::AbstractDecoratorManifold, p, B::AbstractBasis; kwargs...)
function local_metric(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
B::AbstractBasis,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return local_metric(M.manifold, p, B)
end
@doc raw"""
local_metric_jacobian(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend,
)
Get partial derivatives of the local metric of `M` at `p` in basis `B` with respect to the
coordinates of `p`, ``\frac{∂}{∂ p^k} g_{ij} = g_{ij,k}``. The
dimensions of the resulting multi-dimensional array are ordered ``(i,j,k)``.
"""
local_metric_jacobian(::AbstractManifold, ::Any, B::AbstractBasis, ::AbstractDiffBackend)
function local_metric_jacobian(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend=default_differential_backend(),
)
n = size(p, 1)
∂g = reshape(_jacobian(q -> local_metric(M, q, B), p, backend), n, n, n)
return ∂g
end
@trait_function local_metric_jacobian(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
log(N::MetricManifold{M,G}, p, q)
Copute the logarithmic map on the [`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` equipped with the
[`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric) `G`.
If the metric was declared the default metric using the [`IsDefaultMetric`](@ref) trait or [`is_default_metric`](@ref), this method
falls back to `log(M,p,q)`. Otherwise, you have to provide an implementation for the non-default
`AbstractMetric` `G` metric within its [`MetricManifold`](@ref)`{M,G}`.
"""
log(::MetricManifold, ::Any...)
function log(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
q,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return log(M.manifold, p, q)
end
function log!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
X,
p,
q,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return log!(M.manifold, X, p, q)
end
@doc raw"""
log_local_metric_density(M::AbstractManifold, p, B::AbstractBasis)
Return the natural logarithm of the metric density ``ρ`` of `M` at `p`, which
is given by ``ρ = \log \sqrt{|\det [g_{ij}]|}`` for the metric tensor expressed in basis `B`.
"""
log_local_metric_density(::AbstractManifold, ::Any, ::AbstractBasis)
function log_local_metric_density(M::AbstractManifold, p, B::AbstractBasis)
return log(abs(det_local_metric(M, p, B))) / 2
end
@trait_function log_local_metric_density(M::AbstractDecoratorManifold, p, B::AbstractBasis)
manifold_dimension(M::MetricManifold) = manifold_dimension(M.manifold)
@doc raw"""
metric(M::MetricManifold)
Get the metric ``g`` of the manifold `M`.
"""
metric(::MetricManifold)
function metric(M::MetricManifold)
return M.metric
end
function norm(::TraitList{IsMetricManifold}, M::AbstractDecoratorManifold, p, X::TFVector)
return sqrt(dot(X.data, local_metric(M, p, X.basis) * X.data))
end
function parallel_transport_to(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
q,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return parallel_transport_to(M.manifold, p, X, q)
end
function parallel_transport_to!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
q,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return parallel_transport_to!(M.manifold, Y, p, X, q)
end
function project(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return project(M.manifold, p)
end
function project!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
q,
p,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return project!(M.manifold, q, p)
end
function project(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return project(M.manifold, p, X)
end
function project!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return project!(M.manifold, Y, p, X)
end
representation_size(M::MetricManifold) = representation_size(M.manifold)
@doc raw"""
ricci_curvature(M::AbstractManifold, p, B::AbstractBasis; backend::AbstractDiffBackend = default_differential_backend())
Compute the Ricci scalar curvature of the manifold `M` at the point `p` using basis `B`.
The curvature is computed as the trace of the Ricci curvature tensor with respect to
the metric, that is ``R=g^{ij}R_{ij}`` where ``R`` is the scalar Ricci curvature at `p`,
``g^{ij}`` is the inverse local metric (see [`inverse_local_metric`](@ref)) at `p` and
``R_{ij}`` is the Riccie curvature tensor, see [`ricci_tensor`](@ref). Both the tensor and
inverse local metric are expressed in local coordinates defined by `B`, and the formula
uses the Einstein summation convention.
"""
ricci_curvature(::AbstractManifold, ::Any, ::AbstractBasis)
function ricci_curvature(
M::AbstractManifold,
p,
B::AbstractBasis;
backend::AbstractDiffBackend=default_differential_backend(),
)
Ginv = inverse_local_metric(M, p, B)
Ric = ricci_tensor(M, p, B; backend=backend)
S = sum(Ginv .* Ric)
return S
end
@trait_function ricci_curvature(
M::AbstractDecoratorManifold,
p,
B::AbstractBasis;
kwargs...,
)
@doc raw"""
sharp(N::MetricManifold{M,G}, p, ξ::CoTFVector)
Compute the musical isomorphism to transform the cotangent vector `ξ` from the
[`AbstractManifold`](https://juliamanifolds.github.io/Manifolds.jl/latest/interface.html#ManifoldsBase.AbstractManifold) `M` equipped with
[`AbstractMetric`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/manifolds.html#ManifoldsBase.AbstractMetric) `G` to a tangent by
computing
````math
ξ^♯ = G_p^{-1} ξ,
````
where ``G_p`` is the local matrix representation of `G`, i.e. one employs
[`inverse_local_metric`](@ref) here to obtain ``G_p^{-1}``.
"""
sharp(::MetricManifold, ::Any, ::CoTFVector)
function sharp!(
::TraitList{IsMetricManifold},
M::AbstractDecoratorManifold,
X::TFVector,
p,
ξ::CoTFVector,
)
Ginv = inverse_local_metric(M, p, X.basis)
copyto!(X.data, Ginv * ξ.data)
return X
end
function sharp!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
X::TFVector,
p,
ξ::CoTFVector,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
sharp!(M.manifold, X, p, ξ)
return X
end
function Base.show(io::IO, M::MetricManifold)
return print(io, "MetricManifold($(M.manifold), $(M.metric))")
end
function Base.show(io::IO, i::IsDefaultMetric)
return print(io, "IsDefaultMetric($(i.metric))")
end
function vector_transport_along(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
c::AbstractVector,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_along(M.manifold, p, X, c, m)
end
function vector_transport_along!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
c::AbstractVector,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_along!(M.manifold, Y, p, X, c, m)
end
function vector_transport_direction(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
d,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_direction(M.manifold, p, X, d, m)
end
function vector_transport_direction!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
d,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_direction!(M.manifold, Y, p, X, d, m)
end
function vector_transport_to(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
q,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_to(M.manifold, p, X, q, m)
end
function vector_transport_to!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
q,
m::AbstractVectorTransportMethod=default_vector_transport_method(M, typeof(p)),
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return vector_transport_to!(M.manifold, Y, p, X, q, m)
end
function Weingarten(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
p,
X,
V,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return Weingarten(M.manifold, p, X, V)
end
function Weingarten!(
::TraitList{IsDefaultMetric{G}},
M::MetricManifold{𝔽,TM,G},
Y,
p,
X,
V,
) where {𝔽,G<:AbstractMetric,TM<:AbstractManifold}
return Weingarten!(M.manifold, Y, p, X, V)
end
zero_vector(M::MetricManifold, p) = zero_vector(M.manifold, p)
zero_vector!(M::MetricManifold, X, p) = zero_vector!(M.manifold, X, p)
is_metric_function(::Any) = false
for mf in [
change_metric,
change_metric!,
change_representer,
change_representer!,
christoffel_symbols_first,
christoffel_symbols_second,
christoffel_symbols_second_jacobian,
det_local_metric,
einstein_tensor,
exp,
exp!,
flat!,
gaussian_curvature,
get_basis,
get_coordinates,
get_coordinates!,
get_vector,
get_vector!,
get_vectors,
inner,
inverse_local_metric,
inverse_retract,
inverse_retract!,
local_metric,
local_metric_jacobian,
log,
log!,
log_local_metric_density,
mean,
mean!,
median,
median!,
mid_point,
norm,
parallel_transport_along,
parallel_transport_along!,
parallel_transport_direction,
parallel_transport_direction!,
parallel_transport_to,
parallel_transport_to!,
retract,
retract!,
ricci_curvature,
ricci_tensor,
riemann_tensor,
riemannian_gradient,
riemannian_gradient!,
riemannian_Hessian,
riemannian_Hessian!,
sharp!,
vector_transport_along,
vector_transport_along!,
vector_transport_direction,
vector_transport_direction!,
vector_transport_to,
vector_transport_to!,
Weingarten,
Weingarten!,
]
@eval is_metric_function(::typeof($mf)) = true
end