def _mod_OO_CC(t1, t2): """ Returns an iterator over Triples representing all values in t1 (OO) modulus all values in t2 (CC). ### list(_mod_OO_CC(T(None, None, 5, 0), T(35, 105, 7))) [(0, 98, 1)] ### list(_mod_OO_CC(T(None, None, 5, 0), T(35, 42, 7))) [(0, 35, 5)] ### list(_mod_OO_CC(T(None, None, 5, 3), T(35, 56, 7))) [(0, 49, 1)] """ gcf = rational.gcf(t1.skip, t2.skip) if (t2.stop - t2.start) < (t1.skip * t2.skip // gcf): # not enough for a full cycle, so do indiv values s = set() for n in t2: s.update(_modConstant_set(t1, n)) for obj in utilities.tripleIteratorFromIterable(s): yield obj else: # enough for a full cycle tTemp = next(_mod_OO_OO(t1, T(None, None, t2.skip, t2.phase))) newStop = (t2.stop - t2.skip - 1) + tTemp.skip yield T(tTemp.start, newStop, tTemp.skip)
def _mod_CC_CC(t1, t2): """ Returns an iterator over Triples representing all values in t1 (CC) modulus all values in t2 (CC). ### list(_mod_CC_CC(T(1, 11, 2), T(4, 16, 3))) [(0, 4, 2), (1, 11, 2)] """ return utilities.tripleIteratorFromIterable(_mod_CC_CC_gen(t1, t2))
def _powConstant_CC_pos_any(t, k): """ ### C(_powConstant_CC_pos_any(T(1, 11, 2), 1)).asList() [(1, 11, 2)] ### C(_powConstant_CC_pos_any(T(1, 11, 2), 2)).asList() [(1, 17, 8), (25, 33, 8), (49, 57, 8), (81, 89, 8)] """ for aTriple in utilities.tripleIteratorFromIterable(set(n ** k for n in t)): yield aTriple
def _modConstant(t, k): """ Returns an iterator over Triples representing the modulus of the specified Triple t by the specified constant k. ### list(_modConstant(T(None, None, 6, 5), 4)) [(1, 5, 2)] ### list(_modConstant(T(14, 29, 5), 7)) [(0, 2, 2), (3, 7, 2)] """ return utilities.tripleIteratorFromIterable(_modConstant_set(t, k))
def _and_CC_CC(t1, t2): """ Returns an iterator of the bitwise-AND of t1 (CC) and t2 (CC). >>> T = triple.Triple >>> list(_and_CC_CC(T(1, 11, 2), T(10, 15, 5))) [(0, 1, 1), (2, 14, 6)] """ if len(t1) < len(t2): s = set(a & b for a in t1 for b in t2) else: s = set(a & b for a in t2 for b in t1) return utilities.tripleIteratorFromIterable(s)
def _powConstant_OC_neg_odd(t, k): """ ### C(_powConstant_OC_neg_odd(T(None, -100, 7), -3)).asList() [(-1, 0, 1)] ### C(_powConstant_OC_neg_odd(T(None, 6, 5), -3)).asList() [(-1, 3, 2)] ### C(_powConstant_OC_neg_odd(T(None, 100, 7), -3)).asList() [(-1, 1, 1)] ### C(_powConstant_OC_neg_odd(T(None, 11, 5), -3)).asList() [(-1, 2, 1)] """ s = set([-1]) if (t.stop - t.skip) > 1: s.add(0) if 1 in t: s.add(1) return utilities.tripleIteratorFromIterable(s)
def _powConstant_CC_neg_odd(t, k): """ ### C(_powConstant_CC_neg_odd(T(-100, -10, 5), -3)).asList() [(-1, 0, 1)] ### C(_powConstant_CC_neg_odd(T(10, 100, 5), -3)).asList() [(0, 1, 1)] ### C(_powConstant_CC_neg_odd(T(1, 11, 2), -3)).asList() [(0, 2, 1)] ### C(_powConstant_CC_neg_odd(T(-100, 100, 1), -1)).asList() [(-1, 2, 1)] """ s = set() if t.start < 0: s.add(-1) if 1 in t: s.add(1) if (t.stop - t.skip) > 1: s.add(0) return utilities.tripleIteratorFromIterable(s)
def _andConstant(t, k): """ Returns an iterator of Triples with the results of a constant logical-ANDed with t. >>> T = triple.Triple >>> list(_andConstant(T(1, 100, 1), 0)) [(0, 1, 1)] >>> list(_andConstant(T(0, None, 12), 4)) [(0, 8, 4)] >>> list(_andConstant(T(14, 770, 7), 5)) [(1, 7, 3), (0, 10, 5)] >>> list(_andConstant(T(None, None, 7, 2), -3)) [(*, *, 28, phase=0), (*, *, 28, phase=9), (*, *, 28, phase=16), (*, *, 28, phase=21)] >>> list(_andConstant(T(None, 303, 7), -3)) [(*, 308, 28), (*, 317, 28), (*, 324, 28), (*, 301, 28)] >>> list(_andConstant(T(51, None, 7), -3)) [(56, *, 28), (65, *, 28), (72, *, 28), (49, *, 28)] >>> list(_andConstant(T(16, 37, 7), -3)) [(28, 56, 28), (16, 44, 28), (21, 49, 28)] """ Triple = triple.Triple if k == 0: yield Triple(0, 1, 1) elif k == -1: yield t elif k < 0: # because binlist only works with non-negative numbers, create an # equivalent positive value for k which will result in the same output bl = binlist(-k-1) pow2 = 2 ** len(bl) cycle = pow2 // rational.gcf(pow2, t.skip) bigCycle = cycle * t.skip if t.start is None and t.stop is None: for start in range(t.phase, t.phase + bigCycle, t.skip): yield Triple(None, None, bigCycle, start & k) elif t.start is None: last = t.stop - t.skip d = dict((n % bigCycle, n) for n in range(last, last - bigCycle, -t.skip)) for start in range(t.phase, t.phase + bigCycle, t.skip): yield Triple(None, (d[start] & k) + bigCycle, bigCycle, start & k) elif t.stop is None: d = dict((n % bigCycle, n) for n in range(t.start, t.start + bigCycle, t.skip)) for start in range(t.phase, t.phase + bigCycle, t.skip): yield Triple(d[start] & k, None, bigCycle, start & k) else: last = t.stop - t.skip dStarts = {} for n in range(t.start, t.start + bigCycle, t.skip): if n > last: break dStarts[n % bigCycle] = n & k dStops = {} for n in range(last, last - bigCycle, -t.skip): if n < t.start: break dStops[n % bigCycle] = (n & k) + bigCycle for start in range(t.phase, t.phase + bigCycle, t.skip): if start in dStarts: actualStart = dStarts[start] stop = dStops.get(start, actualStart + bigCycle) yield Triple(actualStart, stop, bigCycle) else: bl = binlist(k) pow2 = 2 ** len(bl) cycle = pow2 // rational.gcf(pow2, t.skip) if t.start is None: if t.stop is None: start = t.phase stop = start + cycle * t.skip else: stop = t.stop start = stop - cycle * t.skip elif t.stop is None: start = t.start stop = start + cycle * t.skip else: start = t.start stop = min(t.stop, start + cycle * t.skip) s = set(n & k for n in range(start, stop, t.skip)) for t in utilities.tripleIteratorFromIterable(s): yield t