Skip to content

Commit

Permalink
Fix zero argument result allocation for nested power manifold (#95)
Browse files Browse the repository at this point in the history
* Fix zero argument result allocation for nested power manifold

* complex allocation

* disambiguate

* test & fix
  • Loading branch information
mateuszbaran authored Mar 30, 2022
1 parent ded0ea4 commit 3f834ae
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 6 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.12"
version = "0.12.13"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
10 changes: 7 additions & 3 deletions src/PowerManifold.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ function Base.:^(
end

function allocate_result(M::PowerManifoldNested, f, x...)
if representation_size(M.manifold) === ()
if representation_size(M.manifold) === () && length(x) > 0
return allocate(x[1])
else
return [
Expand All @@ -209,8 +209,12 @@ function allocate_result(M::PowerManifoldNested, f, x...)
]
end
end
function allocate_result(::PowerManifoldNestedReplacing, f, x...)
return copy(x[1])
function allocate_result(M::PowerManifoldNestedReplacing, f, x...)
if length(x) == 0
return [allocate_result(M.manifold, f) for _ in get_iterator(M)]
else
return copy(x[1])
end
end

for PowerRepr in [PowerManifoldNested, PowerManifoldNestedReplacing]
Expand Down
2 changes: 1 addition & 1 deletion src/bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ end
@inline function allocate_result_type(
M::AbstractManifold,
f::Union{typeof(get_coordinates),typeof(get_vector)},
args::Tuple,
args::Tuple{Any,Vararg{Any}},
)
apf = allocation_promotion_function(M, f, args)
return apf(
Expand Down
8 changes: 8 additions & 0 deletions src/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ const ℝ = RealNumbers()
const= ComplexNumbers()
const= QuaternionNumbers()

@inline function allocate_result_type(
::AbstractManifold{ℂ},
f::TF,
args::Tuple{},
) where {TF}
return ComplexF64
end

"""
_unify_number_systems(𝔽s::AbstractNumbers...)
Expand Down
7 changes: 6 additions & 1 deletion test/allocation.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using ManifoldsBase
using Test
using ManifoldsBase:
combine_allocation_promotion_functions, allocation_promotion_function, ℝ
combine_allocation_promotion_functions, allocation_promotion_function, ℝ, ℂ

struct AllocManifold{𝔽} <: AbstractManifold{𝔽} end
AllocManifold() = AllocManifold{ℝ}()
Expand All @@ -16,6 +16,9 @@ end
struct AllocManifold2 <: AbstractManifold{ℝ} end
ManifoldsBase.representation_size(::AllocManifold2) = (2, 3)

struct AllocManifold3 <: AbstractManifold{ℂ} end
ManifoldsBase.representation_size(::AllocManifold3) = (2, 3)

@testset "Allocation" begin
a = [[1.0], [2.0], [3.0]]
b = [[2.0], [3.0], [-3.0]]
Expand Down Expand Up @@ -58,4 +61,6 @@ ManifoldsBase.representation_size(::AllocManifold2) = (2, 3)
alloc2 = ManifoldsBase.allocate_result(AllocManifold2(), rand)
@test alloc2 isa Matrix{Float64}
@test size(alloc2) == representation_size(AllocManifold2())

@test ManifoldsBase.allocate_result(AllocManifold3(), rand) isa Matrix{ComplexF64}
end
3 changes: 3 additions & 0 deletions test/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,8 @@ power_array_wrapper(::Type{NestedReplacingPowerRepresentation}, i::Int) = SVecto
DN = DummyDecorator(N)
@test p[DN, 1] == p[N, 1]
end
@testset "zero argument allocation" begin
@test size(ManifoldsBase.allocate_result(N, rand)) == size(p)
end
end
end

2 comments on commit 3f834ae

@mateuszbaran
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/57622

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.13 -m "<description of version>" 3f834ae924c6274f4639e97f246507c3ff99b458
git push origin v0.12.13

Please sign in to comment.