最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Sympy having trouble simplifying fractions, which are easily simplified by hand - Stack Overflow

programmeradmin5浏览0评论

I have found this annoying problem in sympy. I want to simply the following simple fraction expression below but can't make it work... Sympy even recognizes the two equations as equal making it even more frustrating.

import sympy as sp
a,b,c,d = sp.symbols('a, b, c, d', positive = True)
expr = (a*b*c+2*d*a*c+b*c*d)/(2*a+b)
correct_simpl = c*(d +  a*b/(2*a+b))
display(expr)
display(expr.simplify())
display(correct_simpl)
correct_simpl.equals(expr)

Returns:

I have found this annoying problem in sympy. I want to simply the following simple fraction expression below but can't make it work... Sympy even recognizes the two equations as equal making it even more frustrating.

import sympy as sp
a,b,c,d = sp.symbols('a, b, c, d', positive = True)
expr = (a*b*c+2*d*a*c+b*c*d)/(2*a+b)
correct_simpl = c*(d +  a*b/(2*a+b))
display(expr)
display(expr.simplify())
display(correct_simpl)
correct_simpl.equals(expr)

Returns:

Share Improve this question asked 14 hours ago Pancake_stack_with_syrupPancake_stack_with_syrup 332 bronze badges 4
  • 3 Why do you think the third version is better from the second? Possibly it is just a question of taste (and so on how to program). I definitely prefer the second one (as the simplest) – Giacomo Catenazzi Commented 14 hours ago
  • The third version has fewer symbols (7 vs 11 symbols in version 2) and shows the dependency of the expression on the different parameters more clearly. – Pancake_stack_with_syrup Commented 13 hours ago
  • 1 Other tools had different function to simplify (according preferences). I do not know about sympy. Just different people have different opinion on the "standard simplification". So I think it is the answer: it depends on opinion (of the programmer) – Giacomo Catenazzi Commented 12 hours ago
  • 2 Most computer algebra systems and libraries, sympy included, are not designed to produce, not even in simplification, expressions in the form considered by humans as the nicest. The fact that it verified the expressions are equal is an excellent result and that's typically what you'd use sympy for. You can usually "wrestle" an expression to the form you want, applying more or less naturally the various functions available to rewrite it - for your expression you may use expr.apart(d).collect(c). – kikon Commented 10 hours ago
Add a comment  | 

2 Answers 2

Reset to default 0

The following can give you your expression of taste. You can use your interactive prompt to see what each of the steps is doing. The cse is there to collect that 2a+b into a single term that doesn't expand when doing the expansion: r,e=cse(factor_terms(collect(expr,d)));expand(e[0]).subs(r)

The fact that expr.simplify() does not return the exact result is due to the heuristics used by Sympy’s general simplify() function—it doesn’t guarantee a unique “simplest” form, only that the result is mathematically equivalent (as confirmed by correct_simpl.equals(expr)).

If you require the result in that specific form, you can try using a more targeted function such as:

sp.factor_terms(expr)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论