Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new module for morphic words #31378

Closed
sagetrac-jlepsova mannequin opened this issue Feb 11, 2021 · 59 comments
Closed

Create a new module for morphic words #31378

sagetrac-jlepsova mannequin opened this issue Feb 11, 2021 · 59 comments

Comments

@sagetrac-jlepsova
Copy link
Mannequin

sagetrac-jlepsova mannequin commented Feb 11, 2021

The goal of this ticket is to create a new module for morphic words.

As a consequence, it will improve the following computations which take a lot of time:

sage: m = WordMorphism('a->ab,b->a')
sage: w = m.fixed_point('a')
sage: w
word: abaababaabaababaababaabaababaabaababaaba...
sage: %time w[1000]
CPU times: user 1.45 ms, sys: 0 ns, total: 1.45 ms
Wall time: 1.45 ms
'a'
sage: %time w[10000]
CPU times: user 82.9 ms, sys: 0 ns, total: 82.9 ms
Wall time: 82.1 ms
'a'
sage: %time w[100000]
CPU times: user 5.19 s, sys: 6.26 ms, total: 5.2 s
Wall time: 5.19 s
'b'
sage: %time w[1000000]
CPU times: user 12min 45s, sys: 93.4 ms, total: 12min 45s
Wall time: 12min 45s
'a'

CC: @videlec

Component: combinatorics

Author: Jana Lepšová, Sébastien Labbé

Branch/Commit: 016f022

Reviewer: Vincent Delecroix

Issue created by migration from https://trac.sagemath.org/ticket/31378

@sagetrac-jlepsova sagetrac-jlepsova mannequin added this to the sage-9.3 milestone Feb 11, 2021
@fchapoton
Copy link
Contributor

comment:1

duplicate of #31370 ?

@sagetrac-jlepsova

This comment has been minimized.

@seblabbe
Copy link
Contributor

comment:3

Replying to @fchapoton:

duplicate of #31370 ?

Let's close #31370 and keep this one (Jana is learning how to contribute to Sage).

@seblabbe
Copy link
Contributor

Changed branch from u/jlepsova/morphic to u/slabbe/31378

@seblabbe
Copy link
Contributor

Changed commit from 7876b86 to 86990a0

@seblabbe
Copy link
Contributor

comment:4

I rebased the branch on top of 9.3.beta7. I also added a commit which links the new module added by Jana to the existing sage words library.


New commits:

77c4645adding more efficient way of stating a letter at an n-th position of a fixed point
86990a0linking morphic.py file to the sage words library

@seblabbe
Copy link
Contributor

comment:5

Jana, the next step is to update the examples in the documentation in the morphic.py file.

With the current branch,

$ sage -bt src/sage/combinat/words/morphic.py 

returns

[...]

**********************************************************************
File "src/sage/combinat/words/morphic.py", line 157, in sage.combinat.words.morphic.WordDatatype_morphic.__iter__
Failed example:
    print('update the examples here')
Expected nothing
Got:
    update the examples here
**********************************************************************

[...]

**********************************************************************
3 items had failures:
   1 of   2 in sage.combinat.words.morphic
   1 of   2 in sage.combinat.words.morphic.WordDatatype_morphic.__getitem__
   6 of  13 in sage.combinat.words.morphic.WordDatatype_morphic.__iter__
    [30 tests, 8 failures, 0.02 s]
----------------------------------------------------------------------
sage -t --warn-long 72.7 --random-seed=0 src/sage/combinat/words/morphic.py  # 8 doctests failed
----------------------------------------------------------------------

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Feb 18, 2021

Changed branch from u/slabbe/31378 to u/jlepsova/morphic

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Feb 18, 2021

Changed commit from 86990a0 to d4e7081

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Feb 18, 2021

comment:6

I fixed the doctests in morphic.py.


New commits:

6587375adding more efficient way of stating a letter at an n-th position of a fixed point
20ae22elinking morphic.py file to the sage words library
a1f414331378: incomplete changes
d4e7081fixed doctests in morphic.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 20, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

b8690fcfixing some of the errors in word.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Feb 20, 2021

Changed commit from d4e7081 to b8690fc

@mkoeppe
Copy link
Member

mkoeppe commented Mar 24, 2021

comment:8

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

@mkoeppe mkoeppe modified the milestones: sage-9.3, sage-9.4 Mar 24, 2021
@seblabbe
Copy link
Contributor

seblabbe commented Apr 2, 2021

New commits:

76fcfd6Merge branch 'u/jlepsova/morphic' into 9.3.rc0
9573c3431378: fixing doctests in word.py

@seblabbe
Copy link
Contributor

seblabbe commented Apr 2, 2021

Changed branch from u/jlepsova/morphic to u/slabbe/31378

@seblabbe
Copy link
Contributor

seblabbe commented Apr 2, 2021

Changed commit from b8690fc to 9573c34

@seblabbe
Copy link
Contributor

seblabbe commented Apr 2, 2021

comment:10

I fixed the doctests with respect to saving the objects (loads, dumps, reduce, etc.).

It remains only one issue:

            sage: m = WordMorphism("a->ab,b->")          
            sage: w = m.fixed_point("a")                                      
            sage: w.representation(0)                
            []                                      
            sage: w.representation(1)             
            [1]                         
            sage: w.representation(2)  #infinite loop

