【问题标题】:Python - Inversing complex arraysPython - 反转复杂数组
【发布时间】:2021-08-11 17:22:52
【问题描述】:

我有一些具有复数值的方阵,想求逆。

如果我尝试:

(my_matrix)**(-1)

我明白了:

ZeroDivisionError: 0.0 to a negative or complex power

如果我尝试:

import numpy as np
np.linalg.inv(my_matrix)

我明白了:

TypeError: No loop matching the specified signature and casting was found for ufunc inv

如何反转复杂的数组?

【问题讨论】:

  • 你能出示minimal reproducible example吗?
  • 您不能简单地使用 python 来处理矩阵。你可以使用熊猫。 stackoverflow.com/questions/40858835/…
  • 告诉我们更多关于这个数组的信息。 shapedtype 和一些值(如果不是太大,则全部)
  • x**-1 是元素明智的划分,除非xnp.matrix
  • 显然my_matrix不是numpy的arraymatrix,或者如果是,它的数据类型不是数字类型。 type(my_matrix) 是什么?如果是numpy数组或者矩阵,my_matrix.dtype是什么?

标签: python numpy matrix numpy-ndarray matrix-inverse


【解决方案1】:

根据讨论中发布的示例 (https://chat.stackoverflow.com/rooms/232746/discussion-between-boetis-and-wiktor-kujawa),我能够重新创建一个 (4,4) complex_dtype 数组(需要进行一些繁琐的编辑):

In [501]: np.array(my_matrix)
Out[501]: 
array([[-4.90995593-0.0388j    , -0.        -0.j        ,
        -1.21084206-0.9213362j , -0.05935173-0.04516105j],
       [-0.        -0.j        , -4.90995593-0.0388j    ,
        -0.05935173-0.04516105j, -1.21084206-0.9213362j ],
       [-1.21084206+0.9213362j , -0.05935173+0.04516105j,
        -4.90995593-0.0388j    , -0.        -0.j        ],
       [-0.05935173+0.04516105j, -1.21084206+0.9213362j ,
        -0.        -0.j        , -4.90995593-0.0388j    ]])
In [502]: x=_
In [503]: np.linalg.inv(x)
Out[503]: 
array([[-0.22536285+2.16108358e-03j, -0.00234657+6.35524029e-05j,
         0.05633771+4.13312304e-02j,  0.003354  +2.44488004e-03j],
       [-0.00234657+6.35524029e-05j, -0.22536285+2.16108358e-03j,
         0.003354  +2.44488004e-03j,  0.05633771+4.13312304e-02j],
       [ 0.0548569 -4.32773491e-02j,  0.00325069-2.58066414e-03j,
        -0.22536285+2.16108358e-03j, -0.00234657+6.35524029e-05j],
       [ 0.00325069-2.58066414e-03j,  0.0548569 -4.32773491e-02j,
        -0.00234657+6.35524029e-05j, -0.22536285+2.16108358e-03j]])

scipy.linalg.inv 做同样的事情。

In [505]: x.dtype
Out[505]: dtype('complex128')
In [506]: x.shape
Out[506]: (4, 4)

对象数据类型:

产生你的错误:

In [512]: np.linalg.inv(x.astype(object))
Traceback (most recent call last):
  File "<ipython-input-512-615f0ffc3570>", line 1, in <module>
    np.linalg.inv(x.astype(object))
  File "<__array_function__ internals>", line 5, in inv
  File "/usr/local/lib/python3.8/dist-packages/numpy/linalg/linalg.py", line 545, in inv
    ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
TypeError: No loop matching the specified signature and casting was found for ufunc inv

In [513]: x.astype(object)**-1
Traceback (most recent call last):
  File "<ipython-input-513-65fe9688f840>", line 1, in <module>
    x.astype(object)**-1
ZeroDivisionError: 0.0 to a negative or complex power

【讨论】:

    【解决方案2】:

    您不能严格使用 python 来处理矩阵和分析数组。我建议使用 Pandas 或 Numpy,它们是易于使用的库,能够解决您的问题。

    【讨论】:

    • Thought Numpy 工作,我用过它,它对我有用。我认为如果您使用 numpy 进行操作,您需要使用其他库
    • 所以你用 COMPLEX 值反转了数组?
    • 半复杂,它会工作。我使用 Jupyter Notebook IDE
    • 我也用jupyter
    猜你喜欢
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2018-04-14
    • 2018-02-09
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多