0

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))
3
  • a*ainverse is not matrix cross product. For that, you should call numpy.cross Commented Dec 13, 2020 at 21:05
  • @zvone I edited the question and used a.dot() method. Commented Dec 13, 2020 at 21:14
  • 2
    You're dealing with floating point. You've got something extraordinarily close to the identity matrix. Commented Dec 13, 2020 at 21:35

1 Answer 1

3

Is there any way not to use float data type and not to encounter with this problem?

Yes and no.

When you take the inverse of an array, you give rise to essentially arbitrary fractions. The example in your question has −4/10 and 6/10, and these are not representable in a binary-based floating-point (in which every representable number is a power of two multiplied by some integer, such as 3•2−5 = 3/32). They would be representable in a decimal-based floating-point, but an array inverse could easily have values such as 5/7 or 1234/9729102223. So no floating-point format will be able to represent arbitrary array inverses.

Rational arithmetic (fractions) can represent inverses of arrays of integers or other rational numbers. But it has a number of drawbacks, such as requiring support for integers of unlimited size to accommodate whatever denominators arise and being unable to represent the results of functions like square root or sine. (I mention rational arithmetic as a possible mathematical solution; I am unfamiliar with what Python features or packages might be available to support it.)

There are other options, such as symbolic mathematical software, but they are likely overkill for the problem you are working on and may be more burdensome than rational arithmetic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.