The core module in SymPy package contains Number class which represents atomic numbers. This class has two subclasses: Float and Rational class. Rational class is further extended by Integer class.
Float class represents a floating point number of arbitrary precision.
>>> from sympy import Float >>> Float(6.32)
The output for the above code snippet is as follows −
6.32
SymPy can convert an integer or a string to float.
>>> Float(10)
10.0
Float('1.33E5')# scientific notation
133000.0
While converting to float, it is also possible to specify number of digits for precision as given below −
>>> Float(1.33333,2)
The output for the above code snippet is as follows −
1.3
A representation of a number (p/q) is represented as object of Rational class with q being a non-zero number.
>>> Rational(3/4)
The output for the above code snippet is as follows −
$\frac{3}{4}$
If a floating point number is passed to Rational() constructor, it returns underlying value of its binary representation
>>> Rational(0.2)
The output for the above code snippet is as follows −
$\frac{3602879701896397}{18014398509481984}$
For simpler representation, specify denominator limitation.
>>> Rational(0.2).limit_denominator(100)
The output for the above code snippet is as follows −
$\frac{1}{5}$
When a string is passed to Rational() constructor, a rational number of arbitrary precision is returned.
>>> Rational("3.65")
The output for the above code snippet is as follows −
$\frac{73}{20}$
Rational object can also be obtained if two number arguments are passed. Numerator and denominator parts are available as properties.
>>> a=Rational(3,5) >>> print (a) >>> print ("numerator:{}, denominator:{}".format(a.p, a.q))
The output for the above code snippet is as follows −
3/5
numerator:3, denominator:5
>>> a
The output for the above code snippet is as follows −
$\frac{3}{5}$
Integer class in SymPy represents an integer number of any size. The constructor can accept a Float or Rational number, but the fractional part is discarded
>>> Integer(10)
The output for the above code snippet is as follows −
10
>>> Integer(3.4)
The output for the above code snippet is as follows −
3
>>> Integer(2/7)
The output for the above code snippet is as follows −
0
SymPy has a RealNumber class that acts as alias for Float. SymPy also defines Zero and One as singleton classes accessible with S.Zero and S.One respectively as shown below −
>>> S.Zero
The output is as follows −
0
>>> S.One
The output is as follows −
1
Other predefined Singleton number objects are Half, NaN, Infinity and ImaginaryUnit
>>> from sympy import S >>> print (S.Half)
The output is as follows −
½
>>> print (S.NaN)
The output is as follows −
nan
Infinity is available as oo symbol object or S.Infinity
>>> from sympy import oo >>> oo
The output for the above code snippet is as follows −
$\infty$
>>> S.Infinity
The output for the above code snippet is as follows −
$\infty$
ImaginaryUnit number can be imported as I symbol or accessed as S.ImaginaryUnit and represents square root of -1
>>> from sympy import I >>> I
When you execute the above code snippet, you get the following output −
i
>>> S.ImaginaryUnit
The output of the above snippet is as follows −
i
>>> from sympy import sqrt >>> i=sqrt(-1) >>> i*i
When you execute the above code snippet, you get the following output −
-1