Skip to content

Instantly share code, notes, and snippets.

@amitsaha
Forked from anonymous/sympy_quadratic.py
Last active December 18, 2015 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitsaha/5787802 to your computer and use it in GitHub Desktop.
Save amitsaha/5787802 to your computer and use it in GitHub Desktop.
A bit of SymPy:
>>> 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