daily WTF?

132 views
Skip to first unread message

William Stein

unread,
Aug 20, 2016, 5:51:42 AM8/20/16
to sage-devel
At Sage Days a new developer we're onboarding decided to look at some
code in Sage and ran into this:

if sum([G.is_directed(), H.is_directed()]) == 1:
raise ValueError("One graph can not be directed while the
other is not.")

This is line 551 of src/sage/graphs/generic_graph_pyx.pyx:

https://github.com/sagemath/sage/blame/master/src/sage/graphs/generic_graph_pyx.pyx#L551

It seems to be equivalent to

if G.is_directed() != H.is_directed():
raise ValueError("One graph can not be directed while the
other is not.")

Frankly, I'm really embarrassed about this code. What do you guys think?

-- William


--
William (http://wstein.org)

Volker Braun

unread,
Aug 20, 2016, 6:08:01 AM8/20/16
to sage-devel
The many ways of implementing xor...

Incidentally, if there is some global switch to return certificates instead of booleans then the sum version is the correct one ;-)

cir...@gmail.com

unread,
Aug 20, 2016, 8:18:00 AM8/20/16
to sage-devel
The sum version works of course, but I'd favor the other code because it is so much clearer. It's the difference between having to figure out what is intended and being able to see at a glance the meaning of the code.

leif

unread,
Aug 20, 2016, 9:09:34 AM8/20/16
to sage-...@googlegroups.com
Volker Braun wrote:
> The many ways of implementing xor...
>
> Incidentally, if there is some global switch to return certificates
> instead of booleans then the sum version is the correct one ;-)

Besides the perhaps clumsy syntax in Python, it's IMHO ok. Just imagine
we had a list of more than two graphs; actually 'sum(l)==1' should be
'sum(l) not in (0, len(l))'.

Of course there are nicer versions with map(), filter(), any() or all()
etc. (sum() is of course also just an application of reduce(), just
like any() and all() are, but the latter do not necessarily have to
evaluate each list element, so can be faster in the average case.)


-leif

P.S.: Wouldn't it make more sense to raise a TypeError? After all,
directed and undirected graphs are different structures.

Dima Pasechnik

unread,
Aug 20, 2016, 9:47:14 AM8/20/16
to sage-devel


On Saturday, August 20, 2016 at 2:09:34 PM UTC+1, leif wrote:
Volker Braun wrote:
> The many ways of implementing xor...
>
> Incidentally, if there is some global switch to return certificates
> instead of booleans then the sum version is the correct one ;-)

Besides the perhaps clumsy syntax in Python, it's IMHO ok.  Just imagine
we had a list of more than two graphs;  actually 'sum(l)==1' should be
'sum(l) not in (0, len(l))'.

Of course there are nicer versions with map(), filter(), any() or all()
etc.  (sum() is of course also just an application of reduce(), just
like any() and all() are, but the latter do not necessarily have to
evaluate each list element, so can be faster in the average case.)


-leif

P.S.:  Wouldn't it make more sense to raise a TypeError?  After all,
directed and undirected graphs are different structures.

they are different, but there are canonical ways to convert one into the other.
directed => undirected -- forget the direction;
undirected => directed -- replace every edge {a,b} by a pair of arcs (a,b),(b,a)

So it's an implementation detail, one could instead agree on a conversion done
automatically.

leif

unread,
Aug 20, 2016, 12:39:17 PM8/20/16
to sage-...@googlegroups.com
Not really. Undirected graphs are a special case of directed graphs,
and even more so with edge labeling.

I don't even know whether Sage considers the empty graph (and graphs
without edges) directed, undirected, both, or none of that...

(I *guess* that mainly depends on how you construct them, so again a
matter of types or classes.)


-leif

leif

unread,
Aug 20, 2016, 12:45:08 PM8/20/16
to sage-...@googlegroups.com
P.S.: So you think for now, we should raise NotImplementedError instead?


-leif

Robert Dodier

unread,
Aug 22, 2016, 11:47:37 PM8/22/16
to sage-...@googlegroups.com
On 2016-08-20, Volker Braun <vbrau...@gmail.com> wrote:

> The many ways of implementing xor...
>
> Incidentally, if there is some global switch to return certificates instead
> of booleans then the sum version is the correct one ;-)

I don't understand this point, but it sounds interesting. Can you
explain?

best

Robert Dodier

Reply all
Reply to author
Forward
0 new messages