Sage Trucos

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Archivo: /home/jgarcia/Dropbox/sage/sage.

trucos Pgina 1 de 4

Constantes
----------
http://doc.sagemath.org/html/en/reference/constants/sage/symbolic/constants.html

Sage tiene definidas varias constantes, p.e.:

sage: pi
pi
sage: e # base of the natural logarithm
e
sage: NaN # Not a number
NaN
sage: golden_ratio
golden_ratio
sage: log2 # natural logarithm of the real number 2
log2
sage: euler_gamma # Euler's gamma constant
euler_gamma
sage: catalan # the Catalan constant
catalan
sage: khinchin # Khinchin's constant
khinchin
sage: twinprime
twinprime
sage: mertens
mertens

Que se pueden utilizar en los clculos sin problemas

sage: a = pi + e*4/5; a
pi + 4/5*e

Pero si queremos los resultados en nmeros hay que hacer:

sage: RealField(15)(a) # 15 *bits* of precision


5.316

Interpolacin
-------------
http://sage.unex.es/home/pub/61/

SageTex: Tables with a random number of columns/rows

http://www.highschoolmathandchess.com/2014/11/13/sagetex-tables-with-a-random-number-of-columnsrows/

A tutorial for anyone wanting to use SageMathCloud for teaching

https://github.com/mikecroucher/SMC_tutorial

Ejecutar un script de sagemath


------------------------------
http://pub.ist.ac.at/~jguzman/doc/programming/Sage/script.html

This document describes the different ways to use Sage in console mode (i.e without the notebook
option).
Execute a Sage script from Sage

The easiest way is to write a Sage script to run it using Sage itself. A Sage script has the file
extension .sage. If you want, for example, evaluate the following script:

"""
simple.sage
this is a simple sage script
"""

f(x) = x**2
Archivo: /home/jgarcia/Dropbox/sage/sage.trucos Pgina 2 de 4

print f(2)

def solve_square(n):
""" returns the value of f(x) in
f(x) = x**2
"""
return f(n)

now to run it simply type:

>>> sage simple.sage


>>> 4

Import Sage scripts from Sage

You can simply use the sage sesion to load the script and have all the available functions in your
shell.

>>> sage
sage: load("simple.sage")
sage: solve_square(2)
sage: 4

we can use the attach command to link the file to our Sage session. In that way, we do not need to
reload the file every time we make a change in the file.

sage: attach(simple.sage)

Import Sage scripts from Python

This is if we want to use Sage scripts in the Python shell. For that we need:

We have to run the Python version that is bundled with Sage.


To use any part of Sage we absolutely must import sage.all.

We can call sage with the -ipython option and then.

>>> from sage.all import *


>>> load("simple.sage")
>>> solve_square(2)
>>> 4

Again, we can use the attach command here.

Import Python scripts from Sage

To load a Python module simply from the Sage console:

>>> sage: load(simple.py)

And to reload type:

>>> sage: attach("simply.py")

Now, everytime that you edit the file simply.py, the changes will be updated without loading againg.

Using IPython notebook from Sage

You sometimes need uuid-dev:

>>> sudo apt-get install uuid-dev

After that apply the following steps:

Install tornado

>>> sage -sh


>>> easy_install-2.7 tornado

Install zeromp and pyzmq:

>>> sage -i zeromp-2.2.0.p0


Archivo: /home/jgarcia/Dropbox/sage/sage.trucos Pgina 3 de 4

>>> sage -i pyzmq-2.1.11.p1

Now you can execute Sage with the IPython notebook option

>>> sage -ipython notebook

Salir de sage
-------------
http://doc.sagemath.org/html/en/tutorial/interactive_shell.html

To quit Sage either press Ctrl-D or type quit or exit.

Usando el shell dentro de sage


------------------------------
http://doc.sagemath.org/html/en/tutorial/interactive_shell.html

When using the interactive shell, any UNIX shell command can be executed from Sage by prefacing it by
an exclamation point !. For example,

sage: !ls
auto example.sage glossary.tex t tmp tut.log tut.tex

returns the listing of the current directory.

Salvar y cargar sesiones completas


----------------------------------
http://doc.sagemath.org/html/en/tutorial/interactive_shell.html#section-save

The command save_session(sessionname) saves all the variables youve defined in the current session as
a dictionary in the given sessionname. (In the rare case when a variable does not support saving, it
is simply not saved to the dictionary.) The resulting file is an .sobj file and can be loaded just
like any other object that was saved. When you load the objects saved in a session, you get a
dictionary whose keys are the variables names and whose values are the objects.

You can use the load_session(sessionname) command to load the variables defined in sessionname into
the current session. Note that this does not wipe out variables youve already defined in your current
session; instead, the two sessions are merged.

First we start Sage and define some variables.

sage: E = EllipticCurve('11a')
sage: M = ModularSymbols(37)
sage: a = 389
sage: t = M.T(2003).matrix(); t.charpoly().factor()
_4 = (x - 2004) * (x - 12)^2 * (x + 54)^2

Next we save our session, which saves each of the above variables into a file. Then we view the file,
which is about 3K in size.

sage: save_session('misc')
Saving a
Saving M
Saving t
Saving E
sage: quit
was@form:~/tmp$ ls -l misc.sobj
-rw-r--r-- 1 was was 2979 2006-01-28 19:47 misc.sobj

Finally we restart Sage, define an extra variable, and load our saved session.

sage: b = 19
sage: load_session('misc')
Loading a
Loading M
Loading E
Loading t

Each saved variable is again available. Moreover, the variable b was not overwritten.

sage: M
Full Modular Symbols space for Gamma_0(37) of weight 2 with sign 0
Archivo: /home/jgarcia/Dropbox/sage/sage.trucos Pgina 4 de 4

and dimension 5 over Rational Field


sage: E
Elliptic Curve defined by y^2 + y = x^3 - x^2 - 10*x - 20 over Rational
Field
sage: b
19
sage: a
389

Errores encontrados y no se que los causa


-----------------------------------------

lambda No utilizar, da error en la compilacion, usar Lambda

Resultado numerico
------------------

Para obtener un resultado numerico utilizar el metodo ".n()"


Ejemplo:

s=pi/2+4
s
1/2*pi + 4
s.n()
5.57079632679490

Si se coloca entre los parentesis el parametro digits= un numero,


este es el numero de digitos mostrados:

s.n(digits=4)
5.571

el numero de bits de precision se ajusta mediante el parametro


prec=numero de bits

You might also like