# SageMath

SageMath can be used in development of analysis methods and validation of algorithms and software.

## Installation

SageMath is free and open-source software, which can installed locally. SageMath can also be used from cloud, which is the preferred method for occasional use. Paid cloud version is available for heavy use.

Local installation requires lot of disk space, which must be considered if it is to be installed
in a virtual machine (which is one option in Windows platforms). In Linux, the tarball can be
unpacked in any folder, but `/opt/`

or `/usr/local/`

are recommended.
Folder `SageMath`

will be created, and inside it the script `./sage`

will run the installer when run for the first time, and then starts SageMath.
You probably want to create a link to start Sagemath from any folder, for example:

cd /usr/local/bin sudo ln -s /usr/local/etc/SageMath/sage

In Windows, SageMath binaries for Ubuntu can also be installed in Windows Subsystem for Linux (WSL). When you start sage, it may tell that it needs to be compiled, which is not true, but may happen because Sage needs older Python version than is installed in WSL; in that case install it with command

sudo apt install python-minimal

## Usage

Start SageMath shell with the command `sage`

. For GUI, enter command
`notebook()`

inside the shell; GUI will open in web browser.

## Examples

### Definite integral function

A function that is often used to fit PTACs can be integrated between times t1 and t2:

var('A1, A2, A3, L1, L2, L3, t, u, t1, t2') f(x) = (A1*x - A2 - A3) * exp(L1*x) + A2 * exp(L2*x) + A3 * exp(L3*x) f.show()

assume(x>0, t1>0, t2>t1) g = integral(f, x, t1, t2) g.show()

, or from zero time to t:

assume(t>0, u>0) h = integral(f, x, 0, t) h.show()

, and its second integral:

h2 = integral(h, t, 0, u) h2.show()

You can also plot the functions. In this example we set the function parameters and the time range on the command line:

p1 = plot(f(A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1), x, xmin=0.001, xmax=40) p1.plot()

p2 = plot(h(A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1), t, xmin=0.001, xmax=40) p2.plot()

p3 = plot(h2(A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1), u, xmin=0.001, xmax=40) p3.plot()

And you can save the plots in file. Plot file format is determined by the extension of the file name. At least PNG, SVG, and PDF formats are supported:

p1.save('/home/username/data/p1.svg') p2.save('/home/username/data/p2.svg') p3.save('/home/username/data/p3.svg')

Function values can be printed, for example at times 1, 8, and 40:

v1 = h2(u=1, A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1) v2 = h2(u=8, A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1) v3 = h2(u=40, A1=200, L1=-0.6, A2=40, L2=-0.5, A3=50, L3=-0.1) print('h2(1)='str(v1)+', h2(8)='str(v2)+', h2(40)='str(v3))

h2(1)=28.9459884181535, h2(8)=3421.92740450284, h2(40)=32751.9485660432

## See also:

- Sage Tutorial
- TPCCLIB development in Linux
- List of TPCCLIB software
- Command-line interface (CLI)
- CSC Notebooks

## Literature

Anastassiou GA, Mezei RA: *Numerical Analysis Using Sage*. Springer, 2015.
doi: 10.1007/978-3-319-16739-8.

Bard GV: *Sage for Undergraduates*. American Mathematical Society, 2015.
ISBN: 9781470411114.

Finch C: *Sage Beginner's Guide*. Packt Publishing, 2011.

Mezei RA: *An Introduction to SAGE Programming*. Wiley, 2016.
ISBN: 978-1-119-12278-4.

Updated at: 2018-02-06

Created at: 2016-06-24

Written by: Vesa Oikonen