Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #17013: added is_square in WordDatatype_char
Browse files Browse the repository at this point in the history
  • Loading branch information
seblabbe authored and videlec committed Sep 20, 2014
1 parent a25e286 commit 398b33c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/sage/combinat/words/word_char.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,46 @@ cdef class WordDatatype_char(WordDatatype):
return True

raise TypeError("not able to initialize a word from {}".format(other))

def is_square(self):
r"""
Returns True if self is a square, and False otherwise.
EXAMPLES::
sage: w = Word([n % 4 for n in range(48)], alphabet=[0,1,2,3])
sage: w.is_square()
True
::
sage: w = Word([n % 4 for n in range(49)], alphabet=[0,1,2,3])
sage: w.is_square()
False
sage: (w*w).is_square()
True
TESTS:
The above tests correspond to the present class (char)::
sage: type(w)
<class 'sage.combinat.words.word.FiniteWord_char'>
::
sage: Word([], alphabet=[0,1]).is_square()
True
sage: Word([0], alphabet=[0,1]).is_square()
False
sage: Word([0,0], alphabet=[0,1]).is_square()
True
"""
cdef size_t l
if self._length % 2 != 0:
return False
else:
l = self._length // 2
return memcmp(self._data,
self._data + l,
l * sizeof(unsigned char)) == 0

0 comments on commit 398b33c

Please sign in to comment.