sync to Atlas repo
This commit is contained in:
48
iterables/aritprog_v1.py
Normal file
48
iterables/aritprog_v1.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end
|
||||
self._build()
|
||||
|
||||
def _build(self):
|
||||
self._numbers = array.array('d')
|
||||
n = self.begin
|
||||
while n < self.end:
|
||||
self._numbers.append(n)
|
||||
n += self.step
|
||||
|
||||
def __iter__(self):
|
||||
return ArithmeticProgressionIterator(self._numbers)
|
||||
|
||||
|
||||
class ArithmeticProgressionIterator(abc.Iterator):
|
||||
|
||||
def __init__(self, series):
|
||||
self._series = series
|
||||
self._index = 0
|
||||
|
||||
def __next__(self):
|
||||
if self._index < len(self._series):
|
||||
item = self._series[self._index]
|
||||
self._index += 1
|
||||
return item
|
||||
else:
|
||||
raise StopIteration
|
||||
|
||||
|
||||
|
||||
37
iterables/aritprog_v1b.py
Normal file
37
iterables/aritprog_v1b.py
Normal file
@@ -0,0 +1,37 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end
|
||||
|
||||
def __iter__(self):
|
||||
return ArithmeticProgressionIterator(self)
|
||||
|
||||
|
||||
class ArithmeticProgressionIterator(abc.Iterator):
|
||||
|
||||
def __init__(self, arithmetic_progression):
|
||||
self._ap = arithmetic_progression
|
||||
self._index = 0
|
||||
|
||||
def __next__(self):
|
||||
result = self._ap.begin + self._ap.step * self._index
|
||||
if result < self._ap.end:
|
||||
self._index += 1
|
||||
return result
|
||||
else:
|
||||
raise StopIteration
|
||||
32
iterables/aritprog_v2.py
Normal file
32
iterables/aritprog_v2.py
Normal file
@@ -0,0 +1,32 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end
|
||||
self._build()
|
||||
|
||||
def _build(self):
|
||||
self._numbers = array.array('d')
|
||||
n = self.begin
|
||||
while n < self.end:
|
||||
self._numbers.append(n)
|
||||
n += self.step
|
||||
|
||||
def __iter__(self):
|
||||
for item in self._numbers:
|
||||
yield item
|
||||
return StopIteration
|
||||
27
iterables/aritprog_v3.py
Normal file
27
iterables/aritprog_v3.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end=None):
|
||||
self.begin = float(begin)
|
||||
self.step = float(step)
|
||||
self.end = end # None -> "infinite" series
|
||||
|
||||
def __iter__(self):
|
||||
result = self.begin
|
||||
forever = self.end is None
|
||||
while forever or result < self.end:
|
||||
yield result
|
||||
result += self.step
|
||||
raise StopIteration
|
||||
29
iterables/aritprog_v4.py
Normal file
29
iterables/aritprog_v4.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""
|
||||
Arithmetic progression class
|
||||
|
||||
>>> ap = ArithmeticProgression(1, .5, 3)
|
||||
>>> list(ap)
|
||||
[1.0, 1.5, 2.0, 2.5]
|
||||
|
||||
|
||||
"""
|
||||
|
||||
import array
|
||||
from collections import abc
|
||||
|
||||
class ArithmeticProgression:
|
||||
|
||||
def __init__(self, begin, step, end=None):
|
||||
self.begin = begin
|
||||
self.step = step
|
||||
self.end = end # None -> "infinite" series
|
||||
|
||||
def __iter__(self):
|
||||
result = type(self.step)(self.begin)
|
||||
forever = self.end is None
|
||||
index = 0
|
||||
while forever or result < self.end:
|
||||
yield result
|
||||
index += 1
|
||||
result = self.begin + self.step * index
|
||||
raise StopIteration
|
||||
24
iterables/test_aritprog.py
Normal file
24
iterables/test_aritprog.py
Normal file
@@ -0,0 +1,24 @@
|
||||
"""
|
||||
Demonstrate difference between Arithmetic Progression calculated
|
||||
as a series of increments accumulating errors versus one addition
|
||||
and one multiplication.
|
||||
"""
|
||||
|
||||
from fractions import Fraction
|
||||
from aritprog_v3 import ArithmeticProgression as APv3
|
||||
from aritprog_v4 import ArithmeticProgression as APv4
|
||||
|
||||
ap3 = iter(APv3(1, .1))
|
||||
ap4 = iter(APv4(1, .1))
|
||||
ap_frac = iter(APv4(Fraction(1, 1), Fraction(1, 10)))
|
||||
epsilon = 10**-10
|
||||
iteration = 0
|
||||
delta = next(ap3) - next(ap4)
|
||||
frac = next(ap_frac)
|
||||
while abs(delta) <= epsilon:
|
||||
delta = next(ap3) - next(ap4)
|
||||
frac = next(ap_frac)
|
||||
iteration +=1
|
||||
|
||||
print('iteration: {}\tfraction: {}\tepsilon: {}\tdelta: {}'.
|
||||
format(iteration, frac, epsilon, delta))
|
||||
Reference in New Issue
Block a user