Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce r-norms on manifolds with components #206

Merged
merged 26 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
61e6443
On manifolds, that consist of components, allow to (outer) r-norms.
kellertuer Oct 18, 2024
f229843
Resolve an ambiguity.
kellertuer Oct 18, 2024
77d5eb0
NEWS.md and Project.TOML.
kellertuer Oct 18, 2024
3f34424
adapt `norm(M, p, X)` as well to have an `r=2.0` for product and Defa…
kellertuer Oct 18, 2024
204dceb
Fix a final test for today.
kellertuer Oct 18, 2024
b64d5e5
runs formatter.
kellertuer Oct 18, 2024
d29bfba
Finish code features with the norm on pwer manifold to also have an r.
kellertuer Oct 18, 2024
ffafbeb
Finish norms.
kellertuer Oct 18, 2024
cab6856
Refactor power to be loadable more than once, extend code coverage.
kellertuer Oct 19, 2024
6765f14
Apply suggestions from code review
kellertuer Oct 19, 2024
151fbaf
further checks.
kellertuer Oct 19, 2024
afac395
Merge branch 'kellertuer/product_norms' of github.com:JuliaManifolds/…
kellertuer Oct 19, 2024
5258cf0
Maybe run formatter?
kellertuer Oct 19, 2024
e1b6059
add and test also -inf case.
kellertuer Oct 19, 2024
4947ba3
Fix a typo in code.
kellertuer Oct 19, 2024
7950513
Inverse Product Retractions.
kellertuer Oct 19, 2024
47ad4d3
Apply suggestions from code review
kellertuer Oct 19, 2024
aed8ed4
Rephrase docs.
kellertuer Oct 19, 2024
470e4a7
fix syntax errors that appeared in accepting changes.
kellertuer Oct 19, 2024
5637e7b
runs formatter.
kellertuer Oct 19, 2024
b6c48f1
Add yet another corner case.
kellertuer Oct 19, 2024
2489652
Maybe now?
kellertuer Oct 19, 2024
53a7ddf
improve style.
kellertuer Oct 19, 2024
008cc54
Apply suggestions from code review
kellertuer Oct 19, 2024
e7b3190
reactivate a few tests.
kellertuer Oct 19, 2024
577a5ad
Fix a few tests.
kellertuer Oct 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.15.18] 18/10/2024

### Added

* `distance(M, p, q, r)` to compute `r`-norms on manifolds that have components.
* `distance(M, p, q, m, r)` to compute (approximate) `r`-norms on manifolds that have components
using an `AbstractInverseRetractionMethod m` within every (inner) distance call.
* `norm(M, p, X, r)` to compute `r`-norms on manifolds that have components.

## [0.15.17] 04/10/2024

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ManifoldsBase"
uuid = "3362f125-f0bb-47a3-aa74-596ffd7ef2fb"
authors = ["Seth Axen <[email protected]>", "Mateusz Baran <[email protected]>", "Ronny Bergmann <[email protected]>", "Antoine Levitt <[email protected]>"]
version = "0.15.17"
version = "0.15.18"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
6 changes: 4 additions & 2 deletions src/DefaultManifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function check_approx(M::DefaultManifold, p, X, Y; kwargs...)
return ApproximatelyError(v, s)
end

distance(::DefaultManifold, p, q) = norm(p - q)
distance(::DefaultManifold, p, q, r::Real = 2.0) = norm(p - q, r)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

embed!(::DefaultManifold, q, p) = copyto!(q, p)

