Symbolic integration with SymPy.jl

F-YF
Mon, 28 Mar 2022 15:24:14 +0000
General Usage

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

7 posts - 4 participants