def test_permutation_raises(self):
   perm = constant_op.constant(0, dtype=dtypes.int32)
   with self.assertRaisesRegex(ValueError, "must have at least 1 dimension"):
     permutation.LinearOperatorPermutation(perm)
   perm = [0., 1., 2.]
   with self.assertRaisesRegex(TypeError, "must be integer dtype"):
     permutation.LinearOperatorPermutation(perm)
   perm = [-1, 2, 3]
   with self.assertRaisesRegex(ValueError,
                               "must be a vector of unique integers"):
     permutation.LinearOperatorPermutation(perm)
 def test_to_dense_4x4(self):
     perm = [0, 1, 2, 3]
     self.assertAllClose(
         permutation.LinearOperatorPermutation(perm).to_dense(),
         linalg_ops.eye(4))
     perm = [1, 0, 3, 2]
     self.assertAllClose(
         permutation.LinearOperatorPermutation(perm).to_dense(),
         [[0., 1, 0, 0], [1., 0, 0, 0], [0., 0, 0, 1], [0., 0, 1, 0]])
     perm = [3, 2, 0, 1]
     self.assertAllClose(
         permutation.LinearOperatorPermutation(perm).to_dense(),
         [[0., 0, 0, 1], [0., 0, 1, 0], [1., 0, 0, 0], [0., 1, 0, 0]])
    def operator_and_matrix(self,
                            build_info,
                            dtype,
                            use_placeholder,
                            ensure_self_adjoint_and_pd=False):
        shape = list(build_info.shape)
        perm = math_ops.range(0, shape[-1])
        perm = array_ops.broadcast_to(perm, shape[:-1])
        perm = random_ops.random_shuffle(perm)

        if use_placeholder:
            perm = array_ops.placeholder_with_default(perm, shape=None)

        operator = permutation.LinearOperatorPermutation(perm, dtype=dtype)
        matrix = math_ops.cast(
            math_ops.equal(math_ops.range(0, shape[-1]),
                           perm[..., array_ops.newaxis]), dtype)
        return operator, matrix