Skip to content

Commit cff4db7

Browse files
author
Andy Ferris
committed
Tuples of transformations for tuples of data
More-or-less makes the code in core.jl a simple monoidal category, where the "product" is creating a tuple.
1 parent 8d88a6a commit cff4db7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/core.jl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ performing transformations. Subtypes should be able to apply a coordinate system
1111
transformation on the correct data types by overloading `transform()`, and
1212
usually would have the corresponding inverse transformation defined by `Base.inv()`.
1313
Efficient compositions can optionally be defined by `compose()` (equivalently `∘`).
14+
15+
Furthermore, transformations can be combined in a tuple to be applied in parallel
16+
to a tuple of inputs (for example, performing a rotation on a spatial variable
17+
while leaving the time variable constant). This becomes particularly powerful
18+
when
1419
"""
1520
abstract AbstractTransformation{OutType, InType}
1621

@@ -45,6 +50,8 @@ end
4550
return :($(S.parameters[1]))
4651
end
4752

53+
# TODO generated functions for intype and outtype of tuples of AbstractTransformations
54+
4855

4956
# Built-in identity transform
5057
immutable IdentityTransformation{T} <: AbstractTransformation{T,T}; end
@@ -63,13 +70,25 @@ function transform{OutType, InType}(trans::AbstractTransformation{OutType, InTyp
6370
end
6471
end
6572

73+
"""
74+
transform((trans1, trans2, ...), (x1, x2, ...))
75+
76+
A set of transformations `trans1`, etc, are applied in parallel to data `x1`, etc,
77+
returning a tuple of the transformed coordinates.
78+
"""
79+
function transform{N}(trans::NTuple{N,AbstractTransformation}, x::NTuple{N})
80+
# TODO generated function for type stability in Julia 0.4
81+
return ntuple(i->transform(trans[i], x[i]), Val{N})
82+
end
83+
6684
transform{T}(::IdentityTransformation{T}, x::T) = x
6785

86+
6887
"""
6988
A `ComposedTransformation` simply executes two transformations successively, and
7089
is the fallback output type of `compose()`.
7190
"""
72-
immutable ComposedTransformation{OutType, InType, T1 <: AbstractTransformation, T2 <: AbstractTransformation} <: AbstractTransformation{OutType, InType}
91+
immutable ComposedTransformation{OutType, InType, T1 <: Union{AbstractTransformation, Tuple{Vararg{AbstractTransformation}}}, T2 <: Union{AbstractTransformation, Tuple{Vararg{AbstractTransformation}}}} <: AbstractTransformation{OutType, InType}
7392
t1::T1
7493
t2::T2
7594

@@ -78,7 +97,7 @@ immutable ComposedTransformation{OutType, InType, T1 <: AbstractTransformation,
7897
new(trans1,trans2)
7998
end
8099
end
81-
@generated function check_composable{OutType, InType}(out_type::Type{OutType},in_type::Type{InType},trans1::AbstractTransformation, trans2::AbstractTransformation)
100+
@generated function check_composable{OutType, InType}(out_type::Type{OutType},in_type::Type{InType},trans1::Union{AbstractTransformation, Tuple{Vararg{AbstractTransformation}}}, trans2::Union{AbstractTransformation, Tuple{Vararg{AbstractTransformation}}})
82101
if InType != intype(trans2)
83102
str = "Can't compose transformations: input coordinates types $InType and $(intype(trans2)) do not match."
84103
error(str)
@@ -117,6 +136,8 @@ compose{InOutType}(trans::IdentityTransformation{InOutType}, ::IdentityTransform
117136
compose{OutType, InType}(::IdentityTransformation{OutType}, trans::AbstractTransformation{OutType, InType}) = trans
118137
compose{OutType, InType}(trans::AbstractTransformation{OutType, InType}, ::IdentityTransformation{InType}) = trans
119138

139+
# TODO compose for tuples of AbstractTransformations (generated function)
140+
120141
const = compose
121142

122143
"""
@@ -131,6 +152,8 @@ end
131152
Base.inv(trans::ComposedTransformation) = inv(trans.t2) inv(trans.t1)
132153
Base.inv(trans::IdentityTransformation) = trans
133154

155+
# TODO compose for inverse of AbstractTransformations (generated function)
156+
134157
"""
135158
transform_deriv(trans::AbstractTransformation, x)
136159
@@ -148,6 +171,8 @@ function transform_deriv{OutType, InType}(trans::ComposedTransformation{OutType,
148171
return m1 * m2
149172
end
150173

174+
# TODO compose for derivatives of AbstractTransformations (generated function)
175+
151176
"""
152177
transform_deriv_params(trans::AbstractTransformation, x)
153178
@@ -165,3 +190,5 @@ function transform_deriv_params{OutType, InType}(trans::ComposedTransformation{O
165190
p1 = transform_deriv_params(trans.t1, x2)
166191
return hcat(p1, m1*p2)
167192
end
193+
194+
# TODO compose for parameter derivatives of AbstractTransformations (generated function)

0 commit comments

Comments
 (0)