Skip to content

Commit

Permalink
recenter: accept origin::Tuple (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 authored Aug 6, 2021
1 parent 9fadc33 commit 561fde6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Base.show(io::IO, trans::LinearMap) = print(io, "LinearMap($(trans.linear))") #
function (trans::LinearMap{M})(x) where {M}
trans.linear * x
end
(trans::LinearMap{M})(x::Tuple) where {M} = trans(SVector(x))

Base.inv(trans::LinearMap) = LinearMap(inv(trans.linear))

Expand Down Expand Up @@ -204,7 +205,7 @@ function Base.:(==)(t1::LinearMap, t2::AffineMap)
0 == vecnorm(t2.translation)
end

recenter(trans::AbstractMatrix, origin::AbstractVector) = recenter(LinearMap(trans), origin)
recenter(trans::AbstractMatrix, origin::Union{AbstractVector, Tuple}) = recenter(LinearMap(trans), origin)

transform_deriv(trans::AffineMap, x) = trans.linear
# TODO transform_deriv_params
3 changes: 2 additions & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Base.inv(trans::ComposedTransformation) = inv(trans.t2) ∘ inv(trans.t1)
Base.inv(trans::IdentityTransformation) = trans

"""
recenter(trans::Union{AbstractMatrix,Transformation}, origin::AbstractVector) -> ctrans
recenter(trans::Union{AbstractMatrix,Transformation}, origin::Union{AbstractVector, Tuple}) -> ctrans
Return a new transformation `ctrans` such that point `origin` serves
as the origin-of-coordinates for `trans`. Translation by `±origin`
Expand All @@ -97,6 +97,7 @@ space around `origin`.
function recenter(trans::Transformation, origin::AbstractVector)
Translation(origin) trans Translation(-origin)
end
recenter(trans::Transformation, origin::Tuple) = recenter(trans, SVector(origin))


"""
Expand Down
7 changes: 7 additions & 0 deletions test/affine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ end
@test c(origin) == origin
@test c(zero(origin)) == [6,-6]
end

# Tuple is converted to SVector first
origin = (5, -3)
new_origin = SVector(origin)
c = recenter(M, origin)
@test c(origin) == new_origin
@test c(zero(new_origin)) == [6, -6]
end

@testset "application of AffineMap in terms of LinearMap and Translation" begin
Expand Down

0 comments on commit 561fde6

Please sign in to comment.