Skip to content

Instantly share code, notes, and snippets.

Created June 15, 2013 11:20
Show Gist options
  • Select an option

  • Save anonymous/5787793 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/5787793 to your computer and use it in GitHub Desktop.
A bit of SymPy (symbolic math) awesomeness.
>>> from sympy import Symbol, solve, pprint
>>> a=Symbol('a')
>>> b=Symbol('b')
>>> c=Symbol('c')
>>> expr = a*x*x + b*x + c
>>> solve(expr)
[{a: -(b*x + c)/x**2}]
>>> solve(expr,x, dict=True)
[{x: (-b + sqrt(-4*a*c + b**2))/(2*a)}, {x: -(b + sqrt(-4*a*c + b**2))/(2*a)}]
>>> pprint(solve(expr,x, dict=True))
⎡⎧ _____________⎫ ⎧ ⎛ _____________⎞⎫⎤
⎢⎪ ╱ 2 ⎪ ⎪ ⎜ ╱ 2 ⎟⎪⎥
⎢⎨ -b + ╲╱ -4⋅a⋅c + b ⎬ ⎨ -⎝b + ╲╱ -4⋅a⋅c + b ⎠⎬⎥
⎢⎪x: ─────────────────────⎪, ⎪x: ───────────────────────⎪⎥
⎣⎩ 2⋅a ⎭ ⎩ 2⋅a ⎭⎦
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment