Comparison and logarithm on .py vs. interactive

63 views
Skip to first unread message

Jori Mäntysalo

unread,
Aug 26, 2016, 3:08:36 AM8/26/16
to sage-...@googlegroups.com
sage: def foo():
....: from sage.combinat.posets.poset_examples import Posets
....: from sage.misc.functional import log
....: P = Posets.BooleanLattice(3); n = P.cardinality(); s = P._hasse_diagram.size()
....: if 2*s > n*log(n, 2): print "True"
....:
sage: foo()
sage:

So, in this case the comparison is False. Now when I add exactly same
function on end of lattices.py and compile I got

sage: sage.combinat.posets.lattices.foo()
True
sage:

What's wrong? Somehow comparison and logaritms don't mix (again).

* * *

Is there a function to get 2-based logarithm of integer in Sage? In Python
3.3 there is, but we use Python 2.x.

--
Jori Mäntysalo

leif

unread,
Aug 26, 2016, 5:26:18 AM8/26/16
to sage-...@googlegroups.com
Jori Mäntysalo wrote:
> sage: def foo():
> .....: from sage.combinat.posets.poset_examples import Posets
> .....: from sage.misc.functional import log
> .....: P = Posets.BooleanLattice(3); n = P.cardinality(); s =
> P._hasse_diagram.size()
> .....: if 2*s > n*log(n, 2): print "True"
> .....:
> sage: foo()
> sage:
>
> So, in this case the comparison is False. Now when I add exactly same
> function on end of lattices.py and compile I got
>
> sage: sage.combinat.posets.lattices.foo()
> True
> sage:
>
> What's wrong? Somehow comparison and logaritms don't mix (again).

Apparently log() behaves differently depending on whether it is called
with Sage Integers or Python ints. (Not sure whether the other integer
literal also matters here.)

(You should get the same in the Sage session when using 2r instead of 2,
or Integer(2) instead of 2 in the Python file.)


-leif

Jori Mäntysalo

unread,
Aug 26, 2016, 5:28:59 AM8/26/16
to sage-...@googlegroups.com
On Fri, 26 Aug 2016, leif wrote:

> Apparently log() behaves differently depending on whether it is called
> with Sage Integers or Python ints. (Not sure whether the other integer
> literal also matters here.)
>
> (You should get the same in the Sage session when using 2r instead of 2,
> or Integer(2) instead of 2 in the Python file.)

Tested with 2r on Sage session, makes no difference.

--
Jori Mäntysalo

leif

unread,
Aug 26, 2016, 5:45:46 AM8/26/16
to sage-...@googlegroups.com
Hmmm, does Posets.BooleanLattice() care whether you pass an int or an
Integer? (I.e., does the return type of its methods change?)


-leif


Jori Mäntysalo

unread,
Aug 26, 2016, 5:47:10 AM8/26/16
to sage-...@googlegroups.com
On Fri, 26 Aug 2016, leif wrote:

> Hmmm, does Posets.BooleanLattice() care whether you pass an int or an
> Integer? (I.e., does the return type of its methods change?)

No:

sage: P = Posets.BooleanLattice(3)
sage: Q = Posets.BooleanLattice(3r)
sage: P == Q
True
sage: P is Q
True

--
Jori Mäntysalo

leif

unread,
Aug 26, 2016, 5:55:10 AM8/26/16
to sage-...@googlegroups.com
I rather meant e.g. type(P.cardinality()), but since 'P is Q' holds,
they won't differ.

No idea...


-leif


leif

unread,
Aug 26, 2016, 10:01:19 AM8/26/16
to sage-...@googlegroups.com
For me, it /does/ make a difference (using Python vs. Sage literals):

$ ./sage
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 7.3, Release Date: 2016-08-04 │
│ Type "notebook()" for the browser-based notebook interface. │
│ Type "help()" for help. │
└────────────────────────────────────────────────────────────────────┘
sage: from sage.combinat.posets.poset_examples import Posets
sage: from sage.misc.functional import log
sage: P = Posets.BooleanLattice(3); n = P.cardinality(); s =
P._hasse_diagram.size()
sage: if 2*s > n*log(n, 2): print "True"
sage: from sage.combinat.posets.poset_examples import Posets
sage: from sage.misc.functional import log
sage: P = Posets.BooleanLattice(3r); n = P.cardinality(); s =
P._hasse_diagram.size()
sage: if 2r*s > n*log(n, 2r): print "True"
True
sage:


And it's caused by this:

sage: type(n*log(n, 2))
<type 'sage.rings.integer.Integer'>
sage: type(n*log(n, 2r))
<type 'sage.symbolic.expression.Expression'>


(FWIW, while .cardinality() returns Integer, ._hasse_diagram.size()
returns int, but that doesn't matter here.)


-leif


Jori Mäntysalo

unread,
Aug 27, 2016, 1:05:28 AM8/27/16
to sage-...@googlegroups.com
On Fri, 26 Aug 2016, leif wrote:

> For me, it /does/ make a difference (using Python vs. Sage literals):

> sage: if 2*s > n*log(n, 2): print "True"

> sage: if 2r*s > n*log(n, 2r): print "True"
> True

OK, I don't know what I did wrong in my test. Now I got the same result.
Thanks!

* * *

But shouldn't it work in any case? I.e. comparison of log(a+b*c^2...) to
some number should work when a,b,c... are sage Integers.

--
Jori Mäntysalo

Ralf Stephan

unread,
Aug 27, 2016, 3:36:04 AM8/27/16
to sage-devel
On Saturday, August 27, 2016 at 7:05:28 AM UTC+2, Jori Mäntysalo wrote:
But shouldn't it work in any case? I.e. comparison of log(a+b*c^2...) to
some number should work when a,b,c... are sage Integers.

log(integer) will not be expanded numerically except for log(0) and log(1).
If you want it expanded, either give a float argument, eg log(2.), or
append n().

leif

unread,
Aug 27, 2016, 11:08:12 AM8/27/16
to sage-...@googlegroups.com
N() wasn't the problem, but (also) rounding:

(s is 12 here.)

sage: n*log(n, 2)
24
sage: n*log(n, 2r)
8*log(8)/log(2)
sage: N(n*log(n, 2r))
24.0000000000000
sage: 2*s < n*log(n, 2)
False
sage: 2*s > n*log(n, 2)
False
sage: 2*s > n*log(n, 2r)
24 > 8*log(8)/log(2)
sage: bool(2*s > n*log(n, 2r))
True
sage: bool(2r*s > n*log(n, 2r))
True
sage: bool(2r*s > n*log(n, 2))
False
sage: 24 > N(n*log(n, 2r))
True


-leif


Ralf Stephan

unread,
Aug 29, 2016, 10:21:36 AM8/29/16
to sage-devel
Reply all
Reply to author
Forward
0 new messages