简体   繁体   中英

Python: Why do I get error message when I try to calculate the inverse of my 2x2-matrix (Hessian)?

My Hessian (which is a 2x2-matrix) looks like the following:

Hessian1


[[array([[ -400451.22705586, -1472873.29657509, -1353698.36178183],
         [-1472873.29657509, -5425857.74291764, -4978945.85451078],
         [-1353698.36178183, -4978945.85451078, -4591731.95233015]]),
  array([[-2.51920250e-07],
         [-9.37914803e-07],
         [-4.97061494e-07]])],
 [array([[-2.51920250e-07, -9.37914803e-07, -4.97061494e-07]]),
  array([[-1600445.78266049]])]]

That is, it is a 2x2 matrix with 3x3 matrix as its first element (1,1), a 3x1-matrix as its second element (1,2) and so on.

Now I want to take the inverse of this matrix.

np.linalg.inv(Hessian1)

But I get the following error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-d820d7b5b57d> in <module>
----> 1 np.linalg.inv(Hessian1)

<__array_function__ internals> in inv(*args, **kwargs)

~\anaconda3\lib\site-packages\numpy\linalg\linalg.py in inv(a)
    545     signature = 'D->D' if isComplexType(t) else 'd->d'
    546     extobj = get_linalg_error_extobj(_raise_linalgerror_singular)
--> 547     ainv = _umath_linalg.inv(a, signature=signature, extobj=extobj)
    548     return wrap(ainv.astype(result_t, copy=False))
    549 

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

I don't understand the message because I'm not doing a loop. Can somebody help me to get the inverse, please?

Here are the first line of the documentation of np.linalg.inv

Docstring:
Compute the (multiplicative) inverse of a matrix.

Given a square matrix `a`, return the matrix `ainv` satisfying
``dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])``

By square it does mean that each element of your matrix should be same type (float8,16,32,etc..). You do not have a 2 by 2 matrix but in fact a 4 by 4 matrix. Here is a drawing of the matrix:

x  x   x   y
x  x   x   y
x  x   x   y
r  r   r   s

x is the 3x3 matrix, y 3x1, r 1x3 and s 1x1 that was stored in Hessian1.

This is how you can map hessian1 into a matrix you can manipulate:

H = np.vstack([
np.hstack([Hessian1[0][0],Hessian1[0][1]]),
np.hstack([Hessian1[0][1].T,Hessian1[1][1]])
])

You can apply np.linalg.inv(H) and find:

array([[-2.31020535e-03,  4.28778776e-04,  2.16139552e-04,
         4.52342092e-17],
       [ 4.28778776e-04, -1.16557869e-04, -2.21714591e-08,
         8.21218296e-19],
       [ 2.16139552e-04, -2.21714591e-08, -6.39143074e-05,
        -1.41584265e-17],
       [ 4.52342092e-17,  8.21218296e-19, -1.41584265e-17,
        -6.24825915e-07]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM