r'''Execute an indexed sum over a term that does not depend on the
    summation indices

    .. math::

        \sum_{j=1}^{N} a = N a

    >>> a = symbols('a')
    >>> i, j  = (IdxSym(s) for s in ('i', 'j'))
    >>> unicode(Sum(i, 1, 2)(a))
    '2 a'
    >>> unicode(Sum(j, 1, 2)(Sum(i, 1, 2)(a * i)))
    '∑_{i=1}^{2} 2 i a'

def indexed_sum_over_const(cls, ops, kwargs):
    r'''Execute an indexed sum over a term that does not depend on the
    summation indices

    .. math::

        \sum_{j=1}^{N} a = N a

    >>> a = symbols('a')
    >>> i, j  = (IdxSym(s) for s in ('i', 'j'))
    >>> unicode(Sum(i, 1, 2)(a))
    '2 a'
    >>> unicode(Sum(j, 1, 2)(Sum(i, 1, 2)(a * i)))
    '∑_{i=1}^{2} 2 i a'
    '''
    term, *ranges = ops
    new_ranges = []
    new_term = term
    for r in ranges:
        if r.index_symbol not in term.free_symbols:
            try:
                new_term *= len(r)
            except TypeError:
                new_ranges.append(r)
        else:
            new_ranges.append(r)
    if len(new_ranges) == 0:
        return new_term
    else:
        return (new_term, ) + tuple(new_ranges), kwargs