def migrSteppingStoneRates(r, n, circular=False):
    """migration rate matrix for circular stepping stone model (X=1-m)

::

           X   m/2               m/2
           m/2 X   m/2           0
           0   m/2 x   m/2 ......0
           ...
           m/2 0 ....       m/2  X

or non-circular

::

           X   m/2               m/2
           m/2 X   m/2           0
           0   m/2 X   m/2 ......0
           ...
           ...              m   X

    This function returns [[1]] when there is only one subpopulation.
    """
    if n < 2:
        return [[1]]
    elif n == 2:
        return [[1 - r, r], [r, 1 - r]]
    m = []
    for i in range(0, n):
        m.append([0] * n)
        m[i][i] = 1 - r
        m[i][(i + 1) % n] = r / 2.0
        m[i][(i + n - 1) % n] = r / 2.0
    if not circular:
        m[0][1] = r
        m[0][-1] = 0
        m[n - 1][0] = 0
        m[n - 1][n - 2] = r
    return m
