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

Add interface for optionally static size #160

Merged
merged 8 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 23 additions & 7 deletions src/DefaultManifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ This manifold further illustrates how to type your manifold points and tangent v
that the interface does not require this, but it might be handy in debugging and educative
situations to verify correctness of involved variabes.
"""
struct DefaultManifold{𝔽,T<:NTuple{N,Int} where {N}} <: AbstractManifold{𝔽}
struct DefaultManifold{𝔽,T<:AbstractManifoldSize} <: AbstractManifold{𝔽}
size::T
end
function DefaultManifold(n::Vararg{Int}; field = ℝ)
return DefaultManifold{field,typeof(n)}(n)
function ManifoldsBase.DefaultManifold(
n::Vararg{Int};
field = ManifoldsBase.ℝ,
static = false,
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
)
if static
size = ManifoldsBase.StaticSize(n)
else
size = ManifoldsBase.RTSize(n)
end
return ManifoldsBase.DefaultManifold{field,typeof(size)}(size)
end

change_representer!(M::DefaultManifold, Y, ::EuclideanMetric, p, X) = copyto!(M, Y, p, X)
Expand Down Expand Up @@ -112,7 +121,8 @@ is_flat(::DefaultManifold) = true
log!(::DefaultManifold, Y, p, q) = (Y .= q .- p)

function manifold_dimension(M::DefaultManifold{𝔽}) where {𝔽}
return length(M.size) == 0 ? 1 : *(M.size...) * real_dimension(𝔽)
size = getsize(M.size)
return length(size) == 0 ? 1 : *(size...) * real_dimension(𝔽)
end

number_system(::DefaultManifold{𝔽}) where {𝔽} = 𝔽
Expand All @@ -122,10 +132,16 @@ norm(::DefaultManifold, p, X) = norm(X)
project!(::DefaultManifold, q, p) = copyto!(q, p)
project!(::DefaultManifold, Y, p, X) = copyto!(Y, X)

representation_size(M::DefaultManifold) = M.size
representation_size(M::DefaultManifold) = getsize(M.size)

function Base.show(io::IO, M::DefaultManifold{𝔽}) where {𝔽}
return print(io, "DefaultManifold($(join(M.size, ", ")); field = $(𝔽))")
function Base.show(io::IO, M::DefaultManifold{𝔽,<:StaticSize}) where {𝔽}
return print(
io,
"DefaultManifold($(join(getsize(M.size), ", ")); field = $(𝔽), static = true)",
)
end
function Base.show(io::IO, M::DefaultManifold{𝔽,<:RTSize}) where {𝔽}
return print(io, "DefaultManifold($(join(getsize(M.size), ", ")); field = $(𝔽))")
end

function parallel_transport_to!(::DefaultManifold, Y, p, X, q)
Expand Down
28 changes: 28 additions & 0 deletions src/maintypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,31 @@ matrix internally, it is possible to use [`@manifold_element_forwards`](@ref) an
[`@default_manifold_fallbacks`](@ref) to reduce implementation overhead.
"""
abstract type AbstractManifoldPoint end

"""
abstract type AbstractManifoldSize end

Abstract representation of manifold size. Can be either [`StaticSize`](@ref) or
[`RTSize`](@ref).
"""
abstract type AbstractManifoldSize end

"""
StaticSize{T}

Static size of a manifold.
"""
struct StaticSize{T} <: AbstractManifoldSize end
StaticSize(t::NTuple) = StaticSize{t}()

"""
RTSize{TS<:NTuple{N,Int} where N}

Runtime size of a manifold.
"""
struct RTSize{TS<:NTuple{N,Int} where {N}} <: AbstractManifoldSize
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
size::TS
end

getsize(::StaticSize{T}) where {T} = T
getsize(S::RTSize) = S.size
kellertuer marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions test/default_manifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,10 @@ Base.size(x::MatrixVectorTransport) = (size(x.m, 2),)
@test copy(M, p) === p
@test copy(M, p, X) === X
end

@testset "static" begin
MS = ManifoldsBase.DefaultManifold(3; static = true)
@test (@inferred representation_size(MS)) == (3,)
@test repr(MS) == "DefaultManifold(3; field = ℝ, static = true)"
end
end