How to avoid Sympify to drop parentheses automatically

血红的双手。 提交于 2021-02-09 10:59:12

问题


Let's say I have the following string

str1 = "a*(-d*(b+d)+d*(e*f))"

If I sympify it the result is

a*(d*e*f - d*(b + d))

But I don't want that Sympy drop those parenthesis. I want the final expression to be

a*(d*(e*f) - d*(b + d))

How can this be done?


回答1:


That's a known bug. That same bug in the google code archive.

If you follow the google code archive link, you'll find a patch that possibly fixes your issue.

Other than that, there's not much you can do. The issue's status is still open

There's also this pull request that you might find interesting, since it presumably fixes the issue.




回答2:


Products in SymPy are automatically flattened.

You can manually avoid this with Mul(..., evaluate=False), like

a*(Mul(d, e*f, evaluate=False) - d*(b + d))


来源:https://stackoverflow.com/questions/48462442/how-to-avoid-sympify-to-drop-parentheses-automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!