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}
1
13
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
+ """
3
19
struct ComposedMap{T,MAPS} <: CompositeLazyMap{T}
4
20
maps :: MAPS
5
21
end
6
22
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)
9
35
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)... )
12
37
13
- codomaintype (m:: ComposedMap ) = codomaintype (m. maps[ end ] )
38
+ codomaintype (m:: ComposedMap ) = codomaintype (last ( m. maps) )
14
39
15
40
# Maps are applied in the order that they appear in m.maps
16
41
applymap (m:: ComposedMap , x) = applymap_rec (x, m. maps... )
@@ -19,7 +44,7 @@ applymap_rec(x, map1, maps...) = applymap_rec(applymap(map1, x), maps...)
19
44
20
45
# The size of a composite map depends on the first and the last map to be applied
21
46
# 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) ))
23
48
_composed_mapsize (m, m_end, m1, S_end:: Tuple{Int,Int} , S1:: Tuple{Int,Int} ) = (S_end[1 ],S1[2 ])
24
49
_composed_mapsize (m, m_end, m1, S_end:: Tuple{Int,Int} , S1:: Tuple{Int} ) =
25
50
is_vector_to_scalar (m_end) ? () : (S_end[1 ],)
@@ -109,9 +134,10 @@ struct MulMap{T,MAPS} <: CompositeLazyMap{T}
109
134
maps :: MAPS
110
135
end
111
136
137
+ MulMap (map1:: Map , maps:: Map... ) = MulMap (promote_maps (map1, maps... )... )
112
138
MulMap (map1:: Map{T} , maps:: Map{T} ...) where {T} = MulMap {T} (map1, maps... )
113
139
MulMap {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)... )
115
141
_mulmap (:: Type{T} , maps... ) where {T} = MulMap {T,typeof(maps)} (maps)
116
142
117
143
similarmap (m:: MulMap , :: Type{T} ) where {T} = MulMap {T} (m. maps... )
@@ -155,9 +181,10 @@ struct SumMap{T,MAPS} <: CompositeLazyMap{T}
155
181
maps :: MAPS
156
182
end
157
183
184
+ SumMap (map1:: Map , maps:: Map... ) = SumMap (promote_maps (map1, maps... )... )
158
185
SumMap (map1:: Map{T} , maps:: Map{T} ...) where {T} = SumMap {T} (map1, maps... )
159
186
SumMap {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)... )
161
188
_summap (:: Type{T} , maps... ) where {T} = SumMap {T,typeof(maps)} (maps)
162
189
163
190
similarmap (m:: SumMap , :: Type{T} ) where {T} = SumMap {T} (m. maps... )
0 commit comments