Skip to content

Commit 596159e

Browse files
Update mapreducethen docstrings
1 parent ca6f222 commit 596159e

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/vmapreducethen.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,35 @@ end
361361
################
362362
# Threaded version
363363

364+
"""
365+
vtmapreducethen(f, op, g, A::AbstractArray; dims=:, init)
366+
367+
Apply function `f` to each element of `A`, reduce the result over the dimensions
368+
`dims` using the binary function `op`, then apply `g` to the result. Equivalent to
369+
`g.(mapreduce(f, op, A, dims=dims, init=init))` but avoids the intermediate implied
370+
by said expression while also fusing the post-transform `g` such that the output array
371+
is populated in a single pass. Threaded.
372+
373+
The reduction necessitates an initial value `init` which may be `<:Number` or a function
374+
which accepts a single type argument (e.g. `zero`);
375+
`init` is optional for binary operators `+`, `*`, `min`, and `max`.
376+
377+
# Examples
378+
```jldoctest
379+
julia> vtmapreducethen(abs2, +, √, [1 2; 3 4], dims=1) # L₂-norm; see `vnorm`
380+
1×2 Matrix{Float64}:
381+
3.16228 4.47214
382+
383+
julia> vtmapreducethen(abs2, +, √, [1 2; 3 4], dims=2, init=1000.0)
384+
2×1 Matrix{Float64}:
385+
31.701734968294716
386+
32.01562118716424
387+
388+
julia> vtmapreducethen(exp, +, log, [5 6; 7 8], dims=1) # LSE, but recommend `vlogsumexp`
389+
1×2 Matrix{Float64}:
390+
7.12693 8.12693
391+
```
392+
"""
364393
function vtmapreducethen(f::F, op::OP, g::G, init::I, A::AbstractArray{T, N}, dims::NTuple{M, Int}) where {F, OP, G, I, T, N, M}
365394
Dᴬ = size(A)
366395
Dᴮ′ = ntuple(d -> d dims ? 1 : Dᴬ[d], Val(N))

src/vmapreducethen_vararg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ end
415415
"""
416416
vtmapreducethen(f, op, g, init, As::Vararg{AbstractArray, N}) where {N}
417417
418-
Version of mapreducethen for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction occurring over
418+
Version of `mapreducethen` for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction occurring over
419419
all dimensions.
420420
"""
421421
vtmapreducethen(f::F, op::OP, g::G, init::I, As::Vararg{AbstractArray, P}) where {F, OP, G, I, P} =
@@ -432,7 +432,7 @@ vtmapreducethen(f, op, g, As::Vararg{AbstractArray, P}; dims=:, init) where {P}
432432
"""
433433
vtmapreducethen(f, op, g, init, As::Tuple{Vararg{AbstractArray}}, dims=:)
434434
435-
Version of mapreducethen for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction over given `dims`.
435+
Version of `mapreducethen` for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction over given `dims`.
436436
"""
437437
function vtmapreducethen(f::F, op::OP, g::G, init::I, As::Tuple{Vararg{AbstractArray, P}}, dims::NTuple{M, Int}) where {F, OP, G, I, M, P}
438438
ax = axes(As[1])

0 commit comments

Comments
 (0)