Difference between revisions of "SageMath"

From ArchWiki
Jump to: navigation, search
(SageTeX)
m (typos)
(14 intermediate revisions by 11 users not shown)
Line 1: Line 1:
[[Category:Software (English)]]
+
[[Category:Mathematics and science]]
[http://www.sagemath.org Sage] is a program for numerical and symbolic mathematical computation. It is meant to provide an alternative for commercial programs such as Maple, Matlab, and Mathematica. It is actually mantained in the community repository.
+
{{Article summary start}}
 +
{{Article summary text|This article contains information about the installation, configuration and use of Sage.}}
 +
{{Article summary heading|Related}}
 +
{{Article summary wiki|Matlab}}
 +
{{Article summary wiki|Octave}}
 +
{{Article summary wiki|Mathematica}}
 +
{{Article summary end}}
 +
[http://www.sagemath.org Sage] is a program for numerical and symbolic mathematical computation that uses [[Python]] as its main language. It is meant to provide an alternative for commercial programs such as Maple, Matlab, and Mathematica.
  
==Installation==
+
Sage provides support for the following:
To install binaries from community repository:
+
* '''Calculus''': using [[Wikipedia:Maxima (software)|Maxima]] and [[Wikipedia:SymPy|SymPy]].
# pacman -S sage-mathematics
+
* '''Linear Algebra''': using the [[Wikipedia:GNU Scientific Library|GSL]], [[Wikipedia:SciPy|SciPy]] and [[Wikipedia:NumPy|NumPy]].
 +
* '''Statistics''': using [[Wikipedia:R (programming language)|R]] (through RPy) and SciPy.
 +
* '''Graphs''': using [[Wikipedia:matplotlib|matplotlib]].
 +
* An '''interactive shell''' using [[Wikipedia:IPython|IPython]].
 +
* Access to '''Python modules''' such as [[Wikipedia:Python Imaging Library|PIL]], [[Wikipedia:SQLAlchemy|SQLAlchemy]], etc.
  
==Usage==
+
== Installation ==
===Sage-Commandline===
+
Sage can be [[Pacman|installed]] with the package {{Pkg|sage-mathematics}}, available in the [[official repositories]].
Once installed, users should be able to start the sage commandline in bash.
+
 
 +
== Usage ==
 +
Sage mainly uses Python as a scripting language with a few [http://www.sagemath.org/doc/tutorial/afterword.html#section-mathannoy modifications] to make it better suited for mathematical computations.
 +
 
 +
=== Sage command-line ===
 +
Sage can be started from the command-line:
 
  $ sage
 
  $ sage
Math can then be typed at this commandline.
+
 
sage: 2+2
+
For information on the Sage command-line see [http://www.sagemath.org/doc/reference/cmd.html this page].
4
+
 
Note, however, that the CLI is not very comforable for certain purposed. For instance, if you plot a function,
+
The command-line is based on the IPython shell so you can use all its [http://www.sagemath.org/doc/tutorial/interactive_shell.html tricks] with Sage. For an extensive tutorial on IPython see the community maintained [http://wiki.ipython.org/Cookbook IPython Cookbook].
 +
 
 +
Note, however, that it is not very comfortable for some uses such as plotting. When you try to plot something, for example:
 
  sage: plot(sin,(x,0,10))
 
  sage: plot(sin,(x,0,10))
this is opened in a browser window. In these cases, you can start the notebook server by
+
Sage opens a browser window with the Sage Notebook.
sage: notebook()
+
  
===Sage-Notebook===
+
=== Sage Notebook ===
To start the Sage-Notebook server from bash, without going thru Sage-Commandline:
+
A better suited interface for advanced usage in Sage is the Notebook.
 +
To start the Notebook server from the command-line, execute:
 
  $ sage -n
 
  $ sage -n
The notebook will be accessable in the browser from http://localhost:8000 , and will require you to login.  However, If you are only intending to run the server for personal use, and not across the internet, the login will be an annoyance.
 
  
You can instead start Sage notebook without requiring login, and have it automatically pop up in a browser.
+
The notebook will be accessible in the browser from http://localhost:8000 and will require you to login.  
$ BROWSER="chromium" sage -c "notebook(require_login=false,open_viewer=true)"
+
  
More detailed documentation can be read about the [http://www.sagemath.org/doc/reference/sagenb/notebook/notebook_object.html "notebook() command."]
+
However, if you only run the server for personal use, and not across the internet, the login will be an annoyance. You can instead start the Notebook without requiring login, and have it automatically pop up in a browser, with the following command:
 +
$ sage -c "notebook(require_login=false,open_viewer=true)"
  
===Documentation===
+
For a more comprehensive tutorial on the Sage Notebook see the [http://www.sagemath.org/doc/reference/notebook.html Sage documentation]. For more information on the {{ic|notebook()}} command see [http://www.sagemath.org/doc/reference/sagenb/notebook/notebook_object.html this page].
For local Documentation, one can compile it into html or even pdfs.  Apparently, this needs to be executed as root (perhaps unsafely):
+
# sage -docbuild reference html
+
This builds html docs for the whole tree "reference" (which is alot; Expect many many hours).  An option is to build a smaller part of the documentation tree, but you would need to know what it is you want.  Until then, you might consider just browsing reference online.
+
One place this can be found is at http://www.sagemath.org/doc/ .
+
  
==Optional additions==
+
=== Cantor ===
 +
[http://edu.kde.org/applications/mathematics/cantor/ Cantor] is an application included in the KDE Edu Project. It acts as a front-end for various mathematical applications such as Maxima, Sage, Octave, Scilab, etc. See the [http://wiki.sagemath.org/Cantor Cantor page] on the Sage wiki for more information on how to use it with Sage.
  
===SageTeX===
+
Cantor can be installed with the {{Pkg|kdeedu-cantor}} package or as part of the {{Grp|kde}} or {{Grp|kdeedu}} groups, available in the [[official repositories]].
If you have installed Texlive on your system, you may be interested in using SageTeX, a package that makes possible the inclusion of sage code in LaTeX files. Starting from version 4.6.1-3, Texlive is made aware of SageTeX automagically, and you can start using it straight away.
+
  
As a simple example, here is how you include a Sage 2D plot in your latex document (assuming you use pdfLaTeX):
+
=== Documentation ===
* include the sagetex package in the preamble of your document with the usual
+
For local documentation, one can compile it into multiple formats such as HTML or PDF. To build the whole Sage reference, execute the following command (as root):
 +
# sage --docbuild reference html
 +
 
 +
This builds the HTML documentation for the whole ''reference'' tree (may take longer than an hour). An option is to build a smaller part of the documentation tree, but you would need to know what it is you want.  Until then, you might consider just browsing  the [http://www.sagemath.org/doc/ online reference].
 +
 
 +
For a list of documents see {{ic|sage --docbuild --documents}} and for a list of supported formats see {{ic|sage --docbuild --formats}}.
 +
 
 +
== Optional additions ==
 +
=== SageTeX ===
 +
If you have installed [[TeX Live]] on your system, you may be interested in [http://www.sagemath.org/doc/tutorial/sagetex.html using SageTeX], a package that makes the inclusion of Sage code in LaTeX files possible. TeX Live is made aware of SageTeX automatically so you can start using it straight away.
 +
 
 +
As a simple example, here is how you include a Sage 2D plot in your TEX document (assuming you use {{ic|pdflatex}}):
 +
* include the {{ic|sagetex}} package in the preamble of your document with the usual
 
  \usepackage{sagetex}
 
  \usepackage{sagetex}
* create a sagesilent environment in which you insert your sage code:
+
 
 +
* create a {{ic|sagesilent}} environment in which you insert your code:
 
  \begin{sagesilent}
 
  \begin{sagesilent}
  dob(x) = sqrt(x^2-1)/(x * arctan(sqrt(x^2-1)))
+
  dob(x) = sqrt(x^2 - 1) / (x * arctan(sqrt(x^2 - 1)))
  dpr(x) = sqrt(x^2-1)/(x * log( x + sqrt(x^2-1)))
+
  dpr(x) = sqrt(x^2 - 1) / (x * log( x + sqrt(x^2 - 1)))
  p1 = plot(dob,(x,1,10),color='blue')
+
  p1 = plot(dob,(x, 1, 10), color='blue')
  p2 = plot(dpr,(x,1,10),color='red')
+
  p2 = plot(dpr,(x, 1, 10), color='red')
  ptot=p1+p2
+
  ptot = p1 + p2
 
  ptot.axes_labels(['$\\xi$','$\\frac{R_h}{\\max(a,b)}$'])
 
  ptot.axes_labels(['$\\xi$','$\\frac{R_h}{\\max(a,b)}$'])
 
  \end{sagesilent}
 
  \end{sagesilent}
* create the plot e.g. inside a float environment:
+
 
 +
* create the plot, e.g. inside a {{ic|float}} environment:
 
  \begin{figure}
 
  \begin{figure}
 
  \begin{center}
 
  \begin{center}
  \caption{Perrin factors for oblate (blue) and prolate (red) ellipsoids.}
+
  \sageplot[width=\linewidth]{ptot}
\label{fig:Perrin}
+
 
  \end{center}
 
  \end{center}
 
  \end{figure}
 
  \end{figure}
 +
 
* compile your document with the following procedure:
 
* compile your document with the following procedure:
  $pdflatex <doc.tex>
+
  $ pdflatex <doc.tex>
  $sage <doc.sage>
+
  $ sage <doc.sage>
  $pdflatex <doc.tex>
+
  $ pdflatex <doc.tex>
 +
 
 
* you can have a look at your output document.
 
* you can have a look at your output document.
  
The full documentation of SageTeX is available on [http://tug.ctan.org/cgi-bin/ctanPackageInformation.py?id=sagetex CTAN].
+
The full documentation of SageTeX is available on [http://www.ctan.org/pkg/sagetex CTAN].
  
====Troubleshooting====
+
== Troubleshooting ==
If your Texlive installation does not find the sagetex package, you can try the following procedure (do it as root, or with sudo, or use a local folder):
+
=== TeX Live does not recognize SageTex ===
 +
If your TeX Live installation does not find the SageTex package, you can try the following procedure (as root or use a local folder):
 
* Copy the files to the texmf directory:
 
* Copy the files to the texmf directory:
 
  # cp /opt/sage/local/share/texmf/tex/* /usr/share/texmf/tex/
 
  # cp /opt/sage/local/share/texmf/tex/* /usr/share/texmf/tex/
* Refresh Texlive:
+
* Refresh TeX Live:
{{Command|texhash /usr/share/texmf/|texhash: Updating /usr/share/texmf/.//ls-R...  
+
{{bc|
texhash: Done.|prompt=#}}
+
# texhash /usr/share/texmf/
 
+
texhash: Updating /usr/share/texmf/.//ls-R...  
===Additional Packages===
+
texhash: Done.
 +
}}
  
If you need to compile your own custom packages in addition to the standard ones, you might need to install aur/sage-mathematics-spkgs so that you will have the spkgs of the original packages.
+
== See also ==
 +
* [http://www.sagemath.org/ Official Website]
 +
* [http://www.sagemath.org/doc/ Sage Documentation]
 +
* [http://planet.sagemath.org/ Planet Sage]
 +
* [http://wiki.sagemath.org/ Sage Wiki]
 +
* [http://www.sagemath.org/links-components.html Software Used by Sage]

Revision as of 10:24, 13 October 2012

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary wiki Template:Article summary end Sage is a program for numerical and symbolic mathematical computation that uses Python as its main language. It is meant to provide an alternative for commercial programs such as Maple, Matlab, and Mathematica.

Sage provides support for the following:

Installation

Sage can be installed with the package sage-mathematics, available in the official repositories.

Usage

Sage mainly uses Python as a scripting language with a few modifications to make it better suited for mathematical computations.

Sage command-line

Sage can be started from the command-line:

$ sage

For information on the Sage command-line see this page.

The command-line is based on the IPython shell so you can use all its tricks with Sage. For an extensive tutorial on IPython see the community maintained IPython Cookbook.

Note, however, that it is not very comfortable for some uses such as plotting. When you try to plot something, for example:

sage: plot(sin,(x,0,10))

Sage opens a browser window with the Sage Notebook.

Sage Notebook

A better suited interface for advanced usage in Sage is the Notebook. To start the Notebook server from the command-line, execute:

$ sage -n

The notebook will be accessible in the browser from http://localhost:8000 and will require you to login.

However, if you only run the server for personal use, and not across the internet, the login will be an annoyance. You can instead start the Notebook without requiring login, and have it automatically pop up in a browser, with the following command:

$ sage -c "notebook(require_login=false,open_viewer=true)"

For a more comprehensive tutorial on the Sage Notebook see the Sage documentation. For more information on the notebook() command see this page.

Cantor

Cantor is an application included in the KDE Edu Project. It acts as a front-end for various mathematical applications such as Maxima, Sage, Octave, Scilab, etc. See the Cantor page on the Sage wiki for more information on how to use it with Sage.

Cantor can be installed with the kdeedu-cantor package or as part of the kde or kdeedu groups, available in the official repositories.

Documentation

For local documentation, one can compile it into multiple formats such as HTML or PDF. To build the whole Sage reference, execute the following command (as root):

# sage --docbuild reference html

This builds the HTML documentation for the whole reference tree (may take longer than an hour). An option is to build a smaller part of the documentation tree, but you would need to know what it is you want. Until then, you might consider just browsing the online reference.

For a list of documents see sage --docbuild --documents and for a list of supported formats see sage --docbuild --formats.

Optional additions

SageTeX

If you have installed TeX Live on your system, you may be interested in using SageTeX, a package that makes the inclusion of Sage code in LaTeX files possible. TeX Live is made aware of SageTeX automatically so you can start using it straight away.

As a simple example, here is how you include a Sage 2D plot in your TEX document (assuming you use pdflatex):

  • include the sagetex package in the preamble of your document with the usual
\usepackage{sagetex}
  • create a sagesilent environment in which you insert your code:
\begin{sagesilent}
dob(x) = sqrt(x^2 - 1) / (x * arctan(sqrt(x^2 - 1)))
dpr(x) = sqrt(x^2 - 1) / (x * log( x + sqrt(x^2 - 1)))
p1 = plot(dob,(x, 1, 10), color='blue')
p2 = plot(dpr,(x, 1, 10), color='red')
ptot = p1 + p2
ptot.axes_labels(['$\\xi$','$\\frac{R_h}{\\max(a,b)}$'])
\end{sagesilent}
  • create the plot, e.g. inside a float environment:
\begin{figure}
\begin{center}
\sageplot[width=\linewidth]{ptot}
\end{center}
\end{figure}
  • compile your document with the following procedure:
$ pdflatex <doc.tex>
$ sage <doc.sage>
$ pdflatex <doc.tex>
  • you can have a look at your output document.

The full documentation of SageTeX is available on CTAN.

Troubleshooting

TeX Live does not recognize SageTex

If your TeX Live installation does not find the SageTex package, you can try the following procedure (as root or use a local folder):

  • Copy the files to the texmf directory:
# cp /opt/sage/local/share/texmf/tex/* /usr/share/texmf/tex/
  • Refresh TeX Live:
# texhash /usr/share/texmf/
texhash: Updating /usr/share/texmf/.//ls-R... 
texhash: Done.

See also