Expand Down Expand Up @@ -123,6 +123,8 @@ function get_vector_orthonormal!(M::DefaultManifold{ℂ}, Y, p, c, ::RealNumbers
return copyto!(Y, reshape(c[1:n] + c[(n + 1):(2n)] * 1im, representation_size(M)))
end

has_components(::DefaultManifold) = true

injectivity_radius(::DefaultManifold) = Inf

@inline inner(::DefaultManifold, p, X, Y) = dot(X, Y)
Expand All @@ -138,7 +140,7 @@ end

number_system(::DefaultManifold{𝔽}) where {𝔽} = 𝔽

norm(::DefaultManifold, p, X) = norm(X)
norm(::DefaultManifold, p, X, r = 2.0) = norm(X, r)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

project!(::DefaultManifold, q, p) = copyto!(q, p)
project!(::DefaultManifold, Y, p, X) = copyto!(Y, X)
Expand Down
10 changes: 10 additions & 0 deletions src/ManifoldsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,15 @@ function embed_project!(M::AbstractManifold, Y, p, X)
return project!(M, Y, p, embed(M, p, X))
end

"""
has_components(M::AbstractManifold)

Return whether the [`AbstractManifold`](@ref)`(M)` consists of components,
like the [`PowerManifold`](@ref) or the [`ProductManifold`](@ref), that one can iterate over.
By default, this function returns `false`.
"""
has_components(M::AbstractManifold) = false

@doc raw"""
injectivity_radius(M::AbstractManifold)

Expand Down Expand Up @@ -1354,6 +1363,7 @@ export ×,
get_vector,
get_vector!,
get_vectors,
has_components,
hat,
hat!,
injectivity_radius,
Expand Down
145 changes: 129 additions & 16 deletions src/PowerManifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,20 +530,103 @@
return default_vector_transport_method(M.manifold, eltype(t))
end

@doc raw"""
distance(M::AbstractPowerManifold, p, q)
_doc_distance_pow = """
distance(M::AbstractPowerManifold, p, q, r::Real=2.0)
distance(M::AbstractPowerManifold, p, q, m::AbstractInverseRetractionMethod=LogarithmicInverseRetraction(), r::Real=2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

Compute the distance between `q` and `p` on an [`AbstractPowerManifold`](@ref).

Compute the distance between `q` and `p` on an [`AbstractPowerManifold`](@ref),
i.e. from the element wise distances the Forbenius norm is computed.
First, the componentwise distances are computed. These can be approximated using the
`norm` of an [`AbstractInverseRetractionMethod`](@ref) `m`.
Then, the `r`-norm of these elements is computed.
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
"""

function distance(M::AbstractPowerManifold, p, q)
sum_squares = zero(number_eltype(p))
return _distance_r(M, p, q, 2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
end

@doc "$(_doc_distance_pow)"
function distance(M::AbstractPowerManifold, p, q, r::Real)
isinf(r) && return _distance_Inf(M, p, q)
(r == 1) && return _distance_1(M, p, q)
return _distance_r(M, p, q, r)
end
function distance(
M::AbstractPowerManifold,
p,
q,
::LogarithmicInverseRetraction,
r::Real = 2.0,
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
)
return distance(M, p, q, r)
end

@doc "$(_doc_distance_pow)"
function distance(

Check warning on line 565 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L565

Added line #L565 was not covered by tests
M::AbstractPowerManifold,
p,
q,
m::AbstractInverseRetractionMethod,
r::Real = 2.0,
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
)
isinf(r) && return _distance_Inf(M, p, q, m)
(r == 1) && return _distance_1(M, p, q, m)
return _distance_Inf(M, p, q, r)

Check warning on line 574 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L572-L574

Added lines #L572 - L574 were not covered by tests
end
#
#
# The three special cases
function _distance_r(M::AbstractPowerManifold, p, q, m::AbstractInverseRetractionMethod, r)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
rep_size = representation_size(M.manifold)
values = [

Check warning on line 581 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L579-L581

Added lines #L579 - L581 were not covered by tests
distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i), m) for
i in get_iterator(M)
]
return norm(values, r)

Check warning on line 585 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L585

Added line #L585 was not covered by tests
end
function _distance_r(M::AbstractPowerManifold, p, q, r)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
rep_size = representation_size(M.manifold)
values = [
distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i)) for
i in get_iterator(M)
]
return norm(values, r)
end
function _distance_1(M::AbstractPowerManifold, p, q, m::AbstractInverseRetractionMethod)
s = zero(number_eltype(p))
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
s += distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i), m)
end
return s

