sympy acceptable expression :'x sin(1/x)+abs(x)' or 'x sin(1/x)+|x|' or 'x Sin[1/x] + Abs[x]'?

119 views
Skip to first unread message

Lee Philip

unread,
Nov 22, 2014, 6:04:41 AM11/22/14
to sy...@googlegroups.com

is 'x sin(1/x)+abs(x)' or 'x sin(1/x)+|x|' or 'x Sin[1/x] + Abs[x]' sympy acceptable?

I got  exceptions by  these expressions ,wonder is this a bug or not ?

 

test code here

http://codepad.org/NyO5tj1I

Joachim Durchholz

unread,
Nov 22, 2014, 6:39:44 AM11/22/14
to sy...@googlegroups.com
You won't get faster answers by double-posting.
If nobody answered, nobody had the time or knowledge to give a useful
answer.


Regards,
Jo

Ondřej Čertík

unread,
Nov 22, 2014, 11:13:22 AM11/22/14
to sympy
Hi Lee,

On Sat, Nov 22, 2014 at 4:04 AM, Lee Philip <redsto...@163.com> wrote:
> is 'x sin(1/x)+abs(x)' or 'x sin(1/x)+|x|' or 'x Sin[1/x] + Abs[x]' sympy
> acceptable?
>
> I got exceptions by these expressions ,wonder is this a bug or not ?

You seem to found a bug in the Mathematica parser, i.e. this works:

In [7]: M("x*Sin[x]")
Out[7]: x⋅sin(x)

In [9]: M("Sin[1/x]")
Out[9]:
⎛1⎞
sin⎜─⎟
⎝x⎠

But this fails:

In [10]: M("x*Sin[1/x]")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-e58ebb4f8e06> in <module>()
----> 1 M("x*Sin[1/x]")

/home/ondrej/repos/sympy/sympy/parsing/mathematica.py in mathematica(s)
6
7 def mathematica(s):
----> 8 return sympify(parse(s))
9
10

/home/ondrej/repos/sympy/sympy/core/sympify.pyc in sympify(a, locals,
convert_xor, strict, rational, evaluate)
315 try:
316 a = a.replace('\n', '')
--> 317 expr = parse_expr(a, local_dict=locals,
transformations=transformations, evaluate=evaluate)
318 except (TokenError, SyntaxError) as exc:
319 raise SympifyError('could not parse %r' % a, exc)

/home/ondrej/repos/sympy/sympy/parsing/sympy_parser.pyc in
parse_expr(s, local_dict, transformations, global_dict, evaluate)
818 code = compile(evaluateFalse(code), '<string>', 'eval')
819
--> 820 return eval_expr(code, local_dict, global_dict)
821
822

/home/ondrej/repos/sympy/sympy/parsing/sympy_parser.pyc in
eval_expr(code, local_dict, global_dict)
731 """
732 expr = eval(
--> 733 code, global_dict, local_dict) # take local objects
in preference
734
735 return expr

<string> in <module>()

TypeError: 'Symbol' object has no attribute '__getitem__'


I reported it here:

https://github.com/sympy/sympy/issues/8501

However, if you just want to use sympy, don't use the Mathematica
parser, but rather sympy directly, like this:

In [11]: var("x")
Out[11]: x

In [12]: x*sin(1/x)+abs(x)
Out[12]:
⎛1⎞
x⋅sin⎜─⎟ + │x│
⎝x⎠

In [13]: _.diff(x)
Out[13]:
d d ⎛1⎞
re(x)⋅──(re(x)) + im(x)⋅──(im(x)) cos⎜─⎟
dx dx ⎛1⎞ ⎝x⎠
───────────────────────────────── + sin⎜─⎟ - ──────
│x│ ⎝x⎠ x

Ondrej

>
>
>
> test code here
>
> http://codepad.org/NyO5tj1I
>
> --
> 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/988b0ad3-bb8d-4ba6-9509-ad395f4353d2%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Aaron Meurer

unread,
Nov 22, 2014, 3:22:26 PM11/22/14
to sy...@googlegroups.com
People often double post because they don't realize that new members
of the group are moderated. Moderators should avoid double posting
messages from the moderation queue.

Aaron Meurer
> --
> 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/547075F8.3000202%40durchholz.org.

Ondřej Čertík

unread,
Nov 22, 2014, 3:57:16 PM11/22/14
to sympy
On Sat, Nov 22, 2014 at 1:22 PM, Aaron Meurer <asme...@gmail.com> wrote:
> People often double post because they don't realize that new members
> of the group are moderated. Moderators should avoid double posting
> messages from the moderation queue.

Exactly. I always delete one duplicate and only post the other one.

Ondrej
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6Lxb5Lfi8sFN1SPDs%3D_6dXppE6rywhgWpK7c%2Bttz%2B_MEg%40mail.gmail.com.

Lee Philip

unread,
Nov 23, 2014, 1:01:17 AM11/23/14
to sy...@googlegroups.com
thanks for your kind help ,but there is no '*' in x Sin[1/x] + Abs[x]

在 2014年11月23日星期日UTC+8上午12时13分22秒,Ondřej Čertík写道:

Lee Philip

unread,
Nov 23, 2014, 1:07:02 AM11/23/14
to sy...@googlegroups.com
I post this question again is because something was lack  in my first post(but I have already deleted it before I post  this question again ).
anyway ,there is a bug in the Mathematica parser
https://github.com/sympy/sympy/issues/8501

在 2014年11月22日星期六UTC+8下午7时04分41秒,Lee Philip写道:
Reply all
Reply to author
Forward
0 new messages