|
6 | 6 | ############################################################################################
|
7 | 7 |
|
8 | 8 | """
|
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} |
10 | 10 |
|
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 | +``` |
13 | 28 | """
|
14 | 29 | vmapreducethen(f::F, op::OP, g::G, init::I, As::Vararg{AbstractArray, P}) where {F, OP, G, I, P} =
|
15 | 30 | vmapreducethen(f, op, init, As, :)
|
16 | 31 |
|
17 |
| -""" |
18 |
| - vmapreducethen(f, op, g, As::Vararg{AbstractArray, N}; dims=:, init) where {N} |
19 |
| -
|
20 |
| -Keyword args version for `f` : ℝᴺ → ℝ, then `g` : ℝ → ℝ. |
21 |
| -""" |
22 | 32 | vmapreducethen(f, op, g, As::Vararg{AbstractArray, P}; dims=:, init) where {P} =
|
23 | 33 | vmapreducethen(f, op, g, init, As, dims)
|
24 | 34 |
|
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 |
| -""" |
30 | 35 | 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}
|
31 | 36 | ax = axes(As[1])
|
32 | 37 | for p = 2:P
|
@@ -237,7 +242,7 @@ function vmapreducethen(f::F, op::OP, g::G, init::I, As::Tuple{Vararg{AbstractAr
|
237 | 242 | axes(As[p]) == ax || throw(DimensionMismatch)
|
238 | 243 | end
|
239 | 244 | 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ᴮ′) |
241 | 246 | _vmapreducethen_vararg_init!(f, op, g, init, B, As, dims)
|
242 | 247 | end
|
243 | 248 |
|
|
0 commit comments