Check warning on line 601 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L595-L601

Added lines #L595 - L601 were not covered by tests
end
function _distance_1(M::AbstractPowerManifold, p, q)
s = zero(number_eltype(p))
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
s += distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i))
end
return s
end
function _distance_Inf(M::AbstractPowerManifold, p, q, m::AbstractInverseRetractionMethod)
d = 0.0
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
v = 0.0

Check warning on line 613 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L611-L613

Added lines #L611 - L613 were not covered by tests
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
v = distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i), m)
d = (v > d) ? v : d

Check warning on line 617 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L616-L617

Added lines #L616 - L617 were not covered by tests
end
return sqrt(sum_squares)
return d

Check warning on line 619 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L619

Added line #L619 was not covered by tests
end
function _distance_Inf(M::AbstractPowerManifold, p, q)
d = 0.0
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
v = 0.0
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
v = distance(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, q, i))
d = (v > d) ? v : d
end
return d
end

@doc raw"""
Expand Down Expand Up @@ -881,6 +964,13 @@
return TangentSpace(M.manifold, p[M, I...])
end

"""
has_components(::AbstractPowerManifold)

Return `true, since points on an [`AbstractPowerManifold`](@ref) consist of components.
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
"""
has_components(::AbstractPowerManifold) = true

@doc raw"""
injectivity_radius(M::AbstractPowerManifold[, p])

Expand Down Expand Up @@ -1092,23 +1182,46 @@
end

@doc raw"""
norm(M::AbstractPowerManifold, p, X)
norm(M::AbstractPowerManifold, p, X, r::REal=2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

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.
[`AbstractPowerManifold`](@ref) `M`, i.e. from the element wise norms `r`-norm is computed,
where the default `r=2.0` yields the Frobenius norm is computed.
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
"""
function LinearAlgebra.norm(M::AbstractPowerManifold, p, X)
sum_squares = zero(number_eltype(X))
function LinearAlgebra.norm(M::AbstractPowerManifold, p, X, r::Real = 2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
isinf(r) && return _norm_Inf(M, p, X)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
(r == 1) && return _norm_1(M, p, X)
return _norm_r(M, p, X, r)
end
function _norm_r(M::AbstractPowerManifold, p, X, r)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
rep_size = representation_size(M.manifold)
values = [
norm(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i)) for
i in get_iterator(M)
]
return norm(values, r)
end
function _norm_1(M::AbstractPowerManifold, p, X)
s = zero(number_eltype(p))
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
s += norm(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i))
end
return s

Check warning on line 1210 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L1204-L1210

Added lines #L1204 - L1210 were not covered by tests
end
function _norm_Inf(M::AbstractPowerManifold, p, X)
d = 0.0
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
v = 0.0

Check warning on line 1214 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L1212-L1214

Added lines #L1212 - L1214 were not covered by tests
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
sum_squares +=
norm(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i))^2
v = norm(M.manifold, _read(M, rep_size, p, i), _read(M, rep_size, X, i))
d = (v > d) ? v : d

Check warning on line 1218 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L1217-L1218

Added lines #L1217 - L1218 were not covered by tests
end
return sqrt(sum_squares)
return d

Check warning on line 1220 in src/PowerManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/PowerManifold.jl#L1220

Added line #L1220 was not covered by tests
end



function parallel_transport_direction!(M::AbstractPowerManifold, Y, p, X, d)
rep_size = representation_size(M.manifold)
for i in get_iterator(M)
Expand Down
82 changes: 55 additions & 27 deletions src/ProductManifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,49 @@
return ProductVectorTransport(map(default_vector_transport_method, M.manifolds)...)
end

@doc raw"""
distance(M::ProductManifold, p, q)

Compute the distance between two points `p` and `q` on the [`ProductManifold`](@ref) `M`, which is
the 2-norm of the elementwise distances on the internal manifolds that build `M`.
"""
function distance(M::ProductManifold, p, q)
return sqrt(
sum(
map(
distance,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
) .^ 2,
_doc_distance_prod = """
distance(M::ProductManifold, p, q, r::Real=2.0)
distance(M::ProductManifold, p, q, m::AbstractInverseRetractionMethod=LogarithmicInverseRetraction(), r::Real=2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

Compute the distance between `q` and `p` on an [`ProductManifold`](@ref).

First, the componentwise distances are computed. These can be approximated using the
`norm` of an [`AbstractInverseRetractionMethod`](@ref) `m`.
Then, the `r`-norm of these elements is computed.
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
"""

@doc "$(_doc_distance_prod)"
function distance(M::ProductManifold, p, q, r::Real = 2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
return norm(
map(
distance,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
),
r,
)
end
function distance(M::ProductManifold, p, q, ::LogarithmicInverseRetraction, r::Real = 2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
return distance(M, p, q, r)

Check warning on line 399 in src/ProductManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/ProductManifold.jl#L398-L399

Added lines #L398 - L399 were not covered by tests
end

@doc "$(_doc_distance_prod)"
function distance(

Check warning on line 403 in src/ProductManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/ProductManifold.jl#L403

Added line #L403 was not covered by tests
M::ProductManifold,
p,
q,
m::AbstractInverseRetractionMethod,
r::Real = 2.0,
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
)
return norm(

Check warning on line 410 in src/ProductManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/ProductManifold.jl#L410

Added line #L410 was not covered by tests
map(
(M, p, q) -> distance(M, p, q, m),

Check warning on line 412 in src/ProductManifold.jl

View check run for this annotation

Codecov / codecov/patch

src/ProductManifold.jl#L412

Added line #L412 was not covered by tests
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, q),
),
r,
)
end

Expand Down Expand Up @@ -557,6 +584,13 @@
return X
end

"""
has_components(::ProductManifold)

Return `true` since points on an [`ProductManifold`](@ref) consist of components.
"""
has_components(::ProductManifold) = true

@doc raw"""
injectivity_radius(M::ProductManifold)
injectivity_radius(M::ProductManifold, x)
Expand Down Expand Up @@ -725,21 +759,15 @@
end

@doc raw"""
norm(M::ProductManifold, p, X)
norm(M::ProductManifold, p, X, r::Real=2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved

Compute the norm of `X` from the tangent space of `p` on the [`ProductManifold`](@ref),
Compute the (`r`-)norm of `X` from the tangent space of `p` on the [`ProductManifold`](@ref),
i.e. from the element wise norms the 2-norm is computed.
"""
function LinearAlgebra.norm(M::ProductManifold, p, X)
norms_squared = (
map(
norm,
M.manifolds,
submanifold_components(M, p),
submanifold_components(M, X),
) .^ 2
)
return sqrt(sum(norms_squared))
function LinearAlgebra.norm(M::ProductManifold, p, X, r::Real = 2.0)
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
norms =
(map(norm, M.manifolds, submanifold_components(M, p), submanifold_components(M, X)))
return norm(norms, r)
end

"""
Expand Down
1 change: 1 addition & 0 deletions test/default_manifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ using ManifoldsBaseTestUtils
@test isa(manifold_dimension(M), Integer)
@test manifold_dimension(M) ≥ 0
@test base_manifold(M) == M
@test has_components(M)
@test number_system(M) == ManifoldsBase.ℝ
@test ManifoldsBase.representation_size(M) == (3,)

Expand Down
1 change: 1 addition & 0 deletions test/empty_manifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using Test
@test base_manifold(M) === M
@test number_system(M) === ℝ
@test representation_size(M) === nothing
@test !has_components(M)

@test_throws MethodError manifold_dimension(M)

Expand Down
Loading
Loading