Nabeel Eh
Nabeel Eh

Reputation: 35

Matrix inverse in numpy/python not giving correct matrix?

I have a nxn matrix C and use inv from numpy.linalg to take the inverse to get Cinverse. My Cmatrix has elements of order 10**4 but my Cinverse matrix has elements of order 10**12 and higher (not sure if thats correct). When I do numpyp.dot(C,Cinverse), I do not get the identity matrix. Why is this?

I have a vector x which I multiply by itself to get a matrix.

x=array([ 121.41191662,   74.22830468,   73.23156336,   75.48354975,
     79.89580817])
c=np.outer(xvector,xvector)

this is a 5x5 matrix.

then I get its inverse by

from numpy.linalg import inv
cinverse=inv(c)

then I want to see if I can get identity matrix back.

identity=np.dot(C00,C00inv)

However, I do not get the identity matrix. cinverse has very large matrix elements around 10**13 and higher while c has matrix elements around 10,000.

Upvotes: 0

Views: 4906

Answers (2)

Paul Panzer
Paul Panzer

Reputation: 53029

The outer product of two vectors (be they the same or not) is not invertible. Since it is just a stack of scaled copies of the same vector its rank is one. Rank defective matrices cannot be inverted.

I'm surprised that numpy is not raising an exception or at least giving a warning.

Upvotes: 1

RexFuzzle
RexFuzzle

Reputation: 1432

So here is some code that generates the inverse matrix, and I will comment about it afterwards.

import numpy as np
x = np.random.rand(5,5)*10000 # makes a 5x5 matrix with elements around 10000
xin = np.linalg.inv(x)
iden = np.dot(x,xinv)

Now the first line of your iden matrix probably looks something like this: [ 1.00000000e+00, -2.05382445e-16, -5.61067365e-16, 1.99719718e-15, -2.12322957e-16] . Notice that the first element is exactly 1, as it should be, but there others are not exactly 0, however they are essentially zero and should be regarded as zero according to machine precision.

Upvotes: 0

Related Questions