def choose(aliases, exprs, mapper, selector):
    '\n    Analyze the detected aliases and, after applying a cost model to rule out\n    the aliases with a bad flops/memory trade-off, inject them into the original\n    expressions.\n    '
    tot = 0
    retained = AliasMapper()
    candidates = OrderedDict()
    aliaseds = []
    others = []
    for (e, v) in aliases.items():
        score = selector(e, len(v.aliaseds))
        if (score > 0):
            candidates[e] = score
            aliaseds.extend(v.aliaseds)
        else:
            others.append(e)
    if (not candidates):
        return (exprs, retained, tot)
    mapper = {k: v for (k, v) in mapper.items() if (v.free_symbols & set(aliaseds))}
    templated = [uxreplace(e, mapper) for e in exprs]
    owset = wset((others + templated))
    for (e, v) in aliases.items():
        try:
            score = candidates[e]
        except KeyError:
            score = 0
        if ((score > 1) or ((score == 1) and (max(len(wset(e)), 1) > len((wset(e) & owset))))):
            retained[e] = v
            tot += score
    if (not retained):
        return (exprs, retained, tot)
    mapper = {k: v for (k, v) in mapper.items() if (v.free_symbols & set(retained.aliaseds))}
    exprs = [uxreplace(e, mapper) for e in exprs]
    return (exprs, retained, tot)
Analyze the detected aliases and, after applying a cost model to rule out
the aliases with a bad flops/memory trade-off, inject them into the original
expressions.
def choose(aliases, exprs, mapper, selector):
    '\n    Analyze the detected aliases and, after applying a cost model to rule out\n    the aliases with a bad flops/memory trade-off, inject them into the original\n    expressions.\n    '
    tot = 0
    retained = AliasMapper()
    candidates = OrderedDict()
    aliaseds = []
    others = []
    for (e, v) in aliases.items():
        score = selector(e, len(v.aliaseds))
        if (score > 0):
            candidates[e] = score
            aliaseds.extend(v.aliaseds)
        else:
            others.append(e)
    if (not candidates):
        return (exprs, retained, tot)
    mapper = {k: v for (k, v) in mapper.items() if (v.free_symbols & set(aliaseds))}
    templated = [uxreplace(e, mapper) for e in exprs]
    owset = wset((others + templated))
    for (e, v) in aliases.items():
        try:
            score = candidates[e]
        except KeyError:
            score = 0
        if ((score > 1) or ((score == 1) and (max(len(wset(e)), 1) > len((wset(e) & owset))))):
            retained[e] = v
            tot += score
    if (not retained):
        return (exprs, retained, tot)
    mapper = {k: v for (k, v) in mapper.items() if (v.free_symbols & set(retained.aliaseds))}
    exprs = [uxreplace(e, mapper) for e in exprs]
    return (exprs, retained, tot)