/
PowerManifold.jl
1694 lines (1553 loc) · 51.2 KB
/
PowerManifold.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
"""
AbstractPowerRepresentation
An abstract representation type of points and tangent vectors on a power manifold.
"""
abstract type AbstractPowerRepresentation end
"""
NestedPowerRepresentation
Representation of points and tangent vectors on a power manifold using arrays
of size equal to `TSize` of a [`PowerManifold`](@ref).
Each element of such array stores a single point or tangent vector.
For modifying operations, each element of the outer array is modified in-place, differently
than in [`NestedReplacingPowerRepresentation`](@ref).
"""
struct NestedPowerRepresentation <: AbstractPowerRepresentation end
"""
NestedReplacingPowerRepresentation
Representation of points and tangent vectors on a power manifold using arrays
of size equal to `TSize` of a [`PowerManifold`](@ref).
Each element of such array stores a single point or tangent vector.
For modifying operations, each element of the outer array is replaced using non-modifying
operations, differently than for [`NestedReplacingPowerRepresentation`](@ref).
"""
struct NestedReplacingPowerRepresentation <: AbstractPowerRepresentation end
@doc raw"""
AbstractPowerManifold{𝔽,M,TPR} <: AbstractManifold{𝔽}
An abstract [`AbstractManifold`](@ref) to represent manifolds that are build as powers
of another [`AbstractManifold`](@ref) `M` with representation type `TPR`, a subtype of
[`AbstractPowerRepresentation`](@ref).
"""
abstract type AbstractPowerManifold{
𝔽,
M<:AbstractManifold{𝔽},
TPR<:AbstractPowerRepresentation,
} <: AbstractManifold{𝔽} end
@doc raw"""
PowerManifold{𝔽,TM<:AbstractManifold,TSize,TPR<:AbstractPowerRepresentation} <: AbstractPowerManifold{𝔽,TM}
The power manifold ``\mathcal M^{n_1× n_2 × … × n_d}`` with power geometry.
`TSize` defines the number of elements along each axis, either statically using
`TypeParameter` or storing it in a field.
For example, a manifold-valued time series would be represented by a power manifold with
``d`` equal to 1 and ``n_1`` equal to the number of samples. A manifold-valued image
(for example in diffusion tensor imaging) would be represented by a two-axis power
manifold (``d=2``) with ``n_1`` and ``n_2`` equal to width and height of the image.
While the size of the manifold is static, points on the power manifold
would not be represented by statically-sized arrays.
# Constructor
PowerManifold(M::PowerManifold, N_1, N_2, ..., N_d; parameter::Symbol=:field)
PowerManifold(M::AbstractManifold, NestedPowerRepresentation(), N_1, N_2, ..., N_d; parameter::Symbol=:field)
M^(N_1, N_2, ..., N_d)
Generate the power manifold ``M^{N_1 × N_2 × … × N_d}``.
By default, a [`PowerManifold`](@ref) is expanded further, i.e. for `M=PowerManifold(N, 3)`
`PowerManifold(M, 2)` is equivalent to `PowerManifold(N, 3, 2)`. Points are then 3×2 matrices
of points on `N`.
Providing a [`NestedPowerRepresentation`](@ref) as the second argument to the constructor
can be used to nest manifold, i.e. `PowerManifold(M, NestedPowerRepresentation(), 2)`
represents vectors of length 2 whose elements are vectors of length 3 of points on N
in a nested array representation.
Since there is no default [`AbstractPowerRepresentation`](@ref) within this interface, the
`^` operator is only available for `PowerManifold`s and concatenates dimensions.
`parameter`: whether a type parameter should be used to store `n`. By default size
is stored in a field. Value can either be `:field` or `:type`.
"""
struct PowerManifold{𝔽,TM<:AbstractManifold{𝔽},TSize,TPR<:AbstractPowerRepresentation} <:
AbstractPowerManifold{𝔽,TM,TPR}
manifold::TM
size::TSize
end
"""
_parameter_symbol(M::PowerManifold)
Return `:field` if size of [`PowerManifold`](@ref) `M` is stored in a field and `:type`
if in a `TypeParameter`.
"""
_parameter_symbol(::PowerManifold) = :field
function _parameter_symbol(
::PowerManifold{𝔽,<:AbstractManifold{𝔽},<:TypeParameter},
) where {𝔽}
return :type
end
function PowerManifold(
M::AbstractManifold{𝔽},
::TPR,
size::Integer...;
parameter::Symbol = :field,
) where {𝔽,TPR<:AbstractPowerRepresentation}
size_w = wrap_type_parameter(parameter, size)
return PowerManifold{𝔽,typeof(M),typeof(size_w),TPR}(M, size_w)
end
function PowerManifold(
M::PowerManifold{𝔽,TM,TSize,TPR},
size::Integer...;
parameter::Symbol = _parameter_symbol(M),
) where {𝔽,TM<:AbstractManifold{𝔽},TSize,TPR<:AbstractPowerRepresentation}
size_w = wrap_type_parameter(parameter, (get_parameter(M.size)..., size...))
return PowerManifold{𝔽,TM,typeof(size_w),TPR}(M.manifold, size_w)
end
function PowerManifold(
M::PowerManifold{𝔽,TM},
::TPR,
size::Integer...;
parameter::Symbol = _parameter_symbol(M),
) where {𝔽,TM<:AbstractManifold{𝔽},TPR<:AbstractPowerRepresentation}
size_w = wrap_type_parameter(parameter, (get_parameter(M.size)..., size...))
return PowerManifold{𝔽,TM,typeof(size_w),TPR}(M.manifold, size_w)
end
function PowerManifold(
M::PowerManifold{𝔽},
::TPR,
size::Integer...;
parameter::Symbol = _parameter_symbol(M),
) where {𝔽,TPR<:Union{NestedPowerRepresentation,NestedReplacingPowerRepresentation}}
size_w = wrap_type_parameter(parameter, size)
return PowerManifold{𝔽,typeof(M),typeof(size_w),TPR}(M, size_w)
end
"""
PowerBasisData{TB<:AbstractArray}
Data storage for an array of basis data.
"""
struct PowerBasisData{TB<:AbstractArray}
bases::TB
end
const PowerManifoldNested =
AbstractPowerManifold{𝔽,<:AbstractManifold{𝔽},NestedPowerRepresentation} where {𝔽}
const PowerManifoldNestedReplacing = AbstractPowerManifold{
𝔽,
<:AbstractManifold{𝔽},
NestedReplacingPowerRepresentation,
} where {𝔽}
# _access_nested(::AbstractManifold, x, i::Tuple) can be overloaded to achieve
# manifold-specific nested element access (for example to `Identity` on power manifolds).
@inline _access_nested(M::AbstractManifold, x, i::Int) = _access_nested(M, x, (i,))
@inline _access_nested(::AbstractManifold, x, i::Tuple) = _access_nested(x, i)
@inline _access_nested(x, i::Tuple) = x[i...]
function Base.:^(
M::PowerManifold{
𝔽,
TM,
TSize,
<:Union{NestedPowerRepresentation,NestedReplacingPowerRepresentation},
},
size::Integer...,
) where {𝔽,TM<:AbstractManifold{𝔽},TSize}
return PowerManifold(M, size...)
end
"""
_allocate_access_nested(M::PowerManifoldNested, y, i)
Helper function for `allocate_result` on `PowerManifoldNested`. In allocation `y` can be
a number in which case `_access_nested` wouldn't work.
"""
_allocate_access_nested(M::PowerManifoldNested, y, i) = _access_nested(M, y, i)
_allocate_access_nested(::PowerManifoldNested, y::Number, i) = y
function allocate_result(M::PowerManifoldNested, f, x...)
if representation_size(M.manifold) === () && length(x) > 0
return allocate(M, x[1])
else
return [
allocate_result(
M.manifold,
f,
map(y -> _allocate_access_nested(M, y, i), x)...,
) for i in get_iterator(M)
]
end
end
# avoid ambituities - though usually not used
function allocate_result(
M::PowerManifoldNested,
f::typeof(get_coordinates),
p,
X,
B::AbstractBasis,
)
return invoke(
allocate_result,
Tuple{AbstractManifold,typeof(get_coordinates),Any,Any,AbstractBasis},
M,
f,
p,
X,
B,
)
end
function allocate_result(M::PowerManifoldNestedReplacing, f, x...)
if length(x) == 0
return [allocate_result(M.manifold, f) for _ in get_iterator(M)]
else
return copy(x[1])
end
end
# the following is not used but necessary to avoid ambiguities
function allocate_result(
M::PowerManifoldNestedReplacing,
f::typeof(get_coordinates),
p,
X,
B::AbstractBasis,
)
return invoke(
allocate_result,
Tuple{AbstractManifold,typeof(get_coordinates),Any,Any,AbstractBasis},
M,
f,
p,
X,
B,
)
end
function allocate_result(M::PowerManifoldNested, f::typeof(get_vector), p, X)
return [
allocate_result(M.manifold, f, _access_nested(M, p, i)) for i in get_iterator(M)
]
end
function allocate_result(::PowerManifoldNestedReplacing, ::typeof(get_vector), p, X)
return copy(p)
end
function allocation_promotion_function(M::AbstractPowerManifold, f, args::Tuple)
return allocation_promotion_function(M.manifold, f, args)
end
"""
change_representer(M::AbstractPowerManifold, ::AbstractMetric, p, X)
Since the metric on a power manifold decouples, the change of a representer can be done elementwise
"""
change_representer(::AbstractPowerManifold, ::AbstractMetric, ::Any, ::Any)
function change_representer!(M::AbstractPowerManifold, Y, G::AbstractMetric, p, X)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
change_representer!(
M.manifold,
_write(M, rep_size, Y, i),
G,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
)
end
return Y
end
"""
change_metric(M::AbstractPowerManifold, ::AbstractMetric, p, X)
Since the metric on a power manifold decouples, the change of metric can be done elementwise.
"""
change_metric(M::AbstractPowerManifold, ::AbstractMetric, ::Any, ::Any)
function change_metric!(M::AbstractPowerManifold, Y, G::AbstractMetric, p, X)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
change_metric!(
M.manifold,
_write(M, rep_size, Y, i),
G,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
)
end
return Y
end
"""
check_point(M::AbstractPowerManifold, p; kwargs...)
Check whether `p` is a valid point on an [`AbstractPowerManifold`](@ref) `M`,
i.e. each element of `p` has to be a valid point on the base manifold.
If `p` is not a point on `M` a [`CompositeManifoldError`](@ref) 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::AbstractPowerManifold, p; kwargs...)
rep_size = representation_size(M.manifold)
e = [
(i, check_point(M.manifold, _read(M, rep_size, p, i); kwargs...)) for
i in get_iterator(M)
]
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
"""
check_power_size(M, p)
check_power_size(M, p, X)
Check whether `p`` has the right size to represent points on `M`` generically, i.e. just
checking the overall sizes, not the individual ones per manifold.
"""
function check_power_size(M::AbstractPowerManifold, p)
d = prod(representation_size(M.manifold)) * prod(power_dimensions(M))
(d != length(p)) && return DomainError(
length(p),
"The point $p can not be a point on $M, since its number of elements does not match the required overall representation size ($d)",
)
return nothing
end
function check_power_size(M::Union{PowerManifoldNested,PowerManifoldNestedReplacing}, p)
d = prod(power_dimensions(M))
(d != length(p)) && return DomainError(
length(p),
"The point $p can not be a point on $M, since its number of elements does not match the power dimensions ($d)",
)
return nothing
end
function check_power_size(M::AbstractPowerManifold, p, X)
d = prod(representation_size(M.manifold)) * prod(power_dimensions(M))
(d != length(X)) && return DomainError(
length(X),
"The tangent vector $X can not belong to a trangent space at on $M, since its number of elements does not match the required overall representation size ($d)",
)
return nothing
end
function check_power_size(M::Union{PowerManifoldNested,PowerManifoldNestedReplacing}, p, X)
d = prod(power_dimensions(M))
(d != length(X)) && return DomainError(
length(X),
"The point $p can not be a point on $M, since its number of elements does not match the power dimensions ($d)",
)
return nothing
end
function check_size(M::AbstractPowerManifold, p)
cps = check_power_size(M, p)
(cps === nothing) || return cps
rep_size = representation_size(M.manifold)
e = [(i, check_size(M.manifold, _read(M, rep_size, p, i))) for i in get_iterator(M)]
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::AbstractPowerManifold, p, X)
cps = check_power_size(M, p, X)
(cps === nothing) || return cps
rep_size = representation_size(M.manifold)
e = [
(i, check_size(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i);)) for i in get_iterator(M)
]
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
"""
check_vector(M::AbstractPowerManifold, p, X; kwargs... )
Check whether `X` is a tangent vector to `p` an the [`AbstractPowerManifold`](@ref)
`M`, i.e. atfer [`check_point`](@ref)`(M, p)`, and all projections to
base manifolds must be respective tangent vectors.
If `X` is not a tangent vector to `p` on `M` a [`CompositeManifoldError`](@ref) 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::AbstractPowerManifold, p, X; kwargs...)
rep_size = representation_size(M.manifold)
e = [
(
i,
check_vector(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i);
kwargs...,
),
) for i in get_iterator(M)
]
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
@doc raw"""
copyto!(M::PowerManifoldNested, q, p)
Copy the values elementwise, i.e. call `copyto!(M.manifold, b, a)` for all elements `a` and
`b` of `p` and `q`, respectively.
"""
function copyto!(M::PowerManifoldNested, q, p)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
copyto!(M.manifold, _write(M, rep_size, q, i), _read(M, rep_size, p, i))
end
return q
end
@doc raw"""
copyto!(M::PowerManifoldNested, Y, p, X)
Copy the values elementwise, i.e. call `copyto!(M.manifold, B, a, A)` for all elements
`A`, `a` and `B` of `X`, `p`, and `Y`, respectively.
"""
function copyto!(M::PowerManifoldNested, Y, p, X)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
copyto!(
M.manifold,
_write(M, rep_size, Y, i),
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
)
end
return Y
end
@doc raw"""
default_retraction_method(M::PowerManifold)
Use the default retraction method of the internal `M.manifold` also in defaults of
functions defined for the power manifold, meaning that this is used elementwise.
"""
function default_retraction_method(M::PowerManifold)
return default_retraction_method(M.manifold)
end
function default_retraction_method(M::PowerManifold, t::Type)
return default_retraction_method(M.manifold, eltype(t))
end
@doc raw"""
default_inverse_retraction_method(M::PowerManifold)
Use the default inverse retraction method of the internal `M.manifold` also in defaults of
functions defined for the power manifold, meaning that this is used elementwise.
"""
function default_inverse_retraction_method(M::PowerManifold)
return default_inverse_retraction_method(M.manifold)
end
function default_inverse_retraction_method(M::PowerManifold, t::Type)
return default_inverse_retraction_method(M.manifold, eltype(t))
end
@doc raw"""
default_vector_transport_method(M::PowerManifold)
Use the default vector transport method of the internal `M.manifold` also in defaults of
functions defined for the power manifold, meaning that this is used elementwise.
"""
function default_vector_transport_method(M::PowerManifold)
return default_vector_transport_method(M.manifold)
end
function default_vector_transport_method(M::PowerManifold, t::Type)
return default_vector_transport_method(M.manifold, eltype(t))
end
@doc raw"""
distance(M::AbstractPowerManifold, p, q)
Compute the distance between `q` and `p` on an [`AbstractPowerManifold`](@ref),
i.e. from the element wise distances the Forbenius norm is computed.
"""
function distance(M::AbstractPowerManifold, p, q)
sum_squares = zero(number_eltype(p))
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
sum_squares +=
distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i))^2
end
return sqrt(sum_squares)
end
@doc raw"""
exp(M::AbstractPowerManifold, p, X)
Compute the exponential map from `p` in direction `X` on the [`AbstractPowerManifold`](@ref) `M`,
which can be computed using the base manifolds exponential map elementwise.
"""
exp(::AbstractPowerManifold, ::Any...)
function exp!(M::AbstractPowerManifold, q, p, X)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
exp!(
M.manifold,
_write(M, rep_size, q, i),
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
)
end
return q
end
function exp!(M::PowerManifoldNestedReplacing, q, p, X)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
q[i...] = exp(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i))
end
return q
end
function get_basis(M::AbstractPowerManifold, p, B::AbstractBasis)
rep_size = representation_size(M.manifold)
vs = [get_basis(M.manifold, _read(M, rep_size, p, i), B) for i in get_iterator(M)]
return CachedBasis(B, PowerBasisData(vs))
end
function get_basis(M::AbstractPowerManifold, p, B::DiagonalizingOrthonormalBasis)
rep_size = representation_size(M.manifold)
vs = [
get_basis(
M.manifold,
_read(M, rep_size, p, i),
DiagonalizingOrthonormalBasis(_read(M, rep_size, B.frame_direction, i)),
) for i in get_iterator(M)
]
return CachedBasis(B, PowerBasisData(vs))
end
"""
get_component(M::AbstractPowerManifold, p, idx...)
Get the component of a point `p` on an [`AbstractPowerManifold`](@ref) `M` at index `idx`.
"""
function get_component(M::AbstractPowerManifold, p, idx...)
rep_size = representation_size(M.manifold)
return _read(M, rep_size, p, idx)
end
function get_coordinates(M::AbstractPowerManifold, p, X, B::AbstractBasis)
rep_size = representation_size(M.manifold)
vs = [
get_coordinates(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i), B) for i in get_iterator(M)
]
return reduce(vcat, reshape(vs, length(vs)))
end
function get_coordinates(
M::AbstractPowerManifold,
p,
X,
B::CachedBasis{𝔽,<:AbstractBasis,<:PowerBasisData},
) where {𝔽}
rep_size = representation_size(M.manifold)
vs = [
get_coordinates(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
_access_nested(M, B.data.bases, i),
) for i in get_iterator(M)
]
return reduce(vcat, reshape(vs, length(vs)))
end
function get_coordinates!(M::AbstractPowerManifold, c, p, X, B::AbstractBasis)
rep_size = representation_size(M.manifold)
dim = manifold_dimension(M.manifold)
v_iter = 1
for i in get_iterator(M)
# TODO: this view is really suboptimal when `dim` can be statically determined
get_coordinates!(
M.manifold,
view(c, v_iter:(v_iter + dim - 1)),
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
B,
)
v_iter += dim
end
return c
end
function get_coordinates!(
M::AbstractPowerManifold,
c,
p,
X,
B::CachedBasis{𝔽,<:AbstractBasis,<:PowerBasisData},
) where {𝔽}
rep_size = representation_size(M.manifold)
dim = manifold_dimension(M.manifold)
v_iter = 1
for i in get_iterator(M)
# TODO: this view is really suboptimal when `dim` can be statically determined
get_coordinates!(
M.manifold,
view(c, v_iter:(v_iter + dim - 1)),
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
_access_nested(M, B.data.bases, i),
)
v_iter += dim
end
return c
end
function get_iterator(
::PowerManifold{𝔽,<:AbstractManifold{𝔽},TypeParameter{Tuple{N}}},
) where {𝔽,N}
return Base.OneTo(N)
end
function get_iterator(M::PowerManifold{𝔽,<:AbstractManifold{𝔽},Tuple{Int}}) where {𝔽}
return Base.OneTo(M.size[1])
end
@generated function get_iterator(
::PowerManifold{𝔽,<:AbstractManifold{𝔽},TypeParameter{SizeTuple}},
) where {𝔽,SizeTuple}
size_tuple = size_to_tuple(SizeTuple)
return Base.product(map(Base.OneTo, size_tuple)...)
end
function get_iterator(M::PowerManifold{𝔽,<:AbstractManifold{𝔽},NTuple{N,Int}}) where {𝔽,N}
size_tuple = M.size
return Base.product(map(Base.OneTo, size_tuple)...)
end
function get_vector(
M::AbstractPowerManifold,
p,
c,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:PowerBasisData},
) where {𝔽}
Y = allocate_result(M, get_vector, p, c)
return get_vector!(M, Y, p, c, B)
end
function get_vector!(
M::AbstractPowerManifold,
Y,
p,
c,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:PowerBasisData},
) where {𝔽}
dim = manifold_dimension(M.manifold)
rep_size = representation_size(M.manifold)
v_iter = 1
for i in get_iterator(M)
get_vector!(
M.manifold,
_write(M, rep_size, Y, i),
_read(M, rep_size, p, i),
c[v_iter:(v_iter + dim - 1)],
_access_nested(M, B.data.bases, i),
)
v_iter += dim
end
return Y
end
function get_vector!(
M::PowerManifoldNestedReplacing,
Y,
p,
c,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:PowerBasisData},
) where {𝔽}
dim = manifold_dimension(M.manifold)
rep_size = representation_size(M.manifold)
v_iter = 1
for i in get_iterator(M)
Y[i...] = get_vector(
M.manifold,
_read(M, rep_size, p, i),
c[v_iter:(v_iter + dim - 1)],
_access_nested(M, B.data.bases, i),
)
v_iter += dim
end
return Y
end
function get_vector(M::AbstractPowerManifold, p, c, B::AbstractBasis)
Y = allocate_result(M, get_vector, p, c)
return get_vector!(M, Y, p, c, B)
end
function get_vector!(M::AbstractPowerManifold, Y, p, c, B::AbstractBasis)
dim = manifold_dimension(M.manifold)
rep_size = representation_size(M.manifold)
v_iter = 1
for i in get_iterator(M)
get_vector!(
M.manifold,
_write(M, rep_size, Y, i),
_read(M, rep_size, p, i),
c[v_iter:(v_iter + dim - 1)],
B,
)
v_iter += dim
end
return Y
end
function get_vector!(M::PowerManifoldNestedReplacing, Y, p, c, B::AbstractBasis)
dim = manifold_dimension(M.manifold)
rep_size = representation_size(M.manifold)
v_iter = 1
for i in get_iterator(M)
Y[i...] = get_vector(
M.manifold,
_read(M, rep_size, p, i),
c[v_iter:(v_iter + dim - 1)],
B,
)
v_iter += dim
end
return Y
end
function _get_vectors(
M::PowerManifoldNested,
p,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:PowerBasisData},
) where {𝔽}
zero_tv = zero_vector(M, p)
rep_size = representation_size(M.manifold)
vs = typeof(zero_tv)[]
for i in get_iterator(M)
b_i = _access_nested(M, B.data.bases, i)
p_i = _read(M, rep_size, p, i)
for v in b_i.data
new_v = copy(M, p, zero_tv)
copyto!(M.manifold, _write(M, rep_size, new_v, i), p_i, v)
push!(vs, new_v)
end
end
return vs
end
function _get_vectors(
M::PowerManifoldNestedReplacing,
p,
B::CachedBasis{𝔽,<:AbstractBasis{𝔽},<:PowerBasisData},
) where {𝔽}
zero_tv = zero_vector(M, p)
vs = typeof(zero_tv)[]
for i in get_iterator(M)
b_i = _access_nested(M, B.data.bases, i)
for v in b_i.data
new_v = copy(M, p, zero_tv)
new_v[i...] = v
push!(vs, new_v)
end
end
return vs
end
"""
getindex(p, M::AbstractPowerManifold, i::Union{Integer,Colon,AbstractVector}...)
p[M::AbstractPowerManifold, i...]
Access the element(s) at index `[i...]` of a point `p` on an [`AbstractPowerManifold`](@ref)
`M` by linear or multidimensional 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::AbstractArray,
M::AbstractPowerManifold,
I::Union{Integer,Colon,AbstractVector}...,
)
return get_component(M, p, I...)
end
@doc raw"""
injectivity_radius(M::AbstractPowerManifold[, p])
the injectivity radius on an [`AbstractPowerManifold`](@ref) is for the global case
equal to the one of its base manifold. For a given point `p` it's equal to the
minimum of all radii in the array entries.
"""
function injectivity_radius(M::AbstractPowerManifold, p)
radius = 0.0
initialized = false
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
cur_rad = injectivity_radius(M.manifold, _read(M, rep_size, p, i))
if initialized
radius = min(cur_rad, radius)
else
radius = cur_rad
initialized = true
end
end
return radius
end
injectivity_radius(M::AbstractPowerManifold) = injectivity_radius(M.manifold)
function injectivity_radius(M::AbstractPowerManifold, ::AbstractRetractionMethod)
return injectivity_radius(M)
end
@doc raw"""
inner(M::AbstractPowerManifold, p, X, Y)
Compute the inner product of `X` and `Y` from the tangent space at `p` on an
[`AbstractPowerManifold`](@ref) `M`, i.e. for each arrays entry the tangent
vector entries from `X` and `Y` are in the tangent space of the corresponding
element from `p`.
The inner product is then the sum of the elementwise inner products.
"""
function inner(M::AbstractPowerManifold, p, X, Y)
result = zero(number_eltype(X))
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
result += inner(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
_read(M, rep_size, Y, i),
)
end
return result
end
"""
is_flat(M::AbstractPowerManifold)
Return true if [`AbstractPowerManifold`](@ref) is flat. It is flat if and only if the
wrapped manifold is flat.
"""
is_flat(M::AbstractPowerManifold) = is_flat(M.manifold)
function _isapprox(M::AbstractPowerManifold, p, q; kwargs...)
result = true
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
result &= isapprox(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, q, i);
kwargs...,
)
end
return result
end
function _isapprox(M::AbstractPowerManifold, p, X, Y; kwargs...)
result = true
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
result &= isapprox(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, X, i),
_read(M, rep_size, Y, i);
kwargs...,
)
end
return result
end
@doc raw"""
inverse_retract(M::AbstractPowerManifold, p, q, m::AbstractInverseRetractionMethod)
Compute the inverse retraction from `p` with respect to `q` on an [`AbstractPowerManifold`](@ref) `M`
using an [`AbstractInverseRetractionMethod`](@ref).
Then this method is performed elementwise, so the inverse
retraction method has to be one that is available on the base [`AbstractManifold`](@ref).
"""
inverse_retract(::AbstractPowerManifold, ::Any...)
function inverse_retract(
M::AbstractPowerManifold,
p,
q,
m::AbstractInverseRetractionMethod = default_inverse_retraction_method(M, typeof(p)),
)
X = allocate_result(M, inverse_retract, p, q)
return inverse_retract!(M, X, p, q, m)
end
function inverse_retract!(
M::AbstractPowerManifold,
X,
p,
q,
m::AbstractInverseRetractionMethod = default_inverse_retraction_method(M, typeof(p)),
)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
inverse_retract!(
M.manifold,
_write(M, rep_size, X, i),
_read(M, rep_size, p, i),
_read(M, rep_size, q, i),
m,
)
end
return X
end
function inverse_retract!(
M::PowerManifoldNestedReplacing,
X,
p,
q,
m::AbstractInverseRetractionMethod = default_inverse_retraction_method(M, typeof(p)),
)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
X[i...] = inverse_retract(
M.manifold,
_read(M, rep_size, p, i),
_read(M, rep_size, q, i),
m,
)
end
return X
end
@doc raw"""
log(M::AbstractPowerManifold, p, q)
Compute the logarithmic map from `p` to `q` on the [`AbstractPowerManifold`](@ref) `M`,
which can be computed using the base manifolds logarithmic map elementwise.
"""
log(::AbstractPowerManifold, ::Any...)
function log!(M::AbstractPowerManifold, X, p, q)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
log!(
M.manifold,
_write(M, rep_size, X, i),
_read(M, rep_size, p, i),
_read(M, rep_size, q, i),
)
end
return X
end
function log!(M::PowerManifoldNestedReplacing, X, p, q)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
X[i...] = log(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i))
end
return X
end
@doc raw"""
manifold_dimension(M::PowerManifold)
Returns the manifold-dimension of an [`PowerManifold`](@ref) `M`
``=\mathcal N = (\mathcal M)^{n_1,…,n_d}``, i.e. with ``n=(n_1,…,n_d)`` the array
size of the power manifold and ``d_{\mathcal M}`` the dimension of the base manifold
``\mathcal M``, the manifold is of dimension
````math
\dim(\mathcal N) = \dim(\mathcal M)\prod_{i=1}^d n_i = n_1n_2⋅…⋅ n_d \dim(\mathcal M).
````
"""
function manifold_dimension(M::PowerManifold)
size = get_parameter(M.size)
return manifold_dimension(M.manifold) * prod(size)
end
function mid_point!(M::AbstractPowerManifold, q, p1, p2)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
mid_point!(
M.manifold,
_write(M, rep_size, q, i),
_read(M, rep_size, p1, i),
_read(M, rep_size, p2, i),
)
end
return q
end
function mid_point!(M::PowerManifoldNestedReplacing, q, p1, p2)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
q[i...] =
mid_point(M.manifold, _read(M, rep_size, p1, i), _read(M, rep_size, p2, i))
end
return q
end
@doc raw"""
norm(M::AbstractPowerManifold, p, X)
Compute the norm of `X` from the tangent space of `p` on an
[`AbstractPowerManifold`](@ref) `M`, i.e. from the element wise norms the
Frobenius norm is computed.
"""
function LinearAlgebra.norm(M::AbstractPowerManifold, p, X)
sum_squares = zero(number_eltype(X))
rep_size = representation_size(M.manifold)