Skip to main content
Mathematics LibreTexts

1.9: Miscellaneous Features

  • Page ID
    188590
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Sage is feature rich, and the following is a brief introduction of some of its miscellaneous features. Keep in mind that 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 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 datasets). In such cases, it would be more appropriate to use these commands within a standalone Sage environment. These commands are presented here just for the sake of completeness.

    1.9.1 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:
    with open("output.txt", "w") as file:
        file.write("Hello, Sage!")
     

    Reading from a file:

    with open("output.txt", "r") as file:
        content = file.read()
        print(content)  # Output: Hello, Sage!
    Hello, Sage!
    

    1.9.2 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:

    !ls -la
    total 96
    drwxr-x---. 1 jovyan jovyan   115 May  6 22:12 .
    drwxr-xr-x. 1 root   root      20 May  5 05:56 ..
    -rw-r--r--. 1 jovyan jovyan  1961 May  5 05:40 apt.txt
    -rw-r--r--. 1 jovyan jovyan    88 May  5 05:40 AUTHORS
    -rw-r--r--. 1 jovyan jovyan   220 Jan  6  2022 .bash_logout
    -rw-r--r--. 1 jovyan jovyan  3771 Jan  6  2022 .bashrc
    drwxr-xr-x. 1 jovyan jovyan    18 May  5 06:02 .cache
    drwxrwsr-x. 2 jovyan jovyan    30 May  5 05:49 .conda
    drwx------. 3 jovyan jovyan    25 May  6 19:55 .config
    -rw-r--r--. 1 jovyan jovyan  4707 May  5 05:40 environment.yml
    drwxr-xr-x. 8 jovyan jovyan   180 May  5 05:40 .git
    drwxr-xr-x. 1 jovyan jovyan    29 May  6 19:55 .ipython
    drwxr-xr-x. 1 jovyan jovyan    22 May  5 06:02 .jupyter
    -rw-r--r--. 1 jovyan jovyan 16263 May  6 22:12 .jupyter-server-log.txt
    -rw-r--r--. 1 jovyan jovyan  1081 May  5 05:40 LICENSE
    -rw-r--r--. 1 jovyan jovyan 22179 May  5 06:01 Manifest.toml
    -rw-r--r--. 1 jovyan jovyan    12 May  6 22:12 output.txt
    -rwxr-xr-x. 1 jovyan jovyan   849 May  5 05:40 postBuild
    -rw-r--r--. 1 jovyan jovyan   807 Jan  6  2022 .profile
    -rw-r--r--. 1 jovyan jovyan   414 May  5 05:40 Project.toml
    -rw-r--r--. 1 jovyan jovyan 10496 May  5 05:40 README.md
    drwxr-xr-x. 8 jovyan jovyan    95 May  6 20:06 .sage
    drwxr-xr-x. 3 jovyan jovyan    17 May  5 06:03 .yarn
    

    1.9.3 Importing and Exporting Data (CSV, JSON, TXT)

    Sage supports structured data formats such as CSV and JSON.

    Generating a CSV file using shell command:
    !printf "Name,Age,Country\nAlice,25,USA\nBob,30,UK\nCharlie,28,Canada\n" > data.csv
     

    Reading a CSV file in Sage:

    import csv
    
    with open("data.csv", "r") as file:
        reader = csv.reader(file)
        for row in reader:
            print(row)
    ['Name', 'Age', 'Country']
    ['Alice', '25', 'USA']
    ['Bob', '30', 'UK']
    ['Charlie', '28', 'Canada']
    

    1.9.4 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:
    import numpy as np
    array = np.array([1, 2, 3])
    print(array)  # Output: [1 2 3]
    [1 2 3]
    

    1.9: Miscellaneous Features is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?