Skip to content

Add ProjectTo{Tuple} #457

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

Closed
wants to merge 4 commits into from
Closed
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
40 changes: 36 additions & 4 deletions src/projection.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This unwraps Val, which is used for storing some types, because:
# (;type = Type{typeof((1,2))}) isa NamedTuple{(:type,), Tuple{DataType}}
_val(::Val{val}) where {val} = val

"""
(p::ProjectTo{T})(dx)

Expand Down Expand Up @@ -262,19 +266,47 @@ end
##### `Base`, part II: return of the Tangent
#####

# Tuple
function ProjectTo(xs::Tuple)
elements = map(ProjectTo, xs)
if elements isa Tuple{Vararg{ProjectTo{<:AbstractZero}}}
ProjectTo{NoTangent}() # short-circuit if all elements project to zero
else
return ProjectTo{Tuple}(; type=Val(typeof(xs)), elements=elements)
end
end

(project::ProjectTo{Tuple})(dx::Tangent) = project(backing(dx))
function (project::ProjectTo{Tuple})(dx::Tuple)
if length(dx) != length(project.elements)
throw(_projection_mismatch(axes(project.elements), size(dx)))
end
dz = map((f, y) -> f(y), project.elements, dx)
return Tangent{_val(project.type)}(dz...)
end
function (project::ProjectTo{Tuple})(dx::AbstractArray)
for d in 1:ndims(dx)
if size(dx, d) != get(length(project.elements), d, 1)
throw(_projection_mismatch(axes(project.elements), size(dx)))
end
end
dz = ntuple(i -> project.elements[i](dx[i]), length(project.elements))
return Tangent{_val(project.type)}(dz...)
end

# Ref
function ProjectTo(x::Ref)
sub = ProjectTo(x[]) # should we worry about isdefined(Ref{Vector{Int}}(), :x)?
if sub isa ProjectTo{<:AbstractZero}
return ProjectTo{NoTangent}()
else
return ProjectTo{Ref}(; type=typeof(x), x=sub)
return ProjectTo{Ref}(; type=Val(typeof(x)), x=sub)
end
end
(project::ProjectTo{Ref})(dx::Tangent{<:Ref}) = Tangent{project.type}(; x=project.x(dx.x))
(project::ProjectTo{Ref})(dx::Ref) = Tangent{project.type}(; x=project.x(dx[]))
(project::ProjectTo{Ref})(dx::Tangent{<:Ref}) = Tangent{_val(project.type)}(; x=project.x(dx.x))
(project::ProjectTo{Ref})(dx::Ref) = Tangent{_val(project.type)}(; x=project.x(dx[]))
# Since this works like a zero-array in broadcasting, it should also accept a number:
(project::ProjectTo{Ref})(dx::Number) = Tangent{project.type}(; x=project.x(dx))
(project::ProjectTo{Ref})(dx::Number) = Tangent{_val(project.type)}(; x=project.x(dx))

#####
##### `LinearAlgebra`
Expand Down
19 changes: 19 additions & 0 deletions test/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ struct NoSuperType end
@test pzed isa ProjectTo{AbstractArray}
end

@testset "Base: Tuple" begin
pt1 = ProjectTo((1.0,))
@test pt1((1+im,)) == Tangent{Tuple{Float64}}(1.0,)
@test pt1(pt1((1,))) == pt1(pt1((1,))) # accepts correct Tangent
@test pt1(Tangent{Any}(1)) == pt1((1,)) # accepts Tangent{Any}
@test pt1([1,]) == Tangent{Tuple{Float64}}(1.0,) # accepts Vector
@test pt1(1+im) == Tangent{Tuple{Float64}}(1.0,) # accepts Number
@test pt1(NoTangent()) === NoTangent()
@test pt1(ZeroTangent()) === ZeroTangent()

pt1([1,2]) # not an error, NTuple just takes N
@test_throws Exception pt1([]) # ArgumentError: too few elements for tuple type Tuple

pt3 = ProjectTo(([1,2,3], false, :gamma))
@test pt3((1:3, 4, 5)) == Tangent{Tuple{Vector{Int}, Bool, Symbol}}([1.0, 2.0, 3.0], NoTangent(), 5)

@test ProjectTo((true, [false])) isa ProjectTo{NoTangent}
end

@testset "Base: Ref" begin
pref = ProjectTo(Ref(2.0))
@test pref(Ref(3 + im)).x === 3.0
Expand Down