Ask Your Question
17

Hidden features of Sage

asked 2013-04-29 01:09:50 +0200

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.

edit retag flag offensive close merge delete

15 Answers

Sort by » oldest newest most voted
2

answered 2013-04-29 01:11:50 +0200

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
edit flag offensive delete link more
13

answered 2013-04-29 01:17:51 +0200

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)
edit flag offensive delete link more
1

answered 2013-04-29 01:48:17 +0200

this post is marked as community wiki

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

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

edit flag offensive delete link more

Comments

1

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

Eviatar Bach gravatar imageEviatar Bach ( 2013-04-29 02:06:38 +0200 )edit
1

answered 2013-04-29 01:49:56 +0200

this post is marked as community wiki

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

Sébastien Labbé has a blog post with various Sage tricks.

edit flag offensive delete link more
1

answered 2013-04-29 05:13:24 +0200

this post is marked as community wiki

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

Though is general for python, it is always worth reading: Code like a Pythonista

edit flag offensive delete link more
6

answered 2013-05-16 08:31:06 +0200

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)"
edit flag offensive delete link more
8

answered 2013-05-16 08:32:41 +0200

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
edit flag offensive delete link more

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 ( 2016-09-12 11:11:43 +0200 )edit
3

answered 2013-05-16 10:00:10 +0200

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
edit flag offensive delete link more
3

answered 2013-05-20 16:25:42 +0200

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.

edit flag offensive delete link more
5

answered 2013-05-28 16:24:02 +0200

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
edit flag offensive delete link more

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: 2013-04-29 01:09:50 +0200

Seen: 3,568 times

Last updated: Oct 15 '20