TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

SymPy – simplify

69 pointsby ptypeover 11 years ago

8 comments

ivan_ahover 11 years ago
Sympy is pretty awesome. I use live.sympy.org a lot with my students because you can bookmark an entire interactive session as a (long) URL. For example, suppose you had to answer the following question. <i>Find the position of the object at t=3 seconds, if it starts from x_i=20[m], with v_i=10[m&#x2F;s] and undergoes a constant acceleration of a=5[m&#x2F;s^2].</i> Ans: 72.5[m]. Sol.: <a href="http://bit.ly/1hJS9P3" rel="nofollow">http:&#x2F;&#x2F;bit.ly&#x2F;1hJS9P3</a><p>Below are some more examples of cool stuff sympy can do.<p>Expand, factor, and solve polynomials:<p><pre><code> &gt;&gt;&gt; P = (x-1)*(x-2)*(x-3) &gt;&gt;&gt; P.expand() x**3 - 6*x**2 + 11*x - 6 &gt;&gt;&gt; P.factor() (x - 1)*(x - 2)*(x - 3) &gt;&gt;&gt; roots = solve(P,x) &gt;&gt;&gt; roots [1, 2, 3] </code></pre> Derive the solutions of a quadratic equation:<p><pre><code> &gt;&gt;&gt; solve( a*x**2 + b*x + c, x) [(-b + sqrt(-4*a*c + b**2))&#x2F;(2*a), -(b + sqrt(-4*a*c + b**2))&#x2F;(2*a)] </code></pre> Compute symbolic outputs of trig functions:<p><pre><code> &gt;&gt;&gt; sin(pi&#x2F;6) 1&#x2F;2 &gt;&gt;&gt; cos(pi&#x2F;6) sqrt(3)&#x2F;2 </code></pre> Sympy knows about trig identities:<p><pre><code> &gt;&gt;&gt; sin(x) == cos(x - pi&#x2F;2) True &gt;&gt;&gt; simplify( sin(x)*cos(y)+cos(x)*sin(y) ) sin(x + y) &gt;&gt;&gt; e = sin(x)**2 + cos(x)**2 &gt;&gt;&gt; trigsimp(e) 1 </code></pre> Find the solution to the simple harmonic oscillator differential equation (x&#x27;&#x27;(t)+w^2x(t)=0):<p><pre><code> &gt;&gt;&gt; sol = dsolve( diff(x(t),t,t) + w**2*x(t), x(t) ) &gt;&gt;&gt; sol x(t) == C1*sin(w*t) + C2*cos(w*t) </code></pre> For v4.1 of my math book, I&#x27;m going to add a short sympy tutorial. I&#x27;ll post a printable version of it to HN when I release, so stay tuned ;)
评论 #7216060 未加载
Cogitoover 11 years ago
Is there any way to ensure simplified expressions are only defined over the same range as the original expression?<p>My guess is no, but it is often important to understand that, for example, a polynomial fraction is not defined when the denominator is 0.<p>To use an example from the page,<p><pre><code> simplify((x**3 + x**2 - x - 1)&#x2F;(x**2 + 2*x + 1)) </code></pre> gives<p><pre><code> x - 1 </code></pre> but should give<p><pre><code> x - 1; x != 1 + sqrt(2), x != 1 - sqrt(2) </code></pre> This is not always what you want to see, but it would be cool if you could turn it on :)
评论 #7215604 未加载
chisophugisover 11 years ago
Appears to need a bit more robustification.<p><pre><code> &gt;&gt;&gt; simplify(sqrt(x^2)) Traceback (most recent call last): File &quot;&lt;string&gt;&quot;, line 1, in &lt;module&gt; File &quot;&#x2F;base&#x2F;data&#x2F;home&#x2F;apps&#x2F;s~sympy-live-hrd&#x2F;43.373169527249054993&#x2F;sympy&#x2F;sympy&#x2F;functions&#x2F;elementary&#x2F;miscellaneous.py&quot;, line 110, in sqrt return C.Pow(arg, S.Half) File &quot;&#x2F;base&#x2F;data&#x2F;home&#x2F;apps&#x2F;s~sympy-live-hrd&#x2F;43.373169527249054993&#x2F;sympy&#x2F;sympy&#x2F;core&#x2F;cache.py&quot;, line 93, in wrapper r = func(*args, **kw_args) File &quot;&#x2F;base&#x2F;data&#x2F;home&#x2F;apps&#x2F;s~sympy-live-hrd&#x2F;43.373169527249054993&#x2F;sympy&#x2F;sympy&#x2F;core&#x2F;power.py&quot;, line 119, in __new__ obj = b._eval_power(e) AttributeError: &#x27;Not&#x27; object has no attribute &#x27;_eval_power&#x27;</code></pre>
评论 #7215796 未加载
评论 #7215599 未加载
评论 #7215749 未加载
Scene_Cast2over 11 years ago
For anyone looking for a symbolic computation environment - check out Maxima, I find it to be more pleasant than command-line SymPy.
评论 #7215610 未加载
评论 #7215970 未加载
shmageggyover 11 years ago
Great, another tool that decreases my motivation to actually work on sharpening my lagging calc skills.
评论 #7215963 未加载
评论 #7216774 未加载
NamTafover 11 years ago
This is basically the sym() and simple() functions from Matlab, which is something I really felt wasn&#x27;t nearly widespread enough. Knowing about this is fantastic and I&#x27;m going to use it heaps. Thanks!
sheetjsover 11 years ago
If anyone from sympy is reading this:<p><pre><code> simplify(exp(x)&#x2F;exp(x - 1)) </code></pre> is causing a runtime error:<p><pre><code> RuntimeError: maximum recursion depth exceeded while calling a Python object</code></pre>
评论 #7215237 未加载
rchover 11 years ago
Nice. The taylor series capabilities seem pretty alright too.