Skip to content

Commit b20468d

Browse files
Update docstrings
1 parent c9cd664 commit b20468d

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

src/vmapreducethen.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,35 @@
55
#
66
############################################################################################
77

8+
"""
9+
vmapreducethen(f, op, g, A::AbstractArray; dims=:, init)
10+
11+
Apply function `f` to each element of `A`, reduce the result over the dimensions
12+
`dims` using the binary function `op`, then apply `g` to the result. Equivalent to
13+
`g.(mapreduce(f, op, A, dims=dims, init=init))` but avoids the intermediate implied
14+
by said expression while also fusing the post-transform `g` such that the output array
15+
is populated in a single pass.
16+
17+
The reduction necessitates an initial value `init` which may be `<:Number` or a function
18+
which accepts a single type argument (e.g. `zero`);
19+
`init` is optional for binary operators `+`, `*`, `min`, and `max`.
20+
21+
# Examples
22+
```jldoctest
23+
julia> vmapreducethen(abs2, +, √, [1 2; 3 4], dims=1) # L₂-norm; see `vnorm`
24+
1×2 Matrix{Float64}:
25+
3.16228 4.47214
26+
27+
julia> vmapreducethen(abs2, +, √, [1 2; 3 4], dims=2, init=1000.0)
28+
2×1 Matrix{Float64}:
29+
31.701734968294716
30+
32.01562118716424
31+
32+
julia> vmapreducethen(exp, +, log, [5 6; 7 8], dims=1) # LSE, but recommend `vlogsumexp`
33+
1×2 Matrix{Float64}:
34+
7.12693 8.12693
35+
```
36+
"""
837
function vmapreducethen(f::F, op::OP, g::G, init::I, A::AbstractArray{T, N}, dims::NTuple{M, Int}) where {F, OP, G, I, T, N, M}
938
Dᴬ = size(A)
1039
Dᴮ′ = ntuple(d -> d dims ? 1 : Dᴬ[d], Val(N))

src/vmapreducethen_vararg.jl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@
66
############################################################################################
77

88
"""
9-
vmapreducethen(f, op, g, init, As::Vararg{AbstractArray, N}) where {N}
9+
vmapreducethen(f, op, g, As::Vararg{AbstractArray, N}; dims=:, init) where {N}
1010
11-
Version of mapreducethen for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction occurring over
12-
all dimensions.
11+
Version of `mapreducethen` wherein `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction over
12+
the dimensions `dims`.
13+
14+
# Examples
15+
```jldoctest
16+
julia> x, y, z = [1 2; 3 4], [5 6; 7 8], [9 10; 11 12];
17+
18+
julia> vmapreducethen((a, b) -> abs2(a - b), +, √, x, y, dims=2) # Euclidean distance
19+
2×1 Matrix{Float64}:
20+
5.656854249492381
21+
5.656854249492381
22+
23+
julia> vmapreducethen(*, *, exp, x, y, z, dims=2, init=-1.0)
24+
2×1 Matrix{Float64}:
25+
0.0
26+
0.0
27+
```
1328
"""
1429
vmapreducethen(f::F, op::OP, g::G, init::I, As::Vararg{AbstractArray, P}) where {F, OP, G, I, P} =
1530
vmapreducethen(f, op, init, As, :)
1631

17-
"""
18-
vmapreducethen(f, op, g, As::Vararg{AbstractArray, N}; dims=:, init) where {N}
19-
20-
Keyword args version for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ.
21-
"""
2232
vmapreducethen(f, op, g, As::Vararg{AbstractArray, P}; dims=:, init) where {P} =
2333
vmapreducethen(f, op, g, init, As, dims)
2434

25-
"""
26-
vmapreducethen(f, op, g, init, As::Tuple{Vararg{AbstractArray}}, dims=:)
27-
28-
Version of mapreducethen for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ, with reduction over given `dims`.
29-
"""
3035
function vmapreducethen(f::F, op::OP, g::G, init::I, As::Tuple{Vararg{AbstractArray, P}}, dims::NTuple{M, Int}) where {F, OP, G, I, M, P}
3136
ax = axes(As[1])
3237
for p = 2:P
@@ -237,7 +242,7 @@ function vmapreducethen(f::F, op::OP, g::G, init::I, As::Tuple{Vararg{AbstractAr
237242
axes(As[p]) == ax || throw(DimensionMismatch)
238243
end
239244
Dᴮ′ = ntuple(d -> d dims ? 1 : length(ax[d]), ndims(As[1]))
240-
B = similar(As[1], Base.promote_op(op, Base.promote_op(f, ntuple(p -> eltype(As[p]), Val(P))...), Int), Dᴮ′)
245+
B = similar(As[1], Base.promote_op(g, Base.promote_op(op, Base.promote_op(f, ntuple(p -> eltype(As[p]), Val(P))...), Int)), Dᴮ′)
241246
_vmapreducethen_vararg_init!(f, op, g, init, B, As, dims)
242247
end
243248

0 commit comments

Comments
 (0)