ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 15 May 2021 16:16:05 +0200Integer types and log()https://ask.sagemath.org/question/28679/integer-types-and-log/I am working with a bunch of lists whose lengths are all powers of 2. I'd like to be able to extract the power by taking the base-2 log of the length. However, Sage wasn't able to simplify an expression like log(len(L),2), because apparently len() returns the wrong kind of integer:
sage: A=list(range(8))
sage: len(A)
8
sage: log(len(A),2)
log(8)/log(2)
sage: log(8,2)
3
sage: type(len(A))
<type 'int'>
sage: type(8)
<type 'sage.rings.integer.Integer'>
sage: log(Integer(len(A)),2)
3
This is the first math function I've come across that seems to care about the distinction between these two kinds of integers, and it would be nice if it didn't, since it took me quite a while to figure out why Sage wouldn't simplify an expression like log(len(A),2).Tue, 21 Jul 2015 16:57:56 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/Comment by slelievre for <p>I am working with a bunch of lists whose lengths are all powers of 2. I'd like to be able to extract the power by taking the base-2 log of the length. However, Sage wasn't able to simplify an expression like log(len(L),2), because apparently len() returns the wrong kind of integer:</p>
<pre><code>sage: A=list(range(8))
sage: len(A)
8
sage: log(len(A),2)
log(8)/log(2)
sage: log(8,2)
3
sage: type(len(A))
<type 'int'>
sage: type(8)
<type 'sage.rings.integer.Integer'>
sage: log(Integer(len(A)),2)
3
</code></pre>
<p>This is the first math function I've come across that seems to care about the distinction between these two kinds of integers, and it would be nice if it didn't, since it took me quite a while to figure out why Sage wouldn't simplify an expression like log(len(A),2).</p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=57129#post-id-57129This was fixed at some point (thanks!). In Sage 9.3:
sage: a, b = log(8, 2), log(int(8), 2)
sage: a, b
(3, 3)Sat, 15 May 2021 16:16:05 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=57129#post-id-57129Answer by rws for <p>I am working with a bunch of lists whose lengths are all powers of 2. I'd like to be able to extract the power by taking the base-2 log of the length. However, Sage wasn't able to simplify an expression like log(len(L),2), because apparently len() returns the wrong kind of integer:</p>
<pre><code>sage: A=list(range(8))
sage: len(A)
8
sage: log(len(A),2)
log(8)/log(2)
sage: log(8,2)
3
sage: type(len(A))
<type 'int'>
sage: type(8)
<type 'sage.rings.integer.Integer'>
sage: log(Integer(len(A)),2)
3
</code></pre>
<p>This is the first math function I've come across that seems to care about the distinction between these two kinds of integers, and it would be nice if it didn't, since it took me quite a while to figure out why Sage wouldn't simplify an expression like log(len(A),2).</p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?answer=28682#post-id-28682To get the exponent in a number factor use `valuation`:
sage: valuation(8,2)
3
sage: valuation(65536,2)
16
sage: e=[None]*8
sage: valuation(len(e),2)
3
To get the numerical value of a symbolic expression like `log(8)/log(2)` use `n()`:
sage: log(8)/log(2)
log(8)/log(2)
sage: N(log(8)/log(2))
3.00000000000000
This will be floating point however so better use the first method.
Wed, 22 Jul 2015 08:10:22 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?answer=28682#post-id-28682Comment by Jeremy Martin for <p>To get the exponent in a number factor use <code>valuation</code>:</p>
<pre><code>sage: valuation(8,2)
3
sage: valuation(65536,2)
16
sage: e=[None]*8
sage: valuation(len(e),2)
3
</code></pre>
<p>To get the numerical value of a symbolic expression like <code>log(8)/log(2)</code> use <code>n()</code>:</p>
<pre><code>sage: log(8)/log(2)
log(8)/log(2)
sage: N(log(8)/log(2))
3.00000000000000
</code></pre>
<p>This will be floating point however so better use the first method.</p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28698#post-id-28698Thanks. I did not know about the valuation() function before this.Fri, 24 Jul 2015 23:42:01 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28698#post-id-28698Answer by Nathann for <p>I am working with a bunch of lists whose lengths are all powers of 2. I'd like to be able to extract the power by taking the base-2 log of the length. However, Sage wasn't able to simplify an expression like log(len(L),2), because apparently len() returns the wrong kind of integer:</p>
<pre><code>sage: A=list(range(8))
sage: len(A)
8
sage: log(len(A),2)
log(8)/log(2)
sage: log(8,2)
3
sage: type(len(A))
<type 'int'>
sage: type(8)
<type 'sage.rings.integer.Integer'>
sage: log(Integer(len(A)),2)
3
</code></pre>
<p>This is the first math function I've come across that seems to care about the distinction between these two kinds of integers, and it would be nice if it didn't, since it took me quite a while to figure out why Sage wouldn't simplify an expression like log(len(A),2).</p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?answer=28681#post-id-28681Could you please report this bug by sending a message to sage-devel [1]? You only need say that the following looks weird:
sage: log(int(8),2)
log(8)/log(2)
sage: log(8,2)
3
About your problem: you can easily solve it by explicitly turning len(A) into a Sage integer:
sage: e=[None]*8
sage: log(len(e),2)
log(8)/log(2)
sage: log(Integer(len(e)),2)
3
[1] https://groups.google.com/forum/#!forum/sage-devel Tue, 21 Jul 2015 17:49:21 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?answer=28681#post-id-28681Comment by rws for <p>Could you please report this bug by sending a message to sage-devel [1]? You only need say that the following looks weird:</p>
<pre><code>sage: log(int(8),2)
log(8)/log(2)
sage: log(8,2)
3
</code></pre>
<p>About your problem: you can easily solve it by explicitly turning len(A) into a Sage integer:</p>
<pre><code>sage: e=[None]*8
sage: log(len(e),2)
log(8)/log(2)
sage: log(Integer(len(e)),2)
3
</code></pre>
<p>[1] <a href="https://groups.google.com/forum/#!forum/sage-devel">https://groups.google.com/forum/#!for...</a> </p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28683#post-id-28683The reported behaviour is not a bug. Rather that your method works.Wed, 22 Jul 2015 08:12:44 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28683#post-id-28683Comment by Jeremy Martin for <p>Could you please report this bug by sending a message to sage-devel [1]? You only need say that the following looks weird:</p>
<pre><code>sage: log(int(8),2)
log(8)/log(2)
sage: log(8,2)
3
</code></pre>
<p>About your problem: you can easily solve it by explicitly turning len(A) into a Sage integer:</p>
<pre><code>sage: e=[None]*8
sage: log(len(e),2)
log(8)/log(2)
sage: log(Integer(len(e)),2)
3
</code></pre>
<p>[1] <a href="https://groups.google.com/forum/#!forum/sage-devel">https://groups.google.com/forum/#!for...</a> </p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28697#post-id-28697@Nathann: Done.
@rws: Yes, it is not a bug, and I was able to do what I needed to do, but from an end-user standpoint, it would be better if the log() function did not require the user to distinguish between an integer and a Sage integer - most mathematicians would not think that there could be two kinds of integers!Fri, 24 Jul 2015 23:41:13 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28697#post-id-28697Comment by kcrisman for <p>Could you please report this bug by sending a message to sage-devel [1]? You only need say that the following looks weird:</p>
<pre><code>sage: log(int(8),2)
log(8)/log(2)
sage: log(8,2)
3
</code></pre>
<p>About your problem: you can easily solve it by explicitly turning len(A) into a Sage integer:</p>
<pre><code>sage: e=[None]*8
sage: log(len(e),2)
log(8)/log(2)
sage: log(Integer(len(e)),2)
3
</code></pre>
<p>[1] <a href="https://groups.google.com/forum/#!forum/sage-devel">https://groups.google.com/forum/#!for...</a> </p>
https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28703#post-id-28703I guess Sage tries to hide most integers as Sage `Integer` objects, but there is no easy way to mess with the Python built-in `len` to have it return an `Integer` instead of a Python `int` without probably disturbing a lot of other stuff, unfortunately.Sat, 25 Jul 2015 15:31:31 +0200https://ask.sagemath.org/question/28679/integer-types-and-log/?comment=28703#post-id-28703