Sympy 简明教程

SymPy - Numbers

SymPy 包中的核心模块包含表示原子数的数字类。该类有两个子类:Float 和 Rational 类。Rational 类由 Integer 类进一步扩展。

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 类表示任意精度的浮点数。

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

6.32

SymPy 可以将整数或字符串转换为浮点数。

SymPy can convert an integer or a string to float.

>>> Float(10)

10.0

10.0

Float('1.33E5')# scientific notation

133000.0

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

1.3

数字 (p/q) 的表示形式通过 Rational 类对象表示,而 q 是非零数字。

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}$

$\frac{3}{4}$

如果将浮点数传递给 Rational() 构造函数,它将返回其二进制表示的底层值

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}$

$\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}$

$\frac{1}{5}$

当字符串传给 Rational() 构造函数时,将返回任意精度的有理数。

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}$

$\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

3/5

numerator:3, denominator:5

numerator:3, denominator:5

>>> a

上述代码段的输出如下:

The output for the above code snippet is as follows −

$\frac{3}{5}$

$\frac{3}{5}$

SymPy 中的 Integer 类表示任何大小的整数。构造函数可以接受 Float 或 Rational 数,但会舍弃分数部分。

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

10

>>> Integer(3.4)

上述代码段的输出如下:

The output for the above code snippet is as follows −

3

3

>>> Integer(2/7)

上述代码段的输出如下:

The output for the above code snippet is as follows −

0

0

SymPy 有一个 RealNumber 类,充当 Float 的别名。SymPy 还将 Zero 和 One 定义为单例类,分别可以通过 S.Zero 和 S.One 访问,如下所示:

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

0

>>> S.One

输出如下 −

The output is as follows −

1

1

其他预定义单例数字对象包括 Half、NaN、Infinity 和 ImaginaryUnit。

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

nan

可以将 Infinity 表示为 oo 符号对象或 S.Infinity

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$

$\infty$

>>> S.Infinity

上述代码段的输出如下:

The output for the above code snippet is as follows −

$\infty$

$\infty$

可以将 ImaginaryUnit 数字导入为 I 符号,或者以 S.ImaginaryUnit 为索引符号进行访问,它表示 -1 的平方根

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

i

>>> S.ImaginaryUnit

上述片段的输出如下 −

The output of the above snippet is as follows −

i

i

>>> from sympy import sqrt
>>> i=sqrt(-1)
>>> i*i

执行上述代码片段时,你将获得以下输出 −

When you execute the above code snippet, you get the following output −

-1

-1