TEX commands as input in SymPy

141 views
Skip to first unread message

redsto...@163.com

unread,
Oct 8, 2014, 6:05:00 AM10/8/14
to sy...@googlegroups.com

Is there any function in SymPy can take this TEX command as an input

(-6x^2-x-7)(2x^3+3x^2-2x-5)

to find the derivative?

Jason Moore

unread,
Oct 8, 2014, 8:17:47 AM10/8/14
to sy...@googlegroups.com
We don't have a full LaTeX parser in SymPy. If this is the only expression you are interested in, simply:

import sympy
x = sympy.symbols('x')
expr = (-6 * x**2 - x - 7) * (2 * x**3 + 3 * x**2 - 2 * x - 5)
expr.diff(x)

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/04e0cbd4-5b6b-4418-8166-024a0729aff8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mateusz Paprocki

unread,
Oct 8, 2014, 9:42:16 AM10/8/14
to sympy
Hi,
I wanted to suggest a mathematica parser for this:

In [1]: import sympy.parsing.mathematica as m

In [2]: m.parse("(-6x^2-x-7)(2x^3+3x^2-2x-5)")
Out[2]: '(-6*x**2-x-7)*(2*x**3+3*x**2-2*x-5)'

In [3]: eval(_)
Out[3]:
⎛ 2 ⎞ ⎛ 3 2 ⎞
⎝- 6⋅x - x - 7⎠⋅⎝2⋅x + 3⋅x - 2⋅x - 5⎠

But there is bug there and the above output requires the following patch:

diff --git a/sympy/parsing/mathematica.py b/sympy/parsing/mathematica.py
index f86d59a..eeb12f7 100644
--- a/sympy/parsing/mathematica.py
+++ b/sympy/parsing/mathematica.py
@@ -39,7 +39,7 @@ def parse(s):
(r"\A\((.+)\)([\w\.].*)\Z", # Implied multiplication - (a)b
lambda m: "(" + parse(m.group(1)) + ")*" + parse(m.group(2))),

- (r"\A([\d\.]+)([a-zA-Z].*)\Z", # Implied multiplicatin - 2a
+ (r"\A([+-]?[\d\.]+)([a-zA-Z].*)\Z", # Implied multiplicatin - 2a
lambda m: parse(m.group(1)) + "*" + parse(m.group(2))),

(r"\A([^=]+)([\^\-\*/\+=]=?)(.+)\Z", # Infix operator

Mateusz

Aaron Meurer

unread,
Oct 8, 2014, 3:37:07 PM10/8/14
to sy...@googlegroups.com
Have a look at parse_expr() in sympy.parsing.sympy_parser.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAGBZUCZfJVXw27Rh0U31ST9tU%3DwZbkuqUS792ff364JYvdG%2BHg%40mail.gmail.com.

redsto...@163.com

unread,
Oct 8, 2014, 10:28:03 PM10/8/14
to sy...@googlegroups.com
>>> from sympy.parsing.sympy_parser import parse_expr
>>> parse_expr("(-6x^2-x-7)(2x^3+3x^2-2x-5)")
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    parse_expr("(-6x^2-x-7)(2x^3+3x^2-2x-5)")
  File "C:\Python34\lib\site-packages\sympy\parsing\sympy_parser.py", line 757, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "C:\Python34\lib\site-packages\sympy\parsing\sympy_parser.py", line 691, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1
    (-Integer (6 )Symbol ('x' )^Integer (2 )-Symbol ('x' )-Integer (7 ))(Integer (2 )Symbol ('x' )^Integer (3 )+Integer (3 )Symbol ('x' )^Integer (2 )-Integer (2 )Symbol ('x' )-Integer (5 ))
                       ^
SyntaxError: invalid syntax
>>> 

在 2014年10月9日星期四UTC+8上午3时37分07秒,Aaron Meurer写道:

redsto...@163.com

unread,
Oct 8, 2014, 10:36:52 PM10/8/14
to sy...@googlegroups.com
>>> from sympy.parsing.sympy_parser import parse_expr
>>> parse_expr("(-6x^2-x-7)(2x^3+3x^2-2x-5)")
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    parse_expr("(-6x^2-x-7)(2x^3+3x^2-2x-5)")
  File "C:\Python34\lib\site-packages\sympy\parsing\sympy_parser.py", line 757, in parse_expr
    return eval_expr(code, local_dict, global_dict)
  File "C:\Python34\lib\site-packages\sympy\parsing\sympy_parser.py", line 691, in eval_expr
    code, global_dict, local_dict)  # take local objects in preference
  File "<string>", line 1
    (-Integer (6 )Symbol ('x' )^Integer (2 )-Symbol ('x' )-Integer (7 ))(Integer (2 )Symbol ('x' )^Integer (3 )+Integer (3 )Symbol ('x' )^Integer (2 )-Integer (2 )Symbol ('x' )-Integer (5 ))
                       ^
SyntaxError: invalid syntax
>>> 

在 2014年10月9日星期四UTC+8上午3时37分07秒,Aaron Meurer写道:
Have a look at parse_expr() in sympy.parsing.sympy_parser.

redsto...@163.com

unread,
Oct 8, 2014, 10:41:13 PM10/8/14
to sy...@googlegroups.com
can you send me the patch file ,I am not familiar with git .

在 2014年10月8日星期三UTC+8下午9时42分16秒,Mateusz Paprocki写道:

redsto...@163.com

unread,
Oct 8, 2014, 10:45:37 PM10/8/14
to sy...@googlegroups.com
I can get the string representation of (-6x**2-x-7)*(2*x**3+3*x**2-2*x-5),is there anyway we can use this string directly to  find the derivative?
>>> from sympy.parsing.mathematica import *
>>> parse('(-6x^2-x-7)(2x^3+3x^2-2x-5)')
'(-6x**2-x-7)*(2*x**3+3*x**2-2*x-5)'

在 2014年10月8日星期三UTC+8下午8时17分47秒,Jason Moore写道:

Aaron Meurer

unread,
Oct 11, 2014, 10:18:05 PM10/11/14
to sy...@googlegroups.com
You have to enable various transformers like implicit multiplication.
See help(parse_expr)

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/2ca9f2ab-6070-4fe9-b896-131d5b36c53f%40googlegroups.com.

Chris Smith

unread,
Oct 17, 2014, 10:27:44 AM10/17/14
to sy...@googlegroups.com

>>> from sympy.parsing.mathematica import mathematica as m
>>> m('(-6 * x**2 - x - 7) * (2 * x**3 + 3 * x**2 - 2 * x - 5)')
(-6*x**2 - x - 7)*(2*x**3 + 3*x**2 - 2*x - 5)
>>> _.diff(x)
(-12*x - 1)*(2*x**3 + 3*x**2 - 2*x - 5) + (-6*x**2 - x - 7)*(6*x**2 + 6*x - 2)

redstone-cold

unread,
Oct 24, 2014, 7:54:40 AM10/24/14
to sy...@googlegroups.com, smi...@gmail.com

Is there any way to simplify (-12*x - 1)*(2*x3 + 3*x2 - 2*x - 5) + (-6*x2 - x - 7)*(6*x2 + 6*x - 2) ? I want to get the final result ,not an intermediate one.
You received this message because you are subscribed to a topic in the Google Groups "sympy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sympy/RoJ_o598B7Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

Aaron Meurer

unread,
Oct 24, 2014, 5:23:57 PM10/24/14
to sy...@googlegroups.com, Chris Smith
Reply all
Reply to author
Forward
0 new messages