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

Bases and rand for InvertibleMatrices and HeisenbergMatrices #777

Merged
merged 3 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.10.11] - 2025-01-02

### Added

* Bases and rand for `HeisenbergMatrices` and `InvertibleMatrices`.

## [0.10.10] - 2024-12-20

### Added
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 = "Manifolds"
uuid = "1cead3c2-87b3-11e9-0ccd-23c62b72b94e"
authors = ["Seth Axen <[email protected]>", "Mateusz Baran <[email protected]>", "Ronny Bergmann <[email protected]>", "Antoine Levitt <[email protected]>"]
version = "0.10.10"
version = "0.10.11"

[deps]
Einsum = "b7d42ee7-0b51-5a75-98ca-779d3107e4c0"
Expand Down
105 changes: 105 additions & 0 deletions src/manifolds/HeisenbergMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
return HeisenbergMatrices{typeof(size)}(size)
end

function _heisenberg_a_view(M::HeisenbergMatrices, p)
n = get_parameter(M.size)[1]
return view(p, 1, 2:(n + 1))

Check warning on line 36 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L34-L36

Added lines #L34 - L36 were not covered by tests
end
function _heisenberg_b_view(M::HeisenbergMatrices, p)
n = get_parameter(M.size)[1]
return view(p, 2:(n + 1), n + 2)

Check warning on line 40 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L38-L40

Added lines #L38 - L40 were not covered by tests
end

function active_traits(f, ::HeisenbergMatrices, args...)
return merge_traits(IsEmbeddedSubmanifold())
end
Expand Down Expand Up @@ -97,6 +106,33 @@
embed(::HeisenbergMatrices, p) = p
embed(::HeisenbergMatrices, p, X) = X

@doc raw"""
get_coordinates(M::HeisenbergMatrices, p, X, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})

Get coordinates of tangent vector `X` at point `p` from the [`HeisenbergMatrices`](@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(::HeisenbergMatrices, p, X, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})

function get_coordinates_orthonormal(M::HeisenbergMatrices, p, X, ::RealNumbers)
n = get_parameter(M.size)[1]
return vcat(_heisenberg_a_view(M, X), _heisenberg_b_view(M, X), X[1, n + 2])

Check warning on line 125 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L123-L125

Added lines #L123 - L125 were not covered by tests
end

function get_coordinates_orthonormal!(M::HeisenbergMatrices, Xⁱ, p, X, ::RealNumbers)
n = get_parameter(M.size)[1]
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ⁱ

Check warning on line 133 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L128-L133

Added lines #L128 - L133 were not covered by tests
end

function get_embedding(::HeisenbergMatrices{TypeParameter{Tuple{n}}}) where {n}
return Euclidean(n + 2, n + 2)
end
Expand All @@ -105,6 +141,37 @@
return Euclidean(n + 2, n + 2; parameter=:field)
end

@doc raw"""
get_vector(M::HeisenbergMatrices, p, Xⁱ, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})

Get tangent vector with coordinates `Xⁱ` at point `p` from the [`HeisenbergMatrices`](@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::HeisenbergMatrices, p, c, ::DefaultOrthonormalBasis{ℝ,TangentSpaceType})

