1+ # ensure that the domaintype of each map in the sequence agrees with the codomain type of the
2+ # preceding map
3+ function match_domain_codomain_types (:: Type{T} , map, maps... ) where {T}
4+ Y = promote_type (T, domaintype (map))
5+ m1 = convert_domaintype (Y, map)
6+ (m1, match_domain_codomain_types (codomaintype (m1), maps... )... )
7+ end
8+
9+ match_domain_codomain_types (:: Type{T} , map) where {T} = (convert_domaintype (T, map),)
10+
11+ """
12+ ComposedMap{T,MAPS}
113
2- " The composition of several maps."
14+ The composition of several maps.
15+
16+ The `components` of a `ComposedMap` are the maps in the order that they are applied
17+ to the input.
18+ """
319struct ComposedMap{T,MAPS} <: CompositeLazyMap{T}
420 maps :: MAPS
521end
622
7- ComposedMap (maps... ) = ComposedMap {domaintype(maps[1])} (maps... )
8- ComposedMap {T} (maps... ) where {T} = ComposedMap {T,typeof(maps)} (maps)
23+ function ComposedMap (map1, maps... )
24+ P = prectype (map1, maps... )
25+ if P == Any
26+ # don't try to promote types
27+ _ComposedMap (domaintype (map1), map1, maps... )
28+ else
29+ T = to_prectype (P, domaintype (map1))
30+ _ComposedMap (T, match_domain_codomain_types (T, map1, maps... )... )
31+ end
32+ end
33+ ComposedMap {T} (map1, maps... ) where {T} = _ComposedMap (T, match_domain_codomain_types (T, map1, maps... )... )
34+ _ComposedMap (:: Type{T} , maps... ) where {T} = ComposedMap {T,typeof(maps)} (maps)
935
10- # TODO : make proper conversion
11- similarmap (m:: ComposedMap , :: Type{T} ) where {T} = ComposedMap {T} (m. maps... )
36+ similarmap (m:: ComposedMap , :: Type{T} ) where {T} = ComposedMap {T} (components (m)... )
1237
13- codomaintype (m:: ComposedMap ) = codomaintype (m. maps[ end ] )
38+ codomaintype (m:: ComposedMap ) = codomaintype (last ( m. maps) )
1439
1540# Maps are applied in the order that they appear in m.maps
1641applymap (m:: ComposedMap , x) = applymap_rec (x, m. maps... )
@@ -19,7 +44,7 @@ applymap_rec(x, map1, maps...) = applymap_rec(applymap(map1, x), maps...)
1944
2045# The size of a composite map depends on the first and the last map to be applied
2146# We check whether they are scalar_to_vector, vector_to_vector, etcetera
22- mapsize (m:: ComposedMap ) = _composed_mapsize (m, m. maps[ end ], m. maps[ 1 ] , mapsize (m. maps[ end ]) , mapsize (m. maps[ 1 ] ))
47+ mapsize (m:: ComposedMap ) = _composed_mapsize (m, last ( m. maps), first ( m. maps) , mapsize (last ( m. maps)) , mapsize (first ( m. maps) ))
2348_composed_mapsize (m, m_end, m1, S_end:: Tuple{Int,Int} , S1:: Tuple{Int,Int} ) = (S_end[1 ],S1[2 ])
2449_composed_mapsize (m, m_end, m1, S_end:: Tuple{Int,Int} , S1:: Tuple{Int} ) =
2550 is_vector_to_scalar (m_end) ? () : (S_end[1 ],)
@@ -109,9 +134,10 @@ struct MulMap{T,MAPS} <: CompositeLazyMap{T}
109134 maps :: MAPS
110135end
111136
137+ MulMap (map1:: Map , maps:: Map... ) = MulMap (promote_maps (map1, maps... )... )
112138MulMap (map1:: Map{T} , maps:: Map{T} ...) where {T} = MulMap {T} (map1, maps... )
113139MulMap {T} (maps:: Map{T} ...) where {T} = MulMap {T,typeof(maps)} (maps)
114- MulMap {T} (maps... ) where {T} = _mulmap (T, convert .(Map{T} , maps)... )
140+ MulMap {T} (maps... ) where {T} = _mulmap (T, convert_domaintype .( Ref (T) , maps)... )
115141_mulmap (:: Type{T} , maps... ) where {T} = MulMap {T,typeof(maps)} (maps)
116142
117143similarmap (m:: MulMap , :: Type{T} ) where {T} = MulMap {T} (m. maps... )
@@ -155,9 +181,10 @@ struct SumMap{T,MAPS} <: CompositeLazyMap{T}
155181 maps :: MAPS
156182end
157183
184+ SumMap (map1:: Map , maps:: Map... ) = SumMap (promote_maps (map1, maps... )... )
158185SumMap (map1:: Map{T} , maps:: Map{T} ...) where {T} = SumMap {T} (map1, maps... )
159186SumMap {T} (maps:: Map{T} ...) where {T} = SumMap {T,typeof(maps)} (maps)
160- SumMap {T} (maps... ) where {T} = _summap (T, convert .(Map{T} , maps)... )
187+ SumMap {T} (maps... ) where {T} = _summap (T, convert_domaintype .( Ref (T) , maps)... )
161188_summap (:: Type{T} , maps... ) where {T} = SumMap {T,typeof(maps)} (maps)
162189
163190similarmap (m:: SumMap , :: Type{T} ) where {T} = SumMap {T} (m. maps... )
0 commit comments