I wrote a code in Python to find the inverse of an array. I am sure that the code is true but at the end, the multiplication of the array and its inverse gives me an array different from identity as following (a is the array and a*ainverse is the result of multiplication of a and its inverse).
a=np.array([[2,3],
[3,2]],
dtype=float)
a.dot(rowequi(a))=[[ 1.00000000e+00 -2.22044605e-16]
[-2.22044605e-16 1.00000000e+00]]
I guess the problem is with the data type (dtype=float) that I chose for the entries but I don't know how to fix it. Is there any way not to use float data type and not to encounter with this problem?
The following is the code:
import numpy as np
a=np.array([[2,3],
[3,2]],
dtype=float)
def rowequi(a):
counter=[]
n=len(a[0])
b=np.empty((n,n+n),dtype=float)
identity=np.empty((n,n),dtype=float)
for i in range(n):
for j in range(n):
if i==j:
identity[i,j]=1
else:
identity[i,j]=0
for k in range(n):
f=list(a[k])
for t in range(n):
f.append(identity[k,t])
b[k]=np.array(f)
for j in range(n):
for i in range(n):
if b[j,i]!=0:
b[j]=(1/b[j][i])*b[j]
counter.append((j,i))
for t in range(n):
if t!=j:
b[t]=b[t]-b[t][i]*b[j]
break
return b
c=np.array([[-0.4, 0.6],
[0.6, -0.4]])
print(c.dot(a))
a*ainverseis not matrix cross product. For that, you should callnumpy.cross