Jana: can you take a look at this?

@seblabbe
Copy link
Contributor

seblabbe commented Apr 2, 2021

Author: Jana Lepšová, Sébastien Labbé

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Apr 15, 2021

Changed commit from 9573c34 to ec5d4a7

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Apr 15, 2021

New commits:

cc41397Merge branch 'u/slabbe/31378' into 9.3.rc3
ec5d4a7trying to repair the finite fixed point problem, something changed in word_infinite_datatypes.py, however

@sagetrac-jlepsova
Copy link
Mannequin Author

sagetrac-jlepsova mannequin commented Apr 15, 2021

Changed branch from u/slabbe/31378 to u/jlepsova/morphic

@seblabbe
Copy link
Contributor

comment:13

Does the most recent commit also handles the following infinite loop?

sage: m = WordMorphism("a->ab,b->,c->cdab,d->dcab")
sage: w = m.fixed_point("a")
sage: w.representation(2)

What is the remaining problem you mention in the commit message? Can you provide an example?

In the following, the multiplication vMk*M is computed twice. Maybe you want to write vMk1 = vMk*M to represent vM^{k+1} and at the end of the loop, you may do vMk = vMk1.

         while vMk[position] <= n:
+            if vMk == vMk*M:
+                raise ValueError('The morphism has a finite fixed point of length {}.'.format(vMk[position]))
             vMk = vMk*M
             length_of_images.append(vMk)

I would suggest that the error message also include the value of n. Something like IndexError: index (=2) out of range, the fixed point is finite and has length 2.

I think the error should be an IndexError instead of ValueError, as it is for strings:

sage: s = 'ab'                                                                  
sage: s[2]                                                                      
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-2-2d1d9bdb9b26> in <module>
----> 1 s[Integer(2)]

IndexError: string index out of range

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:30

Are you sure that Computing the n-th letter of a fixed point is fast? If the morphism is primitive then I agree but otherwise...

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:31

Here is an example

sage: s = WordMorphism('0->011,1->1')
sage: w = s.fixed_point('0')
sage: w # very stupid word :-)
word: 0111111111111111111111111111111111111111...
sage: w.representation(1000000) # now you can wait !!

EDIT: Computing the representation is long (because the answer is long) but computing w[1000000] should be fast.

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:32

More generally, when the growth of |sigma^n(letter)| is polynomial (and not exponential) then going via the representation is not a good idea.

@seblabbe
Copy link
Contributor

comment:33

Replying to @videlec:

More generally, when the growth of |sigma^n(letter)| is polynomial (and not exponential) then going via the representation is not a good idea.

Well if the growth rate of the |sigma^n(letter)| is quadratic this ticket does an improvement. Here is an example:

On 9.5.beta0:

sage: s = WordMorphism('a->abc,b->bc,c->c')                                     
sage: w = s.fixed_point('a')                                                    
sage: %time w[1000000]                                                          
CPU times: user 6.45 s, sys: 11.5 ms, total: 6.46 s
Wall time: 6.46 s
'c'

On 9.5.beta0 + #31378:

sage: s = WordMorphism('a->abc,b->bc,c->c')                                     
sage: w = s.fixed_point('a')                                                    
sage: %time w[1000000]                                                          
CPU times: user 28.5 ms, sys: 51 µs, total: 28.6 ms
Wall time: 28.4 ms
'c'

Here it grows quadratically:

sage: for i in range(10): u = s('a', i); print(len(u), u)                       
1 a
3 abc
6 abcbcc
10 abcbccbccc
15 abcbccbcccbcccc
21 abcbccbcccbccccbccccc
28 abcbccbcccbccccbcccccbcccccc
36 abcbccbcccbccccbcccccbccccccbccccccc
45 abcbccbcccbccccbcccccbccccccbcccccccbcccccccc
55 abcbccbcccbccccbcccccbccccccbcccccccbccccccccbccccccccc

The problem is when the images grow linearly. Question: can we detect that?

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:34

The answer is yes, see #32594.

We can do that later on.

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:35

Sorry : you should fix trailing whitespaces.

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

Work Issues: trailing whitespaces

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 30, 2021

Changed commit from 79c2834 to 29278b4

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Sep 30, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

29278b431378: removing trailing whitespaces in morphic.py file

@seblabbe
Copy link
Contributor

Changed work issues from trailing whitespaces to none

@seblabbe
Copy link
Contributor

Reviewer: Vincent Delecroix

@seblabbe
Copy link
Contributor

comment:38

I think the issue with trailing whitespaces was mainly with file morphic.py. I removed those from that file.

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:39

there are more in morphism.py.

@seblabbe
Copy link
Contributor

comment:40

Do you want me to remove all of the trailing whitespaces in the file, or only those in lines that were changed?

@videlec
Copy link
Contributor

videlec commented Sep 30, 2021

comment:41

That is fine with me either way.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 7, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

016f02231378: removing trailing whitespaces in morphism.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Oct 7, 2021

Changed commit from 29278b4 to 016f022

@seblabbe
Copy link
Contributor

seblabbe commented Oct 7, 2021

comment:43

I fixed the 2 trailing whitespaces in the file morphism.py that were added in previous commit on this ticket.

@vbraun
Copy link
Member

vbraun commented Oct 10, 2021

Changed branch from u/slabbe/31378 to 016f022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants