SymPy – Numbers

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 โˆ’

3434

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 โˆ’

360287970189639718014398509481984360287970189639718014398509481984

For simpler representation, specify denominator limitation.

>>> Rational(0.2).limit_denominator(100)

The output for the above code snippet is as follows โˆ’

1515

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 โˆ’

73207320

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 โˆ’

3535

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 โˆ’

โˆžโˆž

>>> S.Infinity

The output for the above code snippet is as follows โˆ’

โˆžโˆž

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

This Post Has One Comment

Leave a Reply