Skip to main content

Discrete Math with SageMath: Learn math with open-source software

Section 1.10 Miscellaneous Features

While Sage supports a lot of features that are outside of the scope of this book and hence there is little value covering them in depth beyond a simple explanation of each feature or command.
The primary goal of this book is to introduce Sage software and demonstrate how it can be used to experiment with discrete math concepts within the Sage environment. Sage is used here interactively and mainly covering the basics. Having an understanding of any of the commands presented in this section would be crucial for working on a production-grade project with complex mathematical models (e.g. handling large dataset). In which case, it would be more appropriate to use these commands within a standalone Sage environment, and they are presented here just for the sake of completeness.
Note that these Sage cells allows the user to experiment freely with any of Sage supported commands. The content of the cells can be altered at run time (on the live web version of the book) and executed in real-time on a remote Sage server. Users can modify the content of cells and execute any other Sage commands to explore various mathematical concepts interactively.

Subsection 1.10.1 Working with Matrices and Vectors

Sage does provide built-in support for matrices and vectors.
Creating and manipulating matrices:
Performing vector operations:

Subsection 1.10.2 Reading and Writing Files in Sage

Sage provides various ways to handle input and output (I/O) operations.
This subsection explores writing data to files and importing data from files.
Sage allows reading from and writing to files using standard Python file-handling functions.
Writing to a file:
Reading from a file:

Subsection 1.10.3 Executing Shell Commands in Sage

Sage allows executing shell commands directly using the `!` operator (prefix the shell command to be executed).
listing the content of the current directory showing the file that we just created:

Subsection 1.10.4 Importing and Exporting Data (CSV, JSON, TXT)

Sage supports structured data formats such as CSV and JSON.
Generating a CSV file using shell command:
Reading a CSV file in Sage:

Subsection 1.10.5 Using External Libraries in Sage

Sage allows using external Python libraries to do advance calculation or access and communicate over a network (urllib.request library) .
Using NumPy for numerical computations: