Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add promote_symtype for _map and _mapreduce. #814

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions src/array-lib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,21 +290,45 @@ end
@wrapped Base.map(f, x, y, z::AbstractArray, w...) = _map(f, x, y, z, w...)

function _map(f, x, xs...)
return ArrayOp(
SymbolicUtils._promote_symtype(_map, (x,xs)),
(idx...,),
expr,
+,
Term{Any}(map, [f, x, xs...])
)
end

function SymbolicUtils._promote_symtype(::typeof(_map), args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we should be extending this function... It looks like a very internal helper function. Why can't we do this with just promote_symtype?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I attempted initially in 47801aa (btw sorry for the 3rd commit, did not know about git rebase)

I hope I remember correctly: The problem with promote_symtype for _map and _mapreduce is that promote_symtype is meant to have a signature promote_symtype(f, arg_symtypes...).
_promote_symtype receives the arguments instead of their types, i.e., _promote_symtype(f, args...).
In case of _map or _mapreduce we would like to recursively call promote_symtype for the mapped function and maybe a binary operator.
With promote_symtype I tried to obtain function instances by accessing the corresponding field of the type:

mapreduce(Base.Fix1(promote_symtype, F.instance), promote_type, eltype.(XS))
Works only for singleton types, but that should still catch almost all use-cases. Feels awkward nonetheless.
With _promote_symtype and the arguments available, we can use exactly the same logic as if actually calling _map and _mapreduce to get consistent type predictions (another problem with the promote_symtype approach).

f, x, xs... = args

N = ndims(x)
idx = makesubscripts(N)

expr = f(map(a->a[idx...], [x, xs...])...)

Atype = propagate_atype(map, f, x, xs...)
ArrayOp(Atype{symtype(expr), N},
(idx...,),
expr,
+,
Term{Any}(map, [f, x, xs...]))

return Atype{symtype(expr), N}
end

@inline _mapreduce(f, g, x, dims, kw) = mapreduce(f, g, x; dims=dims, kw...)

function SymbolicUtils._promote_symtype(::typeof(_mapreduce), args)
@assert length(args) == 5
f, op, x, dims, kw = args

N = ndims(x)
idx = makesubscripts(N)
expr = f(x[idx...])
T = symtype(op(expr, expr))
if dims === (:)
return T
end
Atype = propagate_atype(_mapreduce, f, op, x, dims, (kw...,))
return Atype{T, N}
end

function scalarize_op(::typeof(_mapreduce), t)
f,g,x,dims,kw = arguments(t)
# we wrap and unwrap to make things work smoothly.
Expand All @@ -313,20 +337,19 @@ function scalarize_op(::typeof(_mapreduce), t)
end

@wrapped function Base.mapreduce(f, g, x::AbstractArray; dims=:, kw...)
idx = makesubscripts(ndims(x))
out_idx = [dims == (:) || i in dims ? 1 : idx[i] for i = 1:ndims(x)]
expr = f(x[idx...])
T = symtype(g(expr, expr))
Stype = SymbolicUtils._promote_symtype(_mapreduce, (f,g,x,dims,kw))
if dims === (:)
return Term{T}(_mapreduce, [f, g, x, dims, (kw...,)])
return Term{Stype}(_mapreduce, [f, g, x, dims, (kw...,)])
end

Atype = propagate_atype(_mapreduce, f, g, x, dims, (kw...,))
ArrayOp(Atype{T, ndims(x)},
(out_idx...,),
expr,
g,
Term{Any}(_mapreduce, [f, g, x, dims, (kw...,)]))
idx = makesubscripts(ndims(x))
out_idx = [dims == (:) || i in dims ? 1 : idx[i] for i = 1:ndims(x)]
return ArrayOp(
Stype,
(out_idx...,),
expr,
g,
Term{Any}(_mapreduce, [f, g, x, dims, (kw...,)])
)
end

for (ff, opts) in [sum => (identity, +, false),
Expand Down
10 changes: 9 additions & 1 deletion test/arrays.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Symbolics
using SymbolicUtils, Test
using Symbolics: symtype, shape, wrap, unwrap, Unknown, Arr, arrterm, jacobian, @variables, value, get_variables, @arrayop, getname, metadata, scalarize
using Symbolics: symtype, shape, wrap, unwrap, Unknown, Arr, arrterm, jacobian, @variables, value, get_variables, @arrayop, getname, metadata, scalarize, simplify
using Base: Slice
using SymbolicUtils: Sym, term, operation

Expand Down Expand Up @@ -97,6 +97,14 @@ getdef(v) = getmetadata(v, Symbolics.VariableDefaultValue)
# #417
@test isequal(Symbolics.scalarize(x', (1,1)), x[1])

# #814
@test isa(simplify(sum(b .* 1) ), Num)
@test isa(simplify(prod(x .+ 2.0) ), Num)
@test isa(simplify(mapreduce(x -> (x+1)/(0.1 + abs(x)), ^, u)), Num) # exponent(s) must not be Int until #455 is fixed
@test Symbolics.symtype(simplify(sum(b .* 1))) <: Real
@test Symbolics.symtype(simplify(prod(x .+ 2.0))) <: Real
@test Symbolics.symtype(simplify(mapreduce(x -> (x+1)/(0.1 + abs(x)), ^, u))) <: Real

# #483
# examples by @gronniger
@variables A[1:2, 1:2]
Expand Down