From a46753223b3a6a9926e73472b5e4670e6b7da9e1 Mon Sep 17 00:00:00 2001 From: Jamain Arnaud Date: Thu, 10 Jun 2021 11:10:01 +0200 Subject: [PATCH 1/2] naive versions & tests --- .../core/support/maximum_subarray_problem.py | 55 +++++++++++++++ pyske/test/list/test_mps_naive.py | 41 +++++++++++ pyske/test/list/test_mps_opti.py | 26 +++++++ pyske/test/list/test_mss_naive.py | 69 +++++++++++++++++++ pyske/test/list/test_mss_opti.py | 26 +++++++ 5 files changed, 217 insertions(+) create mode 100644 pyske/core/support/maximum_subarray_problem.py create mode 100644 pyske/test/list/test_mps_naive.py create mode 100644 pyske/test/list/test_mps_opti.py create mode 100644 pyske/test/list/test_mss_naive.py create mode 100644 pyske/test/list/test_mss_opti.py diff --git a/pyske/core/support/maximum_subarray_problem.py b/pyske/core/support/maximum_subarray_problem.py new file mode 100644 index 0000000..3b99a8f --- /dev/null +++ b/pyske/core/support/maximum_subarray_problem.py @@ -0,0 +1,55 @@ +import random + + +# ------- Maximum Segment Sum ------- + + +def list_to_segment(list): + res = [] + for i in range(0, len(list)): + for j in range(i + 1, len(list) + 1): + res.append(list[i:j]) + return res + + +# ------- Maximum Segment Sum (two-dimensional lists)------- + + +def is_matrix(list): + length = len(list[0]) + for elem in list: + if len(elem) != length: + return False + return True + + +def list_2d_to_segment(list): + assert is_matrix(list) + largeur = len(list) + hauteur = len(list[0]) + res = [] + for j in range(0, largeur): + for k in range(0, hauteur): + for x in range(0, largeur): + for y in range(0, hauteur): + sousliste = [] + for a in range(j, largeur - x): + for b in range(k, hauteur - y): + sousliste.append(list[a][b]) + if sousliste: + res.append(sousliste) + return res + + +# ------- Maximum Prefix Sum ------- + + +def list_to_prefix(list): + res = [] + for i in range(1, len(list) + 1): + res.append(list[:i]) + return res + + +def frdm(): + return random.randint(-99, 99) \ No newline at end of file diff --git a/pyske/test/list/test_mps_naive.py b/pyske/test/list/test_mps_naive.py new file mode 100644 index 0000000..29bf365 --- /dev/null +++ b/pyske/test/list/test_mps_naive.py @@ -0,0 +1,41 @@ +from pyske.core.support.maximum_subarray_problem import list_to_prefix, frdm +from pyske.examples.list.maximum_prefix_sum import * +from pyske.core.list.slist import * +from pyske.core.support.generate import random_list +import random + + +def test_list_to_prefix(): + list = [1, 2, 3, 4] + prefix = list_to_prefix(list) + res = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]] + assert prefix == res + + +def test_mps_naive1(): + a_list = SList([1, 2, -1, 2, -1, -1, 3, -4]) + segments = list_to_prefix(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 5 + + +def test_mps_naive2(): + a_list = SList([1, 2, 1, 2, 1, 1, 3, 4]) + segments = list_to_prefix(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 15 + + +def test_mps_naive3(): + a_list = SList([-1, -2, -1, -2, -1, -1, -3, -4]) + segments = list_to_prefix(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 0 + + +def test_comparison_mps_naive_to_opti(): + a_list = random_list(frdm, random.randint(1, 15)) + segments = list_to_prefix(a_list) + max_sum_naive = max(max(map(sum, segments)), 0) + max_sum_opti = mps(a_list) + assert max_sum_naive == max_sum_opti \ No newline at end of file diff --git a/pyske/test/list/test_mps_opti.py b/pyske/test/list/test_mps_opti.py new file mode 100644 index 0000000..4c21527 --- /dev/null +++ b/pyske/test/list/test_mps_opti.py @@ -0,0 +1,26 @@ +from pyske.examples.list.maximum_prefix_sum import * +from pyske.core.list.slist import * + + +def test_mps_opti(): + a_list = SList([1, 2, -1, 2, -1, -1, 3, -4]) + best_sum = mps(a_list) + assert best_sum == 5 + + +def test_mps_opti2(): + a_list = SList([1, 2, 1, 2, 1, 1, 3, 4]) + best_sum = mps(a_list) + assert best_sum == 15 + + +def test_mps_opti3(): + a_list = SList([-1, -2, -1, -2, -1, -1, -3, -4]) + best_sum = mps(a_list) + assert best_sum == 0 + + +def test_mps_opti4(): + a_list = SList([]) + best_sum = mps(a_list) + assert best_sum == 0 \ No newline at end of file diff --git a/pyske/test/list/test_mss_naive.py b/pyske/test/list/test_mss_naive.py new file mode 100644 index 0000000..c19f519 --- /dev/null +++ b/pyske/test/list/test_mss_naive.py @@ -0,0 +1,69 @@ +import random +from pyske.core.support.maximum_subarray_problem import list_to_segment, frdm, list_2d_to_segment +from pyske.core.list.slist import * +from pyske.examples.list.maximum_segment_sum import * +from pyske.core.support.generate import random_list + + +def test_list_to_segment(): + list = [1, 2, 3, 4] + segs = list_to_segment(list) + res = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [2], [2, 3], [2, 3, 4], [3], [3, 4], [4]] + assert segs == res + + +def test_list_2d_to_segment(): + list = [[-1, 2], [6, 1]] + segs = list_2d_to_segment(list) + res = [[-1, 2, 6, 1], [-1, 6], [-1, 2], [-1], [2, 1], [2], [6, 1], [6], [1]] + assert segs == res + + +def test_max_segments_sum_naive(): + a_list = SList([-5, 2, 6, -4, 5, -6, -4, 3]) + segments = list_to_segment(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 9 + + +def test_max_segments_sum_naive2(): + a_list = SList([5, 2, 6, 4, 5, 6, 4, 3]) + segments = list_to_segment(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 35 + + +def test_max_segments_sum_naive3(): + a_list = SList([-5, -2, -6, -4, -5, -6, -4, -3]) + segments = list_to_segment(a_list) + max_sum = max(max(map(sum, segments)), 0) + assert max_sum == 0 + + +def test_comparison_mss_naive_to_opti(): + a_list = random_list(frdm, random.randint(1, 15)) + segments = list_to_segment(a_list) + max_sum_naive = max(max(map(sum, segments)), 0) + max_sum_opti = maximum_segment_sum(a_list) + assert max_sum_naive == max_sum_opti + + +def test_max_segment_problem_2d_list(): + list = SList([[-1, 2], [6, 1], [-4, 3]]) + segs = list_2d_to_segment(list) + res = max(max(map(sum, segs)), 0) + assert res == 8 + + +def test_max_segment_problem_2d_list2(): + list = SList([[1, 2], [6, 1], [4, 3]]) + segs = list_2d_to_segment(list) + res = max(max(map(sum, segs)), 0) + assert res == 17 + + +def test_max_segment_problem_2d_list3(): + list = SList([[-1, -2], [-6, -1], [-4, -2]]) + segs = list_2d_to_segment(list) + res = max(max(map(sum, segs)), 0) + assert res == 0 \ No newline at end of file diff --git a/pyske/test/list/test_mss_opti.py b/pyske/test/list/test_mss_opti.py new file mode 100644 index 0000000..39b91a4 --- /dev/null +++ b/pyske/test/list/test_mss_opti.py @@ -0,0 +1,26 @@ +from pyske.examples.list.maximum_segment_sum import * +from pyske.core.list.slist import * + + +def test_max_segments_sum_opti(): + a_list = SList([-5, 2, 6, -4, 5, -6, -4, 3]) + max_sum = maximum_segment_sum(a_list) + assert max_sum == 9 + + +def test_max_segments_sum_opti2(): + a_list = SList([5, 2, 6, 4, 5, 6, 4, 3]) + max_sum = maximum_segment_sum(a_list) + assert max_sum == 35 + + +def test_max_segments_sum_opti3(): + a_list = SList([-5, -2, -6, -4, -5, -6, -4, -3]) + max_sum = maximum_segment_sum(a_list) + assert max_sum == 0 + + +def test_max_segments_sum_opti4(): + a_list = SList([]) + max_sum = maximum_segment_sum(a_list) + assert max_sum == 0 \ No newline at end of file From 4ed99a7ea22306a8d4e2cb38e530869276779028 Mon Sep 17 00:00:00 2001 From: Jamain Arnaud Date: Thu, 24 Jun 2021 17:02:57 +0200 Subject: [PATCH 2/2] documentation mss --- docs/api.rst | 180 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 179 insertions(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index 2a9f345..9be27c6 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1,2 +1,180 @@ PySke API -========= \ No newline at end of file +######### + +PySke API offer applications implemented with list and tree skeletons. +The user can use the sequential or parallel version. +The parallel version allows a faster execution time when its launched on several processors, cores or computers. + +Run examples with parallel computing: + + .. code-block:: console + + mpirun -np NB_CORES python3 PROGRAM_NAME [OPTIONS] + +Examples without :code:`--data` option are only runnable in parallel. + +List Examples +============= + +Dot Product +----------- + +.. py:module:: pyske.examples.list.dot_product + + +Dot Product functions +^^^^^^^^^^^^^^^^^^^^^ + +.. autofunction:: opt_dot_product + +.. autofunction:: dot_product + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:dot_product_parser() + :prog: dot_product_main.py + + +Discrete Fast Fourier Transform +------------------------------- +.. py:module:: pyske.examples.list.fft + +Fast Fourier Transform function +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. autofunction:: fft + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:standard_parser(data_arg=False) + :prog: fft_main.py + + +K-means Clustering +------------------ + +K-means clustering is an unsupervised algorithm that aims to partition group of points in k clusters. + +K-means function +^^^^^^^^^^^^^^^^ + +.. py:module:: pyske.examples.list.k_means + +.. autofunction:: k_means + +Initialization functions +^^^^^^^^^^^^^^^^^^^^^^^^ + +This is the standard method that initializes the centroids. This method chooses the centroids in order that each point is as far as possible from the other. + +.. autofunction:: k_means_init + + +Point Interface +^^^^^^^^^^^^^^^ + +K-means algorithm takes a list of points in parameters. For now two versions implement this class, one for 2 dimension points and another for 3 dimension points. + +Point 2D class implementation: + +.. autoclass:: pyske.core.util.point_2D.Point_2D + :members: + :special-members: + :member-order: bysource + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:k_means_parser() + :prog: k_means_main.py + + +Maximum Prefix Sum +------------------ + +.. py:module:: pyske.examples.list.maximum_prefix_sum + +Maximum Prefix Sum function +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. autofunction:: mps + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:standard_parser() + :prog: maximum_prefix_sum_main.py + +Maximum Segment Sum +------------------- + +Maximum Segment Sum is the task of finding a contiguous subarray with the largest sum. + +Naive version +^^^^^^^^^^^^^^^^ + +.. py:module:: pyske.core.support.maximum_subarray_problem + +One of the naive version is to transform a list of numbers into a list of all its sub-arrays. +Then, we have to sum all of its sub-arrays and take the maximum of them. + +.. autofunction:: list_to_segment + + +Maximum Segment Sum function +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. py:module:: pyske.examples.list.maximum_segment_sum + +.. autofunction:: maximum_segment_sum + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:standard_parser() + :prog: maximum_segment_sum_main.py + +Parallel Regular Sampling Sort +------------------------------ + +.. py:module:: pyske.examples.list.regular_sampling_sort + + +Broadcast function +^^^^^^^^^^^^^^^^^^ + +.. autofunction:: bcast + +Sort function +^^^^^^^^^^^^^ + +.. autofunction:: pssr + + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:standard_parser() + :prog: regular_sampling_sort_main.py + +Variance Example +---------------- + +.. py:module:: pyske.examples.list.variance + +Variance function +^^^^^^^^^^^^^^^^^ + +.. autofunction:: variance + +Running Example +^^^^^^^^^^^^^^^ + +.. autoprogram:: pyske.examples.list.util:standard_parser() + :prog: variance_main.py + + +Tree Examples +============= \ No newline at end of file