First time here? Check out the FAQ!

Ask Your Question
17

Hidden features of Sage

asked 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

In the spirit of the StackOverflow threads of "hidden" language features, 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.

Preview: (hide)

15 Answers

Sort by » oldest newest most voted
13

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

In 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)
Preview: (hide)
link
8

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

The 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
Preview: (hide)
link

Comments

By the way, it would be http://www.faqtory.co/sky/ (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

mariakatosvich gravatar imagemariakatosvich ( 8 years ago )
6

answered 4 years ago

philipp7 gravatar image

To 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)
Preview: (hide)
link
6

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

The 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)"
Preview: (hide)
link
5

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

It 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
Preview: (hide)
link
4

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

A 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?

Preview: (hide)
link

Comments

Wow, this is really cool! Is there any documentation anywhere for this?

Eviatar Bach gravatar imageEviatar Bach ( 11 years ago )
1

Actually 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).

vdelecroix gravatar imagevdelecroix ( 11 years ago )
3

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Locally 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.

Preview: (hide)
link
3

answered 8 years ago

tmonteil gravatar image

updated 8 years ago

The function sage_input (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))))
Preview: (hide)
link

Comments

Sorry 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 :(

tmonteil gravatar imagetmonteil ( 8 years ago )
3

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

To know whether Sage is running from the notebook or the command line, use the misc.embedded() function:

sage: misc.embedded()
False
Preview: (hide)
link
2

answered 11 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Symbolic variables can be created from the command line as follows, faster than typing out var('x y z'):

,var x y z
Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

3 followers

Stats

Asked: 11 years ago

Seen: 4,427 times

Last updated: Oct 15 '20