Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Add interface for word related functions #57

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
9 changes: 8 additions & 1 deletion libsemigroups_cppyy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ def cppyy_version():

cppyy.cppdef("#define FMT_HEADER_ONLY")
cppyy.cppdef("#define HPCOMBI_CONSTEXPR_FUN_ARGS")

cppyy.include("libsemigroups/libsemigroups.hpp")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can fix this by doing:

if compare_version_numbers(libsemigroups_version(), "1.1.0") and not compare_version_numbers("1.3.0", libsemigroups_version()):


if compare_version_numbers(libsemigroups_version(), "1.1.0") and not compare_version_numbers("1.3.0", libsemigroups_version()):
cppyy.include("libsemigroups/wilo.hpp")
cppyy.include("libsemigroups/wislo.hpp")
cppyy.include("libsemigroups/siso.hpp")

cppyy.gbl.libsemigroups

from cppyy.gbl.libsemigroups import ReportGuard
Expand All @@ -150,6 +154,9 @@ def cppyy_version():
from libsemigroups_cppyy.transf import *
from libsemigroups_cppyy.todd_coxeter import ToddCoxeter

if compare_version_numbers(libsemigroups_version(), "1.1.0") and not compare_version_numbers("1.3.0", libsemigroups_version()):
from libsemigroups_cppyy.word import number_of_words, wilo, wislo, sislo, silo

microseconds = cppyy.gbl.std.chrono.microseconds
milliseconds = cppyy.gbl.std.chrono.milliseconds
seconds = cppyy.gbl.std.chrono.seconds
Expand Down
41 changes: 41 additions & 0 deletions libsemigroups_cppyy/word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
This file contains the interface to libsemigroups actions; see

https://libsemigroups.readthedocs.io/en/latest/words.html

