【问题标题】:can't convert complex to float in python无法在python中将复杂转换为浮点数
【发布时间】:2022-01-02 21:43:37
【问题描述】:

这是我的代码的一部分:

import math
import numpy as np

M_Matrix = []
for i in range(0, layer + 1):
    # creat phase retardation tilda matrix
    M = [[math.exp(-1j * phase_retardation_tilda[i]), 0],
         [0, math.exp(1j * phase_retardation_tilda[i])]]
    N = (1 / (2 * n_tilda[i+1] * a_mn[i])) * [[n_tilda[i+1] + n_tilda[i], n_tilda[i+1] - n_tilda[i]],
                                              [n_tilda[i+1] - n_tilda[i], n_tilda[i+1] + n_tilda[i]]]
    matrix = np.dot(M, N)
    M_Matrix.append(matrix)

我收到此错误:

   M = [[math.exp(-1j * phase_retardation_tilda[i]), 0],
   TypeError: can't convert complex to float

我该怎么办?

【问题讨论】:

  • 如果你在做复杂的数学,我建议你使用 numpy 而不是 math。

标签: python python-3.x matrix matrix-multiplication multiplication


【解决方案1】:

来自documentationmath 模块不能用于复数;如果您需要对复数的支持,请使用 cmath 模块中的同名函数。在您的情况下,您可以使用 cmath.exp() 而不是 math.exp()

【讨论】:

  • 谢谢,成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-20
  • 2013-08-19
  • 2015-04-06
  • 1970-01-01
  • 2020-04-17
  • 2016-05-29
  • 1970-01-01
相关资源
最近更新 更多