N.1.2 Not able to return a Boolean

In this question we need to return a Boolean value how to return a Boolean value in this code

def is_hermitian(matrix):
    """Check whether a matrix is hermitian.
    
    Args:
        matrix: (array(array[complex]))
    Returns:
        bool: True if the matrix is Hermitian, False otherwise
    """
 
    ##################
    # YOUR CODE HERE #
    ################## 
    if matrix == matrix.conj().T:
        B = 1
    return B# Return the boolean value

matrix_1 = np.array([[1,1j],[-1j,1]])
matrix_2 = np.array([[1,2],[3,4]])

print("Is matrix [[1,1j],[-1j,1]] Hermitian?")
print(is_hermitian(matrix_1))
print("Is matrix [[1,2],[3,4]] Hermitian?")
print(is_hermitian(matrix_2))

I have to return B as 1 so I am not getting wrong

Hi @SUDHIR_KUMAR_SAHOO , let’s take a look at the error message you get in the N.1.2a codercise when you run this code. :slight_smile:

Error: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

You could use that as a hint to figure out how you can compare these two arrays. Or you could use something like np.allclose, for example.
When figuring out a coding problem, it’s always a good idea to first read the output and try to understand what it’s telling you. :slight_smile: I hope this helps!

1 Like

Thanks I will do that

1 Like