Symbolic integration with SymPy.jl

I ran into a problem with SymPy.jl for symbolic integration,

using SymPy
m = symbols(“m”, integer=true)
n = symbols(“n”, integer=true)
x = symbols(“x”, integer=true)
integrate(diff(x^4,(x,2)),x)

The output is 4*x^3

When I actually need the results of this expression:

integrate(diff(x^m,(x,n)),x)

An error is thrown:

ERROR: PyError ($(Expr(:escape, :(ccall(#= /home/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'TypeError'>
TypeError('\nCannot give expansion for symbolic count. If you just want a list of\nall variables of differentiation, use _wrt_variables.')
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/integrals.py", line 1566, in integrate
    return integral.doit(**doit_flags)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/integrals.py", line 613, in doit
    antideriv = self._eval_integral(
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/integrals.py", line 1098, in _eval_integral
    h = meijerint_indefinite(g, x)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/meijerint.py", line 1672, in meijerint_indefinite
    for a in sorted(_find_splitting_points(f, x) | {S.Zero}, key=default_sort_key):
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/meijerint.py", line 424, in _find_splitting_points
    compute_innermost(expr, innermost)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/integrals/meijerint.py", line 415, in compute_innermost
    m = expr.match(p*x + q)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/core/basic.py", line 1702, in match
    m = canonical(pattern).matches(canonical(self), old=old)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/core/add.py", line 553, in matches
    return self._matches_commutative(expr, repl_dict, old)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/core/operations.py", line 336, in _matches_commutative
    expr = collect(expr, free)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/simplify/radsimp.py", line 391, in collect
    terms = [parse_term(i) for i in args]
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/simplify/radsimp.py", line 391, in <listcomp>
    terms = [parse_term(i) for i in args]
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/simplify/radsimp.py", line 281, in parse_term
    sexpr, deriv = parse_derivative(expr)
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/simplify/radsimp.py", line 211, in parse_derivative
    expr, sym, order = deriv.expr, deriv.variables[0], 1
  File "/home/.julia/conda/3/lib/python3.9/site-packages/sympy/core/function.py", line 1664, in variables
    raise TypeError(filldedent('''

Stacktrace:
  [1] pyerr_check
    @ ~/.julia/packages/PyCall/7a7w0/src/exception.jl:62 [inlined]
  [2] pyerr_check
    @ ~/.julia/packages/PyCall/7a7w0/src/exception.jl:66 [inlined]
  [3] _handle_error(msg::String)
    @ PyCall ~/.julia/packages/PyCall/7a7w0/src/exception.jl:83
  [4] macro expansion
    @ ~/.julia/packages/PyCall/7a7w0/src/exception.jl:97 [inlined]
  [5] #107
    @ ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:43 [inlined]
  [6] disable_sigint
    @ ./c.jl:458 [inlined]
  [7] __pycall!
    @ ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:42 [inlined]
  [8] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{Sym, Sym}, nargs::Int64, kw::Ptr{Nothing})
    @ PyCall ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:29
  [9] _pycall!(ret::PyCall.PyObject, o::PyCall.PyObject, args::Tuple{Sym, Sym}, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ PyCall ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:11
 [10] (::PyCall.PyObject)(::Sym, ::Vararg{Sym}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ PyCall ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:86
 [11] (::PyCall.PyObject)(::Sym, ::Vararg{Sym})
    @ PyCall ~/.julia/packages/PyCall/7a7w0/src/pyfncall.jl:86
 [12] integrate(ex::Sym, args::Sym; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ SymPy ~/.julia/packages/SymPy/ycPbL/src/importexport.jl:102
 [13] integrate(ex::Sym, args::Sym)
    @ SymPy ~/.julia/packages/SymPy/ycPbL/src/importexport.jl:102
 [14] top-level scope
    @ REPL[6]:1

But this works:

integrate(diff(x^m,(x,2)),x)

          ⎛⎧    m             ⎞
          ⎜⎪   x              ⎟
          ⎜⎪───────  for m ≠ 1⎟
m⋅(m - 1)⋅⎜⎨m⋅x - x           ⎟
          ⎜⎪                  ⎟
          ⎜⎪log(x)   otherwise⎟
          ⎝⎩                  ⎠

It looks like the problem is on the symbol n. I can’t find a solution, so I hope you guys can help me

While SymPy.jl should (continue to) work (and I don’t know enough to help you), or if not from Julia then directly from Python, FYI I thought we had a replacement in Julia-only packages such as Symbolics.jl.

While I see “the goal is different”, you may want to look into it:
https://symbolics.juliasymbolics.org/dev/comparison/

Comparison of Julia’s Symbolics.jl vs SymPy for Symbolic Computation

This error message is from the underlying Python library, as it can’t handle the “expansion for symbolic count” (the symbolic n). If this is really your problem, you can do the differentiation by hand, and then integrate the resulting c x^{m-n}, but I don’t know how to tease that out of SymPy.

1 Like

Well, the integral in my calculation is also an irreplaceable step, but Symbolics.jl cannot implement the operation of the integral.

This is a good idea, but there must be derivatives of order n(symbolic variables) in my problem, so, it is inevitable that similar operations[like this: integrate(diff(x^m,(x,n)),x)] will occur.

but

julia> integrate(diff(x^1,(x,2)),x)
0

and also


julia> integrate(diff(x^m,(x,2)),x)(m=>1)
0

which are different from

          ⎛⎧    m             ⎞
          ⎜⎪   x              ⎟
          ⎜⎪───────  for m ≠ 1⎟
m⋅(m - 1)⋅⎜⎨m⋅x - x           ⎟
          ⎜⎪                  ⎟
          ⎜⎪log(x)   otherwise⎟
          ⎝⎩                  ⎠

PS
Why was this convention chosen?

I’m not sure how to interpret them and I don’t know if they can help you, but these kind of expressions produce results :slight_smile:


integrate(diff(x^(n+2), n),x)

integrate(diff(x^(n+m), n),x)

if you redefine the old m as n+m (new) you should have your formula