Skip to content

Conversation

fchapoton
Copy link
Contributor

📚 Description

Add one method to permutations, checking if it is simple (not sending any proper sub-interval to a sub-interval)

Also fixing a few pep8 warnings in the modified file on the way.

📝 Checklist

  • The title is concise, informative, and self-explanatory.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation accordingly.

⌛ Dependencies

Copy link
Contributor

@kevindilks kevindilks left a comment

Choose a reason for hiding this comment

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

I'd like to see a little more in the description/examples so that a user can more readily verify that they understand the definition correctly without having to go to the OEIS link.

Description could mention that [6,1,3,5,2,4] is not simple because it maps the interval [3,4,5,6] to [2,3,4,5], whereas [2,6,3,5,1,4] is simple. And then have those both be tests/examples (along with the cardinality check).

@fchapoton
Copy link
Contributor Author

thanks for the review. I have added your suggestions.

@videlec
Copy link
Contributor

videlec commented Apr 12, 2023

Isn't there a linear algorithm? At least the following is sub-quadratic

def is_simple(self):
    r"""
    EXAMPLES::

            sage: [sum(is_simple(pi) for pi in Permutations(n))  for n in range(8)]
            [1, 1, 2, 0, 2, 6, 46, 338]
    """
    n = len(self)
    if n <= 2:
        return True

    # testing intervals starting at position 0
    start = 0
    left = right = self._list[0]
    end = 1
    while end < n - 1:
        elt = self._list[end]
        if elt < left:
            left = elt
        elif elt > right:
            right = elt
        if right - left == end - start:
            return False
        elif right - left == n - 1:
            break
        end += 1

    # testing intervals starting at later positions
    for start in range(1, n - 1):
        left = right = self._list[start]
        end = start + 1
        while end < n:
            elt = self._list[end]
            if elt < left:
                left = elt
            elif elt > right:
                right = elt
            if right - left == end - start:
                return False
            elif right - left > n - 1 - start:
                break
            end += 1
            
    return True

@fchapoton
Copy link
Contributor Author

oui, c'est déjà mieux. Il faudrait chercher dans la littérature si quelqu'un a un meilleur algo. On peut aussi mettre le code en cython.


TESTS::

sage: [len([pi for pi in Permutations(n) if pi.is_simple()])
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this only a TEST? It is relevant for the user (not the most efficient way to get the list of simple permutations though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comme tu veux. Mais je ne vois pas ce que ca apporte. On a le lien OEIS.

Copy link
Contributor

Choose a reason for hiding this comment

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

Précisément, c'est un lien, il faut cliquer dessus pour avoir l'info.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Voilà. On cythonise ou pas ? J'ai la flemme, et d'autres occupations plus pressantes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Je vois pas trop l'intérêt à ce stade.

@github-actions
Copy link

Documentation preview for this PR is ready! 🎉
Built with commit: 048e0dd

@fchapoton
Copy link
Contributor Author

merci Vincent.

@vbraun vbraun merged commit c18a3fb into sagemath:develop Apr 23, 2023
@fchapoton fchapoton deleted the method_is_simple branch July 16, 2023 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants