Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 179 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,180 @@
PySke API
=========
#########

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
=============
55 changes: 55 additions & 0 deletions pyske/core/support/maximum_subarray_problem.py
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 41 additions & 0 deletions pyske/test/list/test_mps_naive.py
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions pyske/test/list/test_mps_opti.py
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions pyske/test/list/test_mss_naive.py
Original file line number Diff line number Diff line change
@@ -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
Loading