Posts Tagged ‘Pyhton’

Hello again! If you are looking for a Differential Geometry tool, a Sage package which is under development is SageManifold. Let’s see how to install it.

  1. Download the package using the link (currently v.0.2). I’d assume it is saved on your Downloads folder.
  2. Assuming you have SAGE installed, and you have created an alias to call it (see my previous post), run the following command
    $ sage -f ~/Downloads/manifolds-0.2.spkg
  3. To generate the documentation (in case you don’t want to or cannot download it), use either of the commands
    $ sage -docbuild sagemanifolds pdf
    $ sage -docbuild sagemanifolds html
  4. In order for using the package, start your WorkSheet with the command line
    from manifolds.all import *
Have a nice calculation!

Read Full Post »

I was playing with Sage(math) because I wanted to manage (a bit) the presentation of the plots. Here is what I found out! (most of it is in Sage help)

Color of Plots.

For changing the color of the plot, use the option color,

sage: plot(sin, -3, 3, color='red')

and you’ll get

Red Sine Function

Nice! but not enough.

One can draw recurrent plots by using a for command,

sage: for n in range(1,6):
... plot(sin(n*x), (x, -3, 3), color=hue(n/5.0))

but this code gives 5 different plots, if you want only one try this

sage: p = plot( sin, -3, 3, color=hue(1/5.0))
sage: for n in range(2,6):
... p += plot(sin(n*x), (x, -3, 3), color=hue(n/5.0))
sage: p

and the result is

Plot of multiple sine functions in colors

Adding Text to Plots.

The command for adding text to plots is text (very intuitive, Isn’t it?), and the parameters are, the actual text you’d like to insert between single quote marks, the coordinates, and any other characteristic… as color.

Additionally, the text environment accepts mathematical LaTeX inputs by using the dollar signs.

sage: p = plot(sin(x/10.0), -10, 10, color=hue(1/6.0))
   + text('$n$=1', (12,1), color=hue(1/6.0))
sage: for n in range(2,6):
... p += plot(sin(n*x/10.0), -10, 10, color=hue(n/6.0))
   + text('$n$=%s'%n, (12,1/n), color=hue(n/6.0))
sage: p

and get,

Adding multi-text to multi-plot

Note that the text may include `running` parameters, in the above example the running parameter is n, and is called in the text environment as `%s` and later one explain that the value to insert is n via %n.

NOTE 2: The coordinates of the text might be also changing values.

NOTE 3: The fontsize and horizontal_alignment options are available, for example

sage: text('$E$=%s'%Energy, (5.2, 1.2),
  horizontal_alignment='right', fontsize=15, color='black')

Nice Example.

Try this!

def Harm_Osc(n, x):
 return sqrt(1/(2^n*factorial(n)))/(pi)^(0.25)*e^(-x^2/2)*hermite(n,x)
p = plot(x^2/2, (x, -3.2, 3.2), color='black') 
   + text('$x^2/2$', (3.9, 5), color='black', fontsize=13)
for n in range(5):
 p += plot(Harm_Osc(n, x)/2 + n + 0.5, (x, -5, 5), color=hue(n/5.0), fill=n + 0.5) 
   + text('$\Psi_%s$' %n, (5.2, n+0.5), color=hue(n/5.0), fontsize=13)
show(p, axes_labels=['$x$','$E$'])


You’ll get this,

Wave functions for the quantum harmonic oscillator

This example got colors, text, running text, running color, axes labeling, LaTeX text, fill plot… very complete!

Read Full Post »

Last week I was trying to integrate a power of the Hyperbolic TangentĀ  (tanh) in sage, so I first try,

sage: n,x = var('n,x')
sage: integrate(tanh(x)^n, x)

but Sage didn’t integrate it. So I impose n to be an integer,

sage: n,x = var('n,x')
sage: assume(n, 'integer')
sage: integrate(tanh(x)^n, x)

and still nothing. However, for specific values of n it worked,

sage: for n in range(1,6):
...  integrate(tanh(x)^n, x)

the results were shown.

In the other hand, Mathematica could solve the integration in general,

Integrate[Tanh[x]^n, x]

in terms of Hyperbolic Functions (which Maxima does not manages). Even in the case of specific values of n, the given results were much nicer because the answer were given in term of hyperbolic functions instead of exponential.

Then, I decide to try the Sage-Mathematica synapses.

What do we need?

  • Mathematica installed in the computer.
  • The License information of Mathematica (even if you have already registered it)
  • A working Sage installation.


Open a terminal and call a sage subshell,

$ sage -sh

and call the mathematica kernel,

$ math

Here you will be asked to provide the license information. NOTE: It is possible that if runs without the information of the license, in that case you are ready to use Mathematica within Sage.

After provide the information you are ready.

Using it!

The way of using is a bit weird, at the beginning,Ā  for integrate the sine function, use this,

sage: mathematica.Integrate(sin(x), x)


Since we are welling to use Mathematica kernel, the first word would be mathematica, followed by a Mathematica command separated by a point. Then, using Sage notation the argument.

