62
62
# ##### Generic reduction functions #####
63
63
64
64
# # initialization
65
-
66
- for (Op, initfun) in ((:(typeof (+ )), :zero ), (:(typeof (* )), :one ))
65
+ # initarray! is only called by sum!, prod!, etc.
66
+ for (Op, initfun) in ((:(typeof (add_sum )), :zero ), (:(typeof (mul_prod )), :one ))
67
67
@eval initarray! (a:: AbstractArray{T} , :: $ (Op), init:: Bool , src:: AbstractArray ) where {T} = (init && fill! (a, $ (initfun)(T)); a)
68
68
end
69
69
@@ -75,6 +75,7 @@ for (Op, initval) in ((:(typeof(&)), true), (:(typeof(|)), false))
75
75
@eval initarray! (a:: AbstractArray , :: $ (Op), init:: Bool , src:: AbstractArray ) = (init && fill! (a, $ initval); a)
76
76
end
77
77
78
+ # reducedim_initarray is called by
78
79
reducedim_initarray (A:: AbstractArray , region, v0, :: Type{R} ) where {R} = fill! (similar (A,R,reduced_indices (A,region)), v0)
79
80
reducedim_initarray (A:: AbstractArray , region, v0:: T ) where {T} = reducedim_initarray (A, region, v0, T)
80
81
@@ -104,10 +105,10 @@ reducedim_initarray0_empty(A::AbstractArray, region,::typeof(identity), ops) = m
104
105
promote_union (T:: Union ) = promote_type (promote_union (T. a), promote_union (T. b))
105
106
promote_union (T) = T
106
107
107
- function reducedim_init (f, op:: typeof (+ ), A:: AbstractArray , region)
108
+ function reducedim_init (f, op:: Union{ typeof(+),typeof(add_sum)} , A:: AbstractArray , region)
108
109
_reducedim_init (f, op, zero, sum, A, region)
109
110
end
110
- function reducedim_init (f, op:: typeof (* ), A:: AbstractArray , region)
111
+ function reducedim_init (f, op:: Union{ typeof(*),typeof(mul_prod)} , A:: AbstractArray , region)
111
112
_reducedim_init (f, op, one, prod, A, region)
112
113
end
113
114
function _reducedim_init (f, op, fv, fop, A, region)
@@ -143,19 +144,12 @@ let
143
144
[AbstractArray{t} for t in uniontypes (BitIntFloat)]. .. ,
144
145
[AbstractArray{Complex{t}} for t in uniontypes (BitIntFloat)]. .. }
145
146
146
- global reducedim_init (f:: typeof (identity), op:: typeof (+ ), A:: T , region) =
147
- reducedim_initarray (A, region, zero (eltype (A)))
148
- global reducedim_init (f:: typeof (identity), op:: typeof (* ), A:: T , region) =
149
- reducedim_initarray (A, region, one (eltype (A)))
150
- global reducedim_init (f:: Union{typeof(abs),typeof(abs2)} , op:: typeof (+ ), A:: T , region) =
151
- reducedim_initarray (A, region, real (zero (eltype (A))))
152
- global reducedim_init (f:: Union{typeof(abs),typeof(abs2)} , op:: typeof (* ), A:: T , region) =
153
- reducedim_initarray (A, region, real (one (eltype (A))))
147
+ global reducedim_init (f, op:: Union{typeof(+),typeof(add_sum)} , A:: T , region) =
148
+ reducedim_initarray (A, region, mapreduce_single (f, op, zero (eltype (A))))
149
+ global reducedim_init (f, op:: Union{typeof(*),typeof(mul_prod)} , A:: T , region) =
150
+ reducedim_initarray (A, region, mapreduce_single (f, op, one (eltype (A))))
154
151
end
155
152
156
- reducedim_init (f:: Union{typeof(identity),typeof(abs),typeof(abs2)} , op:: typeof (+ ), A:: AbstractArray{Bool} , region) =
157
- reducedim_initarray (A, region, 0 )
158
-
159
153
# # generic (map)reduction
160
154
161
155
has_fast_linear_indexing (a:: AbstractArray ) = false
@@ -610,7 +604,7 @@ julia> any!([1 1], A)
610
604
"""
611
605
any! (r, A)
612
606
613
- for (fname, op) in [(:sum , :add_tosys ), (:prod , :mul_tosys ),
607
+ for (fname, op) in [(:sum , :add_sum ), (:prod , :mul_prod ),
614
608
(:maximum , :scalarmax ), (:minimum , :scalarmin ),
615
609
(:all , :& ), (:any , :| )]
616
610
fname! = Symbol (fname, ' !' )
0 commit comments