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

py3: replacing some .vertices() by .vertex_iterator() #25832

Closed
fchapoton opened this issue Jul 11, 2018 · 38 comments
Closed

py3: replacing some .vertices() by .vertex_iterator() #25832

fchapoton opened this issue Jul 11, 2018 · 38 comments

Comments

@fchapoton
Copy link
Contributor

mainly inside loops.

  • This is faster
  • This does not sort the vertices

Inspired by ##22349

Found using

git grep "for.*\.vertices()" src/sage/quivers/
git grep "for.*\.vertices()" src/sage/combinat/

CC: @tscrim @jm58660 @dcoudert

Component: combinatorics

Author: Frédéric Chapoton

Branch/Commit: 03972bf

Reviewer: David Coudert

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

@fchapoton fchapoton added this to the sage-8.3 milestone Jul 11, 2018
@fchapoton

This comment has been minimized.

@fchapoton
Copy link
Contributor Author

Branch: u/chapoton/25832

@fchapoton
Copy link
Contributor Author

Commit: a17fd63

@fchapoton
Copy link
Contributor Author

New commits:

2a5d00ctrac 25817: add "conjugate" explicitly to maxima_lib translation dictionary, since automatic discovery doesn't always work.
a17fd63replacing some .vertices() by .vertex_iterator(), in combinat and quivers

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 11, 2018

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

4ad879areplacing some .vertices() by .vertex_iterator(), in combinat and quivers

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 11, 2018

Changed commit from a17fd63 to 4ad879a

@fchapoton
Copy link
Contributor Author

Changed author from frederic chapoton to Frédéric Chapoton

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 11, 2018

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

29fe2aareplacing some .vertices() by .vertex_iterator(), in combinat and quivers

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 11, 2018

Changed commit from 4ad879a to 29fe2aa

@tscrim
Copy link
Collaborator

tscrim commented Jul 12, 2018

comment:5

Needs review?

@jm58660
Copy link
Mannequin

jm58660 mannequin commented Jul 13, 2018

comment:6

Replying to @tscrim:

Needs review?

I guess no, this fails several tests.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

Changed commit from 29fe2aa to 2d6322d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

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

2d6322dtrac 25832 undo wrong change to vertex_iterator

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

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

be764c5trac 25832 another fix

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

Changed commit from 2d6322d to be764c5

@fchapoton
Copy link
Contributor Author

comment:9

There remains failing doctests in species. Probably we can just change the doctests.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

db32dbereplacing some .vertices() by .vertex_iterator(), in combinat and quivers

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 13, 2018

Changed commit from be764c5 to db32dbe

@fchapoton
Copy link
Contributor Author

comment:11

I have undone the change in species, that was making some doctests return random results.

@tscrim
Copy link
Collaborator

tscrim commented Jul 13, 2018

comment:12

Perhaps the thing to do would be to run sorted in each of those places (e.g., in defining var_mapping) as progress towards #22349.

@fchapoton
Copy link
Contributor Author

comment:13

Hmm, what do you mean ? Using "sorted" in species ?

@tscrim
Copy link
Collaborator

tscrim commented Jul 13, 2018

comment:14

Yes, that is correct.

@tscrim
Copy link
Collaborator

tscrim commented Jul 13, 2018

comment:15

Well, ideally we would write doctests that do not depend on the output order, but sometimes that is harder than its worth.

@fchapoton
Copy link
Contributor Author

comment:16

oh, well. Maybe we can just keep that for another ticket, no ?

@dcoudert
Copy link
Contributor

comment:18

A few comments in quiver.py:

  • Apparently, you can replace set(frozen).issubset(set(data.vertex_iterator())) by set(frozen).issubset(data.vertex_iterator()).
sage: G = graphs.PetersenGraph()
sage: set([1,2]).issubset(G.vertex_iterator())
True
sage: set([1,10]).issubset(G.vertex_iterator())
False
  • I don't know the size of mlist in this code, but it might be interesting to first convert it to a set before doing nlist = self._nlist = sorted(x for x in data.vertex_iterator() if x not in mlist)

  • if any((a,a) in edges for a in data.vertex_iterator()): -> if data.has_loops():. Furthermore, the list edges could be turned to a set for the next test (2-cycles).

  • There are places where you could replace edges() by edge_iterator(), but may be you prefer to let that for another ticket.

  • and finally, the following block

if dg.has_multiple_edges():
    multi_edges = {}
    for v1,v2,label in dg.multiple_edges():
        ...

could be changed to

multiple_edges = dg.multiple_edges()
if multiple_edges:
    multi_edges = {}
    for v1,v2,label in multiple_edges:
        ....

Indeed, the cost of has_multiple_edges is almost the same as multiple_edges, that is visiting all the edges. So we can save one call.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2018

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

976e0f5trac 25832 some suggested changes in quiver.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 14, 2018

Changed commit from db32dbe to 976e0f5

@dcoudert
Copy link
Contributor

comment:20

To avoid converting frozen to set and later to list again, and also to benefit from the set, I suggest to do:

            elif isinstance(frozen, list):
                s_frozen = set(frozen)
                if not s_frozen.issubset(data.vertex_iterator()):
                     raise ValueError("the optional list of frozen elements"
                                      " must be vertices of the digraph")
                 else:
                    mlist = self._mlist = frozen
                    nlist = self._nlist = sorted(x for x in data.vertex_iterator() if x not in s_frozen)

@fchapoton
Copy link
Contributor Author

comment:22

I am not sure of what is exactly the point of converting to set. I can see that it can handle bad input, namely duplicate elements in the given frozen list, in which case we do not want to use the bad input itself. But you seem to say that by itself it will be faster in the loop ? Is this just by being a set instead of a list ?

@dcoudert
Copy link
Contributor

comment:23

Testing if an element is in a list takes time O(n) while in a set it takes time O(1). Of course, converting a list to a set has a cost, but if you to several test, it's interesting

sage: L = list(range(1000))
sage: S = set(L)
sage: %timeit [i for i in range(10000) if i in L]
10 loops, best of 3: 137 ms per loop
sage: %timeit [i for i in range(10000) if i in S]
1000 loops, best of 3: 877 µs per loop
sage: %timeit S = set(L)
10000 loops, best of 3: 21.3 µs per loop

I agree it's a minor improvement compared to other modifications.

@fchapoton
Copy link
Contributor Author

comment:24

In the quiver.py file, we are mostly dealing with digraphs with a small number of vertices. Probably almost never above 50. So these 'optimizations" are not really useful, I think.

@dcoudert
Copy link
Contributor

comment:25

I agree that it's not really useful, but as you have to convert frozen to set, you just have to do:

nlist = self._nlist = sorted(x for x in data.vertex_iterator() if x not in frozen)
mlist = self._mlist = list(frozen)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 16, 2018

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

f1a9689Merge branch 'u/chapoton/25832' in 8.3.rc1
03972bftrac 25832 more details in quiver.py

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Jul 16, 2018

Changed commit from 976e0f5 to 03972bf

@fchapoton
Copy link
Contributor Author

comment:27

ok, done. Thanks

@dcoudert
Copy link
Contributor

Reviewer: David Coudert

@dcoudert
Copy link
Contributor

comment:28

LGTM

@vbraun
Copy link
Member

vbraun commented Aug 5, 2018

Changed branch from u/chapoton/25832 to 03972bf

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

4 participants