function get_vector_orthonormal(M::HeisenbergMatrices, p, Xⁱ, ::RealNumbers)
n = get_parameter(M.size)[1]
return [

Check warning on line 159 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L157-L159

Added lines #L157 - L159 were not covered by tests
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!(M::HeisenbergMatrices, X, p, Xⁱ, ::RealNumbers)
n = get_parameter(M.size)[1]
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

Check warning on line 172 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L166-L172

Added lines #L166 - L172 were not covered by tests
end

"""
is_flat(::HeisenbergMatrices)

Expand All @@ -119,6 +186,44 @@
"""
manifold_dimension(M::HeisenbergMatrices) = 2 * get_parameter(M.size)[1] + 1

@doc raw"""
Random.rand(M::HeisenbergMatrices; vector_at = nothing, σ::Real=1.0)

If `vector_at` is `nothing`, return a random point on the [`HeisenbergMatrices`](@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 [`HeisenbergMatrices`](@ref) by using a normal distribution with
mean 0 and standard deviation `σ`.
"""
rand(M::HeisenbergMatrices; vector_at=nothing, σ::Real=1.0)

function Random.rand!(

Check warning on line 202 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L202

Added line #L202 was not covered by tests
rng::AbstractRNG,
M::HeisenbergMatrices,
pX;
σ::Real=one(eltype(pX)),
vector_at=nothing,
)
n = ManifoldsBase.get_parameter(M.size)[1]
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 .*= σ

Check warning on line 217 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L209-L217

Added lines #L209 - L217 were not covered by tests
else
fill!(pX, 0)
randn!(rng, view(pX, 1, 2:(n + 2)))
randn!(rng, view(pX, 2:(n + 1), n + 2))
pX .*= σ

Check warning on line 222 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L219-L222

Added lines #L219 - L222 were not covered by tests
end
return pX

Check warning on line 224 in src/manifolds/HeisenbergMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/HeisenbergMatrices.jl#L224

Added line #L224 was not covered by tests
end

function Base.show(io::IO, ::HeisenbergMatrices{TypeParameter{Tuple{n}}}) where {n}
return print(io, "HeisenbergMatrices($(n))")
end
Expand Down
60 changes: 60 additions & 0 deletions src/manifolds/InvertibleMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
embed(::InvertibleMatrices, p) = p
embed(::InvertibleMatrices, p, X) = X

function get_coordinates(

Check warning on line 62 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L62

Added line #L62 was not covered by tests
::InvertibleMatrices{ℝ,<:Any},
p,
X,
::DefaultOrthonormalBasis{ℝ,TangentSpaceType},
)
return vec(X)

Check warning on line 68 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L68

Added line #L68 was not covered by tests
end

function get_coordinates!(

Check warning on line 71 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L71

Added line #L71 was not covered by tests
::InvertibleMatrices{ℝ,<:Any},
Xⁱ,
p,
X,
::DefaultOrthonormalBasis{ℝ,TangentSpaceType},
)
return copyto!(Xⁱ, X)

Check warning on line 78 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L78

Added line #L78 was not covered by tests
end

function get_embedding(::InvertibleMatrices{𝔽,TypeParameter{Tuple{n}}}) where {n,𝔽}
return Euclidean(n, n; field=𝔽)
end
Expand All @@ -67,6 +86,26 @@
return Euclidean(n, n; field=𝔽, parameter=:field)
end

function get_vector(

Check warning on line 89 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L89

Added line #L89 was not covered by tests
M::InvertibleMatrices{ℝ,<:Any},
p,
Xⁱ,
::DefaultOrthonormalBasis{ℝ,TangentSpaceType},
)
n = get_parameter(M.size)[1]
return reshape(Xⁱ, n, n)

Check warning on line 96 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L95-L96

Added lines #L95 - L96 were not covered by tests
end

function get_vector!(

Check warning on line 99 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L99

Added line #L99 was not covered by tests
::InvertibleMatrices{ℝ,<:Any},
X,
p,
Xⁱ,
::DefaultOrthonormalBasis{ℝ,TangentSpaceType},
)
return copyto!(X, Xⁱ)

Check warning on line 106 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L106

Added line #L106 was not covered by tests
end

"""
is_flat(::InvertibleMatrices)

Expand All @@ -84,6 +123,27 @@
return manifold_dimension(get_embedding(M))
end

@doc raw"""
Random.rand(M::InvertibleMatrices; vector_at=nothing, kwargs...)

If `vector_at` is `nothing`, return a random point on the [`InvertibleMatrices`](@ref)
manifold `M` by using `rand` in the embedding.

If `vector_at` is not `nothing`, return a random tangent vector from the tangent space of
the point `vector_at` on the [`InvertibleMatrices`](@ref) by using by using `rand` in the
embedding.
"""
rand(M::InvertibleMatrices; kwargs...)

function Random.rand!(M::InvertibleMatrices, pX; kwargs...)
rand!(get_embedding(M), pX; kwargs...)
return pX

Check warning on line 140 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L138-L140

Added lines #L138 - L140 were not covered by tests
end
function Random.rand!(rng::AbstractRNG, M::InvertibleMatrices, pX; kwargs...)
rand!(rng, get_embedding(M), pX; kwargs...)
return pX

Check warning on line 144 in src/manifolds/InvertibleMatrices.jl

View check run for this annotation

Codecov / codecov/patch

src/manifolds/InvertibleMatrices.jl#L142-L144

Added lines #L142 - L144 were not covered by tests
end

function Base.show(io::IO, ::InvertibleMatrices{𝔽,TypeParameter{Tuple{n}}}) where {n,𝔽}
return print(io, "InvertibleMatrices($(n), $(𝔽))")
end
Expand Down
3 changes: 3 additions & 0 deletions test/manifolds/heisenberg_matrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ using LinearAlgebra, Manifolds, ManifoldsBase, Test
test_manifold(
M,
pts;
basis_types_to_from=(DefaultOrthonormalBasis(),),
parallel_transport=true,
test_injectivity_radius=true,
test_musical_isomorphisms=false,
test_rand_point=true,
test_rand_tvector=true,
)

@test all(
Expand Down
16 changes: 15 additions & 1 deletion test/manifolds/invertible_matrices.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LinearAlgebra, Manifolds, ManifoldsBase, Test
using LinearAlgebra, Manifolds, ManifoldsBase, Test, Random

@testset "Invertible matrices" begin
M = InvertibleMatrices(3, ℝ)
Expand All @@ -24,6 +24,20 @@ using LinearAlgebra, Manifolds, ManifoldsBase, Test
@test embed(M, A, A) === A
@test manifold_dimension(M) == 9
@test Weingarten(M, A, A, A) == zero(A)

@test is_point(M, rand(M))
@test is_point(M, rand(Random.MersenneTwister(), M))
@test is_vector(M, A, rand(M; vector_at=A))

@test get_coordinates(M, A, A, DefaultOrthonormalBasis()) == vec(A)
c = similar(vec(A))
get_coordinates!(M, c, A, A, DefaultOrthonormalBasis())
@test isapprox(c, vec(A))

@test get_vector(M, A, vec(A), DefaultOrthonormalBasis()) == A
D = similar(A)
get_vector!(M, D, A, vec(A), DefaultOrthonormalBasis())
@test isapprox(D, A)
end
@testset "Complex invertible matrices" begin
@test repr(Mc) == "InvertibleMatrices(3, ℂ)"
Expand Down
Loading