@classmethod
def identity(cls, dim):
    """Returns a dim x dim identity matrix."""
    if dim < 1:
        raise ValueError(
            'Identity matrix must have positive size, %d not allowed.' % dim)
    return cls._matrix_alloc_identity(dim)
