Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/mathfuns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ for (meth, libnm, modu) in [
(:log,:log,:Base),
(:sqrt,:sqrt,:Base),
(:exp,:exp,:Base),
(:sign, :sign, :Base),
(:eta,:dirichlet_eta,:SpecialFunctions),
(:zeta,:zeta,:SpecialFunctions),
]
Expand Down
7 changes: 4 additions & 3 deletions src/subs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ subs(ex::T, d::Pair...) where {T <: SymbolicType} = subs(ex, [(p.first, p.second
## Allow an expression to be called, as with ex(2). When there is more than one symbol, one can rely on order of `free_symbols` or
## be explicit by passing in pairs : `ex(x=>1, y=>2)` or a dict `ex(Dict(x=>1, y=>2))`.
function (ex::Basic)(args...)
xs = free_symbols(ex)
subs(ex, collect(zip(xs, args))...)
xs = free_symbols(ex)
isempty(xs) && return ex
subs(ex, collect(zip(xs, args))...)
end
(ex::Basic)(x::AbstractDict) = subs(ex, x)
(ex::Basic)(x::Pair...) = subs(ex, x...)
(ex::Basic)(x::Pair, xs::Pair...) = subs(ex, x, xs...)


## Lambdify
Expand Down
13 changes: 12 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,18 @@ BasicTrigFunction = Union{[SymEngine.BasicType{Val{i}} for i in trig_types]...}


###
"Is expression a symbol"
is_symbol(x::Basic) = is_symbol(BasicType(x))
is_symbol(x::BasicType{Val{:Symbol}}) = true
is_symbol(x::BasicType) = false


"Does expression contain the symbol"
function has_symbol(ex::Basic, x::Basic)
is_symbol(x) || throw(ArgumentError("Not a symbol"))
res = ccall((:basic_has_symbol, libsymengine), Cuint, (Ref{Basic},Ref{Basic}), ex, x)
Bool(convert(Int, res))
end


" Return free symbols in an expression as a `Set`"
Expand Down Expand Up @@ -305,4 +317,3 @@ function Serialization.deserialize(s::Serialization.AbstractSerializer, ::Type{B
throw_if_error(res)
return a
end

18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ A = [x 2; x 1]
x,y,z = symbols("x y z")
@test length(SymEngine.free_symbols([x*y, y,z])) == 3

# is/has/free symbol(s)
@vars x y z
@test SymEngine.is_symbol(x)
@test !SymEngine.is_symbol(x(2))
@test !SymEngine.is_symbol(x^2)
@test SymEngine.has_symbol(x^2, x)
@test SymEngine.has_symbol(sin(sin(sin(x))), x)
@test !SymEngine.has_symbol(x^2, y)
@test Set(free_symbols(x*y)) == Set([x,y])
@test Set(free_symbols(x*y^z)) != Set([x,y])

# call without specifying variables
@vars x y
z = x(2)
@test x(2) == 2
@test (x*y^2)(1,2) == subs(x*y^2, x=>1, y=>2) == (x*y^2)(x=>1, y=>2)
@test z() == 2
@test z(1) == 2

## check that callable symengine expressions can be used as functions for duck-typed functions
@vars x
Expand Down
Loading