Skip to content

Commit b48095c

Browse files
committed
rcum_convert
1 parent bec650a commit b48095c

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

base/multidimensional.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,13 +734,14 @@ end
734734
# see discussion in #18364 ... we try not to widen type of the resulting array
735735
# from cumsum or cumprod, but in some cases (+, Bool) we may not have a choice.
736736
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)
739738

740739
# handle sums of Vector{Bool} and similar. it would be nice to handle
741740
# any AbstractArray here, but it's not clear how that would be possible
742741
rcum_promote_type(op, ::Type{Array{T,N}}) where {T,N} = Array{rcum_promote_type(op,T), N}
743742

743+
rcum_convert(::Type{T}, x) where {T} = convert(T,x)
744+
rcum_convert(::Type{T}, c::Char) where {T <: AbstractString} = T(string(c))
744745
# accumulate_pairwise slightly slower then accumulate, but more numerically
745746
# stable in certain situations (e.g. sums).
746747
# it does double the number of operations compared to accumulate,
@@ -1021,7 +1022,7 @@ function accumulate!(op, B, A, dim::Integer)
10211022
# register usage and will be slightly faster
10221023
ind1 = inds_t[1]
10231024
@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])
10251026
B[first(ind1), I] = tmp
10261027
for i_1 = first(ind1)+1:last(ind1)
10271028
tmp = op(tmp, A[i_1, I])
@@ -1071,7 +1072,7 @@ end
10711072
# Copy the initial element in each 1d vector along dimension `dim`
10721073
ii = first(ind)
10731074
@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])
10751076
end
10761077
# Accumulate
10771078
@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)
11191120
inds == linearindices(B) || throw(DimensionMismatch("linearindices of A and B don't match"))
11201121
dim > 1 && return copyto!(B, A)
11211122
i1 = inds[1]
1122-
cur_val = v1
1123+
cur_val = rcum_convert(eltype(B), v1)
11231124
B[i1] = cur_val
11241125
@inbounds for i in inds[2:end]
11251126
cur_val = op(cur_val, A[i])

test/arrayops.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,10 @@ end
21792179

21802180
#25506
21812181
@test accumulate((acc, x) -> acc+x[1], 0, [(1,2), (3,4), (5,6)]) == [1, 4, 9]
2182+
@test accumulate(*, ['a', 'b']) == ["a", "ab"]
2183+
@inferred accumulate(*, String[])
2184+
@test accumulate(*, ['a' 'b'; 'c' 'd'], 1) == ["a" "b"; "ac" "bd"]
2185+
@test accumulate(*, ['a' 'b'; 'c' 'd'], 2) == ["a" "ab"; "c" "cd"]
21822186
end
21832187

21842188
struct F21666{T <: Base.TypeArithmetic}

0 commit comments

Comments
 (0)