Skip to content

Commit

Permalink
patch holes in call syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jverzani committed Mar 2, 2025
1 parent 1ff8629 commit b223c02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ x,y,z = symbols("x y z")
@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
function simple_newton(f, fp, x0)
Expand Down

0 comments on commit b223c02

Please sign in to comment.