def arr2text(arr):
  """ format a numpy array into a text string """
  text = ''
  if len(arr.shape) == 1:  # vector
    text = " ".join(arr.astype(str))
  elif len(arr.shape) == 2:  # matrix
    mat  = [arr2text(line) for line in arr]
    text = "\n" + "\n".join(mat) + "\n"
  else:
    raise RuntimeError('arr2text can only convert vector or matrix.')
  return text