How to keep fractions in your equation output
Question:
I’ve been using Python to calculate math equations. For example:
from sympy import Symbol, Derivative, Integral
x = Symbol('x')
d = Symbol('d')
Integral(8*x**(6/5)-7*x**(3/2),x).doit()
Which results in the output:
3.63636363636364*x**2.2 - 2.8*x**2.5
Is there a way to show this answer as fractions as opposed to decimals? I would like to see the output as:
(40/11)*x**(11/5)-(14/5)*x**(5/2)+C
Answers:
you can work with the fractions
module in order to have integral fractions:
from sympy import Symbol, Derivative, Integral
from fractions import Fraction
x = Symbol('x')
d = Symbol('d')
ii = Integral(8*x**Fraction(6,5)-7*x**Fraction(3,2),x).doit()
# 40*x**(11/5)/11 - 14*x**(5/2)/5
there is also the Rational
class in sympy itself:
from sympy import Symbol, Derivative, Integral, Rational
x = Symbol('x')
d = Symbol('d')
ii = Integral(8*x**Rational(6,5)-7*x**Rational(3,2),x).doit()
Use sympy’s rational
instead of 6/5
. Python will immediately interpret 6/5
and return some floating point number (1.2
in this case).
from sympy import Symbol, Derivative, Integral, Rational
x = Symbol('x')
d = Symbol('d')
Integral(8*x**(Rational(6,5))-7*x**(Rational(3,2)),x).doit()
SymPy has Rational class for rational numbers.
from sympy import *
# other stuff
integrate(8*x**Rational(6, 5) - 7*x**Rational(3, 2),x)
No need for Integral().doit()
unless you actually want to print out the un-evaluated form.
Other versions:
integrate(8*x**Rational('6/5') - 7*x**Rational('3/2'),x)
(rational number can be parsed from a string);
integrate(8*x**(S.One*6/5) - 7*x**(S.One*3/2),x)
(beginning the computation with the SymPy object for “1” turns it into SymPy object manipulation, avoiding plain Python division, which would give a float)
I’ve been using Python to calculate math equations. For example:
from sympy import Symbol, Derivative, Integral
x = Symbol('x')
d = Symbol('d')
Integral(8*x**(6/5)-7*x**(3/2),x).doit()
Which results in the output:
3.63636363636364*x**2.2 - 2.8*x**2.5
Is there a way to show this answer as fractions as opposed to decimals? I would like to see the output as:
(40/11)*x**(11/5)-(14/5)*x**(5/2)+C
you can work with the fractions
module in order to have integral fractions:
from sympy import Symbol, Derivative, Integral
from fractions import Fraction
x = Symbol('x')
d = Symbol('d')
ii = Integral(8*x**Fraction(6,5)-7*x**Fraction(3,2),x).doit()
# 40*x**(11/5)/11 - 14*x**(5/2)/5
there is also the Rational
class in sympy itself:
from sympy import Symbol, Derivative, Integral, Rational
x = Symbol('x')
d = Symbol('d')
ii = Integral(8*x**Rational(6,5)-7*x**Rational(3,2),x).doit()
Use sympy’s rational
instead of 6/5
. Python will immediately interpret 6/5
and return some floating point number (1.2
in this case).
from sympy import Symbol, Derivative, Integral, Rational
x = Symbol('x')
d = Symbol('d')
Integral(8*x**(Rational(6,5))-7*x**(Rational(3,2)),x).doit()
SymPy has Rational class for rational numbers.
from sympy import *
# other stuff
integrate(8*x**Rational(6, 5) - 7*x**Rational(3, 2),x)
No need for Integral().doit()
unless you actually want to print out the un-evaluated form.
Other versions:
integrate(8*x**Rational('6/5') - 7*x**Rational('3/2'),x)
(rational number can be parsed from a string);
integrate(8*x**(S.One*6/5) - 7*x**(S.One*3/2),x)
(beginning the computation with the SymPy object for “1” turns it into SymPy object manipulation, avoiding plain Python division, which would give a float)