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

Introduces the manifold of centered matrices #185

Merged
merged 27 commits into from
May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8782dab
adds the manifold of centered matrices and its check_manifold_point f…
r-dornig Apr 21, 2020
175d5d0
add CenteredMatrices to documentation
r-dornig Apr 21, 2020
e64feb5
add tolerance to check_manifold_point, define M as an AbstractEmbedde…
r-dornig Apr 27, 2020
5c7ec12
add function representation_size to CenteredMatrices.jl
r-dornig Apr 29, 2020
b034218
change computation of manifold dimension in CenteredMatrices to match…
r-dornig Apr 29, 2020
eddc177
add a test document for CenteredMatrices (centered_matrices.jl) and i…
r-dornig Apr 29, 2020
c878de6
rewrite parts of the documentation of CenteredMatrices.
r-dornig May 2, 2020
836ed34
finish test for CenteredMatrices
r-dornig May 7, 2020
68482a0
Merge branch 'master' into CenteredMatrix
r-dornig May 7, 2020
b3f7b36
fixed a typing error
r-dornig May 7, 2020
76831a9
runs Formatter
r-dornig May 7, 2020
389ebd6
change is_tangent_atol_multiplier and is_point_atol_multiplier in tes…
r-dornig May 11, 2020
f1dfaeb
add tolerances to test_manifold in test/centered_matrices.jl
r-dornig May 11, 2020
a24794b
Merge branch 'master' into CenteredMatrix
r-dornig May 15, 2020
c4c2071
deletes two lines in .github/workflows/ci.yml
r-dornig May 18, 2020
37dd285
exchange the action in .github/workflows/ci.yml
r-dornig May 19, 2020
2b105a0
add tangent tolerance to test/skewsymmetric.jl
r-dornig May 20, 2020
ab2d0fd
corrects typo
r-dornig May 20, 2020
3e1eede
Merge branch 'master' into CenteredMatrix
r-dornig May 21, 2020
c4d1664
add tangent tolerance to tests sphere.jl, symmetric.jl, symmetric_pos…
r-dornig May 22, 2020
6ee5dfe
add tangent tolerance to test/product_manifold.jl, test/torus.jl and …
r-dornig May 22, 2020
ba42627
set is_tangent_atol_multiplier in test/grassmann.jl to 20.0
r-dornig May 22, 2020
9c1575c
add test for complex centered matrices
r-dornig May 26, 2020
e5deb96
Merge branch 'master' into CenteredMatrix
r-dornig May 29, 2020
e4690c4
adds test for complex centered matrices
r-dornig May 29, 2020
e10bc7d
bump version
r-dornig May 29, 2020
7cbf687
runs Formatter
r-dornig May 29, 2020
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
1 change: 1 addition & 0 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ makedocs(
"ManifoldsBase.jl" => "interface.md",
"Manifolds" => [
"Basic manifolds" => [
"Centered matrices" => "manifolds/centeredmatrices.md",
"Cholesky space" => "manifolds/choleskyspace.md",
"Circle" => "manifolds/circle.md",
"Euclidean" => "manifolds/euclidean.md",
Expand Down
7 changes: 7 additions & 0 deletions docs/src/manifolds/centeredmatrices.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Centered matrices

```@autodocs
Modules = [Manifolds]
Pages = ["manifolds/CenteredMatrices.jl"]
Order = [:type, :function]
```
2 changes: 2 additions & 0 deletions src/Manifolds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ include("manifolds/GraphManifold.jl")
include("manifolds/Euclidean.jl")
include("manifolds/Lorentz.jl")

include("manifolds/CenteredMatrices.jl")
include("manifolds/CholeskySpace.jl")
include("manifolds/Circle.jl")
include("manifolds/FixedRankMatrices.jl")
Expand Down Expand Up @@ -169,6 +170,7 @@ export CoTVector, Manifold, MPoint, TVector, Manifold
export AbstractSphere
export Euclidean,
ArraySphere,
CenteredMatrices,
CholeskySpace,
Circle,
FixedRankMatrices,
Expand Down
144 changes: 144 additions & 0 deletions src/manifolds/CenteredMatrices.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
@doc raw"""
CenteredMatrices{m,n,𝔽} <: AbstractEmbeddedManifold{𝔽,TransparentIsometricEmbedding}

The manifold of $m × n$ real-valued or complex-valued matrices whose columns sum to zero, i.e.
````math
\bigl\{ p ∈ 𝔽^{m × n}\ \big|\ [1 … 1] * p = [0 … 0] \bigr\},
````
where $𝔽 ∈ \{ℝ,ℂ\}$.

# Constructor
CenteredMatrices(m, n[, field=ℝ])

Generate the manifold of `m`-by-`n` (`field`-valued) matrices whose columns sum to zero.
"""
struct CenteredMatrices{M,N,𝔽} <: AbstractEmbeddedManifold{𝔽,TransparentIsometricEmbedding} end

function CenteredMatrices(m::Int, n::Int, field::AbstractNumbers = ℝ)
return CenteredMatrices{m,n,field}()
end

@doc raw"""
check_manifold_point(M::CenteredMatrices{m,n,𝔽}, p; kwargs...)

Check whether the matrix is a valid point on the
[`CenteredMatrices`](@ref) `M`, i.e. is an `m`-by-`n` matrix whose columns sum to
zero.

The tolerance for the column sums of `p` can be set using `kwargs...`.
"""
function check_manifold_point(M::CenteredMatrices{m,n,𝔽}, p; kwargs...) where {m,n,𝔽}
mpv =
invoke(check_manifold_point, Tuple{supertype(typeof(M)),typeof(p)}, M, p; kwargs...)
mpv === nothing || return mpv
if !isapprox(sum(p, dims = 1), zeros(1, n); kwargs...)
return DomainError(
p,
string("The point $(p) does not lie on $(M), since its columns do not sum to zero."),
)
end
return nothing
end


"""
check_tangent_vector(M::CenteredMatrices{m,n,𝔽}, p, X; check_base_point = true, kwargs... )

Check whether `X` is a tangent vector to manifold point `p` on the
[`CenteredMatrices`](@ref) `M`, i.e. that `X` is a matrix of size `(m,n)` whose columns
sum to zero and its values are from the correct [`AbstractNumbers`](@ref).
The optional parameter `check_base_point` indicates, whether to call
[`check_manifold_point`](@ref) for `p`.
The tolerance for the column sums of `p` and `X` can be set using `kwargs...`.
"""
function check_tangent_vector(
M::CenteredMatrices{m,n,𝔽},
p,
X;
check_base_point = true,
kwargs...,
) where {m,n,𝔽}
if check_base_point
mpe = check_manifold_point(M, p; kwargs...)
mpe === nothing || return mpe
end
mpv = invoke(
check_tangent_vector,
Tuple{supertype(typeof(M)),typeof(p),typeof(X)},
M,
p,
X;
check_base_point = false,
kwargs...,
)
mpv === nothing || return mpv
if !isapprox(sum(X, dims = 1), zeros(1, n); kwargs...)
return DomainError(
X,
"The vector $(X) is not a tangent vector to $(p) on $(M), since its columns do not sum to zero.",
)
end
return nothing
end

decorated_manifold(M::CenteredMatrices{m,n,𝔽}) where {m,n,𝔽} = Euclidean(m, n; field = 𝔽)

embed!(M::CenteredMatrices, q, p) = copyto!(q, p)
embed!(M::CenteredMatrices, Y, p, X) = copyto!(Y, X)

@doc raw"""
manifold_dimension(M::CenteredMatrices{m,n,𝔽})

Return the manifold dimension of the [`CenteredMatrices`](@ref) `m`-by-`n` matrix `M` over the number system
`𝔽`, i.e.

````math
\dim(\mathcal M) = (m*n - n) \dim_ℝ 𝔽,
````
where $\dim_ℝ 𝔽$ is the [`real_dimension`](@ref) of `𝔽`.
"""
function manifold_dimension(::CenteredMatrices{m,n,𝔽}) where {m,n,𝔽}
return (m * n - n) * real_dimension(𝔽)
end

@doc raw"""
project(M::CenteredMatrices, p)

Projects `p` from the embedding onto the [`CenteredMatrices`](@ref) `M`, i.e.

````math
\operatorname{proj}_{\mathcal M}(p) = p - \begin{bmatrix}
1\\
⋮\\
1
\end{bmatrix} * [c_1 \dots c_n],
````
where $c_i = \frac{1}{m}\sum_{j=1}^m p_{j,i}$ for $i = 1, \dots, n$.
"""
project(::CenteredMatrices, ::Any)

project!(M::CenteredMatrices, q, p) = copyto!(q, p .- mean(p, dims = 1))

@doc raw"""
project(M::CenteredMatrices, p, X)

Project the matrix `X` onto the tangent space at `p` on the [`CenteredMatrices`](@ref) `M`, i.e.

````math
\operatorname{proj}_p(X) = X - \begin{bmatrix}
1\\
⋮\\
1
\end{bmatrix} * [c_1 \dots c_n],
````
where $c_i = \frac{1}{m}\sum_{j=1}^m x_{j,i}$ for $i = 1, \dots, n$.
"""
project(::CenteredMatrices, ::Any, ::Any)

project!(M::CenteredMatrices, Y, p, X) = (Y .= X .- mean(X, dims = 1))

@generated representation_size(::CenteredMatrices{m,n,𝔽}) where {m,n,𝔽} = (m, n)

function Base.show(io::IO, ::CenteredMatrices{m,n,𝔽}) where {m,n,𝔽}
return print(io, "CenteredMatrices($(m), $(n), $(𝔽))")
end
46 changes: 46 additions & 0 deletions test/centered_matrices.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
include("utils.jl")

@testset "CenteredMatrices" begin
M = CenteredMatrices(3, 2)
M_complex = CenteredMatrices(3, 2, ℂ)
@test repr(M_complex) == "CenteredMatrices(3, 2, ℂ)"
@test manifold_dimension(M_complex) == 8
A = [1 2; 4 5; -5 -7]
B = [1 2 3; 4 5 6; -5 -7 -9] #wrong dimensions
C = [-3 -im; 2 im; 1 0] #complex
D = [1 2; 3 4; 5 6] #not centered
@testset "Real Centered Matrices Basics" begin
@test repr(M) == "CenteredMatrices(3, 2, ℝ)"
@test representation_size(M) == (3, 2)
@test base_manifold(M) === M
@test typeof(get_embedding(M)) === Euclidean{Tuple{3,2},ℝ}
@test check_manifold_point(M, A) === nothing
@test_throws DomainError is_manifold_point(M, B, true)
@test_throws DomainError is_manifold_point(M, C, true)
@test_throws DomainError is_manifold_point(M, D, true)
@test check_tangent_vector(M, A, A) === nothing
@test_throws DomainError is_tangent_vector(M, A, D, true)
@test_throws DomainError is_tangent_vector(M, D, A, true)
@test_throws DomainError is_tangent_vector(M, A, B, true)
@test manifold_dimension(M) == 4
@test A == project!(M, A, A)
@test A == project(M, A, A)
A2 = similar(A)
embed!(M, A2, A)
A3 = embed(M, A)
@test A2 == A
@test A3 == A
end
types = [Matrix{Float64}]
E = [0 0; 1 -1; -1 1]
F = [0.5 1; -1 -0.7; 0.5 -0.3]
test_manifold(
M,
[A, E, F],
test_injectivity_radius = false,
test_reverse_diff = false,
test_project_tangent = true,
test_musical_isomorphisms = true,
test_vector_transport = true,
r-dornig marked this conversation as resolved.
Show resolved Hide resolved
)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ include("groups/group_utils.jl")
include("sized_abstract_array.jl")

# starting with tests of simple manifolds
include("centered_matrices.jl")
include("circle.jl")
include("cholesky_space.jl")
include("euclidean.jl")
Expand Down
7 changes: 4 additions & 3 deletions test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function test_manifold(
projection_atol_multiplier = 0,
rand_tvector_atol_multiplier = 0,
is_tangent_atol_multiplier = 1,
is_point_atol_multiplier = 1,
r-dornig marked this conversation as resolved.
Show resolved Hide resolved
)

length(pts) ≥ 3 || error("Not enough points (at least three expected)")
Expand Down Expand Up @@ -152,9 +153,9 @@ function test_manifold(

@testset "is_manifold_point" begin
for pt in pts
@test is_manifold_point(M, pt)
@test check_manifold_point(M, pt) === nothing
@test check_manifold_point(M, pt) === nothing
atol = is_point_atol_multiplier * find_eps(pt)
@test is_manifold_point(M, pt; atol = atol)
@test check_manifold_point(M, pt; atol = atol) === nothing
end
end

Expand Down