2024-03-19 06:58:00 发布
网友
我是个数学新手。现在我将深入研究Python数据类型。我不懂如何使用复数。请给我一些在Python中使用复数的例子。
下面的complex numbers示例应该是自解释的,包括末尾的错误消息
>>> x=complex(1,2) >>> print x (1+2j) >>> y=complex(3,4) >>> print y (3+4j) >>> z=x+y >>> print x (1+2j) >>> print z (4+6j) >>> z=x*y >>> print z (-5+10j) >>> z=x/y >>> print z (0.44+0.08j) >>> print x.conjugate() (1-2j) >>> print x.imag 2.0 >>> print x.real 1.0 >>> print x>y Traceback (most recent call last): File "<pyshell#149>", line 1, in <module> print x>y TypeError: no ordering relation is defined for complex numbers >>> print x==y False >>>
在python中,可以将“j”或“j”放在数字后面,使其成为虚构的,这样就可以轻松地编写复杂的文字:
>>> 1j 1j >>> 1J 1j >>> 1j * 1j (-1+0j)
“j”后缀来自电气工程,其中变量“i”通常用于电流。(Reasoning found here.)
The type of a complex number is ^{},如果您愿意,可以将该类型用作构造函数:
>>> complex(2,3) (2+3j)
复数有一些内置访问器:
>>> z = 2+3j >>> z.real 2.0 >>> z.imag 3.0 >>> z.conjugate() (2-3j)
几个内置函数支持复数:
>>> abs(3 + 4j) 5.0 >>> pow(3 + 4j, 2) (-7+24j)
The standard module ^{}有更多处理复数的函数:
>>> import cmath >>> cmath.sin(2 + 3j) (9.15449914691143-4.168906959966565j)
下面的complex numbers示例应该是自解释的,包括末尾的错误消息
在python中,可以将“j”或“j”放在数字后面,使其成为虚构的,这样就可以轻松地编写复杂的文字:
“j”后缀来自电气工程,其中变量“i”通常用于电流。(Reasoning found here.)
The type of a complex number is ^{} ,如果您愿意,可以将该类型用作构造函数:
复数有一些内置访问器:
几个内置函数支持复数:
The standard module ^{} 有更多处理复数的函数:
相关问题 更多 >
编程相关推荐