Skip to main content
Mathematics LibreTexts

8: Appendix A Sage Reference

  • Page ID
    214183
  • \( \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{\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}\)

    We have introduced a number of Sage commands throughout the text, and the most important ones are summarized here in a single place.

    Accessing Sage

    In addition to the Sage cellls included throughout the book, there are a number of ways to access Sage.

    1. There is a freely available Sage cell at sagecell.sagemath.org.
    2. You can save your Sage work by creating an account at cocalc.com and working in a Sage worksheet.
    3. There is a page of Sage cells at gvsu.edu/s/0Ng. The results obtained from evaluating one cell are available in other cells on that page. However, you will lose any work once the page is reloaded.

    Creating matrices

    There are a couple of ways to create matrices. For instance, the matrix

    \[\left[\begin{array}{cccc}
    -2 & 3 & 0 & 4 \\
    1 & -2 & 1 & -3 \\
    0 & 2 & 3 & 0
    \end{array}\right]\]

    can be created in either of the two following ways.

    matrix(3, 4, [-2, 3, 0, 4,
                   1,-2, 1,-3,
    	           0, 2, 3, 0])

     

    matrix([ [-2, 3, 0, 4],
             [ 1,-2, 1,-3],
    	     [ 0, 2, 3, 0])

    Be aware that Sage can treat mathematically equivalent matrices in different ways depending on how they are entered. For instance, the matrix

    matrix([ [1, 2],
             [2, 1] ])

    has integer entries while

    matrix([ [1.0, 2.0],
             [2.0, 1.0] ])

    has floating point entries.

    If you would like the entries to be considered as floating point numbers, you can include RDF in the definition of the matrix.

    matrix(RDF, [ [1, 2],
                  [2, 1] ])

    Special matrices

    The  identity matrix can be created with

    identity_matrix(4)	    	  
    

    A diagonal matrix can be created from a list of its diagonal entries. For instance,

    diagonal_matrix([3,-4,2])

    Reduced row echelon form

    The reduced row echelon form of a matrix can be obtained using the rref() function. For instance,

    A = matrix([ [1,2], [2,1] ])
    A.rref()

    Vectors

    A vector is defined by listing its components.

    v = vector([3,-1,2])

    Addition

    The + operator performs vector and matrix addition.

    v = vector([2,1])
    w = vector([-3,2])
    print(v+w)	
    
    A = matrix([[2,-3],[1,2]])
    B = matrix([[-4,1],[3,-1]])
    print(A+B)

    Multiplication

    The * operator performs scalar multiplication of vectors and matrices.

    v = vector([2,1])
    print(3*v)
    A = matrix([[2,1],[-3,2]])	    
    print(3*A)	  
    

    Similarly, the * is used for matrix-vector and matrix-matrix multiplication.

    A = matrix([[2,-3],[1,2]])
    v = vector([2,1])	    
    print(A*v)
    B = matrix([[-4,1],[3,-1]])
    print(A*B)

    Operations on vectors

    1. The length of a vector v is found using v.norm().

    2. The dot product of two vectors v and w is v*w.

    Operations on matrices

    1. The transpose of a matrix A is obtained using either A.transpose() or A.T.

    2. The inverse of a matrix A is obtained using either A.inverse() or A^-1.

    3. The determinant of A is A.det().

    4. A basis for the null space Nul is found with A.right_kernel().

    5. Pull out a column of A using, for instance, A.column(0), which returns the vector that is the first column of A.

    6. The command A.matrix_from_columns([0,1,2]) returns the matrix formed by the first three columns of A.

    Eigenvectors and eigenvalues

    1. The eigenvalues of a matrix A can be found with A.eigenvalues(). The number of times that an eigenvalue appears in the list equals its multiplicity.

    2. The eigenvectors of a matrix having rational entries can be found with A.eigenvectors_right().

    3. If  \(A\) can be diagonalized as \(A=P D P^{-1}\), then

      D, P = A.right_eigenmatrix()
      		

      provides the matrices D and P.

      1. The characteristic polynomial of A is A.charpoly('x') and its factored form A.fcp('x').

    Matrix factorizations

    1. The  \(LU\)factorization of a matrix

      P, L, U = A.LU()	    
      		

      gives matrices so that .

    2. A singular value decomposition is obtained with

      U, Sigma, V = A.SVD()	    
      		

      It’s important to note that the matrix must be defined using RDF.  For instance, A = matrix(RDF, 3,2,[1,0,-1,1,1,1]).

    3. The  factorization of A is A.QR() provided that A is defined using RDF.

     


    8: Appendix A Sage Reference is shared under a CC BY license and was authored, remixed, and/or curated by LibreTexts.