-
Notifications
You must be signed in to change notification settings - Fork 56
/
heisenberg.jl
396 lines (345 loc) · 12.7 KB
/
heisenberg.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
@doc raw"""
HeisenbergGroup{n} <: AbstractDecoratorManifold{ℝ}
Heisenberg group `HeisenbergGroup(n)` is the group of ``(n+2) × (n+2)`` matrices [BinzPods:2008](@cite)
```math
\begin{bmatrix} 1 & \mathbf{a} & c \\
\mathbf{0} & I_n & \mathbf{b} \\
0 & \mathbf{0} & 1 \end{bmatrix}
```
where ``I_n`` is the ``n×n`` unit matrix, ``\mathbf{a}`` is a row vector of length ``n``,
``\mathbf{b}`` is a column vector of length ``n`` and ``c`` is a real number.
The group operation is matrix multiplication.
The left-invariant metric on the manifold is used.
"""
struct HeisenbergGroup{n} <: AbstractDecoratorManifold{ℝ} end
function active_traits(f, ::HeisenbergGroup, args...)
return merge_traits(
IsGroupManifold(MultiplicationOperation()),
IsEmbeddedManifold(),
HasLeftInvariantMetric(),
)
end
function _heisenberg_a_view(::HeisenbergGroup{n}, p) where {n}
return view(p, 1, 2:(n + 1))
end
function _heisenberg_b_view(::HeisenbergGroup{n}, p) where {n}
return view(p, 2:(n + 1), n + 2)
end
HeisenbergGroup(n::Int) = HeisenbergGroup{n}()
function check_point(G::HeisenbergGroup{n}, p; kwargs...) where {n}
if !isone(p[1, 1])
return DomainError(
p[1, 1],
"The matrix $(p) does not lie on $(G), since p[1, 1] is not equal to 1.",
)
end
if !isone(p[n + 2, n + 2])
return DomainError(
p[n + 2, n + 2],
"The matrix $(p) does not lie on $(G), since p[n+2, n+2] is not equal to 1.",
)
end
if !iszero(p[2:(n + 2), 1])
return DomainError(
norm(iszero(p[2:(n + 2), 1])),
"The matrix $(p) does not lie on $(G), since p[2:(n + 2), 1] is not equal to 0.",
)
end
if !iszero(p[n + 2, 1:(n + 1)])
return DomainError(
norm(iszero(p[n + 2, 1:(n + 1)])),
"The matrix $(p) does not lie on $(G), since p[n + 2, 1:(n+1)] is not equal to 0.",
)
end
if !isapprox(I, p[2:(n + 1), 2:(n + 1)])
return DomainError(
det(p[2:(n + 1), 2:(n + 1)] - I),
"The matrix $(p) does not lie on $(G), since p[2:(n+1), 2:(n+1)] is not an identity matrix.",
)
end
return nothing
end
function check_vector(G::HeisenbergGroup{n}, p, X; kwargs...) where {n}
if !iszero(X[1, 1])
return DomainError(
X[1, 1],
"The matrix $(X) does not lie in the tangent space of $(G), since X[1, 1] is not equal to 0.",
)
end
if !iszero(X[n + 2, n + 2])
return DomainError(
X[n + 2, n + 2],
"The matrix $(X) does not lie in the tangent space of $(G), since X[n+2, n+2] is not equal to 0.",
)
end
if !iszero(X[2:(n + 2), 1:(n + 1)])
return DomainError(
norm(X[2:(n + 2), 1:(n + 1)]),
"The matrix $(X) does not lie in the tangent space of $(G), since X[2:(n + 2), 1:(n + 1)] is not a zero matrix.",
)
end
return nothing
end
embed(::HeisenbergGroup, p) = p
embed(::HeisenbergGroup, p, X) = X
@doc raw"""
get_coordinates(M::HeisenbergGroup, p, X, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})
Get coordinates of tangent vector `X` at point `p` from the [`HeisenbergGroup`](@ref) `M`.
Given a matrix
```math
\begin{bmatrix} 1 & \mathbf{a} & c \\
\mathbf{0} & I_n & \mathbf{b} \\
0 & \mathbf{0} & 1 \end{bmatrix}
```
the coordinates are concatenated vectors ``\mathbf{a}``, ``\mathbf{b}``, and number ``c``.
"""
get_coordinates(::HeisenbergGroup, p, X, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})
function get_coordinates_orthonormal(M::HeisenbergGroup{n}, p, X, ::RealNumbers) where {n}
return vcat(_heisenberg_a_view(M, X), _heisenberg_b_view(M, X), X[1, n + 2])
end
function get_coordinates_orthonormal!(
M::HeisenbergGroup{n},
Xⁱ,
p,
X,
::RealNumbers,
) where {n}
Xⁱ[1:n] .= _heisenberg_a_view(M, X)
Xⁱ[(n + 1):(2 * n)] .= _heisenberg_b_view(M, X)
Xⁱ[2 * n + 1] = X[1, n + 2]
return Xⁱ
end
get_embedding(::HeisenbergGroup{n}) where {n} = Euclidean(n + 2, n + 2)
@doc raw"""
get_vector(M::HeisenbergGroup, p, Xⁱ, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})
Get tangent vector with coordinates `Xⁱ` at point `p` from the [`HeisenbergGroup`](@ref) `M`.
Given a vector of coordinates ``\begin{bmatrix}\mathbb{a} & \mathbb{b} & c\end{bmatrix}`` the tangent vector is equal to
```math
\begin{bmatrix} 1 & \mathbf{a} & c \\
\mathbf{0} & I_n & \mathbf{b} \\
0 & \mathbf{0} & 1 \end{bmatrix}
```
"""
get_vector(M::HeisenbergGroup, p, c, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})
function get_vector_orthonormal(::HeisenbergGroup{n}, p, Xⁱ, ::RealNumbers) where {n}
return [
0 Xⁱ[1:n] Xⁱ[2 * n + 1]
zeros(n, n + 1) Xⁱ[(n + 1):(2 * n)]'
zeros(1, n + 2)
]
end
function get_vector_orthonormal!(::HeisenbergGroup{n}, X, p, Xⁱ, ::RealNumbers) where {n}
fill!(X, 0)
X[1, 2:(n + 1)] .= Xⁱ[1:n]
X[2:(n + 1), n + 2] .= Xⁱ[(n + 1):(2 * n)]
X[1, n + 2] = Xⁱ[2 * n + 1]
return X
end
@doc raw"""
exp_lie(M::HeisenbergGroup, X)
Lie group exponential for the [`HeisenbergGroup`](@ref) `M` of the vector `X`.
The formula reads
```math
\exp\left(\begin{bmatrix} 0 & \mathbf{a} & c \\
\mathbf{0} & 0_n & \mathbf{b} \\
0 & \mathbf{0} & 0 \end{bmatrix}\right) = \begin{bmatrix} 1 & \mathbf{a} & c + \mathbf{a}⋅\mathbf{b}/2 \\
\mathbf{0} & I_n & \mathbf{b} \\
0 & \mathbf{0} & 1 \end{bmatrix}
```
where ``I_n`` is the ``n×n`` identity matrix, ``0_n`` is the ``n×n`` zero matrix
and ``\mathbf{a}⋅\mathbf{b}`` is dot product of vectors.
"""
exp_lie(M::HeisenbergGroup, X)
function exp_lie!(M::HeisenbergGroup{n}, q, X) where {n}
copyto!(q, I)
a_view = _heisenberg_a_view(M, X)
b_view = _heisenberg_b_view(M, X)
q[1, 2:(n + 1)] .= a_view
q[2:(n + 1), n + 2] .= b_view
q[1, n + 2] = X[1, n + 2] + dot(a_view, b_view) / 2
return q
end
@doc raw"""
exp(M::HeisenbergGroup, p, X)
Exponential map on the [`HeisenbergGroup`](@ref) `M` with the left-invariant metric.
The expression reads
```math
\exp_{\begin{bmatrix} 1 & \mathbf{a}_p & c_p \\
\mathbf{0} & I_n & \mathbf{b}_p \\
0 & \mathbf{0} & 1 \end{bmatrix}}\left(\begin{bmatrix} 0 & \mathbf{a}_X & c_X \\
\mathbf{0} & 0_n & \mathbf{b}_X \\
0 & \mathbf{0} & 0 \end{bmatrix}\right) =
\begin{bmatrix} 1 & \mathbf{a}_p + \mathbf{a}_X & c_p + c_X + \mathbf{a}_X⋅\mathbf{b}_X/2 + \mathbf{a}_p⋅\mathbf{b}_X \\
\mathbf{0} & I_n & \mathbf{b}_p + \mathbf{b}_X \\
0 & \mathbf{0} & 1 \end{bmatrix}
```
where ``I_n`` is the ``n×n`` identity matrix, ``0_n`` is the ``n×n`` zero matrix
and ``\mathbf{a}⋅\mathbf{b}`` is dot product of vectors.
"""
exp(M::HeisenbergGroup, p, X)
function exp!(M::HeisenbergGroup{n}, q, p, X) where {n}
copyto!(q, I)
a_p_view = _heisenberg_a_view(M, p)
b_p_view = _heisenberg_b_view(M, p)
a_X_view = _heisenberg_a_view(M, X)
b_X_view = _heisenberg_b_view(M, X)
q[1, 2:(n + 1)] .= a_p_view .+ a_X_view
q[2:(n + 1), n + 2] .= b_p_view .+ b_X_view
q[1, n + 2] =
p[1, n + 2] + X[1, n + 2] + dot(a_X_view, b_X_view) / 2 + dot(a_p_view, b_X_view)
return q
end
@doc raw"""
injectivity_radius(M::HeisenbergGroup)
Return the injectivity radius on the [`HeisenbergGroup`](@ref) `M`, which is ``∞``.
"""
injectivity_radius(::HeisenbergGroup) = Inf
function inner(M::HeisenbergGroup{n}, p, X, Y) where {n}
X_a_view = _heisenberg_a_view(M, X)
X_b_view = _heisenberg_b_view(M, X)
Y_a_view = _heisenberg_a_view(M, Y)
Y_b_view = _heisenberg_b_view(M, Y)
return dot(X_a_view, Y_a_view) +
dot(X_b_view, Y_b_view) +
X[1, 2 * n + 1] * Y[1, 2 * n + 1]
end
@doc raw"""
log(G::HeisenbergGroup, p, q)
Compute the logarithmic map on the [`HeisenbergGroup`](@ref) group.
The formula reads
```math
\log_{\begin{bmatrix} 1 & \mathbf{a}_p & c_p \\
\mathbf{0} & I_n & \mathbf{b}_p \\
0 & \mathbf{0} & 1 \end{bmatrix}}\left(\begin{bmatrix} 1 & \mathbf{a}_q & c_q \\
\mathbf{0} & I_n & \mathbf{b}_q \\
0 & \mathbf{0} & 1 \end{bmatrix}\right) =
\begin{bmatrix} 0 & \mathbf{a}_q - \mathbf{a}_p & c_q - c_p + \mathbf{a}_p⋅\mathbf{b}_p - \mathbf{a}_q⋅\mathbf{b}_q - (\mathbf{a}_q - \mathbf{a}_p)⋅(\mathbf{b}_q - \mathbf{b}_p) / 2 \\
\mathbf{0} & 0_n & \mathbf{b}_q - \mathbf{b}_p \\
0 & \mathbf{0} & 0 \end{bmatrix}
```
where ``I_n`` is the ``n×n`` identity matrix, ``0_n`` is the ``n×n`` zero matrix
and ``\mathbf{a}⋅\mathbf{b}`` is dot product of vectors.
"""
log(::HeisenbergGroup, p, q)
function log!(M::HeisenbergGroup{n}, X, p, q) where {n}
fill!(X, 0)
a_p_view = _heisenberg_a_view(M, p)
b_p_view = _heisenberg_b_view(M, p)
a_q_view = _heisenberg_a_view(M, q)
b_q_view = _heisenberg_b_view(M, q)
X[1, 2:(n + 1)] .= a_q_view .- a_p_view
X[2:(n + 1), n + 2] .= b_q_view .- b_p_view
pinvq_c = dot(a_p_view, b_p_view) - p[1, n + 2] + q[1, n + 2] - dot(a_p_view, b_q_view)
X[1, n + 2] = pinvq_c - dot(a_q_view - a_p_view, b_q_view - b_p_view) / 2
return X
end
@doc raw"""
log_lie(M::HeisenbergGroup, p)
Lie group logarithm for the [`HeisenbergGroup`](@ref) `M` of the point `p`.
The formula reads
```math
\log\left(\begin{bmatrix} 1 & \mathbf{a} & c \\
\mathbf{0} & I_n & \mathbf{b} \\
0 & \mathbf{0} & 1 \end{bmatrix}\right) =
\begin{bmatrix} 0 & \mathbf{a} & c - \mathbf{a}⋅\mathbf{b}/2 \\
\mathbf{0} & 0_n & \mathbf{b} \\
0 & \mathbf{0} & 0 \end{bmatrix}
```
where ``I_n`` is the ``n×n`` identity matrix, ``0_n`` is the ``n×n`` zero matrix
and ``\mathbf{a}⋅\mathbf{b}`` is dot product of vectors.
"""
log_lie(M::HeisenbergGroup, p)
function log_lie!(M::HeisenbergGroup{n}, X, p) where {n}
fill!(X, 0)
view_a_X = _heisenberg_a_view(M, X)
view_b_X = _heisenberg_b_view(M, X)
view_a_X .= _heisenberg_a_view(M, p)
view_b_X .= _heisenberg_b_view(M, p)
X[1, n + 2] = p[1, n + 2] - dot(view_a_X, view_b_X) / 2
return X
end
function log_lie!(::HeisenbergGroup, X, ::Identity{MultiplicationOperation})
fill!(X, 0)
return X
end
manifold_dimension(::HeisenbergGroup{n}) where {n} = 2 * n + 1
parallel_transport_to(::HeisenbergGroup, p, X, q) = X
parallel_transport_to!(::HeisenbergGroup, Y, p, X, q) = copyto!(Y, X)
"""
project(M::HeisenbergGroup{n}, p)
Project a matrix `p` in the Euclidean embedding onto the [`HeisenbergGroup`](@ref) `M`.
Sets the diagonal elements to 1 and all non-diagonal elements except the first row and the
last column to 0.
"""
function project(M::HeisenbergGroup{n}, p) where {n}
return [
1 p[1, 2:(n + 2)]'
zeros(n, 1) Matrix(I, n, n) _heisenberg_b_view(M, p)
zeros(1, n + 1) 1
]
end
"""
project(M::HeisenbergGroup{n}, p, X)
Project a matrix `X` in the Euclidean embedding onto the Lie algebra of
[`HeisenbergGroup`](@ref) `M`.
Sets the diagonal elements to 0 and all non-diagonal elements except the first row and the
last column to 0.
"""
function project(M::HeisenbergGroup{n}, p, X) where {n}
return [
0 X[1, 2:(n + 2)]'
zeros(n, n + 1) _heisenberg_b_view(M, X)
zeros(1, n + 1) 0
]
end
function project!(M::HeisenbergGroup{n}, q, p) where {n}
copyto!(q, I)
q[1, 2:(n + 2)] .= p[1, 2:(n + 2)]
q[2:(n + 1), n + 2] .= _heisenberg_b_view(M, p)
return q
end
function project!(M::HeisenbergGroup{n}, Y, p, X) where {n}
fill!(Y, 0)
Y[1, 2:(n + 2)] .= X[1, 2:(n + 2)]
Y[2:(n + 1), n + 2] .= _heisenberg_b_view(M, X)
return Y
end
@doc raw"""
Random.rand(M::HeisenbergGroup; vector_at = nothing, σ::Real=1.0)
If `vector_at` is `nothing`, return a random point on the [`HeisenbergGroup`](@ref) `M`
by sampling elements of the first row and the last column from the normal distribution with
mean 0 and standard deviation `σ`.
If `vector_at` is not `nothing`, return a random tangent vector from the tangent space of
the point `vector_at` on the [`HeisenbergGroup`](@ref) by using a normal distribution with
mean 0 and standard deviation `σ`.
"""
rand(M::HeisenbergGroup; vector_at=nothing, σ::Real=1.0)
function Random.rand!(
rng::AbstractRNG,
::HeisenbergGroup{n},
pX;
σ::Real=one(eltype(pX)),
vector_at=nothing,
) where {n}
if vector_at === nothing
copyto!(pX, I)
va = view(pX, 1, 2:(n + 2))
randn!(rng, va)
va .*= σ
vb = view(pX, 2:(n + 1), n + 2)
randn!(rng, vb)
vb .*= σ
else
fill!(pX, 0)
randn!(rng, view(pX, 1, 2:(n + 2)))
randn!(rng, view(pX, 2:(n + 1), n + 2))
pX .*= σ
end
return pX
end
Base.show(io::IO, ::HeisenbergGroup{n}) where {n} = print(io, "HeisenbergGroup($n)")
translate_diff(::HeisenbergGroup, p, q, X, ::LeftForwardAction) = X
translate_diff(::HeisenbergGroup, p, q, X, ::RightBackwardAction) = p \ X * p
function translate_diff!(G::HeisenbergGroup, Y, p, q, X, conv::ActionDirection)
return copyto!(Y, translate_diff(G, p, q, X, conv))
end