Skip to main content

Subsection 1.2 Getting Started with Sage

Sage is simple to use β€” all you need is a web browser with internet access. A convenient starting point is SageMathCell, an easy-to-use web interface for Sage that lets you experiment with commands and explore its features without installation.
Another option is CoCalc, a web-based cloud computing and course management platform for computational mathematics using SageMath.
For long-term or intensive use, it is recommended to install an appropriate distribution of Sage. Details are available on the official SageMath download page and follwoing the instructions for Sage installation.
Getting started depends on your environment:
  • Online: Create a CoCalc account, start a project, and open a Jupyter worksheet.
  • Linux/MacOS: Open a terminal and run sage --notebook, then choose between the Jupyter notebook and the classic Sage notebook.
  • Windows: Use the shortcut icon to launch SageMath in case you have installed Sage using older binaries. After 9.5 version windows binaries are not available. However one can install Sage using Windows Subsystem for Linux (WSL). It allows Windows users to run a Linux environment directly on their machine without setting up a separate virtual machine. Installing SageMath through WSL gives access to a native Linux version of Sage, which ensures better compatibility and performance.
SageMath is built on top of Python. This allows users to employ Python syntax directly within SageMath and integrate Python libraries seamlessly into mathematical computations. This combination of mathematical depth and programming flexibility makes SageMath a powerful environment for problem-solving, research, and education.
Let us now begin exploring SageMath.
To operate with exponents, we use ^ or **
Several standard mathematical functions and constants are already defined in Sage. Let us see some of them. It is easy to guess the name of the function and method.
SageMath provides an in-built reference manual for every function, constant, object, or command. This documentation can be accessed by appending a question mark (? ) to the name of the command. For instance, typing factorial? displays a help page that includes the function description, its syntax, and illustrative examples.
Alternatively, the same information can be obtained using the help() function. For example, help(factorial) will also display the corresponding documentation.
Both methods are convenient for quickly understanding the usage and behavior of SageMath functions.
Variables and Symbolic Expressions
Since Sage is interfaced via the Python programming language, we can facilitate calculations using Python variables in the following manner.
Python variables can be named arbitrarily, it is not recommended to redefine predefined constants or functions. This could lead to confusing results. However Fortunately, the original value can be restored via restore() and reset() commandns
SageMath provides a variety of symbolic manipulations. SageMath thinks of x as a default variable. Any other variable if needed has to be declared using y = var('y') or v=SR.('y').
The solve() function in SageMath is a powerful symbolic tool for solving equations and systems of equations and even inequalities. It works with linear, nonlinear, polynomial, trigonometric, exponential, and logarithmic equations.
Using Dot-Tab Completion in SageMath
SageMath supports dot-tab completion, a powerful feature that helps users quickly discover the available methods and attributes for a given object. By typing the object name followed by a dot (.) and pressing the Tab key, SageMath displays a list of all applicable methods and properties.
This feature is extremely useful for
  • exploring available operations for an object.
  • auto-completing method names.
  • reducing syntax errors due to misspelled method names.
  • learning by exploration, without needing to refer to external documentation.
The dot-tab completion feature in SageMath works when you are using Sage in an interactive environment that supports code introspection. It does not work in every contextβ€”its availability depends on where and how you are running Sage. For example if works
The extended gcd command xgcd(a,b) can be used to display a triplet, \((d,r,s)\) such that \(d=ra+sb\text{.}\) This is called Bezout’s Lemma.
The same gcd Sage function cal also find the gcd of two polynomuials as well.

Checkpoint 1.2.1.

Take any three integers \(a,b, c\) in SageMath and find their gcd. Also find integers \(m, n, p\) such that \(ma+nb+pc=gcd(a,b,c)\text{.}\)
Use gcd([a,b,c])