for further details.
"""

import cppyy

from cppyy.gbl.libsemigroups import number_of_words

from libsemigroups_cppyy.detail import ForwardRange

from cppyy.gbl.libsemigroups import cbegin_wilo, cend_wilo

from cppyy.gbl.libsemigroups import cbegin_wislo, cend_wislo

from cppyy.gbl.libsemigroups import cbegin_sislo, cend_sislo

from cppyy.gbl.libsemigroups import cbegin_silo, cend_silo

def wilo(n, upper_bound, first, last):
return ForwardRange(
cppyy.gbl.libsemigroups.cbegin_wilo(n, upper_bound, first, last),
cppyy.gbl.libsemigroups.cend_wilo(n, upper_bound, first, last),)

def wislo(n, first, last):
return ForwardRange(
cppyy.gbl.libsemigroups.cbegin_wislo(n, first, last),
cppyy.gbl.libsemigroups.cend_wislo(n, first, last),)

def sislo(alphabet, first, last):
return ForwardRange(
cppyy.gbl.libsemigroups.cbegin_sislo(alphabet, first, last),
cppyy.gbl.libsemigroups.cend_sislo(alphabet, first, last),)

def silo(alphabet, upper_bound, first, last):
return ForwardRange(
cppyy.gbl.libsemigroups.cbegin_silo(alphabet, upper_bound, first, last),
cppyy.gbl.libsemigroups.cend_silo(alphabet, upper_bound, first, last),)
72 changes: 72 additions & 0 deletions tests/test_siso.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import unittest, libsemigroups_cppyy
from libsemigroups_cppyy import silo, sislo, number_of_words

class TestSilo(unittest.TestCase):
def test_001(self):
u = ""
v = "aaaaaaaaaa"
w = [x for x in silo("a", 10, "", "aaaaaaaaaa")]
self.assertEqual(len(w), 10)
self.assertEqual(w, ['','a','aa','aaa','aaaa','aaaaa','aaaaaa','aaaaaaa','aaaaaaaa','aaaaaaaaa'])
w = [x for x in silo("", 10 , "", "aaaaaaaaaa")]
self.assertEqual(len(w), 1)
w = [x for x in silo("a", 4, "aa", v)]
self.assertEqual(w, ["aa", "aaa"])

def test_002(self):
u = ""
v = "aaaaaaaaaa"
w1 = [x for x in silo("ab", 4, v, u)]
self.assertEqual(w1, [])
w2 = [x for x in silo("ab", 4, u, u)]
self.assertEqual(w2, [])
w3 = [x for x in silo("ab", 2, "a", "bb")]
self.assertEqual(w3, ["a", "b"])
w4 = [x for x in silo("ab", 1, u, v)]
self.assertEqual(w4, [""])

def test_003(self):
w = [x for x in silo("ab", 4, "a", "bbbbb")]
self.assertEqual(len(w), 14)
self.assertEqual(w, ['a','aa','aaa','aab','ab','aba','abb','b','ba','baa','bab','bb','bba','bbb'])
w = [x for x in silo("ba", 4, "b", "aaaaa")]
self.assertEqual(w, ['b','bb','bbb','bba','ba','bab','baa','a','ab','abb','aba','aa','aab','aaa'])

def test_004(self):
w = [x for x in silo("abc", 13, "", "ccccccccccccc")]
self.assertEqual(len(w), 797161)
self.assertEqual(len(w), number_of_words(3, 0 , 13))

def test_005(self):
first = ""
last = "aaaaaaaaaa"
w = [x for x in sislo("a", first, last)]
self.assertEqual(len(w), 10)
self.assertEqual(w, ["", "a", "aa", "aaa", "aaaa", "aaaaa", "aaaaaa", "aaaaaaa", "aaaaaaaa", "aaaaaaaaa"])
w = [x for x in sislo("a", "aa", "aaaa")]
self.assertEqual(w, ["aa","aaa"])

def test_006(self):
first = ""
last = "bbaaab"
w1 = [x for x in sislo("ab", last, first)]
self.assertEqual(w1, [])
w2 = [x for x in sislo("ab", first, first)]
self.assertEqual(w2, [])
w3 = [x for x in sislo("ab", "a", "aa")]
self.assertEqual(w3, ["a", "b"])
w4 = [x for x in sislo("ab", first, last)]
self.assertEqual(len(w4), 112)
self.assertEqual(w4, ["", "a", "b", "aa", "ab", "ba","bb", "aaa", "aab", "aba", "abb", "baa", "bab", "bba", "bbb", "aaaa", "aaab", "aaba", "aabb", "abaa", "abab", "abba", "abbb", "baaa","baab", "baba", "babb", "bbaa", "bbab", "bbba","bbbb", "aaaaa", "aaaab", "aaaba", "aaabb", "aabaa","aabab", "aabba", "aabbb", "abaaa", "abaab", "ababa", "ababb", "abbaa", "abbab", "abbba", "abbbb", "baaaa","baaab", "baaba", "baabb", "babaa", "babab", "babba","babbb", "bbaaa", "bbaab", "bbaba", "bbabb", "bbbaa","bbbab", "bbbba", "bbbbb", "aaaaaa", "aaaaab", "aaaaba", "aaaabb", "aaabaa", "aaabab", "aaabba", "aaabbb", "aabaaa", "aabaab", "aababa", "aababb", "aabbaa", "aabbab", "aabbba","aabbbb", "abaaaa", "abaaab", "abaaba", "abaabb", "ababaa", "ababab", "ababba", "ababbb", "abbaaa", "abbaab", "abbaba","abbabb", "abbbaa", "abbbab", "abbbba", "abbbbb", "baaaaa","baaaab", "baaaba", "baaabb", "baabaa", "baabab", "baabba", "baabbb", "babaaa", "babaab", "bababa", "bababb", "babbaa","babbab", "babbba", "babbbb", "bbaaaa"])

def test_007(self):
w = [ x for x in sislo("ab", "a", "aaaa")]
self.assertEqual(w, ["a", "b", "aa", "ab", "ba", "bb", "aaa", "aab", "aba", "abb", "baa", "bab", "bba", "bbb"])
self.assertEqual(len(w), 14)
w = [ x for x in sislo("ba", "b", "bbbb")]
self.assertEqual(w, ["b", "a", "bb", "ba", "ab", "aa", "bbb", "bba", "bab", "baa", "abb", "aba", "aab", "aaa"])

def test_008(self):
w = [x for x in sislo("abc", "", "aaaaaaaaaaaaa")]
self.assertEqual(len(w), 797161)
self.assertEqual(len(w), number_of_words(3, 0, 13))
110 changes: 110 additions & 0 deletions tests/test_wilo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import unittest, libsemigroups_cppyy
from libsemigroups_cppyy import wilo, number_of_words

class TestWilo(unittest.TestCase):
def test_001(self):
u = [0, 0, 0, 0]
v = [1, 1, 1, 1]
self.assertEqual([x for x in wilo(2,1,v,u)], [])
self.assertEqual([x for x in wilo(2,1,u,u)], [])
w1 = [x for x in wilo(2, 2, [], [1,1])]
self.assertEqual(len(w1), 3)
w2 = []
for i in range(0, len(w1)):
w2.append([x for x in w1[i]])
self.assertEqual(w2, [[], [0], [1]])
w3 = [x for x in wilo(2, 1, [], [1,1])]
self.assertEqual(len(w3), 1)
self.assertEqual([x for x in w3[0]], [])
w4 = [x for x in wilo(2, 1, [], [0])]
self.assertEqual(len(w4), 1)
self.assertEqual([x for x in w4[0]], [])
def test_002(self):
w = [ x for x in wilo(2, 4, [0], [1, 1, 1, 1])]
u = []
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[0],[0,0],[0,0,0],[0,0,1],[0,1],[0,1,0],[0,1,1],[1],[1,0],[1,0,0],[1,0,1],[1,1],[1,1,0],[1,1,1]])
self.assertEqual(len(u), 14)
def test_003(self):
first = [0]
last = [2, 2, 2, 2]
w = [x for x in wilo(3, 4, first, last)]
u = []
self.assertEqual(len(w), 39)
self.assertEqual(len(w), number_of_words(3,1,4))
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1], [0, 1, 0], [0, 1, 1], [0, 1, 2], [0, 2], [0, 2, 0], [0, 2, 1], [0, 2, 2], [1], [1, 0], [1, 0, 0], [1, 0, 1], [1, 0, 2], [1, 1], [1, 1, 0], [1, 1, 1], [1, 1, 2], [1, 2], [1, 2, 0], [1, 2, 1], [1, 2, 2], [2], [2, 0], [2, 0, 0], [2, 0, 1], [2, 0, 2], [2, 1], [2, 1, 0],[2, 1, 1],[2, 1, 2],[2, 2], [2, 2, 0], [2, 2, 1], [2, 2, 2]])

def test_004(self):
first = []
last = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
w = [x for x in wilo(3, 10, first, last)]
self.assertEqual(len(w), 29524)
self.assertEqual(len(w), number_of_words(3, 0, 10))

def test_004(self):
first = []
last = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
w = [x for x in wilo(3, 13, first, last)]
self.assertEqual(len(w), 797161)
self.assertEqual(len(w), number_of_words(3, 0, 13))

def test_005(self):
first = []
last = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
w = [ x for x in wilo(1, 10, first, last)]
self.assertEqual(len(w), 10)
u = []
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[],
[0],
[0, 0],
[0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]])
w = [ x for x in wilo(0, 0, first, last)]
self.assertEqual(w, [])
first = [0, 0]
last = [0, 0, 0, 0]
w = [x for x in wilo(1, 4, first, last)]
self.assertEqual(len(w), 2)
self.assertEqual([x for x in w[0]], [0, 0])
self.assertEqual([x for x in w[1]], [0, 0, 0])
w = [x for x in wilo(1, 1, first, last)]
self.assertEqual(w, [])
w = [x for x in wilo(1, 5, [0], [1])]
self.assertEqual(len(w), 4)
u = []
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[0], [0, 0], [0, 0, 0], [0, 0, 0, 0]])

def test_006(self):
first = [0, 1, 2, 3]
last = [4, 4, 4, 4, 4]
w = [x for x in wilo(4, 5, first, last)]
self.assertEqual(len(w), 303)

def test_007(self):
first = [0, 1]
last = [1, 1, 1]
w = [ x for x in wilo(2, 3, first, last) ]
self.assertEqual(len(w), 4)
u = []
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[0, 1], [1], [1, 0], [1, 1]])
w = [ x for x in wilo(2, 1, first, last) ]
self.assertEqual(w, [])





87 changes: 87 additions & 0 deletions tests/test_wislo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import unittest, libsemigroups_cppyy
from libsemigroups_cppyy import wislo, number_of_words

class WisloTests(unittest.TestCase):
def test_000(self):
first = [0]
last = [0, 0, 0, 0]
w = [x for x in wislo(2, first, last)]
self.assertEqual(len(w), 14)
u = []
for i in range(0, len(w)):
u.append([x for x in w[i]])
self.assertEqual(u, [[0],
[1],
[0, 0],
[0, 1],
[1, 0],
[1, 1],
[0, 0, 0],
[0, 0, 1],
[0, 1, 0],
[0, 1, 1],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0],
[1, 1, 1]])
def test_001(self):
first = [0, 0, 0, 0]
last = [0, 0, 0, 0, 0]
w1 = [x for x in wislo(2, last, first)]
self.assertEqual(w1, [])
w2 = [x for x in wislo(2, last, last)]
self.assertEqual(w2, [])
w3 = [x for x in wislo(2, last, [0,0,0,0,0,0])]
self.assertEqual(len(w3), 32)
u = []
for i in range(0, len(w3)):
u.append([x for x in w3[i]])
self.assertEqual(u, [[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 0, 1, 1],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 1],
[0, 0, 1, 1, 0],
[0, 0, 1, 1, 1],
[0, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 1, 0, 1, 1],
[0, 1, 1, 0, 0],
[0, 1, 1, 0, 1],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 1],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[1, 0, 0, 1, 0],
[1, 0, 0, 1, 1],
[1, 0, 1, 0, 0],
[1, 0, 1, 0, 1],
[1, 0, 1, 1, 0],
[1, 0, 1, 1, 1],
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 1],
[1, 1, 0, 1, 0],
[1, 1, 0, 1, 1],
[1, 1, 1, 0, 0],
[1, 1, 1, 0, 1],
[1, 1, 1, 1, 0],
[1, 1, 1, 1, 1]])
def test_002(self):
first = []
last = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
w = [ x for x in wislo(3, first, last)]
self.assertEqual(len(w), 29524)
self.assertEqual(len(w), number_of_words(3, 0, 10))

def test_003(self):
first = []
last = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
w = [ x for x in wislo(3, first, last)]
self.assertEqual(len(w), 797161)
self.assertEqual(len(w), number_of_words(3, 0, 13))




13 changes: 13 additions & 0 deletions tests/test_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest, libsemigroups_cppyy
from libsemigroups_cppyy import number_of_words


class TestNumberOfWords(unittest.TestCase):
def test_000(self):
self.assertEqual(number_of_words(3, 1, 4), 39)
self.assertEqual(number_of_words(3, 0, 10), 29524)
self.assertEqual(number_of_words(2, 5, 6), 32)
self.assertEqual(number_of_words(3, 0, 13), 797161)
self.assertEqual(number_of_words(2, 4, 1), 0)
self.assertEqual(number_of_words(2, 4, 4), 0)
self.assertEqual(number_of_words(2, 4, 2), 0)