def map_affine(x, l0, h0, l1=0., h1=1.):
    """Affine map from interval [l0, h0] to [l1, h1].
    Broadcasting is used to match corresponding dimensions.
    """
    x = l1 + (x - l0) / (h0 - l0) * (h1 - l1)
    return x