-
Notifications
You must be signed in to change notification settings - Fork 56
/
ProductManifold.jl
1720 lines (1566 loc) · 52.4 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
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@doc raw"""
ProductManifold{𝔽,TM<:Tuple} <: AbstractManifold{𝔽}
Product manifold $M_1 × M_2 × … × M_n$ with product geometry.
# Constructor
ProductManifold(M_1, M_2, ..., M_n)
generates the product manifold $M_1 × M_2 × … × M_n$.
Alternatively, the same manifold can be contructed using the `×` operator:
`M_1 × M_2 × M_3`.
"""
struct ProductManifold{𝔽,TM<:Tuple} <: AbstractDecoratorManifold{𝔽}
manifolds::TM
end
function ProductManifold(manifolds::AbstractManifold...)
𝔽 = ManifoldsBase._unify_number_systems((number_system.(manifolds))...)
return ProductManifold{𝔽,typeof(manifolds)}(manifolds)
end
"""
getindex(M::ProductManifold, i)
M[i]
access the `i`th manifold component from the [`ProductManifold`](@ref) `M`.
"""
@inline Base.getindex(M::ProductManifold, i::Integer) = M.manifolds[i]
ProductManifold() = throw(MethodError("No method matching ProductManifold()."))
const PRODUCT_BASIS_LIST = [
VeeOrthogonalBasis,
DefaultBasis,
DefaultBasis{<:Any,TangentSpaceType},
DefaultOrthogonalBasis,
DefaultOrthogonalBasis{<:Any,TangentSpaceType},
DefaultOrthonormalBasis,
DefaultOrthonormalBasis{<:Any,TangentSpaceType},
ProjectedOrthonormalBasis{:gram_schmidt,ℝ},
ProjectedOrthonormalBasis{:svd,ℝ},
]
"""
ProductBasisData
A typed tuple to store tuples of data of stored/precomputed bases for a [`ProductManifold`](@ref).
"""
struct ProductBasisData{T<:Tuple}
parts::T
end
const PRODUCT_BASIS_LIST_CACHED = [CachedBasis]
"""
ProductMetric <: AbstractMetric
A type to represent the product of metrics for a [`ProductManifold`](@ref).
"""
struct ProductMetric <: AbstractMetric end
"""
ProductFVectorDistribution([type::VectorBundleFibers], [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<:VectorBundleFibers{<:VectorSpaceType,<:ProductManifold},
TD<:(NTuple{N,Distribution} where {N}),
TX,
} <: FVectorDistribution{TSpace,TX}
type::TSpace
x::TX
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
"""
ProductRetraction(retractions::AbstractRetractionMethod...)
Product retraction of `retractions`. Works on [`ProductManifold`](@ref).
"""
struct ProductRetraction{TR<:Tuple} <: AbstractRetractionMethod
retractions::TR
end
function ProductRetraction(retractions::AbstractRetractionMethod...)
return ProductRetraction{typeof(retractions)}(retractions)
end
"""
InverseProductRetraction(retractions::AbstractInverseRetractionMethod...)
Product inverse retraction of `inverse retractions`. Works on [`ProductManifold`](@ref).
"""
struct InverseProductRetraction{TR<:Tuple} <: AbstractInverseRetractionMethod
inverse_retractions::TR
end
function InverseProductRetraction(inverse_retractions::AbstractInverseRetractionMethod...)
return InverseProductRetraction{typeof(inverse_retractions)}(inverse_retractions)
end
@inline function allocate_result(M::ProductManifold, f)
return ArrayPartition(map(N -> allocate_result(N, f), M.manifolds))
end
function allocation_promotion_function(M::ProductManifold, f, args::Tuple)
apfs = map(MM -> allocation_promotion_function(MM, f, args), M.manifolds)
return reduce(combine_allocation_promotion_functions, apfs)
end
"""
ProductVectorTransport(methods::AbstractVectorTransportMethod...)
Product vector transport type of `methods`. Works on [`ProductManifold`](@ref).
"""
struct ProductVectorTransport{TR<:Tuple} <: AbstractVectorTransportMethod
methods::TR
end
function ProductVectorTransport(methods::AbstractVectorTransportMethod...)
return ProductVectorTransport{typeof(methods)}(methods)
end
function active_traits(f, ::ProductManifold, args...)
return merge_traits(IsDefaultMetric(ProductMetric()))
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
function allocate_coordinates(M::AbstractManifold, p::ArrayPartition, T, n::Int)
return allocate_coordinates(M, p.x[1], T, n)
end
"""
change_representer(M::ProductManifold, ::AbstractMetric, p, X)
Since the metric on a product manifold decouples, the change of a representer can be done elementwise
"""
change_representer(::ProductManifold, ::AbstractMetric, ::Any, ::Any)
function change_representer!(M::ProductManifold, Y, G::AbstractMetric, p, X)
map(
(m, y, P, x) -> change_representer!(m, y, G, P, x),
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, X),
)
return Y
end
"""
change_metric(M::ProductManifold, ::AbstractMetric, p, X)
Since the metric on a product manifold decouples, the change of metric can be done elementwise.
"""
change_metric(::ProductManifold, ::AbstractMetric, ::Any, ::Any)
function change_metric!(M::ProductManifold, Y, G::AbstractMetric, p, X)
map(
(m, y, P, x) -> change_metric!(m, y, G, P, x),
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, X),
)
return Y
end
"""
check_point(M::ProductManifold, p; kwargs...)
Check whether `p` is a valid point on the [`ProductManifold`](@ref) `M`.
If `p` is not a point on `M` a [`CompositeManifoldError`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/functions.html#ManifoldsBase.CompositeManifoldError).consisting of all error messages of the
components, for which the tests fail is returned.
The tolerance for the last test can be set using the `kwargs...`.
"""
function check_point(M::ProductManifold, p::Union{ProductRepr,ArrayPartition}; kwargs...)
ts = ziptuples(Tuple(1:length(M.manifolds)), M.manifolds, submanifold_components(M, p))
e = [(t[1], check_point(t[2:end]...; kwargs...)) for t in ts]
errors = filter((x) -> !(x[2] === nothing), e)
cerr = [ComponentManifoldError(er...) for er in errors]
(length(errors) > 1) && return CompositeManifoldError(cerr)
(length(errors) == 1) && return cerr[1]
return nothing
end
function check_point(M::ProductManifold, p; kwargs...)
return DomainError(
typeof(p),
"The point $p is not a point on $M, since currently only ProductRepr and ArrayPartition are supported types for points on arbitrary product manifolds",
)
end
"""
check_size(M::ProductManifold, p; kwargs...)
Check whether `p` is of valid size on the [`ProductManifold`](@ref) `M`.
If `p` has components of wrong size a [`CompositeManifoldError`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/functions.html#ManifoldsBase.CompositeManifoldError).consisting of all error messages of the
components, for which the tests fail is returned.
The tolerance for the last test can be set using the `kwargs...`.
"""
function check_size(M::ProductManifold, p::Union{ProductRepr,ArrayPartition})
ts = ziptuples(Tuple(1:length(M.manifolds)), M.manifolds, submanifold_components(M, p))
e = [(t[1], check_size(t[2:end]...)) for t in ts]
errors = filter((x) -> !(x[2] === nothing), e)
cerr = [ComponentManifoldError(er...) for er in errors]
(length(errors) > 1) && return CompositeManifoldError(cerr)
(length(errors) == 1) && return cerr[1]
return nothing
end
function check_size(M::ProductManifold, p; kwargs...)
return DomainError(
typeof(p),
"The point $p is not a point on $M, since currently only ProductRepr and ArrayPartition are supported types for points on arbitrary product manifolds",
)
end
function check_size(
M::ProductManifold,
p::Union{ProductRepr,ArrayPartition},
X::Union{ProductRepr,ArrayPartition},
)
ts = ziptuples(
Tuple(1:length(M.manifolds)),
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)
e = [(t[1], check_size(t[2:end]...)) for t in ts]
errors = filter(x -> !(x[2] === nothing), e)
cerr = [ComponentManifoldError(er...) for er in errors]
(length(errors) > 1) && return CompositeManifoldError(cerr)
(length(errors) == 1) && return cerr[1]
return nothing
end
function check_size(M::ProductManifold, p, X; kwargs...)
return DomainError(
typeof(X),
"The vector $X is not a tangent vector to any tangent space on $M, since currently only ProductRepr and ArrayPartition are supported types for tangent vectors on arbitrary product manifolds",
)
end
"""
check_vector(M::ProductManifold, p, X; kwargs... )
Check whether `X` is a tangent vector to `p` on the [`ProductManifold`](@ref)
`M`, i.e. all projections to base manifolds must be respective tangent vectors.
If `X` is not a tangent vector to `p` on `M` a [`CompositeManifoldError`](https://juliamanifolds.github.io/ManifoldsBase.jl/stable/functions.html#ManifoldsBase.CompositeManifoldError).consisting
of all error messages of the components, for which the tests fail is returned.
The tolerance for the last test can be set using the `kwargs...`.
"""
function check_vector(
M::ProductManifold,
p::Union{ProductRepr,ArrayPartition},
X::Union{ProductRepr,ArrayPartition};
kwargs...,
)
ts = ziptuples(
Tuple(1:length(M.manifolds)),
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)
e = [(t[1], check_vector(t[2:end]...; kwargs...)) for t in ts]
errors = filter(x -> !(x[2] === nothing), e)
cerr = [ComponentManifoldError(er...) for er in errors]
(length(errors) > 1) && return CompositeManifoldError(cerr)
(length(errors) == 1) && return cerr[1]
return nothing
end
function check_vector(M::ProductManifold, p, X; kwargs...)
return DomainError(
typeof(X),
"The vector $X is not a tangent vector to any tangent space on $M, since currently only ProductRepr and ArrayPartition are supported types for tangent vectors on arbitrary product manifolds",
)
end
for TP in [ProductRepr, ArrayPartition]
eval(
quote
function copyto!(M::ProductManifold, q::$TP, p::$TP)
map(
copyto!,
M.manifolds,
submanifold_components(q),
submanifold_components(p),
)
return q
end
function copyto!(M::ProductManifold, Y::$TP, p::$TP, X::$TP)
map(
copyto!,
M.manifolds,
submanifold_components(Y),
submanifold_components(p),
submanifold_components(X),
)
return Y
end
end,
)
end
@doc raw"""
cross(M, N)
cross(M1, M2, M3,...)
Return the [`ProductManifold`](@ref) For two `AbstractManifold`s `M` and `N`,
where for the case that one of them is a [`ProductManifold`](@ref) itself,
the other is either prepended (if `N` is a product) or appenden (if `M`) is.
If both are product manifold, they are combined into one product manifold,
keeping the order.
For the case that more than one is a product manifold of these is build with the
same approach as above
"""
cross(::AbstractManifold...)
LinearAlgebra.cross(M1::AbstractManifold, M2::AbstractManifold) = ProductManifold(M1, M2)
function LinearAlgebra.cross(M1::ProductManifold, M2::AbstractManifold)
return ProductManifold(M1.manifolds..., M2)
end
function LinearAlgebra.cross(M1::AbstractManifold, M2::ProductManifold)
return ProductManifold(M1, M2.manifolds...)
end
function LinearAlgebra.cross(M1::ProductManifold, M2::ProductManifold)
return ProductManifold(M1.manifolds..., M2.manifolds...)
end
function default_retraction_method(M::ProductManifold)
return ProductRetraction(map(default_retraction_method, M.manifolds)...)
end
function default_retraction_method(M::ProductManifold, ::Type{T}) where {T<:ArrayPartition}
return ProductRetraction(
map(default_retraction_method, M.manifolds, T.parameters[2].parameters)...,
)
end
function default_retraction_method(M::ProductManifold, ::Type{T}) where {T<:ProductRepr}
return ProductRetraction(
map(default_retraction_method, M.manifolds, T.parameters[1].parameters)...,
)
end
function default_inverse_retraction_method(M::ProductManifold)
return InverseProductRetraction(map(default_inverse_retraction_method, M.manifolds)...)
end
function default_inverse_retraction_method(
M::ProductManifold,
::Type{T},
) where {T<:ArrayPartition}
return InverseProductRetraction(
map(default_inverse_retraction_method, M.manifolds, T.parameters[2].parameters)...,
)
end
function default_inverse_retraction_method(
M::ProductManifold,
::Type{T},
) where {T<:ProductRepr}
return InverseProductRetraction(
map(default_inverse_retraction_method, M.manifolds, T.parameters[1].parameters)...,
)
end
function default_vector_transport_method(M::ProductManifold)
return ProductVectorTransport(map(default_vector_transport_method, M.manifolds)...)
end
function default_vector_transport_method(
M::ProductManifold,
::Type{T},
) where {T<:ArrayPartition}
return ProductVectorTransport(
map(default_vector_transport_method, M.manifolds, T.parameters[2].parameters)...,
)
end
function default_vector_transport_method(
M::ProductManifold,
::Type{T},
) where {T<:ProductRepr}
return ProductVectorTransport(
map(default_vector_transport_method, M.manifolds, T.parameters[1].parameters)...,
)
end
@doc raw"""
distance(M::ProductManifold, p, q)
Compute the distance between two points `p` and `q` on the [`ProductManifold`](@ref) `M`, which is
the 2-norm of the elementwise distances on the internal manifolds that build `M`.
"""
function distance(M::ProductManifold, p, q)
return sqrt(
sum(
map(
distance,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
) .^ 2,
),
)
end
@doc raw"""
exp(M::ProductManifold, p, X)
compute the exponential map from `p` in the direction of `X` on the [`ProductManifold`](@ref) `M`,
which is the elementwise exponential map on the internal manifolds that build `M`.
"""
exp(::ProductManifold, ::Any...)
function Base.exp(M::ProductManifold, p::ProductRepr, X::ProductRepr)
return ProductRepr(
map(
exp,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)...,
)
end
function Base.exp(M::ProductManifold, p::ArrayPartition, X::ArrayPartition)
return ArrayPartition(
map(
exp,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)...,
)
end
function Base.exp(M::ProductManifold, p::ProductRepr, X::ProductRepr, t::Number)
return ProductRepr(
map(
(N, pc, Xc) -> exp(N, pc, Xc, t),
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)...,
)
end
function Base.exp(M::ProductManifold, p::ArrayPartition, X::ArrayPartition, t::Number)
return ArrayPartition(
map(
(N, pc, Xc) -> exp(N, pc, Xc, t),
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
)...,
)
end
function exp!(M::ProductManifold, q, p, X)
map(
exp!,
M.manifolds,
submanifold_components(M, q),
submanifold_components(M, p),
submanifold_components(M, X),
)
return q
end
function exp!(M::ProductManifold, q, p, X, t::Number)
map(
(N, qc, pc, Xc) -> exp!(N, qc, pc, Xc, t),
M.manifolds,
submanifold_components(M, q),
submanifold_components(M, p),
submanifold_components(M, X),
)
return q
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`](@ref) `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 get_basis(M::ProductManifold, p, B::AbstractBasis)
parts = map(t -> get_basis(t..., B), ziptuples(M.manifolds, submanifold_components(p)))
return CachedBasis(B, ProductBasisData(parts))
end
function get_basis(M::ProductManifold, p, B::CachedBasis)
return invoke(get_basis, Tuple{AbstractManifold,Any,CachedBasis}, M, p, B)
end
function get_basis(M::ProductManifold, p, B::DiagonalizingOrthonormalBasis)
vs = map(
ziptuples(
M.manifolds,
submanifold_components(p),
submanifold_components(B.frame_direction),
),
) do t
return get_basis(t[1], t[2], DiagonalizingOrthonormalBasis(t[3]))
end
return CachedBasis(B, ProductBasisData(vs))
end
"""
get_component(M::ProductManifold, p, i)
Get the `i`th component of a point `p` on a [`ProductManifold`](@ref) `M`.
"""
function get_component(M::ProductManifold, p, i)
return submanifold_component(M, p, i)
end
function get_coordinates(M::ProductManifold, p, X, B::AbstractBasis)
reps = map(
t -> get_coordinates(t..., B),
ziptuples(M.manifolds, submanifold_components(M, p), submanifold_components(M, X)),
)
return vcat(reps...)
end
function get_coordinates(
M::ProductManifold,
p,
X,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
reps = map(
get_coordinates,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
B.data.parts,
)
return vcat(reps...)
end
function get_coordinates!(M::ProductManifold, Xⁱ, p, X, B::AbstractBasis)
dim = manifold_dimension(M)
@assert length(Xⁱ) == dim
i = one(dim)
ts = ziptuples(M.manifolds, submanifold_components(M, p), submanifold_components(M, X))
for t in ts
SM = first(t)
dim = manifold_dimension(SM)
tXⁱ = @inbounds view(Xⁱ, i:(i + dim - 1))
get_coordinates!(SM, tXⁱ, Base.tail(t)..., B)
i += dim
end
return Xⁱ
end
function get_coordinates!(
M::ProductManifold,
Xⁱ,
p,
X,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
dim = manifold_dimension(M)
@assert length(Xⁱ) == dim
i = one(dim)
ts = ziptuples(
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
B.data.parts,
)
for t in ts
SM = first(t)
dim = manifold_dimension(SM)
tXⁱ = @inbounds view(Xⁱ, i:(i + dim - 1))
get_coordinates!(SM, tXⁱ, Base.tail(t)...)
i += dim
end
return Xⁱ
end
function _get_dim_ranges(dims::NTuple{N,Any}) where {N}
dims_acc = accumulate(+, vcat(1, SVector(dims)))
return ntuple(i -> (dims_acc[i]:(dims_acc[i] + dims[i] - 1)), Val(N))
end
for TP in [ProductRepr, ArrayPartition]
eval(
quote
function get_vector(
M::ProductManifold,
p::$TP,
Xⁱ,
B::AbstractBasis{𝔽,TangentSpaceType},
) where {𝔽}
dims = map(manifold_dimension, M.manifolds)
@assert length(Xⁱ) == sum(dims)
dim_ranges = _get_dim_ranges(dims)
tXⁱ = map(dr -> (@inbounds view(Xⁱ, dr)), dim_ranges)
ts = ziptuples(M.manifolds, submanifold_components(M, p), tXⁱ)
return $TP(map((@inline t -> get_vector(t..., B)), ts))
end
function get_vector(
M::ProductManifold,
p::$TP,
Xⁱ,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
dims = map(manifold_dimension, M.manifolds)
@assert length(Xⁱ) == sum(dims)
dim_ranges = _get_dim_ranges(dims)
tXⁱ = map(dr -> (@inbounds view(Xⁱ, dr)), dim_ranges)
ts =
ziptuples(M.manifolds, submanifold_components(M, p), tXⁱ, B.data.parts)
return $TP(map((@inline t -> get_vector(t...)), ts))
end
end,
)
end
function get_vector!(M::ProductManifold, X, p, Xⁱ, B::AbstractBasis)
dims = map(manifold_dimension, M.manifolds)
@assert length(Xⁱ) == sum(dims)
dim_ranges = _get_dim_ranges(dims)
tXⁱ = map(dr -> (@inbounds view(Xⁱ, dr)), dim_ranges)
ts = ziptuples(
M.manifolds,
submanifold_components(M, X),
submanifold_components(M, p),
tXⁱ,
)
map(ts) do t
return get_vector!(t..., B)
end
return X
end
function get_vector!(
M::ProductManifold,
X,
p,
Xⁱ,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
dims = map(manifold_dimension, M.manifolds)
@assert length(Xⁱ) == sum(dims)
dim_ranges = _get_dim_ranges(dims)
tXⁱ = map(dr -> (@inbounds view(Xⁱ, dr)), dim_ranges)
ts = ziptuples(
M.manifolds,
submanifold_components(M, X),
submanifold_components(M, p),
tXⁱ,
B.data.parts,
)
map(ts) do t
return get_vector!(t...)
end
return X
end
function get_vectors(
M::ProductManifold,
p::ProductRepr,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
N = number_of_components(M)
xparts = submanifold_components(p)
BVs = map(t -> get_vectors(t...), ziptuples(M.manifolds, xparts, B.data.parts))
zero_tvs = map(t -> zero_vector(t...), ziptuples(M.manifolds, xparts))
vs = typeof(ProductRepr(zero_tvs...))[]
for i in 1:N, k in 1:length(BVs[i])
push!(vs, ProductRepr(zero_tvs[1:(i - 1)]..., BVs[i][k], zero_tvs[(i + 1):end]...))
end
return vs
end
function get_vectors(
M::ProductManifold,
p::ArrayPartition,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:ProductBasisData},
) where {𝔽}
N = number_of_components(M)
xparts = submanifold_components(p)
BVs = map(t -> get_vectors(t...), ziptuples(M.manifolds, xparts, B.data.parts))
zero_tvs = map(t -> zero_vector(t...), ziptuples(M.manifolds, xparts))
vs = typeof(ArrayPartition(zero_tvs...))[]
for i in 1:N, k in 1:length(BVs[i])
push!(
vs,
ArrayPartition(zero_tvs[1:(i - 1)]..., BVs[i][k], zero_tvs[(i + 1):end]...),
)
end
return vs
end
"""
getindex(p, M::ProductManifold, i::Union{Integer,Colon,AbstractVector})
p[M::ProductManifold, i]
Access the element(s) at index `i` of a point `p` on a [`ProductManifold`](@ref) `M` by
linear indexing.
See also [Array Indexing](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1) in Julia.
"""
Base.@propagate_inbounds function Base.getindex(
p::ProductRepr,
M::ProductManifold,
i::Union{Integer,Colon,AbstractVector,Val},
)
return get_component(M, p, i)
end
Base.@propagate_inbounds function Base.getindex(
p::ArrayPartition,
M::ProductManifold,
i::Union{Integer,Colon,AbstractVector,Val},
)
return get_component(M, p, i)
end
@doc raw"""
injectivity_radius(M::ProductManifold)
injectivity_radius(M::ProductManifold, x)
Compute the injectivity radius on the [`ProductManifold`](@ref), which is the
minimum of the factor manifolds.
"""
injectivity_radius(::ProductManifold, ::Any...)
function injectivity_radius(M::ProductManifold, p)
return min(map(injectivity_radius, M.manifolds, submanifold_components(M, p))...)
end
function injectivity_radius(M::ProductManifold, p, m::AbstractRetractionMethod)
return min(
map(
(lM, lp) -> injectivity_radius(lM, lp, m),
M.manifolds,
submanifold_components(M, p),
)...,
)
end
function injectivity_radius(M::ProductManifold, p, m::ProductRetraction)
return min(
map(
(lM, lp, lm) -> injectivity_radius(lM, lp, lm),
M.manifolds,
submanifold_components(M, p),
m.retractions,
)...,
)
end
injectivity_radius(M::ProductManifold) = min(map(injectivity_radius, M.manifolds)...)
function injectivity_radius(M::ProductManifold, m::AbstractRetractionMethod)
return min(map(manif -> injectivity_radius(manif, m), M.manifolds)...)
end
function injectivity_radius(M::ProductManifold, m::ProductRetraction)
return min(map((lM, lm) -> injectivity_radius(lM, lm), M.manifolds, m.retractions)...)
end
@doc raw"""
inner(M::ProductManifold, p, X, Y)
compute the inner product of two tangent vectors `X`, `Y` from the tangent space
at `p` on the [`ProductManifold`](@ref) `M`, which is just the sum of the
internal manifolds that build `M`.
"""
function inner(M::ProductManifold, p, X, Y)
subproducts = map(
inner,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
submanifold_components(M, Y),
)
return sum(subproducts)
end
@doc raw"""
inverse_retract(M::ProductManifold, p, q, m::InverseProductRetraction)
Compute the inverse retraction from `p` with respect to `q` on the [`ProductManifold`](@ref)
`M` using an [`InverseProductRetraction`](@ref), which by default encapsulates a inverse
retraction for each manifold of the product. Then this method is performed elementwise,
so the encapsulated inverse retraction methods have to be available per factor.
"""
inverse_retract(::ProductManifold, ::Any, ::Any, ::Any, ::InverseProductRetraction)
for TP in [ProductRepr, ArrayPartition]
eval(
quote
function inverse_retract(
M::ProductManifold,
p::$TP,
q::$TP,
method::InverseProductRetraction,
)
return $TP(
map(
inverse_retract,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
method.inverse_retractions,
),
)
end
end,
)
end
function inverse_retract!(M::ProductManifold, Y, p, q, method::InverseProductRetraction)
map(
inverse_retract!,
M.manifolds,
submanifold_components(M, Y),
submanifold_components(M, p),
submanifold_components(M, q),
method.inverse_retractions,
)
return Y
end
function _isapprox(M::ProductManifold, p, q; kwargs...)
return all(
t -> isapprox(t...; kwargs...),
ziptuples(M.manifolds, submanifold_components(M, p), submanifold_components(M, q)),
)
end
function _isapprox(M::ProductManifold, p, X, Y; kwargs...)
return all(
t -> isapprox(t...; kwargs...),
ziptuples(
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
submanifold_components(M, Y),
),
)
end
"""
is_flat(::ProductManifold)
Return true if and only if all component manifolds of [`ProductManifold`](@ref) `M` are flat.
"""
function is_flat(M::ProductManifold)
return all(is_flat, M.manifolds)
end
@doc raw"""
log(M::ProductManifold, p, q)
Compute the logarithmic map from `p` to `q` on the [`ProductManifold`](@ref) `M`,
which can be computed using the logarithmic maps of the manifolds elementwise.
"""
log(::ProductManifold, ::Any...)
function Base.log(M::ProductManifold, p::ProductRepr, q::ProductRepr)
return ProductRepr(
map(
log,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
)...,
)
end
function Base.log(M::ProductManifold, p::ArrayPartition, q::ArrayPartition)
return ArrayPartition(
map(
log,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
)...,
)
end
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
function log!(M::ProductManifold, X, p, q)
map(
log!,
M.manifolds,
submanifold_components(M, X),
submanifold_components(M, p),
submanifold_components(M, q),
)
return X
end
@doc raw"""
manifold_dimension(M::ProductManifold)
Return the manifold dimension of the [`ProductManifold`](@ref), which is the sum of the
manifold dimensions the product is made of.
"""
manifold_dimension(M::ProductManifold) = mapreduce(manifold_dimension, +, M.manifolds)
"""
manifold_dimension(M::ProductManifold)
Return the volume of [`ProductManifold`](@ref) `M`, i.e. product of volumes of the
manifolds `M` is constructed from.
"""
manifold_volume(M::ProductManifold) = mapreduce(manifold_volume, *, M.manifolds)
function mid_point!(M::ProductManifold, q, p1, p2)
map(
mid_point!,
M.manifolds,
submanifold_components(M, q),
submanifold_components(M, p1),
submanifold_components(M, p2),
)
return q
end
@doc raw"""
norm(M::ProductManifold, p, X)
Compute the norm of `X` from the tangent space of `p` on the [`ProductManifold`](@ref),
i.e. from the element wise norms the 2-norm is computed.
"""
function LinearAlgebra.norm(M::ProductManifold, p, X)
norms_squared = (
map(
norm,
M.manifolds,
submanifold_components(M, p),