/
Euclidean.jl
434 lines (360 loc) · 13.3 KB
/
Euclidean.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
@doc raw"""
Euclidean{T<:Tuple,𝔽} <: Manifold{𝔽}
Euclidean vector space.
# Constructor
Euclidean(n)
Generate the $n$-dimensional vector space $ℝ^n$.
Euclidean(n₁,n₂,...,nᵢ; field=ℝ)
𝔽^(n₁,n₂,...,nᵢ) = Euclidean(n₁,n₂,...,nᵢ; field=𝔽)
Generate the vector space of $k = n_1 \cdot n_2 \cdot … \cdot n_i$ values, i.e. the
manifold $𝔽^{n_1, n_2, …, n_i}$, $𝔽\in\{ℝ,ℂ\}$, whose
elements are interpreted as $n_1 × n_2 × … × n_i$ arrays.
For $i=2$ we obtain a matrix space.
The default `field=ℝ` can also be set to `field=ℂ`.
The dimension of this space is $k \dim_ℝ 𝔽$, where $\dim_ℝ 𝔽$ is the
[`real_dimension`](@ref) of the field $𝔽$.
"""
struct Euclidean{N,𝔽} <: Manifold{𝔽} where {N<:Tuple} end
function Euclidean(n::Vararg{Int,I}; field::AbstractNumbers = ℝ) where {I}
return Euclidean{Tuple{n...},field}()
end
Base.:^(𝔽::AbstractNumbers, n) = Euclidean(n...; field = 𝔽)
"""
EuclideanMetric <: RiemannianMetric
A general type for any manifold that employs the Euclidean Metric, for example
the [`Euclidean`](@ref) manifold itself, or the [`Sphere`](@ref), where every
tangent space (as a plane in the embedding) uses this metric (in the embedding).
Since the metric is independent of the field type, this metric is also used for
the Hermitian metrics, i.e. metrics that are analogous to the `EuclideanMetric`
but where the field type of the manifold is `ℂ`.
This metric is the default metric for example for the [`Euclidean`](@ref) manifold.
"""
struct EuclideanMetric <: RiemannianMetric end
Base.:^(M::Euclidean, n::Int) = ^(M, (n,))
function Base.:^(::Euclidean{T,𝔽}, n::NTuple{N,Int}) where {T,𝔽,N}
return Euclidean{Tuple{T.parameters...,n...},𝔽}()
end
function allocation_promotion_function(
M::Euclidean{<:Tuple,ℂ},
::Union{typeof(get_vector),typeof(get_coordinates)},
args::Tuple,
)
return complex
end
function check_manifold_point(M::Euclidean{N,𝔽}, p) where {N,𝔽}
if (𝔽 === ℝ) && !(eltype(p) <: Real)
return DomainError(
eltype(p),
"The matrix $(p) is not a real-valued matrix, so it does not lie on $(M).",
)
end
if (𝔽 === ℂ) && !(eltype(p) <: Real) && !(eltype(p) <: Complex)
return DomainError(
eltype(p),
"The matrix $(p) is neither a real- nor complex-valued matrix, so it does not lie on $(M).",
)
end
if size(p) != representation_size(M)
return DomainError(
size(p),
"The matrix $(p) does not lie on $(M), since its dimensions ($(size(p))) are wrong (expected: $(representation_size(M))).",
)
end
return nothing
end
function check_tangent_vector(
M::Euclidean{N,𝔽},
p,
X;
check_base_point = true,
kwargs...,
) where {N,𝔽}
if check_base_point
mpe = check_manifold_point(M, p; kwargs...)
mpe === nothing || return mpe
end
if (𝔽 === ℝ) && !(eltype(X) <: Real)
return DomainError(
eltype(X),
"The matrix $(X) is not a real-valued matrix, so it can not be a tangent vector to $(p) on $(M).",
)
end
if (𝔽 === ℂ) && !(eltype(X) <: Real) && !(eltype(X) <: Complex)
return DomainError(
eltype(X),
"The matrix $(X) is neither a real- nor complex-valued matrix, so it can not be a tangent vector to $(p) on $(M).",
)
end
if size(X) != representation_size(M)
return DomainError(
size(X),
"The matrix $(X) does not lie in the tangent space of $(p) on $(M), since its dimensions $(size(X)) are wrong (expected: $(representation_size(M))).",
)
end
return nothing
end
function det_local_metric(::MetricManifold{𝔽,<:Manifold,EuclideanMetric}, p) where {𝔽}
return one(eltype(p))
end
"""
distance(M::Euclidean, p, q)
Compute the Euclidean distance between two points on the [`Euclidean`](@ref)
manifold `M`, i.e. for vectors it's just the norm of the difference, for matrices
and higher order arrays, the matrix and ternsor Frobenius norm, respectively.
"""
distance(::Euclidean, p, q) = norm(p .- q)
@doc raw"""
exp(M::Euclidean, p, X)
Compute the exponential map on the [`Euclidean`](@ref) manifold `M` from `p` in direction
`X`, which in this case is just
````math
\exp_p X = p + X.
````
"""
exp(::Euclidean, ::Any...)
exp!(M::Euclidean, q, p, X) = (q .= p .+ X)
"""
flat(M::Euclidean, p, X)
Transform a tangent vector `X` into a cotangent. Since they can directly be identified in the
[`Euclidean`](@ref) case, this yields just the identity for a tangent vector `w` in the
tangent space of `p` on `M`.
"""
flat(::Euclidean, ::Any...)
flat!(M::Euclidean, ξ::CoTFVector, p, X::TFVector) = copyto!(ξ, X)
function get_basis(M::Euclidean, p, B::DefaultOrthonormalBasis{ℝ})
vecs = [_euclidean_basis_vector(p, i) for i in eachindex(p)]
return CachedBasis(B, vecs)
end
function get_basis(M::Euclidean{<:Tuple,ℂ}, p, B::DefaultOrthonormalBasis{ℂ})
vecs = [_euclidean_basis_vector(p, i) for i in eachindex(p)]
return CachedBasis(B, [vecs; im * vecs])
end
function get_basis(M::Euclidean, p, B::DiagonalizingOrthonormalBasis)
vecs = get_vectors(M, p, get_basis(M, p, DefaultOrthonormalBasis()))
eigenvalues = zeros(real(eltype(p)), manifold_dimension(M))
return CachedBasis(B, DiagonalizingBasisData(B.frame_direction, eigenvalues, vecs))
end
function get_coordinates!(M::Euclidean, Y, p, X, B::DefaultOrDiagonalizingBasis{ℝ})
S = representation_size(M)
PS = prod(S)
copyto!(Y, reshape(X, PS))
return Y
end
function get_coordinates!(
M::Euclidean{<:Tuple,ℂ},
Y,
p,
X,
B::DefaultOrDiagonalizingBasis{ℂ},
)
S = representation_size(M)
PS = prod(S)
Y .= [reshape(real.(X), PS)..., reshape(imag(X), PS)...]
return Y
end
function get_vector!(M::Euclidean, Y, p, X, B::DefaultOrDiagonalizingBasis{ℝ})
S = representation_size(M)
Y .= reshape(X, S)
return Y
end
function get_vector!(M::Euclidean{<:Tuple,ℂ}, Y, p, X, B::DefaultOrDiagonalizingBasis{ℂ})
S = representation_size(M)
N = div(length(X), 2)
Y .= reshape(X[1:N] + im * X[(N + 1):end], S)
return Y
end
@doc raw"""
injectivity_radius(M::Euclidean)
Return the injectivity radius on the [`Euclidean`](@ref) `M`, which is $∞$.
"""
injectivity_radius(::Euclidean) = Inf
@doc raw"""
inner(M::Euclidean, p, X, Y)
Compute the inner product on the [`Euclidean`](@ref) `M`, which is just
the inner product on the real-valued or complex valued vector space
of arrays (or tensors) of size $n_1 × n_2 × … × n_i$, i.e.
````math
g_p(X,Y) = \sum_{k ∈ I} \overline{X}_{k} Y_{k},
````
where $I$ is the set of vectors $k ∈ ℕ^i$, such that for all
$1 ≤ j ≤ i$ it holds $1 ≤ k_j ≤ n_j$ and $\overline{\cdot}$ denotes the complex conjugate.
For the special case of $i ≤ 2$, i.e. matrices and vectors, this simplifies to
````math
g_p(X,Y) = X^{\mathrm{H}}Y,
````
where $\cdot^{\mathrm{H}}$ denotes the Hermitian, i.e. complex conjugate transposed.
"""
inner(::Euclidean, ::Any...)
@inline inner(::Euclidean, p, X, Y) = dot(X, Y)
@inline inner(::MetricManifold{𝔽,<:Manifold,EuclideanMetric}, p, X, Y) where {𝔽} = dot(X, Y)
function inverse_local_metric(M::MetricManifold{𝔽,<:Manifold,EuclideanMetric}, p) where {𝔽}
return local_metric(M, p)
end
default_metric_dispatch(::Euclidean, ::EuclideanMetric) = Val(true)
function local_metric(::MetricManifold{𝔽,<:Manifold,EuclideanMetric}, p) where {𝔽}
return Diagonal(ones(SVector{size(p, 1),eltype(p)}))
end
@doc raw"""
log(M::Euclidean, p, q)
Compute the logarithmic map on the [`Euclidean`](@ref) `M` from `p` to `q`,
which in this case is just
````math
\log_p q = q-p.
````
"""
log(::Euclidean, ::Any...)
log!(M::Euclidean, X, p, q) = (X .= q .- p)
function log_local_metric_density(
::MetricManifold{𝔽,<:Manifold,EuclideanMetric},
p,
) where {𝔽}
return zero(eltype(p))
end
@generated _product_of_dimensions(::Euclidean{N}) where {N} = prod(N.parameters)
"""
manifold_dimension(M::Euclidean)
Return the manifold dimension of the [`Euclidean`](@ref) `M`, i.e.
the product of all array dimensions and the [`real_dimension`](@ref) of the
underlying number system.
"""
function manifold_dimension(M::Euclidean{N,𝔽}) where {N,𝔽}
return _product_of_dimensions(M) * real_dimension(𝔽)
end
Statistics.mean(::Euclidean{Tuple{1}}, x::AbstractVector{<:Number}; kwargs...) = mean(x)
function Statistics.mean(
::Euclidean{Tuple{1}},
x::AbstractVector{<:Number},
w::AbstractWeights;
kwargs...,
)
return mean(x, w)
end
Statistics.mean(::Euclidean, x::AbstractVector; kwargs...) = mean(x)
function Statistics.mean!(M::Euclidean, p, x::AbstractVector, w::AbstractVector; kwargs...)
return mean!(M, p, x, w, GeodesicInterpolation(); kwargs...)
end
function StatsBase.mean_and_var(
::Euclidean{Tuple{1}},
x::AbstractVector{<:Number};
kwargs...,
)
m, v = mean_and_var(x; kwargs...)
return m, sum(v)
end
function StatsBase.mean_and_var(
::Euclidean{Tuple{1}},
x::AbstractVector{<:Number},
w::AbstractWeights;
corrected = false,
kwargs...,
)
m, v = mean_and_var(x, w; corrected = corrected, kwargs...)
return m, sum(v)
end
function StatsBase.mean_and_var(
M::Euclidean,
x::AbstractVector,
w::AbstractWeights;
kwargs...,
)
return mean_and_var(M, x, w, GeodesicInterpolation(); kwargs...)
end
Statistics.median(::Euclidean{Tuple{1}}, x::AbstractVector{<:Number}; kwargs...) = median(x)
function Statistics.median(
::Euclidean{Tuple{1}},
x::AbstractVector{<:Number},
w::AbstractWeights;
kwargs...,
)
return median(x, w)
end
function Statistics.median!(::Euclidean{Tuple{1}}, p, x::AbstractVector; kwargs...)
return copyto!(p, [median(vcat(x...))])
end
function Statistics.median!(
::Euclidean{Tuple{1}},
p,
x::AbstractVector,
w::AbstractWeights;
kwargs...,
)
return copyto!(p, [median(vcat(x...), w)])
end
@doc raw"""
norm(M::Euclidean, p, X)
Compute the norm of a tangent vector `X` at `p` on the [`Euclidean`](@ref)
`M`, i.e. since every tangent space can be identified with `M` itself
in this case, just the (Frobenius) norm of `X`.
"""
LinearAlgebra.norm(::Euclidean, p, X) = norm(X)
LinearAlgebra.norm(::MetricManifold{ℝ,<:Manifold,EuclideanMetric}, p, X) = norm(X)
"""
normal_tvector_distribution(M::Euclidean, p, σ)
Normal distribution in ambient space with standard deviation `σ`
projected to tangent space at `p`.
"""
function normal_tvector_distribution(M::Euclidean{Tuple{N}}, p, σ) where {N}
d = Distributions.MvNormal(zero(p), σ)
return ProjectedFVectorDistribution(TangentBundleFibers(M), p, d, project!, p)
end
@doc raw"""
project(M::Euclidean, p)
Project an arbitrary point `p` onto the [`Euclidean`](@ref) manifold `M`, which
is of course just the identity map.
"""
project(::Euclidean, ::Any)
project!(M::Euclidean, q, p) = copyto!(q, p)
"""
project(M::Euclidean, p, X)
Project an arbitrary vector `X` into the tangent space of a point `p` on the
[`Euclidean`](@ref) `M`, which is just the identity, since any tangent
space of `M` can be identified with all of `M`.
"""
project(::Euclidean, ::Any, ::Any)
project!(M::Euclidean, Y, p, X) = copyto!(Y, X)
"""
projected_distribution(M::Euclidean, d, [p])
Wrap the standard distribution `d` into a manifold-valued distribution. Generated
points will be of similar type to `p`. By default, the type is not changed.
"""
function projected_distribution(M::Euclidean, d, p)
return ProjectedPointDistribution(M, d, project!, p)
end
function projected_distribution(M::Euclidean, d)
return ProjectedPointDistribution(M, d, project!, rand(d))
end
"""
representation_size(M::Euclidean)
Return the array dimensions required to represent an element on the
[`Euclidean`](@ref) `M`, i.e. the vector of all array dimensions.
"""
@generated representation_size(::Euclidean{N}) where {N} = size_to_tuple(N)
"""
sharp(M::Euclidean, p, ξ)
Transform the cotangent vector `ξ` at `p` on the [`Euclidean`](@ref) `M` to a tangent vector `X`.
Since cotangent and tangent vectors can directly be identified in the [`Euclidean`](@ref)
case, this yields just the identity.
"""
sharp(::Euclidean, ::Any...)
sharp!(M::Euclidean, X::TFVector, p, ξ::CoTFVector) = copyto!(X, ξ)
function Base.show(io::IO, ::Euclidean{N,𝔽}) where {N,𝔽}
return print(io, "Euclidean($(join(N.parameters, ", ")); field = $(𝔽))")
end
"""
vector_transport_to(M::Euclidean, p, X, q, ::ParallelTransport)
Parallely transport the vector `X` from the tangent space at `p` to the tangent space at `q`
on the [`Euclidean`](@ref) `M`, which simplifies to the identity.
"""
vector_transport_to(::Euclidean, ::Any, ::Any, ::Any, ::ParallelTransport)
vector_transport_to!(M::Euclidean, Y, p, X, q, ::ParallelTransport) = copyto!(Y, X)
Statistics.var(::Euclidean, x::AbstractVector; kwargs...) = sum(var(x; kwargs...))
function Statistics.var(::Euclidean, x::AbstractVector{T}, m::T; kwargs...) where {T}
return sum(var(x; mean = m, kwargs...))
end
"""
zero_tangent_vector(M::Euclidean, x)
Return the zero vector in the tangent space of `x` on the [`Euclidean`](@ref)
`M`, which here is just a zero filled array the same size as `x`.
"""
zero_tangent_vector(::Euclidean, ::Any...)
zero_tangent_vector!(M::Euclidean, v, x) = fill!(v, 0)