2

This program tests if a matrix is an identity matrix or not.

I have pasted my code beneath, and would like to know ways in which I can optimize the efficiency of this code. Also I am new to python programming, are there some built in functions that can solve the purpose too?

    def is_identity_matrix(test):
    if (test == []):
        return False
    i = 0
    while (i < len(test)):
        if (len(test[i]) == len(test)):
            j = 0
            while(j < len(test[i])):
                if (j != i):
                    if(test[i][j] != 0):
                        return False
                else:
                    if(test[i][j] != 1):
                        return False
                if(j == (len(test[i]) - 1)):
                    break
                j += 1
            if(i == (len(test) - 1)):
                break
            i += 1
        else:
            return False
    if(i == j and i == (len(test) - 1)):
        return True

# Test Cases:

matrix1 = [[1,0,0,0],
           [0,1,0,0],
           [0,0,1,0],
           [0,0,0,1]]
print is_identity_matrix(matrix1)
#>>>True

matrix2 = [[1,0,0],
           [0,1,0],
           [0,0,0]]

print is_identity_matrix(matrix2)
#>>>False

matrix3 = [[2,0,0],
           [0,2,0],
           [0,0,2]]

print is_identity_matrix(matrix3)
#>>>False

matrix4 = [[1,0,0,0],
           [0,1,1,0],
           [0,0,0,1]]

print is_identity_matrix(matrix4)
#>>>False

matrix5 = [[1,0,0,0,0,0,0,0,0]]

print is_identity_matrix(matrix5)
#>>>False

matrix6 = [[1,0,0,0],  
           [0,1,0,2],  
           [0,0,1,0],  
           [0,0,0,1]]

print is_identity_matrix(matrix6)
#>>>False
5
  • 8
    One word: numpy. Commented May 17, 2013 at 16:49
  • 1
    or two, numpy and identity Commented May 17, 2013 at 16:50
  • 3
    Do you realy need to work with matrices? If you do, should use numpy. If you are just coding to learn: import this Commented May 17, 2013 at 16:53
  • 1
    Sounds like a homework-type question to me. Commented May 17, 2013 at 16:54
  • @Aya it is :P But I wasn't satisfied with my code... Commented May 17, 2013 at 16:57

6 Answers 6

13
def is_identity_matrix(listoflist):
    return all(val == (x == y) 
        for y, row in enumerate(listoflist)  
            for x, val in enumerate(row))

(though, this does not check if the matrix is square, and it returns True for an empty list)

Explanation: Inside all we have a generator expression with nested loops where val loops over each value in the matrix. x == y evaluates to True on the diagonal and False elsewhere. In Python, True == 1 and False == 0, so you can compare val == (x == y). The parentheses are important: val == x == y would be a chained comparison equivalent to val == x and x == y

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

6 Comments

I think I needed to think through this one for about a full minute before deciding that it seems like it would work. Ultimately, I think this deserves kudos (and an upvote) for being able to pull off a not-too-ugly 1-liner in pure python with short-circuiting behavior.
I think val == 1 for ... for ... if x == y would be clearer. I sometimes use booleans as integers too, but generally it's not very obvious.
@Janne Karila: Hey can you help me understand what this code is doing? I would really really appreciate. This code is awesome- its clean :D
@delnan: but you also have to verify that val == 0 where x != y! So in the end I'm not sure Janne's answer can really be improved.
@icecrime D'oh! You're right. I think val == 1 if x == y else val == 0 is still competitive, but not obviously better.
|
6

I'd use numpy:

(np.array(matrix1) == np.identity(len(matrix1))).all()

Of course, it'd be better if you were storing matrix1 as a numpy array in the first place to avoid the conversion.

3 Comments

Hey Thank you! +1 I didn't know of numpy :)
@AbrarKhan -- It's not in the standard library, but it's a pretty common 3rd party extension because it is extremely useful.
Put dtype=np.bool in the call to np.identity for roughly twice the speed! (If the matrix is already a numpy array)
1

Multiply by the all ones vector and check that the result is the same

1 Comment

There are many non-identity matrices that will pass this test.
1

Check the size of your matrix, make sure it is n x n, then create an actual identity matrix using np.identity(n), then compare your matrix with the new one you created.

Comments

1
def is_identity_matrix(test):
    if not test : return False
    le = len(test[0])
    for i,x in enumerate(test):
        if len(x) == le:
            if any(y!=1 if j==i else y!=0 for j,y in enumerate(x)):
                return False
        else:
            return False
    return True if len(test) == le else False

1 Comment

I don't understand your first line -- if len(test)%2: return False. Why does the number of rows have to be odd?
0

If speed is important you should probably look at Numpy, but in a situation where you can't use it or perhaps it isn't worth it to have the matrix as a Numpy array you could also use the following:

def is_identity(mat):
    for i, row in enumerate(mat):
        if any(row[:i]) or row[i]!=1 or any(row[i+1:]):
            return False
    return True

About 12x faster than the code of the currently accepted answer! For a matrix of 2000x2000 at least...

The above does not check the matrix dimensions, but you can easily add something like:

n = len(matrix)
if not all(len(row) == n for row in matrix):
    return False

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.