N.1.2c Codes output is correct but in codebook error is coming. Please help

def is_semi_positive(matrix):
    """Check whether a matrix is positive semidefinite.
    
    Args:
        matrix: (array(array[complex]))
    Returns:
        bool: True if the matrix is positive semidefinite, False otherwise
    """
 
    ##################
    # YOUR CODE HERE #
    ################## 
    eigenvalues = np.linalg.eigvals(matrix)
    return np.all(eigenvalues >= 0)
    
matrix_1 = [[3/4,1/4],[1/4,1/4]]
matrix_2 = [[0,1/4],[1/4,1/4]]

print("Is matrix [[3/4,1/4],[1/4,1/4]] positive semidefinite?")
print(is_semi_positive(matrix_1))
print("Is matrix [[0,1/4],[1/4,1/4]] positive semidefinite?")
print(is_semi_positive(matrix_2))

I am attaching the snippet of error messages
Error: the output of your function isn't the right type.
How ever if I run this code in Google colab I am getting output as

True
Is matrix [[0,1/4],[1/4,1/4]] positive semidefinite?
False```

Hey @SUDHIR_KUMAR_SAHOO , thanks for this, you did everything right, but ended up with an np.bool_ instead of bool type, and we didn’t take that into consideration in the evaluation. :wink:

We’ll update the Codebook to fix this, but in the meantime, you can just use this:

return bool(np.all(eigenvalues >= 0))

instead of your original return, and you’ll pass the codercise. :slight_smile:

1 Like