Skip to content

Commit

Permalink
Introduce a copy(M,p) and a copy(M, p, X) command (#77)
Browse files Browse the repository at this point in the history
* Introduce a `copy` command for points (on manifolds) and (tangent) vectors.
  • Loading branch information
kellertuer authored Jun 28, 2021
1 parent 516a43e commit c53b7a9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
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.12.1"
version = "0.12.2"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
35 changes: 32 additions & 3 deletions src/ManifoldsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Base:
exp,
log,
convert,
copy,
copyto!,
angle,
eltype,
Expand Down Expand Up @@ -81,7 +82,7 @@ end
Return type of element of the array that will represent the result of function `f` and the
[`AbstractManifold`](@ref) `M` on given arguments `args` (passed as a tuple).
"""
function allocate_result_type(M::AbstractManifold, f, args::NTuple{N,Any}) where {N}
function allocate_result_type(::AbstractManifold, f, args::NTuple{N,Any}) where {N}
return typeof(mapreduce(eti -> one(number_eltype(eti)), +, args))
end

Expand Down Expand Up @@ -175,6 +176,33 @@ function check_size(M::AbstractManifold, p, X)
end
end

@doc raw"""
copy(M, p)
Copy the value(s) from the point `p` on the [`AbstractManifold`](@ref) `M` into a new point.
See [`allocate_result`](@ref) for the allocation of new point memory and [`copyto!`](@ref) for the copying.
"""
function copy(M::AbstractManifold, p)
q = allocate_result(M, copy, p)
copyto!(M, q, p)
return q
end

@doc raw"""
copy(M, p, X)
Copy the value(s) from the tangent vector `X` at a point `p` on the
[`AbstractManifold`](@ref) `M` into a new tangent vector.
See [`allocate_result`](@ref) for the allocation of new point memory and [`copyto!`](@ref) for the copying.
"""
function copy(M::AbstractManifold, p, X)
# the order of args switched, since the allocation by default takes the type of the first.
Y = allocate_result(M, copy, X, p)
copyto!(M, Y, p, X)
return Y
end


@doc raw"""
copyto!(M::AbstractManifold, q, p)
Expand Down Expand Up @@ -272,8 +300,7 @@ see [`AbstractEmbeddedManifold`](@ref) how you can avoid reimplementing code fro
See also: [`EmbeddedManifold`](@ref), [`project`](@ref project(M::AbstractManifold, p, X))
"""
function embed(M::AbstractManifold, p, X)
# Note that the order is switched,
# since the allocation by default takes the type of the first.
# the order of args switched, since the allocation by default takes the type of the first.
Y = allocate_result(M, embed, X, p)
embed!(M, Y, p, X)
return Y
Expand Down Expand Up @@ -596,6 +623,8 @@ export allocate,
check_point,
check_vector,
check_size,
copy,
copyto!,
distance,
exp,
exp!,
Expand Down
16 changes: 16 additions & 0 deletions test/default_manifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,20 @@ Base.size(x::MatrixVectorTransport) = (size(x.m, 2),)
a = NLsolveInverseRetraction(ExponentialRetraction())
@test a.retraction isa ExponentialRetraction
end

@testset "copy of points and vectors" begin
M = DefaultManifold(2)
p = [2.0, 3.0]
q = similar(p)
copyto!(M, q, p)
@test p == q
r = copy(M, p)
@test r == p
X = [4.0, 5.0]
Y = similar(X)
copyto!(M, Y, p, X)
@test Y == X
Z = copy(M, p, X)
@test Z == X
end
end

2 comments on commit c53b7a9

@kellertuer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/39756

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.12.2 -m "<description of version>" c53b7a9039448458404db4872cb80b365f94705e
git push origin v0.12.2

Please sign in to comment.