def test_zip_cons(): # pylint: disable=missing-docstring sl1 = SList([1, 2, 3]) sl2 = SList([2, 3, 4]) res = sl1.zip(sl2) exp = SList([(1, 2), (2, 3), (3, 4)]) assert res == exp
def test_head_one(): # pylint: disable=missing-docstring slst = SList() exp = 1 slst.append(exp) res = slst.head() assert res == exp
def test_reduce_nil(): # pylint: disable=missing-docstring initial = 1232 slst = SList() res = slst.reduce(operator.add, initial) exp = initial assert res == exp
def test_scanp_nil(): # pylint: disable=missing-docstring neutral = 0 slst = SList() res = slst.scanp(operator.add, neutral) exp = SList([]) assert res == exp
def test_balance_data(): # pylint: disable=missing-docstring data = SList.init(int, 23) size = data.length() res = data.balance() exp = SList(range(0, size)) assert res == exp
def test_zip_nil(): # pylint: disable=missing-docstring sl1 = SList() sl2 = SList() res = sl1.zip(sl2) exp = SList() assert res == exp
def test_zipwith_cons(): # pylint: disable=missing-docstring sl1 = SList([1, 2, 3]) sl2 = SList([2, 3, 4]) res = sl1.map2(operator.add, sl2) exp = SList([3, 5, 7]) assert res == exp
def test_scanp_cons(): # pylint: disable=missing-docstring neutral = 0 slst = SList([1, 2, 3, 4]) res = slst.scanp(operator.add, neutral) exp = SList([9, 7, 4, 0]) assert res == exp
def test_zipwith_nil(): # pylint: disable=missing-docstring sl1 = SList() sl2 = SList() res = sl1.map2(operator.add, sl2) exp = SList() assert res == exp
def test_distribute_data(): # pylint: disable=missing-docstring data = SList.init(int, 42) size = data.length() distr = Distribution([size]) res = data.distribute(distr) exp = SList(range(0, size)) assert res == exp
def test_balance_data(): # pylint: disable=missing-docstring data = generate_int_plist() size = data.length() res = data.balance().to_seq() exp = SList(range(0, size)) assert res == exp
def _test_scanl_last(size): # pylint: disable=missing-docstring data = PList.init(fun.idt, size) res_pl, res_scalar = data.scanl_last(operator.add, 0) res = (res_pl.to_seq(), res_scalar) exp = SList(range(0, size)).scanl_last(operator.add, 0) assert res == exp
def test_scanl_non_empty(): # pylint: disable=missing-docstring size = 23 data = PList.init(fun.idt, size) res = data.scanl(operator.add, 0).to_seq() exp = SList(range(0, size)).scanl(operator.add, 0) assert res == exp
def norm(vec: DSList): """ Computes the norm of the argument vector. :param vec: DSList :return: float """ return math.sqrt(vec.map(sqr).reduce(add, 0))
def test_reduce_sum_non_empty(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4, 5, 6]) plst = PList.from_seq(slst) exp = 21 res = plst.reduce(operator.add, 0) assert res == exp
def test_reduce_cons(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4]) plst = PList.from_seq(slst) res = plst.reduce(operator.add) exp = 10 assert res == exp
def test_reduce_sum_empty(): # pylint: disable=missing-docstring neutral = 0 slst = SList() plst = PList.from_seq(slst) exp = neutral res = plst.reduce(operator.add, neutral) assert res == exp
def vadd(vec1: DSList, vec2: DSList): """ Computes the sum of two vectors. :param vec1: DSList :param vec2: DSList :return: DSList """ return vec1.map2(add, vec2)
def smul(scalar: float, vec: DSList): """ Compute the product of a scalar and a vector :param scalar: float :param vec: DSList :return: DSList """ return vec.map(lambda num: scalar*num)
def _test3(): if SEQ: data = DSList.init(_vrand, SIZE) else: data = DPList.init(_vrand, SIZE) res1 = test_timing(_vavg, data, "vector average") res2 = test_timing(_wrapped_vavg, data, "vector average [_run]") assert res1 == res2
def test_map_reduce_nil(): # pylint: disable=missing-docstring neutral = 0 slst = SList() plst = PList.from_seq(slst) res = plst.map_reduce(fun.incr, operator.add, neutral) exp = neutral assert res == exp
def test_map_reduce_cons(): # pylint: disable=missing-docstring slst = SList([1, 2, 3, 4]) plst = PList.from_seq(slst) exp = plst.map(fun.incr) exp = exp.reduce(operator.add) res = plst.map_reduce(fun.incr, operator.add) assert res == exp
def _test2(): if SEQ: input2 = DSList.init(lambda i: random.randint(0, 9) <= 4, SIZE) else: input2 = DPList.init(lambda i: random.randint(0, 9) <= 4, SIZE) res1 = test_timing(_test_bool_direct, input2, "map/reduce bool") res2 = test_timing(_test_bool_mr, input2, "map_reduce bool") res3 = test_timing(_test_bool_optimized, input2, "map/reduce bool[_opt]", _opt, _run) assert res1 == res3 assert res1 == res2
def _test1(): if SEQ: input1 = DSList.init(lambda num: random.randint(0, 1000), SIZE) else: input1 = DPList.init(lambda num: random.randint(0, 1000), SIZE) res1 = test_timing(_test_mmr_direct, input1, "map/map/reduce") res2 = test_timing(_test_mmr_direct, input1, "map/reduce[hc]") res3 = test_timing(_test_mmr_run, input1, "map/map/reduce[_opt]") res4 = test_timing(_test_mmr_optimized, input1, "map/map/reduce[_opt/_run]", _opt, _run) assert res1 == res2 and res1 == res3 and res1 == res4
def test_distribute_data(): # pylint: disable=missing-docstring dst = par.randpid() data = generate_int_plist() size = data.length() distr = Distribution([0 for _ in par.procs()]) distr[dst] = size res = data.distribute(distr).to_seq() exp = SList(range(0, size)) assert res == exp
def __main(): parallel_list1 = PList.init(lambda i: __MSG[i], len(__MSG)) parallel_list2 = PList.init(lambda x: x, len(__MSG)) parallel_list4 = parallel_list1.map(lambda x: x.capitalize()).zip( parallel_list2) parallel_list6 = parallel_list4.map(lambda x: x[0]).mapi(lambda i, x: (i, x)) parallel_list7 = parallel_list6.map(lambda x: x[1]) parallel_list8 = parallel_list7.map(lambda x: 1) size = parallel_list8.reduce(lambda x, y: x + y, 0) parallel_list9 = parallel_list7.get_partition() parallel_list10 = parallel_list9.map( lambda l: SList(l).filter(lambda c: c != 'O')).flatten() parallel_list11 = PList.from_seq(["Hello World!"]) filtered = SList(parallel_list10.get_partition().reduce(concat, [])).reduce(add) str1 = SList(parallel_list9.reduce(add)).reduce(add) str2 = parallel_list11.to_seq()[0] par.at_root(lambda: print(f'Capitalized: \t{str1}\n' f'Identity:\t{str2}\n' f'Length:\t\t{size}\n' f'Filtered:\t{filtered}'))
def _vrand(_): return DSList.init(_f_rand, DIM)
def _test2(): if SEQ: input2 = DSList.init(lambda i: random.randint(0, 9) <= 4, SIZE) else: input2 = DPList.init(lambda i: random.randint(0, 9) <= 4, SIZE) res1 = test_timing(_test_bool_direct, input2, "map/reduce bool") res2 = test_timing(_test_bool_mr, input2, "map_reduce bool") res3 = test_timing(_test_bool_optimized, input2, "map/reduce bool[_opt]", _opt, _run) assert res1 == res3 assert res1 == res2 DIM = 10 VZERO = DSList.init(lambda i: 0.0, DIM) def _f_rand(_): return random.randint(0, 1000) def _vrand(_): return DSList.init(_f_rand, DIM) def _vnsum(lst): return lst.map(normalize).reduce(vadd, VZERO) def _vavg(lst):
def test_mapi_id(): # pylint: disable=missing-docstring slst = SList([1, 2, 3]) exp = SList([1, 2, 3]) res = slst.mapi(lambda i, val: val) assert res == exp
def test_mapi_empty(): # pylint: disable=missing-docstring slst = SList() exp = SList() res = slst.mapi(lambda i, val: (i, val)) assert res == exp