@@ -247,6 +247,11 @@ centralize_sumabs2(A::AbstractArray, m, ifirst::Int, ilast::Int) =
247247function centralize_sumabs2! (R:: AbstractArray{S} , A:: AbstractArray , means:: AbstractArray ) where S
248248 # following the implementation of _mapreducedim! at base/reducedim.jl
249249 lsiz = Base. check_reducedims (R,A)
250+ for i in 1 : max (ndims (R), ndims (means))
251+ if axes (means, i) != axes (R, i)
252+ throw (DimensionMismatch (" dimension $i of `mean` should have indices $(axes (R, i)) , but got $(axes (means, i)) " ))
253+ end
254+ end
250255 isempty (R) || fill! (R, zero (S))
251256 isempty (A) && return R
252257
@@ -295,9 +300,9 @@ function varm!(R::AbstractArray{S}, A::AbstractArray, m::AbstractArray; correcte
295300end
296301
297302"""
298- varm(itr, m ; dims, corrected::Bool=true)
303+ varm(itr, mean ; dims, corrected::Bool=true)
299304
300- Compute the sample variance of collection `itr`, with known mean(s) `m `.
305+ Compute the sample variance of collection `itr`, with known mean(s) `mean `.
301306
302307The algorithm returns an estimator of the generative distribution's variance
303308under the assumption that each entry of `itr` is an IID drawn from that generative
@@ -308,7 +313,8 @@ whereas the sum is scaled with `n` if `corrected` is
308313`false` with `n` the number of elements in `itr`.
309314
310315If `itr` is an `AbstractArray`, `dims` can be provided to compute the variance
311- over dimensions, and `m` may contain means for each dimension of `itr`.
316+ over dimensions. In that case, `mean` must be an array with the same shape as
317+ `mean(itr, dims=dims)` (additional trailing singleton dimensions are allowed).
312318
313319!!! note
314320 If array contains `NaN` or [`missing`](@ref) values, the result is also
331337
332338
333339"""
334- var(itr; dims, corrected::Bool=true, mean=nothing)
340+ var(itr; corrected::Bool=true, mean=nothing[, dims] )
335341
336342Compute the sample variance of collection `itr`.
337343
@@ -343,10 +349,12 @@ If `corrected` is `true`, then the sum is scaled with `n-1`,
343349whereas the sum is scaled with `n` if `corrected` is
344350`false` with `n` the number of elements in `itr`.
345351
346- A pre-computed `mean` may be provided.
347-
348352If `itr` is an `AbstractArray`, `dims` can be provided to compute the variance
349- over dimensions, and `mean` may contain means for each dimension of `itr`.
353+ over dimensions.
354+
355+ A pre-computed `mean` may be provided. When `dims` is specified, `mean` must be
356+ an array with the same shape as `mean(itr, dims=dims)` (additional trailing
357+ singleton dimensions are allowed).
350358
351359!!! note
352360 If array contains `NaN` or [`missing`](@ref) values, the result is also
@@ -416,11 +424,13 @@ If `corrected` is `true`, then the sum is scaled with `n-1`,
416424whereas the sum is scaled with `n` if `corrected` is
417425`false` with `n` the number of elements in `itr`.
418426
419- A pre-computed `mean` may be provided.
420-
421427If `itr` is an `AbstractArray`, `dims` can be provided to compute the standard deviation
422428over dimensions, and `means` may contain means for each dimension of `itr`.
423429
430+ A pre-computed `mean` may be provided. When `dims` is specified, `mean` must be
431+ an array with the same shape as `mean(itr, dims=dims)` (additional trailing
432+ singleton dimensions are allowed).
433+
424434!!! note
425435 If array contains `NaN` or [`missing`](@ref) values, the result is also
426436 `NaN` or `missing` (`missing` takes precedence if array contains both).
@@ -445,9 +455,9 @@ std(iterable; corrected::Bool=true, mean=nothing) =
445455 sqrt (var (iterable, corrected= corrected, mean= mean))
446456
447457"""
448- stdm(itr, m ; corrected::Bool=true)
458+ stdm(itr, mean ; corrected::Bool=true)
449459
450- Compute the sample standard deviation of collection `itr`, with known mean(s) `m `.
460+ Compute the sample standard deviation of collection `itr`, with known mean(s) `mean `.
451461
452462The algorithm returns an estimator of the generative distribution's standard
453463deviation under the assumption that each entry of `itr` is an IID drawn from that generative
@@ -457,10 +467,9 @@ If `corrected` is `true`, then the sum is scaled with `n-1`,
457467whereas the sum is scaled with `n` if `corrected` is
458468`false` with `n` the number of elements in `itr`.
459469
460- A pre-computed `mean` may be provided.
461-
462470If `itr` is an `AbstractArray`, `dims` can be provided to compute the standard deviation
463- over dimensions, and `m` may contain means for each dimension of `itr`.
471+ over dimensions. In that case, `mean` must be an array with the same shape as
472+ `mean(itr, dims=dims)` (additional trailing singleton dimensions are allowed).
464473
465474!!! note
466475 If array contains `NaN` or [`missing`](@ref) values, the result is also
@@ -1065,7 +1074,11 @@ end
10651074function centralize_sumabs2! (R:: AbstractArray{S} , A:: SparseMatrixCSC{Tv,Ti} , means:: AbstractArray ) where {S,Tv,Ti}
10661075 require_one_based_indexing (R, A, means)
10671076 lsiz = Base. check_reducedims (R,A)
1068- size (means) == size (R) || error (" size of means must match size of R" )
1077+ for i in 1 : max (ndims (R), ndims (means))
1078+ if axes (means, i) != axes (R, i)
1079+ throw (DimensionMismatch (" dimension $i of `mean` should have indices $(axes (R, i)) , but got $(axes (means, i)) " ))
1080+ end
1081+ end
10691082 isempty (R) || fill! (R, zero (S))
10701083 isempty (A) && return R
10711084
0 commit comments