/
linear_subspaces.jl
738 lines (635 loc) · 20.1 KB
/
linear_subspaces.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
export LinearSubspace,
ExtrinsicDescription,
IntrinsicDescription,
Coordinates,
Intrinsic,
Extrinsic,
intrinsic,
is_linear,
extrinsic,
dim,
codim,
ambient_dim,
rand_subspace,
coord_change,
geodesic_distance,
geodesic,
translate
# References
const _LKK19 = """Lim, Lek-Heng, Ken Sze-Wai Wong, and Ke Ye. "Numerical algorithms on the affine Grassmannian." SIAM Journal on Matrix Analysis and Applications 40.2 (2019): 371-393"""
abstract type AbstractSubspace{T} end
"""
Coordinates
A type used for encoding the used coordinates and for performing coordinate changes.
Currently supported coordinates are:
- [`Intrinsic`](@ref)
- [`Extrinsic`](@ref)
"""
struct Coordinates{T} end
Base.broadcastable(C::Coordinates) = Ref(C)
"""
Intrinsic <: Coordinates
Indicates the use of the intrinsic description of an (affine) linear subspace. See also [`IntrinsicDescription`](@ref).
"""
const Intrinsic = Coordinates{:Intrinsic}()
"""
Extrinsic <: Coordinates
Indicates the use of the extrinsic description of an (affine) linear subspace. See also [`ExtrinsicDescription`](@ref).
"""
const Extrinsic = Coordinates{:Extrinsic}()
# These are not part of the public API
const IntrinsicStiefel = Coordinates{:IntrinsicStiefel}()
const ExtrinsicStiefel = Coordinates{:ExtrinsicStiefel}()
"""
ExtrinsicDescription(A, b)
Extrinsic description of an ``m``-dimensional (affine) linear subspace ``L`` in ``n``-dimensional space.
That is ``L = \\{ x | A x = b \\}``.
Note that internally `A` and `b` will be stored such that the rows of `A` are orthonormal.
"""
struct ExtrinsicDescription{T}
A::Matrix{T}
b::Vector{T}
function ExtrinsicDescription(
A::Matrix{T},
b::Vector{T};
orthonormal::Bool = false,
) where {T}
if orthonormal
new{T}(A, b)
else
if size(A, 1) == 0
new{T}(A, b)
else
svd = LA.svd(A)
new{T}(svd.Vt, (inv.(svd.S) .* (svd.U' * b)))
end
end
end
end
(A::ExtrinsicDescription)(x::AbstractVector) = A.A * x - A.b
function Base.convert(::Type{ExtrinsicDescription{T}}, A::ExtrinsicDescription) where {T}
ExtrinsicDescription(
convert(Matrix{T}, A.A),
convert(Vector{T}, A.b);
orthonormal = true,
)
end
"""
dim(A::ExtrinsicDescription)
Dimension of the (affine) linear subspace `A`.
"""
dim(A::ExtrinsicDescription) = size(A.A, 2) - size(A.A, 1)
"""
codim(A::ExtrinsicDescription)
Codimension of the (affine) linear subspace `A`.
"""
codim(A::ExtrinsicDescription) = size(A.A, 1)
Base.:(==)(A::ExtrinsicDescription, B::ExtrinsicDescription) = A.A == B.A && A.b == B.b
function Base.copy!(A::ExtrinsicDescription, B::ExtrinsicDescription)
copy!(A.A, B.A)
copy!(A.b, B.b)
A
end
Base.copy(A::ExtrinsicDescription) =
ExtrinsicDescription(copy(A.A), copy(A.b); orthonormal = true)
function Base.show(io::IO, A::ExtrinsicDescription{T}) where {T}
println(io, "ExtrinsicDescription{$T}:")
println(io, "A:")
show(io, A.A)
println(io, "\nb:")
show(io, A.b)
end
Base.broadcastable(A::ExtrinsicDescription) = Ref(A)
"""
IntrinsicDescription(A, b₀)
Intrinsic description of an ``m``-dimensional (affine) linear subspace ``L`` in ``n``-dimensional space.
That is ``L = \\{ u | A u + b₀ \\}``. Here, ``A`` and ``b₀`` are in orthogonal coordinates.
That is, the columns of ``A`` are orthonormal and ``A' b₀ = 0``.
"""
struct IntrinsicDescription{T}
# orthogonal coordinates
A::Matrix{T}
b₀::Vector{T}
# stiefel coordinates
Y::Matrix{T}
end
IntrinsicDescription(A::Matrix{T1}, b₀::Vector{T2}) where {T1,T2} =
IntrinsicDescription(A, b₀, stiefel_coordinates(A, b₀))
function Base.convert(::Type{IntrinsicDescription{T}}, A::IntrinsicDescription) where {T}
IntrinsicDescription(
convert(Matrix{T}, A.A),
convert(Vector{T}, A.b₀),
convert(Matrix{T}, A.Y),
)
end
(A::IntrinsicDescription)(u::AbstractVector, ::Coordinates{:Intrinsic}) = A.A * u + A.b₀
(A::IntrinsicDescription)(u::AbstractVector, ::Coordinates{:IntrinsicStiefel}) = A.Y * u
function stiefel_coordinates(A::AbstractMatrix, b::AbstractVector)
n, k = size(A)
Y = zeros(eltype(A), n + 1, k + 1)
stiefel_coordinates!(Y, A, b)
Y
end
function stiefel_coordinates!(Y, A::AbstractMatrix, b::AbstractVector)
γ = sqrt(1 + sum(abs2, b))
n, k = size(A)
Y[1:n, 1:k] .= A
Y[1:n, k+1] .= b ./ γ
Y[n+1, k+1] = 1 / γ
Y
end
"""
dim(A::IntrinsicDescription)
Dimension of the (affine) linear subspace `A`.
"""
dim(I::IntrinsicDescription) = size(I.A, 2)
"""
codim(A::IntrinsicDescription)
Codimension of the (affine) linear subspace `A`.
"""
codim(I::IntrinsicDescription) = size(I.A, 1) - size(I.A, 2)
function Base.:(==)(A::IntrinsicDescription, B::IntrinsicDescription)
A.A == B.A && A.b₀ == B.b₀ && A.Y == B.Y
end
function Base.show(io::IO, A::IntrinsicDescription{T}) where {T}
println(io, "IntrinsicDescription{$T}:")
println(io, "A:")
show(io, A.A)
println(io, "\nb₀:")
show(io, A.b₀)
end
function Base.copy!(A::IntrinsicDescription, B::IntrinsicDescription)
copy!(A.A, B.A)
copy!(A.b₀, B.b₀)
copy!(A.Y, B.Y)
A
end
Base.copy(A::IntrinsicDescription) = IntrinsicDescription(copy(A.A), copy(A.b₀), copy(A.Y))
Base.broadcastable(A::IntrinsicDescription) = Ref(A)
function IntrinsicDescription(E::ExtrinsicDescription)
svd = LA.svd(E.A; full = true)
m, n = size(E.A)
A = Matrix((@view svd.Vt[m+1:end, :])')
if iszero(E.b)
b₀ = zeros(eltype(E.b), size(A, 1))
else
b₀ = svd \ E.b
end
IntrinsicDescription(A, b₀)
end
function ExtrinsicDescription(I::IntrinsicDescription)
svd = LA.svd(I.A; full = true)
m, n = size(I.A)
A = Matrix((@view svd.U[:, n+1:end])')
if iszero(I.b₀)
b = zeros(eltype(A), size(A, 1))
else
b = A * I.b₀
end
ExtrinsicDescription(A, b; orthonormal = true)
end
"""
LinearSubspace(A, b)
An ``m``-dimensional (affine) linear subspace ``L`` in ``n``-dimensional space given by
the extrinsic description ``L = \\{ x | A x = b \\}``.
```julia-repl
julia> A = LinearSubspace([1 0 3; 2 1 3], [5, -2])
1-dim. (affine) linear subspace {x|Ax=b} with eltype Float64:
A:
2×3 Array{Float64,2}:
1.0 0.0 3.0
2.0 1.0 3.0
b:
2-element Array{Float64,1}:
5.0
-2.0
julia> dim(A)
1
julia> codim(A)
2
julia> ambient_dim(A)
3
```
A `LinearSubspace` holds always its [`extrinsic`](@ref) description,
see also [`ExtrinsicDescription`](@ref), as well as its [`intrinsic`](@ref) description,
see [`IntrinsicDescription`](@ref).
```julia-repl
julia> intrinsic(A)
IntrinsicDescription{Float64}:
A:
3×1 Array{Float64,2}:
-0.6882472016116853
0.6882472016116853
0.22941573387056186
b₀:
3-element Array{Float64,1}:
-3.0526315789473677
-3.947368421052632
2.684210526315789
```
A `LinearSubspace` can be evaluated with either using [`Intrinsic`](@ref)
or [`Extrinsic`](@ref) coordinates.
```julia-repl
julia> u = [0.5]
1-element Array{Float64,1}:
0.5
julia> x = A(u, Intrinsic)
3-element Array{Float64,1}:
-3.3967551797532103
-3.6032448202467893
2.79891839325107
julia> A(x, Extrinsic)
2-element Array{Float64,1}:
0.0
0.0
```
To change the used coordinates you can use [`coord_change`](@ref).
```julia-repl
julia> coord_change(A, Extrinsic, Intrinsic, x)
1-element Array{Float64,1}:
0.49999999999999994
```
"""
struct LinearSubspace{T} <: AbstractSubspace{T}
extrinsic::ExtrinsicDescription{T}
intrinsic::IntrinsicDescription{T}
end
LinearSubspace(I::IntrinsicDescription) = LinearSubspace(ExtrinsicDescription(I), I)
LinearSubspace(E::ExtrinsicDescription) = LinearSubspace(E, IntrinsicDescription(E))
function LinearSubspace(
A::AbstractMatrix{T},
b::AbstractVector{T} = zeros(eltype(A), size(A, 1)),
) where {T}
size(A, 1) == length(b) || throw(ArgumentError("Size of A and b not compatible."))
0 ≤ size(A, 1) ≤ size(A, 2) || throw(
ArgumentError(
"Affine subspace has to be given in extrinsic coordinates, i.e., by A x = b.",
),
)
LinearSubspace(ExtrinsicDescription(Matrix(float.(A)), Vector(float.(b))))
end
function Base.convert(::Type{LinearSubspace{T}}, A::LinearSubspace) where {T}
LinearSubspace(
convert(ExtrinsicDescription{T}, A.extrinsic),
convert(IntrinsicDescription{T}, A.intrinsic),
)
end
Base.broadcastable(A::LinearSubspace) = Ref(A)
"""
dim(A::LinearSubspace)
Dimension of the (affine) linear subspace `A`.
"""
dim(A::LinearSubspace) = dim(A.intrinsic)
"""
codim(A::LinearSubspace)
Codimension of the (affine) linear subspace `A`.
"""
codim(A::LinearSubspace) = codim(A.intrinsic)
"""
ambient_dim(A::LinearSubspace)
Dimension of ambient space of the (affine) linear subspace `A`.
"""
ambient_dim(A::LinearSubspace) = dim(A) + codim(A)
"""
is_linear(L::LinearSubspace)
Returns `true` if the space is proper linear subspace, i.e., described by
`L = \\{ x | Ax = 0 \\}.
"""
is_linear(A::LinearSubspace) = iszero(extrinsic(A).b)
function Base.show(io::IO, A::LinearSubspace{T}) where {T}
if is_linear(A)
println(io, "$(dim(A))-dim. linear subspace {x | Ax=0} with eltype $T:")
println(io, "A:")
show(io, A.extrinsic.A)
else
println(io, "$(dim(A))-dim. affine linear subspace {x | Ax=b} with eltype $T:")
println(io, "A:")
show(io, A.extrinsic.A)
println(io, "\nb:")
show(io, A.extrinsic.b)
end
end
"""
intrinsic(A::LinearSubspace)
Obtain the intrinsic description of `A`, see also [`IntrinsicDescription`](@ref).
"""
intrinsic(A::LinearSubspace) = A.intrinsic
"""
extrinsic(A::LinearSubspace)
Obtain the extrinsic description of `A`, see also [`ExtrinsicDescription`](@ref).
"""
extrinsic(A::LinearSubspace) = A.extrinsic
function Base.copy!(A::LinearSubspace, B::LinearSubspace)
copy!(A.intrinsic, B.intrinsic)
copy!(A.extrinsic, B.extrinsic)
A
end
Base.copy(A::LinearSubspace) = LinearSubspace(copy(A.extrinsic), copy(A.intrinsic))
function Base.:(==)(A::LinearSubspace, B::LinearSubspace)
intrinsic(A) == intrinsic(B) && extrinsic(A) == extrinsic(B)
end
Base.isequal(A::LinearSubspace, B::LinearSubspace) = A === B
function (A::LinearSubspace)(x::AbstractVector, ::Coordinates{:Intrinsic})
intrinsic(A)(x, Intrinsic)
end
function (A::LinearSubspace)(x::AbstractVector, ::Coordinates{:IntrinsicStiefel})
intrinsic(A)(x, IntrinsicStiefel)
end
function (A::LinearSubspace)(x::AbstractVector, ::Coordinates{:Extrinsic} = Extrinsic)
extrinsic(A)(x)
end
"""
rand_subspace(n::Integer; dim | codim, affine = true, real = false)
Generate a random [`LinearSubspace`](@ref) with given dimension `dim` or codimension `codim`
(one of them has to be provided) in ambient space of dimension `n`.
If `real` is `true`, then the extrinsic description is real.
If `affine` then an affine linear subspace is generated.
The subspace is generated by drawing each entry of the extrinsic description indepdently
from a normal distribuation using `randn`.
rand_subspace(x::AbstractVector; dim | codim, affine = true)
Generate a random [`LinearSubspace`](@ref) with given dimension `dim` or codimension `codim`
(one of them has to be provided) in ambient space of dimension `length(x)` going through
the given point `x`.
## Example
Construction of a general random subspace:
```julia-repl
julia> rand_subspace(3; dim = 1)
1-dim. (affine) linear subspace {x|Ax=b} with eltype Complex{Float64}:
A:
2×3 Array{Complex{Float64},2}:
-1.73825+1.27987im -0.0871343+0.840408im -0.551957+0.106397im
-0.597132-0.343965im -0.122543-0.172715im -1.04949+0.370917im
b:
2-element Array{Complex{Float64},1}:
0.47083334430689394 + 0.8099804422599071im
-0.12018696822943896 + 0.11723026326952792im
julia> rand_subspace(4; codim = 1)
3-dim. (affine) linear subspace {x|Ax=b} with eltype Complex{Float64}:
A:
1×4 Array{Complex{Float64},2}:
0.345705+0.0893881im -0.430867-0.663249im 0.979969-0.569378im -0.29722-0.192493im
b:
1-element Array{Complex{Float64},1}:
0.7749708228192062 + 0.9762873764567546im
```
"""
function rand_subspace(
n::Integer;
dim::Union{Nothing,Integer} = nothing,
codim::Union{Nothing,Integer} = nothing,
real::Bool = false,
affine::Bool = true,
)
!isnothing(dim) ||
!isnothing(codim) ||
throw(ArgumentError("Neither `dim` nor `codim` specified."))
if !isnothing(dim)
0 < dim < n || throw(ArgumentError("`dim` has to be between 0 and `n`."))
k = dim
else
0 < codim < n || throw(ArgumentError("`codim` has to be between 0 and `n`."))
k = n - codim
end
T = real ? Float64 : ComplexF64
A = randn(T, n - k, n)
if affine
LinearSubspace(A, randn(T, n - k))
else
LinearSubspace(A)
end
end
rand_subspace(x::AbstractVector{Variable}; kwargs...) = rand_subspace(length(x); kwargs...)
function rand_subspace(
x::AbstractVector;
dim::Union{Nothing,Integer} = nothing,
codim::Union{Nothing,Integer} = nothing,
affine::Bool = true,
)
n = length(x)
!isnothing(dim) ||
!isnothing(codim) ||
throw(ArgumentError("Neither `dim` nor `codim` specified."))
if !isnothing(dim)
0 < dim < n || throw(ArgumentError("`dim` has to be between 0 and `n`."))
k = dim
else
0 < codim < n || throw(ArgumentError("`codim` has to be between 0 and `n`."))
k = n - codim
end
if affine
A = randn(eltype(x), n - k, n)
b = A * x
LinearSubspace(A, b)
else
N = LA.nullspace(Matrix(x'))'
A = randn(eltype(N), n - k, size(N, 1)) * N
LinearSubspace(A)
end
end
# Coordinate changes
"""
coord_change(A::LinearSubspace, C₁::Coordinates, C₂::Coordinates, p)
Given an (affine) linear subspace `A` and a point `p` in coordinates `C₁` compute the point
`x` describing p in coordinates `C₂`.
## Example
```julia-repl
julia> A = LinearSubspace([1 0 3; 2 1 3], [5, -2]);
julia> u = [1.25];
julia> x = coord_change(A, Intrinsic, Extrinsic, u)
3-element Array{Float64,1}:
-3.9129405809619744
-3.087059419038025
2.9709801936539915
julia> A(x, Extrinsic)
2-element Array{Float64,1}:
0.0
0.0
julia> x - A(u, Intrinsic)
3-element Array{Float64,1}:
0.0
0.0
0.0
```
"""
coord_change(A::LinearSubspace, ::C, ::C, x) where {C<:Coordinates} = x
coord_change(A::LinearSubspace, ::Coordinates{:Intrinsic}, ::Coordinates{:Extrinsic}, u) =
A(u, Intrinsic)
function coord_change(
A::LinearSubspace,
::Coordinates{:Intrinsic},
::Coordinates{:IntrinsicStiefel},
u,
)
[
u
1 / A.intrinsic.Y[end, end]
]
end
function coord_change(
A::LinearSubspace,
::Coordinates{:Intrinsic},
::Coordinates{:ExtrinsicStiefel},
u,
)
v = coord_change(A, Intrinsic, IntrinsicStiefel, u)
coord_change(A, IntrinsicStiefel, ExtrinsicStiefel, v)
end
coord_change(A::LinearSubspace, ::Coordinates{:Extrinsic}, ::Coordinates{:Intrinsic}, x) =
A.intrinsic.A' * (x - A.intrinsic.b₀)
function coord_change(
A::LinearSubspace,
::Coordinates{:Extrinsic},
::Coordinates{:IntrinsicStiefel},
x,
)
u = coord_change(A, Extrinsic, Intrinsic, x)
coord_change(A, Intrinsic, IntrinsicStiefel, u)
end
function coord_change(
A::LinearSubspace,
::Coordinates{:Extrinsic},
::Coordinates{:ExtrinsicStiefel},
x,
)
[x; 1]
end
function coord_change(
A::LinearSubspace,
::Coordinates{:IntrinsicStiefel},
::Coordinates{:Intrinsic},
u,
)
γ = A.intrinsic.Y[end, end]
[u[i] / (γ * u[end]) for i = 1:(length(u)-1)]
end
function coord_change(
A::LinearSubspace,
::Coordinates{:IntrinsicStiefel},
::Coordinates{:Extrinsic},
u,
)
x = coord_change(A, IntrinsicStiefel, ExtrinsicStiefel, u)
coord_change(A, ExtrinsicStiefel, Extrinsic, x)
end
function coord_change(
A::LinearSubspace,
::Coordinates{:IntrinsicStiefel},
::Coordinates{:ExtrinsicStiefel},
u,
)
A(u, IntrinsicStiefel)
end
function coord_change(
A::LinearSubspace,
::Coordinates{:ExtrinsicStiefel},
::Coordinates{:Intrinsic},
x,
)
u = coord_change(A, ExtrinsicStiefel, IntrinsicStiefel, x)
coord_change(A, IntrinsicStiefel, Intrinsic, u)
end
function coord_change(
A::LinearSubspace,
::Coordinates{:ExtrinsicStiefel},
::Coordinates{:Extrinsic},
x,
)
x[1:end-1] ./ x[end]
end
function coord_change(
A::LinearSubspace,
::Coordinates{:ExtrinsicStiefel},
::Coordinates{:IntrinsicStiefel},
x,
)
A.intrinsic.Y' * x
end
"""
geodesic_distance(A::LinearSubspace, B::LinearSubspace)
Compute the geodesic distance between `A` and `B` in the affine Grassmanian `Graff(k, n)`
where `k = dim(A)` and `n` is the amebient dimension.
This follows the derivation in [^LKK19].
[^LKK19]: $_LKK19.
"""
geodesic_distance(A::LinearSubspace, B::LinearSubspace) =
geodesic_distance(A.intrinsic, B.intrinsic)
function geodesic_distance(A::IntrinsicDescription, B::IntrinsicDescription)
sqrt(sum(σᵢ -> acos(min(σᵢ, 1.0))^2, LA.svdvals(A.Y' * B.Y)))
end
# geodesic
"""
geodesic_svd(A::LinearSubspace, B::LinearSubspace)
Computes the factors ``Q``, ``Θ`` and ``U`` from Corollary 4.3 in [^LKK19].
These values are necessary to construct the geodesic between `A` and `B`.
[^LKK19]: $_LKK19
"""
geodesic_svd(A::LinearSubspace, B::LinearSubspace) = geodesic_svd(A.intrinsic, B.intrinsic)
function geodesic_svd(A::IntrinsicDescription, B::IntrinsicDescription)
U, Σ, V = LA.svd!(A.Y' * B.Y)
# Θ = acos.(min.(Σ, 1.0))
# We have acos(1-eps()) = 2.1073424255447017e-8
# So this is numerically super unstable if the singular value is off by only eps()
# We try to correct this fact by treating σ >= 1 - 2eps() as 1.0
Θ = map(σ -> σ + 2 * eps() > 1.0 ? 0.0 : acos(σ), Σ)
n, k = size(A.Y)
# M = (LA.I - A.Y * A.Y') * B.Y * inv(A.Y' * B.Y)
# Have to compute an SVD of M s.t. M = Q tanΘ U'
# Equivalently M * U = Q tan(Θ)
# We can achieve this by using a *pivoted* QR
# since then R will be a diagonal matrix s.t. the absolute value of R_ii is θ_{k-i}
# inv(A.Y' * B.Y) * U = V * LA.diagm(inv.(Σ))
MU = (LA.I - A.Y * A.Y') * B.Y * V * LA.diagm(inv.(Σ))
Q, R = qr_col_norm!(MU)
# correct signs and ordering of Q
Q′ = Q[:, k:-1:1]
for j = 1:k
# Look if we need to flip signs
real(R[k-j+1, k-j+1]) < 0 || continue
for i = 1:n
Q′[i, j] = -Q′[i, j]
end
end
Q′, Θ, U
end
"""
geodesic(A::LinearSubspace, B::LinearSubspace)
Returns the geodesic ``γ(t)`` connecting `A` and `B` in the Grassmanian ``Gr(k+1,n+1)``
where ``k`` is the dimension of ``A`` and ``n`` is the ambient dimension.
See also Corollary 4.3 in [^LKK19].
[^LKK19]: $_LKK19
"""
geodesic(A::LinearSubspace, B::LinearSubspace) = geodesic(A.intrinsic, B.intrinsic)
function geodesic(A::IntrinsicDescription, B::IntrinsicDescription)
Q, Θ, U = geodesic_svd(A, B)
t -> A.Y * U * LA.diagm(cos.(t .* Θ,)) * U' + Q * LA.diagm(sin.(t .* Θ,)) * U'
end
#
"""
translate(L::LinearSubspace, δb, ::Coordinates = Extrinsic)
Translate the (affine) linear subspace `L` by `δb`.
"""
function translate(L::LinearSubspace, δb, ::Coordinates{:Extrinsic} = Extrinsic)
translate!(copy(L), δb, Extrinsic)
end
function translate!(L::LinearSubspace, δb, ::Coordinates{:Extrinsic} = Extrinsic)
ext = extrinsic(L)
ext.b .+= δb
int = intrinsic(L)
LA.mul!(int.b₀, ext.A', δb, true, true)
stiefel_coordinates!(int.Y, int.A, int.b₀)
L
end
"""
Base.intersect(L₁::LinearSubspace, L₂::LinearSubspace)
Intersect the two given linear subspaces.
Throws an `ErrorException` if the intersection is the sum of the
codimensions is larger than the ambient dimension.
"""
function Base.intersect(L₁::LinearSubspace, L₂::LinearSubspace)
ambient_dim(L₁) == ambient_dim(L₂) || error("Ambient dimensions don't match.")
codim(L₁) + codim(L₂) ≤ ambient_dim(L₁) ||
error("Sum of codimensions larger than ambient dimension.")
ext₁ = extrinsic(L₁)
ext₂ = extrinsic(L₂)
LinearSubspace([ext₁.A; ext₂.A], [ext₁.b; ext₂.b])
end