code: def test_constrained_parafac_hard_sparsity():
      """Test for the CANDECOMP-PARAFAC decomposition with ADMM normalized sparsity constraint"""
      rng = T.check_random_state(1234)
      rank = 3
      init = "random"
      weights_init, factors_init = initialize_constrained_parafac(
          T.zeros([6, 8, 4]), rank, hard_sparsity=[5, 5, 5], init=init
      )
      tensor = cp_to_tensor((weights_init, factors_init))
      for i in range(len(factors_init)):
          factors_init[i] += T.tensor(
              0.1 * rng.random_sample(T.shape(factors_init[i])),
              **T.context(factors_init[i]),
          )
      tensor_init = CPTensor((weights_init, factors_init))
      res, errors = constrained_parafac(
          tensor,
          hard_sparsity=[5, 5, 5],
          rank=rank,
          init=tensor_init,
          random_state=rng,
          return_errors=True,
          tol_outer=1 - 16,
      )
      # Check if factors are normalized and k-sparse
      for i in range(len(factors_init)):
          assert_(T.count_nonzero(res.factors[i]) == 5)
  
file_path: tensorly/tensorly/decomposition/tests/test_constrained_parafac.py
imports: import numpy
  from ...cp_tensor import cp_to_tensor, CPTensor
  from .._constrained_cp import constrained_parafac, initialize_constrained_parafac, ConstrainedCP
  from ...None import backend
  from ...testing import assert_, assert_array_almost_equal, assert_class_wrapper_correctly_passes_arguments
  from ...random import random_cp
label_desc: Use image node_id 11 for calling a global function with example usage: test_constrained_parafac_hard_sparsity() without return types