February 3, 2022
SageMath library: a mathematical software suite based on Python, open-source.
Note: you cannot move SageMath somewhere else on your disk once it is installed.
If the installation is successful, the sage command should give a prompt:
sage:
You can test it with a simple command:
On Linux, to run sage from everywhere: add the SageMath directory into your PATH, or in your .bash_aliases, create an alias to your installation:
alias sage='~/Libs/SageMath/sage'
https://doc.sagemath.org/html/en/tutorial/index.html
https://doc.sagemath.org/html/en/tutorial/introduction.html
(From Cyril Bouvier’s tutorial)
Sage is an open-source computer algebra software.
It is based on Python 3 (from 2020).
It combines lots of open-source libraries (GMP, Pari, FLINT, NTL, MPFR, FFLAS-FFPACK, GMP-ECM, …)
Four ways to use it:
sage
It opens an interactive prompt (using IPython). You can write commands and press Enter to run them; the result is printed below.
Write commands in a file and run it with
sage scriptname.sage
Sage translates the file scriptname.sage into a Python file and executes it.
echo "print(59-17)" > example.sage
sage example.sage
Prints on the terminal:
Similar to the interactive shell but you can use it in your browser.
sage --notebook
Follow the instructions at https://wiki.sagemath.org/SpyderInstructions to configure Spyder.
You can import Sage as a Python library into a Python code with
or alternatively
Then run it with the Python of Sage (without the extension .py):
sage -python myfile
If this is a module file in a directory myproject/test/mytest.py:
sage -python -m myproject.test.mytest
sage -python -m myproject.test.mytest <customized command-line options>
Dynamically type: types of variables are verified at runtime.
Variable name: can contains letters, numbers and underscore _; cannot start with a number; case sensitive. Blocks: whitespace indentation (or tabulations) are used to delimit code blocks. Scope of variables: a block. Comments: Everything after a # is a comment.
b = True boolean, bool, can be True or Falsen = 42 integer, intf = 42.17e14 floating-point number, floats = "Hello World!" string, strL = [42, "ABC", 17.14] list, listL[i] to get the (i+1)-th element,len(L) for its length,L[0] to L[len(l)-1]t = (42, "ABC", 17.14) tuplet[i] to get the element at position iS = {42, "ABC", 17.14} setD = {"answer": 42, "n": 17, 14: (2,7)} dictionary, dictD[key] to get the element corresponding to the key key, for example D["answer"], D["n"], D[14]if condition1:
... # do something if condition1 is true
elif condition2:
... # do something else if condition 1 is false but condition2 is true
elif condition3:
... # do something else if condition3 is true but others are false
else:
... # do something if no condition is trueThe else and elif clauses are optional. The elif clause can be repeated as many times as necessary.
One can use and, or and not to combine conditions.
if myvariable is None:
... # do something if the variable is None (not defined)
if p.is_prime():
Fp = GF(p) # define a prime finite field if p is a primeOne defines a function with def, with input variables, and a return statement. If there is no return, then by default the function returns the special type None.
A method is attached to an instance of a class. This is a function defined inside a class, whose 1st argument is self. To use it on Sage predefined classes:
gives
Syntactic sugar available:
QQx.<x> = QQ[] # defines a polynomial ring in the variable x over the rationals QQ
f = x^3
3^3 # in Sage ^ is powering
3**3 # python powering is still defined in SageCheck at https://doc.sagemath.org/html/en/tutorial/tour_assignment.html for a longer list of arithmetic with SageMath.
The division operation 42/17 gives
2.4705882352941178 in Python3A list of methods attached to a variable can be printed by pressing the Tab key:
The documentation can be printed by appending a `?’ to variables, functions and methods.
The code can be printed by appending a double question mark ?? to functions and methods.
answers
Print the history of all commands you typed in the interpreter:
Then you can copy-paste in a file to save your work.
.sage and .pyFor small SageMath scripts: files myfile.sage with .sage extension, no import needed, the interactive shell (like ipython) imports everything.
Run this file:
within SageMath interactive shell.
This creates a file myfile.sage.py which is a translation into Python of the SageMath script. It removes the syntactic sugar, and add all the needed import statements.
To directly write Python-like code: create a file with .py extension such as myfile.py.
Launch Spyder, then open your file with Spider.
Kate on linux (or maybe you already know vim, emacs etc)notepad++ on Windows, see https://notepad-plus-plus.org/downloads/