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.Thu, 15 Oct 2020 08:47:12 +0200Hidden features of Sagehttps://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.Mon, 29 Apr 2013 01:09:50 +0200https://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>
https://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.Mon, 29 Apr 2013 01:49:56 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14861#post-id-14861Answer 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>
https://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.Mon, 29 Apr 2013 01:48:17 +0200https://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>
https://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).Mon, 29 Apr 2013 02:06:38 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17792#post-id-17792Answer 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>
https://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 16:55:50 +0100https://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>
https://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 16:57:32 +0100https://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>
https://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 16:38:28 +0100https://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>
https://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 16:47:39 +0100https://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>
https://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.Mon, 03 Jun 2013 03:09:57 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=15007#post-id-15007Answer 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>
https://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 16:25:42 +0200https://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>
https://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
Mon, 29 Apr 2013 01:11:50 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14858#post-id-14858Answer 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>
https://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)Mon, 29 Apr 2013 05:13:24 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14827#post-id-14827Answer 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>
https://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 08:32:41 +0200https://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>
https://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 11:11:43 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=34798#post-id-34798Answer 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>
https://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 14:27:15 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14985#post-id-14985Comment 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>
https://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?Mon, 03 Jun 2013 00:51:55 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17587#post-id-17587Comment 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>
https://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).Mon, 03 Jun 2013 04:16:46 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?comment=17586#post-id-17586Answer 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>
https://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 10:00:10 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14936#post-id-14936Answer 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>
https://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 08:31:06 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14933#post-id-14933Answer 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>
https://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 16:24:02 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14981#post-id-14981Answer by philipp7 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>
https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=53924#post-id-53924To create a tuple of variables x0,...,xk you can use `var("x", n=k)`, e.g.
sage: var("x", n=10)
(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)Thu, 15 Oct 2020 08:47:12 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=53924#post-id-53924Answer 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>
https://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)Mon, 29 Apr 2013 01:17:51 +0200https://ask.sagemath.org/question/10073/hidden-features-of-sage/?answer=14859#post-id-14859