This would work! However, the answer is presented in Mathematica notation. If you’d like to have the answer in Sage notation, use something like

sage: eq = mathematica.Integrate(sin(x), x); eq._sage_()

That’s it.

I’d like to thank to schilly for his help.

Read Full Post »

I’ve been trying Sage(math) for the last three weeks, and I can say it’s a charming piece of software. Works very well, the IRC channel is really, really helpful, the interface is simple, the Python platform is rather natural to manage, evolves quickly… and so on.

However, there are some weak points I’d like to comment…

  • Documentation: Since Sage(math) use about 70 different applications, the documentation is FAR of being complete. It would be useful if the documentation is improved, at least if it refers to, say, Maxima documentation… or Numpy… etc.
  • Hypergeometric Functions: Besides the confluent one, the hypergeometric functions are left behind. I guess the problem is the maxima support of Special Functions.
  • Browser Integration: There are still some weak points in browsers other than firefox… like Chromium-browser. A.f.a. I know, Chromium has problems opening new worksheets, and with some permissions settings.

So, I recomend using firefox as the main browser for Sage(math) and if possible integrate it with other calculation programs like Maple or Mathematica. And finally look up further into Maxima, and other pieces of Sage documentation, as well as using the IRC channel (#sage-devel) an google groups for asking questions.

Read Full Post »

A few minuted ago I could install and use the sagetex package for Sage(math). I’d like to thank ElMonkey for helping me via the IRC channel #sage-devel.


It is assumed that you have installed

  • A LaTeX compiler,
  • A LaTeX editor,
  • and Sage(math)

all of them configured and working properly.

Downloading the Package.

Get the last version of the package in http://www.sagemath.org/packages/optional/.

Installing the Package.

Useful Advise.

My Sage script is located in a sub/sub/sub/subfolder/Sage-4.3.1 (from my HOME folder), so when I want to running it, I should type

$ sub/sub/sub/subfolder/Sage-4.3.1/./sage

It isn’t nice to repeat this one and again, every day.

Thus, I created an alias.

Open the .bashrc file in your editor (gedit for example)

$ gedit .bashrc

add (at the end of the file) the line

alias sage='/path/to/your/sage/folder/./sage'

which in my example would be,

alias sage='/home/doxdrum/sub/sub/sub/subfolder/Sage-4.3.1/./sage'

Save and close the editor. Now in the terminal,

$ source .bashrc

and from now on I just type

$ sage

for running the program.

The real installation.

Now, type in the terminal

$ sage -i sagetex-2.2.1.spkg

and that’s it.


After the installation, a new folder is placed into the Sage folder. Look into local/share/texmf. All files (and folders) inside it must be copied to the LaTeX tree.

In a Linux distribution the LaTeX tree should be placed in /usr/share/texmf/tex/latex

$ sudo cp -r /path/to/Sage-4.3.1/local/share/texmf \
  Ā /usr/share/texmf/tex/latex
$ sudo mktexlsr /usr/share/texmf/tex/latex

This should work. However, if it doesn’t… the desperate method is to copy the sagetex.sty from /path/to/Sage-4.3.1/local/share/texmf to the folder of the TEX file.

The TeX File.

It is time to create a file.tex, just as any other TEX file. Add the line


to the preable.

Compiling the TeX file.

When you run

$ pdflatex file.tex

the compilator exits with errors… but you get a file.sage, sage-compile it and run pdflatex again,

$ sage file.sage
$ pdflatex file.tex

There it is!!!

A resume compilation line could be

$ pdflatex file.tex && sage file.sage && pdflatex file.tex

Enjoy it!

Read Full Post »

Yesterday I add my first contribution to sagenb.org/pub.

This has a couple of applications of the Euler method for solving ODE’s (ordinary differential equations. Check it out at my sage notebook

Read Full Post »

Adding a Calculation Line.

For adding a calculation line between other two lines, pass the pointer between them and click the narrow line that appears close to the bottom line.

Text in the Notebook.

When you call the notebook from the terminal, it appear as a calculation line. If you’d like to add text, press the Shift-key when adding a new line where the text would be placed. NOTE: you can add math LaTeX symbols as usual surrounded by dollar symbols.

LaTeX Text.

When you add text in the work-sheet, It look more like OOfice than LaTeX. If in the calculation line you introduce the text precede by a line containing


when you `evaluate’ the line, the LaTeX text would appear as result.

I do not know if it’s possible to drop the text in the command line after its evaluation.

Editing a Work-Sheet.

For editing a worksheet,

  1. Click theĀ  button Edit in the Upper-Right corner of the notebook page.
  2. In the text-like mode appearing erase the lines you’d like to drop.
  3. Save the changes (in the button over the first text line).
  4. Return to the Worksheet-mode.

Another way (easier for small editing)

  1. Erase the text in the calculation line you want to drop
  2. In the empty line click the Backspace-key.

Thanks to the people in the IRC channel #sage-devel.

Read Full Post »

Older Posts »