import math


def n_vector(lat, lon):
  """Converts lat/long to n-vector 3D Cartesian representation."""
  # Convert to radians.
  if not (-90.0 <= lat <= 90):
    raise ValueError("lat={:2.2f}, but must be in [-90,+90]".format(lat))
  rad_lat = math.radians(lat)
  if not (-180.0 <= lon <= 180):
    raise ValueError("lon={:2.2f}, but must be in [-180,+180]".format(lon))
  rad_lon = math.radians(lon)

  x = math.cos(rad_lat) * math.cos(rad_lon)
  y = math.cos(rad_lat) * math.sin(rad_lon)
  z = math.sin(rad_lat)
  return x, y, z