# Difference between revisions of "SageMath"

Iosonofabio (talk | contribs) m (→SageTeX) |
(Change sage-notebook to sagemath-jupyter in installation section, since the former is deprecated in favour of the latter) |
||

(61 intermediate revisions by 29 users not shown) | |||

Line 1: | Line 1: | ||

− | [[Category: | + | [[Category:Numerical analysis]] |

− | [ | + | [[ja:SageMath]] |

+ | {{Related articles start}} | ||

+ | {{Related|Matlab}} | ||

+ | {{Related|Octave}} | ||

+ | {{Related|Mathematica}} | ||

+ | {{Related articles end}} | ||

− | + | [http://www.sagemath.org SageMath] (formerly '''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. | |

− | |||

− | |||

− | ==Usage== | + | SageMath provides support for the following: |

− | === | + | * '''Calculus''': using [[Wikipedia:Maxima (software)|Maxima]] and [[Wikipedia:SymPy|SymPy]]. |

− | + | * '''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. | ||

+ | |||

+ | == Installation == | ||

+ | |||

+ | * {{Pkg|sagemath}} contains the command-line version; | ||

+ | * {{Pkg|sagemath-doc}} for HTML documentation and inline help from the command line. | ||

+ | * {{Pkg|sagemath-jupyter}} includes a kernel for the Jupyter notebook interface. | ||

+ | |||

+ | {{Note|Most of the [http://doc.sagemath.org/html/en/installation/standard_packages.html standard Sage packages] are available as [[pacman#Installing packages|optional dependencies]] of the {{pkg|sagemath}} package or in [[AUR]], therefore they have to be installed additionally as normal Arch packages in order to take advantage of their features. Note that there is no need to install them with {{ic|sage -i}}, in fact this command will not work if you installed SageMath with pacman.}} | ||

+ | |||

+ | == Usage == | ||

+ | SageMath mainly uses Python as a scripting language with a few [http://doc.sagemath.org/html/en/tutorial/afterword.html#section-mathannoy modifications] to make it better suited for mathematical computations. | ||

+ | |||

+ | === SageMath command-line === | ||

+ | SageMath can be started from the command-line: | ||

$ sage | $ sage | ||

− | + | ||

− | + | For information on the SageMath command-line see [http://doc.sagemath.org/reference/repl/index.html this page]. | |

− | + | ||

− | Note, however, that | + | The command-line is based on the IPython shell so you can use all its [http://doc.sagemath.org/html/en/tutorial/interactive_shell.html tricks] with SageMath. 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)) | ||

− | + | SageMath opens the plot in an external application. | |

− | + | ||

+ | === Sage Notebook === | ||

+ | {{Note|The SageMath Flask notebook is deprecated in favour of the Jupyter notebook. The Jupyter notebook is recommended for all new worksheets. You can use the {{pkg|sage-notebook-exporter}} application to convert your Flask notebooks to Jupyter}} | ||

− | + | A better suited interface for advanced usage in SageMath is the Notebook ({{Pkg|sage-notebook}}). | |

− | To start the | + | To start the Notebook server from the command-line, execute: |

$ sage -n | $ sage -n | ||

− | |||

− | You can instead start | + | The notebook will be accessible in the browser from http://localhost:8080 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(automatic_login=True)" | ||

+ | |||

+ | For a more comprehensive tutorial on the Sage Notebook see the [http://doc.sagemath.org/html/en/reference/notebook/index.html Sage documentation]. For more information on the {{ic|notebook()}} command see [http://doc.sagemath.org/html/en/reference/notebook/sagenb/notebook/notebook.html this page]. | ||

− | + | === Jupyter Notebook === | |

+ | SageMath also provides a kernel for the [https://jupyter.org/ Jupyter] notebook in the {{Pkg|sagemath-jupyter}} package. To use it, launch the notebook with the command | ||

+ | $ jupyter notebook | ||

+ | and choose "SageMath" in the drop-down "New..." menu. The SageMath Jupyter notebook supports [[LaTeX]] output via the {{ic|%display latex}} command and 3D plots if {{Pkg|jmol}} is installed. | ||

− | === | + | === 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, SageMath, 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 SageMath. | |

− | |||

− | |||

− | |||

− | + | Cantor can be installed with the {{Pkg|cantor}} package or as part of the {{Grp|kde-applications}} or {{Grp|kdeedu}} groups, available in the [[official repositories]]. | |

− | ===SageTeX=== | + | == Optional additions == |

− | If you have installed | + | === SageTeX === |

+ | If you have [[TeX Live]] installed on your system, you may be interested in [http://doc.sagemath.org/html/en/tutorial/sagetex.html using SageTeX], a package that makes the inclusion of SageMath 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 | + | As a simple example, here is how you include a Sage 2D plot in your TEX document (assuming you use {{ic|pdflatex}}): |

− | * include the sagetex package in the preamble of your document with the usual | + | * 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 | + | |

+ | * 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} | ||

+ | \sageplot[width=\linewidth]{ptot} | ||

\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.sagetex.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:// | + | The full documentation of SageTeX is available on [http://www.ctan.org/pkg/sagetex CTAN]. |

− | ==== | + | == Troubleshooting == |

− | If your | + | === 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 | + | * Refresh TeX Live: |

− | {{ | + | {{bc| |

− | texhash: Done. | + | # texhash /usr/share/texmf/ |

− | + | texhash: Updating /usr/share/texmf/.//ls-R... | |

− | + | texhash: Done. | |

+ | }} | ||

− | + | == See also == | |

+ | * [http://www.sagemath.org/ Official Website] | ||

+ | * [http://doc.sagemath.org/ SageMath Documentation] | ||

+ | * [http://planet.sagemath.org/ Planet Sage] | ||

+ | * [http://wiki.sagemath.org/ SageMath Wiki] | ||

+ | * [http://www.sagemath.org/links-components.html Software Used by SageMath] |

## Latest revision as of 17:04, 10 May 2019

SageMath (formerly **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.

SageMath provides support for the following:

**Calculus**: using Maxima and SymPy.**Linear Algebra**: using the GSL, SciPy and NumPy.**Statistics**: using R (through RPy) and SciPy.**Graphs**: using matplotlib.- An
**interactive shell**using IPython. - Access to
**Python modules**such as PIL, SQLAlchemy, etc.

## Contents

## Installation

- sagemath contains the command-line version;
- sagemath-doc for HTML documentation and inline help from the command line.
- sagemath-jupyter includes a kernel for the Jupyter notebook interface.

**Note:**Most of the standard Sage packages are available as optional dependencies of the sagemath package or in AUR, therefore they have to be installed additionally as normal Arch packages in order to take advantage of their features. Note that there is no need to install them with

`sage -i`

, in fact this command will not work if you installed SageMath with pacman.## Usage

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

### SageMath command-line

SageMath can be started from the command-line:

$ sage

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

The command-line is based on the IPython shell so you can use all its tricks with SageMath. 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))

SageMath opens the plot in an external application.

### Sage Notebook

**Note:**The SageMath Flask notebook is deprecated in favour of the Jupyter notebook. The Jupyter notebook is recommended for all new worksheets. You can use the sage-notebook-exporter application to convert your Flask notebooks to Jupyter

A better suited interface for advanced usage in SageMath is the Notebook (sage-notebook). To start the Notebook server from the command-line, execute:

$ sage -n

The notebook will be accessible in the browser from http://localhost:8080 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(automatic_login=True)"

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

command see this page.

### Jupyter Notebook

SageMath also provides a kernel for the Jupyter notebook in the sagemath-jupyter package. To use it, launch the notebook with the command

$ jupyter notebook

and choose "SageMath" in the drop-down "New..." menu. The SageMath Jupyter notebook supports LaTeX output via the `%display latex`

command and 3D plots if jmol is installed.

### Cantor

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

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

## Optional additions

### SageTeX

If you have TeX Live installed on your system, you may be interested in using SageTeX, a package that makes the inclusion of SageMath 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.sagetex.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.