-2

I have some square matrix with complex values and want to inverse it.

If I try:

(my_matrix)**(-1)

I get:

ZeroDivisionError: 0.0 to a negative or complex power

If I try:

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

I get:

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

How to invert complex Array?

Wiktor Kujawa
  • 595
  • 4
  • 21

2 Answers2

1

You can't use strictly python to work on matrixes and analyze arrays. I would recommend using Pandas or Numpy, which are easy-to-use libraries that will be able to solve your problem.

jahantaila
  • 840
  • 5
  • 26
0

From the sample posted in the discussion (https://chat.stackoverflow.com/rooms/232746/discussion-between-boetis-and-wiktor-kujawa) I was able to recreate a (4,4) complex_dtype array (it took some tedious editing):

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]])

The scipy.linalg.inv does the same thing.

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

with object dtype:

Producing your errors:

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
hpaulj
  • 221,503
  • 14
  • 230
  • 353