SymPy.jl icon indicating copy to clipboard operation
SymPy.jl copied to clipboard

replace error

Open AmplitudeGravity opened this issue 6 years ago • 2 comments

a = SymPy.Wild("a") ex = log(sin(x)) + tan(sin(x^2)) replace(ex, (sin(x)).func, a -> sin(2a))

it export errors as following. Do the developers know how to fix it? PyError ($(Expr(:escape, :(ccall(#= /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'RuntimeError'> RuntimeError('Julia exception: MethodError: no method matching iterate(::getfield(Main, Symbol("##74#75")))\nClosest candidates are:\n iterate(!Matched::Core.SimpleVector) at essentials.jl:568\n iterate(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:568\n iterate(!Matched::ExponentialBackOff) at error.jl:199\n ...\nStacktrace:\n [1] jlwrap_iterator(::Function) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyiterator.jl:150\n [2] pyjlwrap_getiter(::Ptr{PyCall.PyObject_struct}) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyiterator.jl:131\n [3] macro expansion at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/exception.jl:81 [inlined]\n [4] __pycall!(::PyCall.PyObject, ::Ptr{PyCall.PyObject_struct}, ::PyCall.PyObject, ::Ptr{Nothing}) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:44\n [5] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{PyCall.PyObject,getfield(Main, Symbol("##74#75"))}, ::Int64, ::Ptr{Nothing}) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:29\n [6] _pycall!(::PyCall.PyObject, ::PyCall.PyObject, ::Tuple{PyCall.PyObject,getfield(Main, Symbol("##74#75"))}, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:11\n [7] #call#111(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::PyCall.PyObject, ::PyCall.PyObject, ::Vararg{Any,N} where N) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:89\n [8] (::PyCall.PyObject)(::PyCall.PyObject, ::Vararg{Any,N} where N) at /Users/gangchen/.julia/packages/PyCall/ttONZ/src/pyfncall.jl:89\n [9] #replace#91(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::SymPy.Sym, ::PyCall.PyObject, ::Function) at /Users/gangchen/.julia/packages/SymPy/woEOA/src/patternmatch.jl:94\n [10] replace(::SymPy.Sym, ::PyCall.PyObject, ::Function) at /Users/gangchen/.julia/packages/SymPy/woEOA/src/patternmatch.jl:94\n [11] top-level scope at In[181]:3\n [12] eval at ./boot.jl:328 [inlined]\n [13] softscope_include_string(::Module, ::String, ::String) at /Users/gangchen/.julia/packages/SoftGlobalScope/cSbw5/src/SoftGlobalScope.jl:218\n [14] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/gangchen/.julia/packages/IJulia/gI2uA/src/execute_request.jl:67\n [15] #invokelatest#1 at ./essentials.jl:742 [inlined]\n [16] invokelatest at ./essentials.jl:741 [inlined]\n [17] eventloop(::ZMQ.Socket) at /Users/gangchen/.julia/packages/IJulia/gI2uA/src/eventloop.jl:8\n [18] (::getfield(IJulia, Symbol("##15#18")))() at ./task.jl:259') File "/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/core/basic.py", line 1341, in replace value = sympify(value) File "/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/core/sympify.py", line 325, in sympify if iterable(a): File "/Users/gangchen/.julia/conda/3/lib/python3.7/site-packages/sympy/core/compatibility.py", line 288, in iterable iter(i)

AmplitudeGravity avatar May 10 '19 10:05 AmplitudeGravity

The issue is the last argument. SymPy want's a lambda there for anything more complicated. Here is one way. If you think of a better way, it might make a nice addition:

using SymPy
import PyCall

@vars x
ex = log(sin(x)) + tan(sin(x^2))
PyCall.py"""
import sympy
def anonfn(*args):
    return sympy.sin(2*sympy.Mul(*args))
"""
ex.replace(sympy.sin, py"anonfn")

The use of func is a trick to get the enclosing function from a sympy expressions. The sympy.sin is a more direct method for that.

jverzani avatar May 10 '19 16:05 jverzani

Somewhere this got addressed. Closing now. Re-open if there is still an issue.

jverzani avatar Jun 05 '21 10:06 jverzani