-
Notifications
You must be signed in to change notification settings - Fork 56
/
rotation_action.jl
294 lines (242 loc) · 8.55 KB
/
rotation_action.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
@doc raw"""
RotationAction(
M::AbstractManifold,
SOn::SpecialOrthogonal,
AD::ActionDirection = LeftAction(),
)
Space of actions of the [`SpecialOrthogonal`](@ref) group $\mathrm{SO}(n)$ on a
Euclidean-like manifold `M` of dimension `n`.
"""
struct RotationAction{TAD<:ActionDirection,TM<:AbstractManifold,TSO<:SpecialOrthogonal} <:
AbstractGroupAction{TAD}
manifold::TM
SOn::TSO
end
function RotationAction(
M::AbstractManifold,
SOn::SpecialOrthogonal,
::TAD=LeftAction(),
) where {TAD<:ActionDirection}
return RotationAction{TAD,typeof(M),typeof(SOn)}(M, SOn)
end
function Base.show(io::IO, A::RotationAction)
return print(io, "RotationAction($(A.manifold), $(A.SOn), $(direction(A)))")
end
const RotationActionOnVector{TAD,𝔽,TE,TSO} = RotationAction{
TAD,
<:Union{Euclidean{TE,𝔽},TranslationGroup{TE,𝔽}},
SpecialOrthogonal{TSO},
} where {TAD<:ActionDirection,𝔽,TE,TSO}
base_group(A::RotationAction) = A.SOn
group_manifold(A::RotationAction) = A.manifold
function switch_direction(A::RotationAction{TAD}) where {TAD<:ActionDirection}
return RotationAction(A.manifold, A.SOn, switch_direction(TAD()))
end
apply(::RotationActionOnVector{LeftAction}, a, p) = a * p
function apply(A::RotationActionOnVector{RightAction}, a, p)
return inv(base_group(A), a) * p
end
apply!(::RotationActionOnVector{LeftAction}, q, a, p) = mul!(q, a, p)
function inverse_apply(A::RotationActionOnVector{LeftAction}, a, p)
return inv(base_group(A), a) * p
end
inverse_apply(::RotationActionOnVector{RightAction}, a, p) = a * p
apply_diff(::RotationActionOnVector{LeftAction}, a, p, X) = a * X
function apply_diff(
::RotationActionOnVector{LeftAction},
::Identity{MultiplicationOperation},
p,
X,
)
return X
end
function apply_diff(A::RotationActionOnVector{RightAction}, a, p, X)
return inv(base_group(A), a) * X
end
function apply_diff!(::RotationActionOnVector{LeftAction}, Y, a, p, X)
return mul!(Y, a, X)
end
function apply_diff!(A::RotationActionOnVector{RightAction}, Y, a, p, X)
return mul!(Y, inv(base_group(A), a), X)
end
function apply_diff_group(::RotationActionOnVector{LeftAction}, ::Identity, X, p)
return X * p
end
function apply_diff_group!(::RotationActionOnVector{LeftAction}, Y, ::Identity, X, p)
Y .= X * p
return Y
end
function inverse_apply_diff(A::RotationActionOnVector{LeftAction}, a, p, X)
return inv(base_group(A), a) * X
end
function inverse_apply_diff(::RotationActionOnVector{RightAction}, a, p, X)
return a * X
end
function optimal_alignment(::RotationActionOnVector{LeftAction}, p, q)
Xmul = p * transpose(q)
F = svd(Xmul)
L = size(Xmul)[2]
UVt = F.U * F.Vt
Ostar = det(UVt) ≥ 0 ? UVt : F.U * Diagonal([i < L ? 1 : -1 for i in 1:L]) * F.Vt
return convert(typeof(Xmul), Ostar)
end
function optimal_alignment(A::RotationActionOnVector{RightAction}, p, q)
return optimal_alignment(switch_direction(A), q, p)
end
@doc raw"""
RotationAroundAxisAction(axis::AbstractVector)
Space of actions of the circle group [`RealCircleGroup`](@ref) on $ℝ^3$ around given `axis`.
"""
struct RotationAroundAxisAction{TA<:AbstractVector} <: AbstractGroupAction{LeftAction}
axis::TA
end
base_group(::RotationAroundAxisAction) = RealCircleGroup()
group_manifold(::RotationAroundAxisAction) = Euclidean(3)
@doc raw"""
apply(A::RotationAroundAxisAction, θ, p)
Rotate point `p` from [`Euclidean(3)`](@ref) manifold around axis `A.axis` by angle `θ`.
The formula reads
````math
p_{rot} = (\cos(θ))p + (k×p) \sin(θ) + k (k⋅p) (1-\cos(θ)),
````
where $k$ is the vector `A.axis` and `⋅` is the dot product.
"""
function apply(A::RotationAroundAxisAction, θ, p)
sθ, cθ = sincos(θ)
apd = dot(A.axis, p)
return p .* cθ .+ cross(A.axis, p) .* sθ .+ A.axis .* apd .* (1 - cθ)
end
apply(::RotationAroundAxisAction, ::Identity{AdditionOperation}, p) = p
function apply(A::RotationAroundAxisAction, θ::AbstractArray, p)
# this method is here to make sure that θ represented by 1-element vectors works
return apply(A, θ[], p)
end
function apply!(A::RotationAroundAxisAction, q, θ, p)
return copyto!(q, apply(A, θ, p))
end
function inverse_apply(A::RotationAroundAxisAction, θ, p)
return apply(A, -θ, p)
end
###
@doc raw"""
RowwiseMultiplicationAction{
TAD<:ActionDirection,
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} <: AbstractGroupAction{TAD}
Action of the (special) unitary or orthogonal group [`GeneralUnitaryMultiplicationGroup`](@ref)
of type `On` columns of points on a matrix manifold `M`.
# Constructor
RowwiseMultiplicationAction(
M::AbstractManifold,
On::GeneralUnitaryMultiplicationGroup,
AD::ActionDirection = LeftAction(),
)
"""
struct RowwiseMultiplicationAction{
TAD<:ActionDirection,
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} <: AbstractGroupAction{TAD}
manifold::TM
On::TO
end
function RowwiseMultiplicationAction(
M::AbstractManifold,
On::GeneralUnitaryMultiplicationGroup,
::TAD=LeftAction(),
) where {TAD<:ActionDirection}
return RowwiseMultiplicationAction{TAD,typeof(M),typeof(On)}(M, On)
end
const LeftRowwiseMultiplicationAction{
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} = RowwiseMultiplicationAction{LeftAction,TM,TO}
function apply(::LeftRowwiseMultiplicationAction, a, p)
return (a * p')'
end
function apply(::LeftRowwiseMultiplicationAction, ::Identity{MultiplicationOperation}, p)
return p
end
function apply!(::LeftRowwiseMultiplicationAction, q, a, p)
return map((qrow, prow) -> mul!(qrow, a, prow), eachrow(q), eachrow(p))
end
base_group(A::RowwiseMultiplicationAction) = A.On
group_manifold(A::RowwiseMultiplicationAction) = A.manifold
function inverse_apply(::LeftRowwiseMultiplicationAction, a, p)
return (a \ p')'
end
###
@doc raw"""
ColumnwiseMultiplicationAction{
TAD<:ActionDirection,
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} <: AbstractGroupAction{TAD}
Action of the (special) unitary or orthogonal group [`GeneralUnitaryMultiplicationGroup`](@ref)
of type `On` columns of points on a matrix manifold `M`.
# Constructor
ColumnwiseMultiplicationAction(
M::AbstractManifold,
On::GeneralUnitaryMultiplicationGroup,
AD::ActionDirection = LeftAction(),
)
"""
struct ColumnwiseMultiplicationAction{
TAD<:ActionDirection,
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} <: AbstractGroupAction{TAD}
manifold::TM
On::TO
end
function ColumnwiseMultiplicationAction(
M::AbstractManifold,
On::GeneralUnitaryMultiplicationGroup,
::TAD=LeftAction(),
) where {TAD<:ActionDirection}
return ColumnwiseMultiplicationAction{TAD,typeof(M),typeof(On)}(M, On)
end
const LeftColumnwiseMultiplicationAction{
TM<:AbstractManifold,
TO<:GeneralUnitaryMultiplicationGroup,
} = ColumnwiseMultiplicationAction{LeftAction,TM,TO}
function apply(::LeftColumnwiseMultiplicationAction, a, p)
return a * p
end
function apply(::LeftColumnwiseMultiplicationAction, ::Identity{MultiplicationOperation}, p)
return p
end
function apply!(::LeftColumnwiseMultiplicationAction, q, a, p)
return map((qrow, prow) -> mul!(qrow, a, prow), eachcol(q), eachcol(p))
end
base_group(A::LeftColumnwiseMultiplicationAction) = A.On
group_manifold(A::LeftColumnwiseMultiplicationAction) = A.manifold
function inverse_apply(::LeftColumnwiseMultiplicationAction, a, p)
return a \ p
end
@doc raw"""
optimal_alignment(A::LeftColumnwiseMultiplicationAction, p, q)
Compute optimal alignment for the left [`ColumnwiseMultiplicationAction`](@ref), i.e. the
group element ``O^{*}`` that, when it acts on `p`, returns the point closest to `q`. Details
of computation are described in Section 2.2.1 of [SrivastavaKlassen:2016](@cite).
The formula reads
```math
O^{*} = \begin{cases}
UV^T & \text{if } \operatorname{det}(p q^{\mathrm{T}}) \geq 0\\
U K V^{\mathrm{T}} & \text{otherwise}
\end{cases}
```
where ``U \Sigma V^{\mathrm{T}}`` is the SVD decomposition of ``p q^{\mathrm{T}}`` and ``K``
is the unit diagonal matrix with the last element on the diagonal replaced with -1.
"""
function optimal_alignment(A::LeftColumnwiseMultiplicationAction, p, q)
is_point(A.manifold, p; error=:error)
is_point(A.manifold, q; error=:error)
Xmul = p * transpose(q)
F = svd(Xmul)
L = size(Xmul)[2]
UVt = F.U * F.Vt
Ostar = det(UVt) ≥ 0 ? UVt : F.U * Diagonal([i < L ? 1 : -1 for i in 1:L]) * F.Vt
return convert(typeof(Xmul), Ostar)
end