@@ -1354,34 +1354,50 @@ end
1354
1354
1355
1355
# ## Reduction
1356
1356
1357
+ function _sum (f, x:: AbstractSparseVector )
1358
+ n = length (x)
1359
+ n > 0 || return sum (f, nonzeros (x)) # return zero() of proper type
1360
+ m = nnz (x)
1361
+ (m == 0 ? n * f (zero (eltype (x))) :
1362
+ m == n ? sum (f, nonzeros (x)) :
1363
+ Base. add_sum ((n - m) * f (zero (eltype (x))), sum (f, nonzeros (x))))
1364
+ end
1365
+
1366
+ sum (f:: Union{Function, Type} , x:: AbstractSparseVector ) = _sum (f, x) # resolve ambiguity
1367
+ sum (f, x:: AbstractSparseVector ) = _sum (f, x)
1357
1368
sum (x:: AbstractSparseVector ) = sum (nonzeros (x))
1358
1369
1359
- function maximum ( x:: AbstractSparseVector{T} ) where T <: Real
1370
+ function _maximum (f, x:: AbstractSparseVector )
1360
1371
n = length (x)
1361
- n > 0 || throw (ArgumentError (" maximum over empty array is not allowed." ))
1372
+ if n == 0
1373
+ if f === abs || f === abs2
1374
+ return zero (eltype (x)) # preserving maximum(abs/abs2, x) behaviour in 1.0.x
1375
+ else
1376
+ throw (ArgumentError (" maximum over an empty array is not allowed." ))
1377
+ end
1378
+ end
1362
1379
m = nnz (x)
1363
- (m == 0 ? zero (T ) :
1364
- m == n ? maximum (nonzeros (x)) :
1365
- max (zero (T) , maximum (nonzeros (x)))):: T
1380
+ (m == 0 ? f ( zero (eltype (x)) ) :
1381
+ m == n ? maximum (f, nonzeros (x)) :
1382
+ max (f ( zero (eltype (x))) , maximum (f, nonzeros (x))))
1366
1383
end
1367
1384
1368
- function minimum (x:: AbstractSparseVector{T} ) where T<: Real
1385
+ maximum (f:: Union{Function, Type} , x:: AbstractSparseVector ) = _maximum (f, x) # resolve ambiguity
1386
+ maximum (f, x:: AbstractSparseVector ) = _maximum (f, x)
1387
+ maximum (x:: AbstractSparseVector ) = maximum (identity, x)
1388
+
1389
+ function _minimum (f, x:: AbstractSparseVector )
1369
1390
n = length (x)
1370
- n > 0 || throw (ArgumentError (" minimum over empty array is not allowed." ))
1391
+ n > 0 || throw (ArgumentError (" minimum over an empty array is not allowed." ))
1371
1392
m = nnz (x)
1372
- (m == 0 ? zero (T ) :
1373
- m == n ? minimum (nonzeros (x)) :
1374
- min (zero (T) , minimum (nonzeros (x)))):: T
1393
+ (m == 0 ? f ( zero (eltype (x)) ) :
1394
+ m == n ? minimum (f, nonzeros (x)) :
1395
+ min (f ( zero (eltype (x))) , minimum (f, nonzeros (x))))
1375
1396
end
1376
1397
1377
- for f in [:sum , :maximum , :minimum ], op in [:abs , :abs2 ]
1378
- SV = :AbstractSparseVector
1379
- if f === :minimum
1380
- @eval ($ f)(:: typeof ($ op), x:: $SV{T} ) where {T<: Number } = nnz (x) < length (x) ? ($ op)(zero (T)) : ($ f)($ op, nonzeros (x))
1381
- else
1382
- @eval ($ f)(:: typeof ($ op), x:: $SV ) = ($ f)($ op, nonzeros (x))
1383
- end
1384
- end
1398
+ minimum (f:: Union{Function, Type} , x:: AbstractSparseVector ) = _minimum (f, x) # resolve ambiguity
1399
+ minimum (f, x:: AbstractSparseVector ) = _minimum (f, x)
1400
+ minimum (x:: AbstractSparseVector ) = minimum (identity, x)
1385
1401
1386
1402
norm (x:: SparseVectorUnion , p:: Real = 2 ) = norm (nonzeros (x), p)
1387
1403
0 commit comments