Skip to content

Commit 4ce4008

Browse files
hayatoikomaGaika
authored andcommitted
Sum over an iterator (#195)
1 parent 069ad68 commit 4ce4008

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/array.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ function linspace{T}(::Type{AFArray}, a::T, b::T, c::Integer)
106106
dx = (b_fl - a_fl)/(Float64(c) - 1.0)
107107
range(AFArray{Float64}, a_fl, dx, c)
108108
end
109+
function Base.sum(A::AFArray, ndims)
110+
B = A
111+
for i in ndims
112+
B = sum(B, i)
113+
end
114+
return B
115+
end
109116
isfinite(a::AFArray) = !isinf(a) & !isnan(a)
110117

111118
import Base: /, *, +, -, ^, ==, <, >, <=, >=, !, !=, &, |, <<, >>, xor

test/math.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ amf = AFArray(am)
144144

145145
@test Array(sum(amf, 1)) sum(am, 1)
146146
@test Array(sum(amf, 2)) sum(am, 2)
147+
@test Array(sum(amf, [1,2])) sum(am, [1,2])
148+
@test Array(sum(amf, [2,1])) sum(am, [2,1])
149+
@test Array(sum(amf, 1:2)) sum(am, 1:2)
150+
@test Array(sum(amf, (1,2))) sum(am, (1,2))
147151

148152
@test Array(prod(amf, 1)) prod(am, 1)
149153
@test Array(prod(amf, 2)) prod(am, 2)

0 commit comments

Comments
 (0)