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
4

answered 2013-05-29 14:27:15 +0200

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?

edit flag offensive delete link more

Comments

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

Eviatar Bach gravatar imageEviatar Bach ( 2013-06-03 00:51:55 +0200 )edit
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 ( 2013-06-03 04:16:46 +0200 )edit
1

answered 2013-06-03 03:09:57 +0200

this post is marked as community wiki

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

You can clear the screen by simply entering clear, as in a Bash shell.

edit flag offensive delete link more
3

answered 2017-03-20 16:38:28 +0200

tmonteil gravatar image

updated 2017-03-20 16:39:06 +0200

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

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 ( 2017-03-20 16:47:39 +0200 )edit
1

answered 2017-03-20 16:55:50 +0200

tmonteil gravatar image

updated 2017-03-20 16:56:00 +0200

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

Comments

Apparently, 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 :(

tmonteil gravatar imagetmonteil ( 2017-03-20 16:57:32 +0200 )edit
5

answered 2020-10-15 08:47:12 +0200

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