#16935: Faster palindromes function for the Words library
-------------------------------------+-------------------------------------
Reporter: nadialafreniere | Owner:
Type: enhancement | Status: needs_review
Priority: major | Milestone: sage-6.4
Component: combinatorics | Resolution:
Keywords: words, finite | Merged in:
words, palindromes | Reviewers:
Authors: Nadia Lafrenière | Work issues:
Report Upstream: N/A | Commit:
Branch: | 160ccb0ccfb7580fe5d497b3b049fbf2b3c25523
u/nadialafreniere/palindromes__list_of_lps_lengths_and_list_of_maximal_palindromes_length|
Stopgaps:
Dependencies: |
-------------------------------------+-------------------------------------
Comment (by slabbe):
Replying to [ticket:16935 nadialafreniere]:
> You can see here that the algorithm is factor, especially for long
words.
Indeed, I can see that it is currently twice as fast:
{{{
sage: w = words.ThueMorseWord()[:1000]
sage: %time P = w.palindromic_lacunas_study()[2]
CPU times: user 903 ms, sys: 11.4 ms, total: 914 ms
Wall time: 895 ms
sage: %time Q = w.palindromes()
CPU times: user 516 ms, sys: 34.9 ms, total: 551 ms
Wall time: 472 ms
sage: P == Q
True
sage: w = words.ThueMorseWord()[:10000]
sage: %time P = w.palindromic_lacunas_study()[2]
CPU times: user 40.5 s, sys: 200 ms, total: 40.7 s
Wall time: 40.6 s
sage: %time Q = w.palindromes()
CPU times: user 26 s, sys: 256 ms, total: 26.3 s
Wall time: 26.2 s
sage: P == Q
True
}}}
> I did not find any way to fix it, except to change the alphabet when
generating these words.
I am not sure I understand the problem. But, if `w` is a word of length 1
in sage, then `w[0]` will give the the letter it contains. Using this, you
can compare the equality of letters after the application of the function
`f`.
I am very happy with the way the code is now separated. Most of the
methods are written clearly. I am still not convinced by the optimality
and clarity of the `length_maximal_palindrome` method. I started a new
branch where I changed the way the indices are worked out.
[http://git.sagemath.org/sage.git/commit/?h=u/slabbe/16935&id=ef438b50865857394a404adf56b3c6218997419a
My branch is here]. Tell me what you think.
I added a raise Value Error when the parity of m is the same as the parity
of 2j which is an impossible case. I was surprise to realize that it
breaks the other methods:
{{{
sage: Word('01101001').lengths_maximal_palindromes()
...
ValueError: j-(m+1)/2(=5/2) must be an integer, i.e., 2*j(=6) and m(=0)
can't have the same parity
}}}
This means that the impossible case is in fact used. Do you understand
why?
Finally, I think the palindrome method can now be written as a one liner
using a list comprehension...
--
Ticket URL: <http://trac.sagemath.org/ticket/16935#comment:27>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.