Ticket #3750 (closed defect: fixed)
[with patch, positive review] Request for a "log" function for Sage integers
| Reported by: | ljpk | Owned by: | kcrisman |
|---|---|---|---|
| Priority: | minor | Milestone: | sage-3.2 |
| Component: | basic arithmetic | Keywords: | |
| Cc: | Work issues: | ||
| Report Upstream: | Reviewers: | ||
| Authors: | Merged in: | ||
| Dependencies: | Stopgaps: |
Description
The following command
sage: N=8 sage: N.log(2) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/ljpk/<ipython console> in <module>() AttributeError: 'sage.rings.integer.Integer' object has no attribute 'log'
returns an error (as does N.exp()). Would it be possible to add a function to the Sage integers class which worked like the ones for the real numbers?
sage: N=8.0 sage: N.log(2) 3.00000000000000 sage: N.exp() 2980.95798704173
Attachments
Change History
comment:1 Changed 5 years ago by kcrisman
- Owner changed from somebody to kcrisman
- Status changed from new to assigned
- Summary changed from Request for a "log" function for Sage integers to [with patch, needs review] Request for a "log" function for Sage integers
comment:2 follow-up: ↓ 4 Changed 5 years ago by mhansen
- Summary changed from [with patch, needs review] Request for a "log" function for Sage integers to [with patch, needs work] Request for a "log" function for Sage integers
I think that log should default to base 'e' as it does with all the other types. Also both of them should probably take an optional number of bits of precision to use with the default being 53.
comment:3 Changed 5 years ago by was
Somebody should point out the exact_log function in this discussion since it is relevant to the original poster and is superfast:
sage: N = 8 sage: N.exact_log(2) 3
comment:4 in reply to: ↑ 2 Changed 5 years ago by kcrisman
Replying to mhansen:
I think that log should default to base 'e' as it does with all the other types. Also both of them should probably take an optional number of bits of precision to use with the default being 53.
Know what, I don't think I even realized that m was mandatory the way I wrote it - yes, obviously it should default to natural log. I like the idea of specifying precision in the function itself as well - good comments.
comment:5 Changed 5 years ago by kcrisman
The patch now should deal with both of the reviewer's comments.
comment:6 Changed 5 years ago by kcrisman
- Summary changed from [with patch, needs work] Request for a "log" function for Sage integers to [with patch, needs review] Request for a "log" function for Sage integers
comment:7 Changed 5 years ago by mhansen
Hmm... there's an issue that before log(3) would just give log(3) since 3 is exact. After the patch, log(3) will automatically give an approximation which probably isn't desired.
comment:8 Changed 5 years ago by was
Mike, Good point about log(3). This should be dealt with the same way as with sqrt:
sage: 3.sqrt() sqrt(3) sage: 3.sqrt(prec=53) 1.73205080756888
comment:9 Changed 5 years ago by kcrisman
- Milestone changed from sage-3.2.1 to sage-3.2
As long as everyone is okay with
sage: log(1024, 2) 10
instead of the previous behavior of
sage: log(1024, 2) log(1024)/log(2)
then the last version of the patch should address all concerns raised above. Thanks to mhampton for sleuthing down that one must use for exponentiation outside the interpreter when I got confused.
comment:10 Changed 5 years ago by mhansen
- Summary changed from [with patch, needs review] Request for a "log" function for Sage integers to [with patch, positive review] Request for a "log" function for Sage integers
Nice work! I'm pretty happy with how this turned out.
comment:11 Changed 5 years ago by mabshoff
- Summary changed from [with patch, positive review] Request for a "log" function for Sage integers to [with patch, needs work] Request for a "log" function for Sage integers
- Milestone changed from sage-3.2 to sage-3.2.1
When I merge integer-log-exp-final.patch I get one doctest failure:
mabshoff@sage:/scratch/mabshoff/release-cycle/sage-3.2.alpha3$ ./sage -t -long devel/sage/sage/coding/linear_code.py
sage -t -long devel/sage/sage/coding/linear_code.py
**********************************************************************
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/linear_code.py", line 1123:
sage: Cc = C.galois_closure(GF(2))
Exception raised:
Traceback (most recent call last):
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/doctest.py", line 1228, in __run
compileflags, 1) in test.globs
File "<doctest __main__.example_21[3]>", line 1, in <module>
Cc = C.galois_closure(GF(Integer(2)))###line 1123:
sage: Cc = C.galois_closure(GF(2))
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/site-packages/sage/coding/linear_code.py", line 1141, in galois_closure
if not(a.integer_part() == a):
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'integer_part'
**********************************************************************
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/linear_code.py", line 1124:
sage: C; Cc
Exception raised:
Traceback (most recent call last):
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/doctest.py", line 1228, in __run
compileflags, 1) in test.globs
File "<doctest __main__.example_21[4]>", line 1, in <module>
C; Cc###line 1124:
sage: C; Cc
NameError: name 'Cc' is not defined
**********************************************************************
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/linear_code.py", line 1132:
sage: c2 in Cc
Exception raised:
Traceback (most recent call last):
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/doctest.py", line 1228, in __run
compileflags, 1) in test.globs
File "<doctest __main__.example_21[9]>", line 1, in <module>
c2 in Cc###line 1132:
sage: c2 in Cc
NameError: name 'Cc' is not defined
**********************************************************************
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/linear_code.py", line 1541:
sage: Cc = C.galois_closure(GF(2))
Exception raised:
Traceback (most recent call last):
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/doctest.py", line 1228, in __run
compileflags, 1) in test.globs
File "<doctest __main__.example_35[3]>", line 1, in <module>
Cc = C.galois_closure(GF(Integer(2)))###line 1541:
sage: Cc = C.galois_closure(GF(2))
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/site-packages/sage/coding/linear_code.py", line 1141, in galois_closure
if not(a.integer_part() == a):
AttributeError: 'sage.rings.integer.Integer' object has no attribute 'integer_part'
**********************************************************************
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/linear_code.py", line 1547:
sage: c2 in Cc
Exception raised:
Traceback (most recent call last):
File "/scratch/mabshoff/release-cycle/sage-3.2.alpha3/local/lib/python2.5/doctest.py", line 1228, in __run
compileflags, 1) in test.globs
File "<doctest __main__.example_35[8]>", line 1, in <module>
c2 in Cc###line 1547:
sage: c2 in Cc
NameError: name 'Cc' is not defined
**********************************************************************
2 items had failures:
3 of 10 in __main__.example_21
2 of 9 in __main__.example_35
***Test Failed*** 5 failures.
For whitespace errors, see the file /scratch/mabshoff/release-cycle/sage-3.2.alpha3/tmp/.doctest_linear_code.py
[30.6 s]
exit code: 1024
Cheers,
Michael
comment:12 Changed 5 years ago by mhansen
I've attached a patch which fixes the issue.
comment:13 Changed 5 years ago by kcrisman
- Summary changed from [with patch, needs work] Request for a "log" function for Sage integers to [with patch, positive review] Request for a "log" function for Sage integers
This works! I was going to use q.is_power_of(q0), but as far as I can tell there isn't any meaningful difference between these solutions. Use integer-log-exp-final.patch and trac_3750-fix.patch. I assume I can review mhansen's fix; if not, my apologies.
To be honest, how was this working before, when the symbolic logs didn't have an integer_part() method to begin with?
comment:14 Changed 5 years ago by mabshoff
- Status changed from assigned to closed
- Resolution set to fixed
- Milestone changed from sage-3.2.1 to sage-3.2
Merged integer-log-exp-final.patch and trac_3750-fix.patch in Sage 3.2.rc0.
Cheers,
Michael

