@@ -734,13 +734,14 @@ end
734
734
# see discussion in #18364 ... we try not to widen type of the resulting array
735
735
# from cumsum or cumprod, but in some cases (+, Bool) we may not have a choice.
736
736
rcum_promote_type (op, :: Type{T} , :: Type{S} ) where {T,S} = promote_op (op, T, S)
737
- rcum_promote_type (op, :: Type{T} ) where {T<: Number } = rcum_promote_type (op, T,T)
738
- rcum_promote_type (op, :: Type{T} ) where {T} = T
737
+ rcum_promote_type (op, :: Type{T} ) where {T} = rcum_promote_type (op, T,T)
739
738
740
739
# handle sums of Vector{Bool} and similar. it would be nice to handle
741
740
# any AbstractArray here, but it's not clear how that would be possible
742
741
rcum_promote_type (op, :: Type{Array{T,N}} ) where {T,N} = Array{rcum_promote_type (op,T), N}
743
742
743
+ rcum_convert (:: Type{T} , x) where {T} = convert (T,x)
744
+ rcum_convert (:: Type{T} , c:: Char ) where {T <: AbstractString } = T (string (c))
744
745
# accumulate_pairwise slightly slower then accumulate, but more numerically
745
746
# stable in certain situations (e.g. sums).
746
747
# it does double the number of operations compared to accumulate,
@@ -1021,7 +1022,7 @@ function accumulate!(op, B, A, dim::Integer)
1021
1022
# register usage and will be slightly faster
1022
1023
ind1 = inds_t[1 ]
1023
1024
@inbounds for I in CartesianIndices (tail (inds_t))
1024
- tmp = convert (eltype (B), A[first (ind1), I])
1025
+ tmp = rcum_convert (eltype (B), A[first (ind1), I])
1025
1026
B[first (ind1), I] = tmp
1026
1027
for i_1 = first (ind1)+ 1 : last (ind1)
1027
1028
tmp = op (tmp, A[i_1, I])
@@ -1071,7 +1072,7 @@ end
1071
1072
# Copy the initial element in each 1d vector along dimension `dim`
1072
1073
ii = first (ind)
1073
1074
@inbounds for J in R2, I in R1
1074
- B[I, ii, J] = A[I, ii, J]
1075
+ B[I, ii, J] = rcum_convert ( eltype (B), A[I, ii, J])
1075
1076
end
1076
1077
# Accumulate
1077
1078
@inbounds for J in R2, i in first (ind)+ 1 : last (ind), I in R1
@@ -1119,7 +1120,7 @@ function _accumulate1!(op, B, v1, A::AbstractVector, dim::Integer)
1119
1120
inds == linearindices (B) || throw (DimensionMismatch (" linearindices of A and B don't match" ))
1120
1121
dim > 1 && return copyto! (B, A)
1121
1122
i1 = inds[1 ]
1122
- cur_val = v1
1123
+ cur_val = rcum_convert ( eltype (B), v1)
1123
1124
B[i1] = cur_val
1124
1125
@inbounds for i in inds[2 : end ]
1125
1126
cur_val = op (cur_val, A[i])
0 commit comments