def constrain(x, x_max, x_min=None):
  """
  Constrans a given number between a min and max value.

  Parameters
  ----------
  x : scalar
    The number to constrain.
  x_max : scalar
    The upper bound for the constrain.
  x_min : scalar, default=None
    The lower bound for the constrain. If None, defaults to -`x_max`.

  Returns
  -------
  scalar
    The constrained number. 
  """
 
  if x_min is None:
    x_min = -x_max 
  return min(max(x, x_min), x_max)