ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 09 Aug 2018 02:53:11 -0500Hidden features of Sagehttp://ask.sagemath.org/question/10073/hidden-features-of-sage/In the spirit of the [StackOverflow threads of "hidden" language features](http://stackoverflow.com/questions/101268/hidden-features-of-python), we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.Sun, 28 Apr 2013 18:09:50 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14860#post-id-14860To preview LaTeX code from the command line, use the following:
show(LatexExpr(r'\frac{3}{4}x + 3'))
From the notebook,
html(r'$\frac{3}{4}x + 3$')
also works.Sun, 28 Apr 2013 18:48:17 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14860#post-id-14860Comment by Eviatar Bach for <p>To preview LaTeX code from the command line, use the following:</p>
<pre><code>show(LatexExpr(r'\frac{3}{4}x + 3'))
</code></pre>
<p>From the notebook,</p>
<pre><code>html(r'$\frac{3}{4}x + 3$')
</code></pre>
<p>also works.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17792#post-id-17792By the way, it would be useful (if it doesn't already exist) to have a function to render LaTeX code to an image just large enough to fit the expression, for export to environments that do not support LaTeX (an HTML website, for example).Sun, 28 Apr 2013 19:06:38 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17792#post-id-17792Answer by Therfory for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=43293#post-id-43293Good articleThu, 09 Aug 2018 02:53:11 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=43293#post-id-43293Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=37007#post-id-37007The function get_systems from sage.misc.citation will tell you the backends used to evaluate some code. For example,
sage: from sage.misc.citation import get_systems
sage: get_systems('sqrt(x)')
['MPFI', 'MPFR', 'GMP']
sage: get_systems('sqrt(3.4)')
['MPFR']Mon, 20 Mar 2017 10:55:50 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=37007#post-id-37007Comment by tmonteil for <p>The function get_systems from sage.misc.citation will tell you the backends used to evaluate some code. For example,</p>
<pre><code>sage: from sage.misc.citation import get_systems
sage: get_systems('sqrt(x)')
['MPFI', 'MPFR', 'GMP']
sage: get_systems('sqrt(3.4)')
['MPFR']
</code></pre>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37008#post-id-37008Apparently, this question disapeared, i repost it though i was not its original author. Sorry for not making this answer an anonymous wiki as it should be. In the recent versions of askbot, only one answer per user is allowed, so i had to trick by first writing a comment, and then transform it into an answer, but that way i could not make it anonymous :(Mon, 20 Mar 2017 10:57:32 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37008#post-id-37008Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=37005#post-id-37005The function [sage_input](http://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_input.html) (sometimes) alows to get the Sage code to reconstruct an object you like.
sage: m = random_matrix(ZZ,3)
sage: m
[-1 1 -1]
[-2 -2 -1]
[ 1 -5 0]
I like that one, how could i reconstruct it ?
sage: sage_input(m)
matrix(ZZ, [[-1, 1, -1], [-2, -2, -1], [1, -5, 0]])
sage: m == eval(str(sage_input(m)))
True
Another example:
sage: e = m.eigenvalues()[0]
sage: sage_input(e)
R.<x> = QQbar[]
QQbar.polynomial_root(AA.common_polynomial(x^3 + 3*x^2 + 8), CIF(RIF(-RR(3.6128878647175449), -RR(3.6128878647175444)), RIF(RR(0))))
sage: sage_input(e, preparse=False)
R = QQbar['x']
x = R.gen()
QQbar.polynomial_root(AA.common_polynomial(x**3 + 3*x**2 + 8), CIF(RIF(-RR(3.6128878647175449), -RR(3.6128878647175444)), RIF(RR(0))))Mon, 20 Mar 2017 10:38:28 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=37005#post-id-37005Comment by tmonteil for <p>The function <a href="http://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_input.html">sage_input</a> (sometimes) alows to get the Sage code to reconstruct an object you like.</p>
<pre><code>sage: m = random_matrix(ZZ,3)
sage: m
[-1 1 -1]
[-2 -2 -1]
[ 1 -5 0]
</code></pre>
<p>I like that one, how could i reconstruct it ?</p>
<pre><code>sage: sage_input(m)
matrix(ZZ, [[-1, 1, -1], [-2, -2, -1], [1, -5, 0]])
sage: m == eval(str(sage_input(m)))
True
</code></pre>
<p>Another example:</p>
<pre><code>sage: e = m.eigenvalues()[0]
sage: sage_input(e)
R.<x> = QQbar[]
QQbar.polynomial_root(AA.common_polynomial(x^3 + 3*x^2 + 8), CIF(RIF(-RR(3.6128878647175449), -RR(3.6128878647175444)), RIF(RR(0))))
sage: sage_input(e, preparse=False)
R = QQbar['x']
x = R.gen()
QQbar.polynomial_root(AA.common_polynomial(x**3 + 3*x**2 + 8), CIF(RIF(-RR(3.6128878647175449), -RR(3.6128878647175444)), RIF(RR(0))))
</code></pre>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37006#post-id-37006Sorry for not making this answer an anonymous wiki. In the recent versions of askbot, only one answer per user is allowed, so i had to trick by firts writing a comment, and then transform it into an answer, but that way i could not make it anonymous :(Mon, 20 Mar 2017 10:47:39 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37006#post-id-37006Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14861#post-id-14861Sébastien Labbé has a [blog post](http://www.slabbe.org/blogue/2012/12/some-small-sage-tricks/) with various Sage tricks.Sun, 28 Apr 2013 18:49:56 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14861#post-id-14861Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14934#post-id-14934The `import_statements()` function, which allows to know what import statement should we do to use an object.
sage: import_statements('RDF')
from sage.rings.real_double import RDF
Thu, 16 May 2013 01:32:41 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14934#post-id-14934Comment by mariakatosvich for <p>The <code>import_statements()</code> function, which allows to know what import statement should we do to use an object.</p>
<pre><code>sage: import_statements('RDF')
from sage.rings.real_double import RDF
</code></pre>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=34798#post-id-34798By the way, it would be [useful](http://www.faqtory.co/sky/) (if it doesn't already exist) to have a function to render LaTeX code to an image just large enough to fit the expressionMon, 12 Sep 2016 04:11:43 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=34798#post-id-34798Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=15081#post-id-15081The function `get_systems` from `sage.misc.citation` will tell you the backends used to evaluate some code. For example,
sage: get_systems('sqrt(x)')
['ginac']
sage: get_systems('sqrt(3.4)')
['MPFR']Thu, 13 Jun 2013 16:19:56 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=15081#post-id-15081Comment by tmonteil for <p>The function <code>get_systems</code> from <code>sage.misc.citation</code> will tell you the backends used to evaluate some code. For example,</p>
<pre><code>sage: get_systems('sqrt(x)')
['ginac']
sage: get_systems('sqrt(3.4)')
['MPFR']
</code></pre>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37004#post-id-37004The function [sage_input](http://doc.sagemath.org/html/en/reference/misc/sage/misc/sage_input.html) (sometimes) alows to get the Sage code to reconstruct an object you like.
sage: m = random_matrix(ZZ,3)
sage: m
[-1 1 -1]
[-2 -2 -1]
[ 1 -5 0]
I like that one, how could i reconstruct it ?
sage: sage_input(m)
matrix(ZZ, [[-1, 1, -1], [-2, -2, -1], [1, -5, 0]])
sage: m == eval(str(sage_input(m)))
True
Another example:
sage: e = m.eigenvalues()[0]
sage: sage_input(e)
R.<x> = QQbar[]
QQbar.polynomial_root(AA.common_polynomial(x^3 + 3*x^2 + 8), CIF(RIF(-RR(3.6128878647175449), -RR(3.6128878647175444)), RIF(RR(0))))
sage: sage_input(e, preparse=False)
R = QQbar['x']
x = R.gen()
QQbar.polynomial_root(AA.common_poMon, 20 Mar 2017 10:36:46 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=37004#post-id-37004Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14981#post-id-14981It is possible to delete user-defined variables, and reset Sage variables back to their default:
sage: a = 1 ; a
1
sage: reset()
sage: a
NameError: name 'a' is not defined
It is also possible to reset only a few things:
sage: a = b = c = 1
sage: reset(['a','b'])
sage: c
1
Tue, 28 May 2013 09:24:02 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14981#post-id-14981Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14933#post-id-14933The `preparser()` function which allows to understand differences between Python and Sage parsers, and between `.py` and `.sage` files:
sage: 2^2
4
sage: preparser(False)
sage: 2^2
0
sage: 2**2
4
Conversely, the `preparse()` function tells you how Sage preparses the input:
sage: preparse('1.0+2^2')
"RealNumber('1.0')+Integer(2)**Integer(2)"
Thu, 16 May 2013 01:31:06 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14933#post-id-14933Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14859#post-id-14859In the command line, the underscore is a variable holding the result of the last output. This is very useful, e.g., for the following:
sage: integrate(cos(x), x)
sin(x)
sage: diff(_, x)
cos(x)Sun, 28 Apr 2013 18:17:51 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14859#post-id-14859Answer by vdelecroix for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14985#post-id-14985A very hidden feature of Python!
For faster code using itertools you may delete the reference at the end of the loop
sage: from itertools import combinations
sage: sage: timeit('for p in combinations(range(18),5): pass')
625 loops, best of 3: 615 µs per loop
sage: sage: timeit('for p in combinations(range(18),5): del p')
625 loops, best of 3: 322 µs per loop
The reason is that the iterators in itertools recycle the objects they return if they are the only one to reference it! But is it relevant to optimize Python code?Wed, 29 May 2013 07:27:15 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14985#post-id-14985Comment by vdelecroix for <p>A very hidden feature of Python!</p>
<p>For faster code using itertools you may delete the reference at the end of the loop</p>
<pre><code>sage: from itertools import combinations
sage: sage: timeit('for p in combinations(range(18),5): pass')
625 loops, best of 3: 615 µs per loop
sage: sage: timeit('for p in combinations(range(18),5): del p')
625 loops, best of 3: 322 µs per loop
</code></pre>
<p>The reason is that the iterators in itertools recycle the objects they return if they are the only one to reference it! But is it relevant to optimize Python code?</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17586#post-id-17586Actually not ! You have to read the sources of Python to discover that fact. I wrote few months ago to the developer of itertools which told me that it was a minor speedup (if you want speed you will not use Python).Sun, 02 Jun 2013 21:16:46 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17586#post-id-17586Comment by Eviatar Bach for <p>A very hidden feature of Python!</p>
<p>For faster code using itertools you may delete the reference at the end of the loop</p>
<pre><code>sage: from itertools import combinations
sage: sage: timeit('for p in combinations(range(18),5): pass')
625 loops, best of 3: 615 µs per loop
sage: sage: timeit('for p in combinations(range(18),5): del p')
625 loops, best of 3: 322 µs per loop
</code></pre>
<p>The reason is that the iterators in itertools recycle the objects they return if they are the only one to reference it! But is it relevant to optimize Python code?</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17587#post-id-17587Wow, this is really cool! Is there any documentation anywhere for this?Sun, 02 Jun 2013 17:51:55 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17587#post-id-17587Answer by tmonteil for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14936#post-id-14936To know whether Sage is running from the notebook or the command line, use the `misc.embedded()` function:
sage: misc.embedded()
False
Thu, 16 May 2013 03:00:10 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14936#post-id-14936Answer by Jesustc for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14827#post-id-14827Though is general for python, it is always worth reading: [Code like a Pythonista](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html)Sun, 28 Apr 2013 22:13:24 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14827#post-id-14827Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14858#post-id-14858Symbolic variables can be created from the command line as follows, faster than typing out `var('x y z')`:
,var x y z
Sun, 28 Apr 2013 18:11:50 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14858#post-id-14858Answer by vdelecroix for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14951#post-id-14951Locally disable the preparser with the suffix r
It's not really disabling the preparser (the input is still preparsed), but telling the preparser not to process some of the (numerical) input by marking this input as raw (by appending the letter r).
sage: type(12)
<type 'sage.rings.integer.Integer'>
sage: type(12r)
<type 'int'>
sage: type(42.42)
<type 'sage.rings.real_mpfr.RealLiteral'>
sage: type(42.42r)
<type 'float'>
Also works for Python complex numbers:
sage: type(1j)
<type 'sage.rings.complex_number.ComplexNumber'>
sage: type(1jr)
<type 'complex'>
It's a bit similar to specifying some strings as raw by prepending an r to `'...'` or `"..."` or `'''...'''` or `"""..."""`; for instance in `'\t'` the backslash-t produces a tab, but in `r'\t'` it stays backslash-t.Mon, 20 May 2013 09:25:42 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14951#post-id-14951Answer by Eviatar Bach for <p>In the spirit of the <a href="http://stackoverflow.com/questions/101268/hidden-features-of-python">StackOverflow threads of "hidden" language features</a>, we can use this thread (community wiki) to aggregate useful but little-known features or tricks of Sage. Perhaps these can be collected and added to the documentation in the future.</p>
http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=15007#post-id-15007You can clear the screen by simply entering `clear`, as in a Bash shell.Sun, 02 Jun 2013 20:09:57 -0500http://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=15007#post-id-15007