Example #1
0
    def eliminate_error(n, s):
        """Eliminate error term of given asymptotic order n.

        The stream s must be based on halving h at each step
        for the formula used here to work."""
        while True:
            a, b, s = unpack(2, s, k=1)
            yield (b * 2**n - a) / (2**(n - 1))
Example #2
0
 def len_gt(k, s):
     # see also unpythonic.slicing.islice; could implement as  return islice(s)[k + 1]
     a, _ = unpack(1, drop(k, s))
     return a  # None if no item
Example #3
0
 def euler_transform(s):
     while True:
         a, b, c, s = unpack(3, s, k=1)
         yield c - ((c - b)**2 / (a - 2 * b + c))
Example #4
0
 def order(s):
     """Estimate asymptotic order of s, consuming the first three terms."""
     a, b, c, _ = unpack(3, s)
     return round(log2(abs((a - c) / (b - c)) - 1))
Example #5
0
 def within(eps, s):  # in real code, prefer unpythonic.it.within
     while True:
         # unpack with peek (but be careful, the rewinded tail is a tee'd copy)
         a, b, s = unpack(2, s, k=1)
         if abs(a - b) < eps:
             return b
Example #6
0
 def len_gt(k, s):
     a, _ = unpack(1, drop(k, s))
     return a  # None if no item
Example #7
0
 def within(eps, s):
     while True:
         # unpack with peek (but be careful, the rewinded tail is a tee'd copy)
         a, b, s = unpack(2, s, k=1)
         if abs(a - b) < eps:
             return b