︠a26e7dcf-a1c1-446e-84cf-3208030fc609i︠ %html

Links to other worksheets:

Book, (1) Linear Algebra Arithmetic, (2) Linear Algebra Applications, (4) Linear Transformations, (5) Changing Bases

Some basics - How do you evaluate cells, add new cells, and add text to explain what you are doing?

You can evaluate cells by typing Shift+Enter (or clicking in them and clicking "evaluate"). Do this on the 2+3 cell below.

You can add new input cells in which to type Sage commands by clicking above or below each cell when a horizontal bar appears. Try adding a new cell above and below the 2+3 cell.

You can add new text cells by holding down shift when you click to enter a new cell. Try doing this on the cell below.

You can edit existing text cells by double clicking them.  Try modifying this text cell by double clicking on it.

︡f074ea8a-77e8-4afa-a42d-7d8a6b9b58bb︡{"html": "

Links to other worksheets:

\n

Book, (1) Linear Algebra Arithmetic, (2) Linear Algebra Applications, (4) Linear Transformations, (5) Changing Bases

\n

Some basics - How do you evaluate cells, add new cells, and add text to explain what you are doing?

\n

You can evaluate cells by typing Shift+Enter (or clicking in them and clicking \"evaluate\"). Do this on the 2+3 cell below.

\n

You can add new input cells in which to type Sage commands by clicking above or below each cell when a horizontal bar appears. Try adding a new cell above and below the 2+3 cell.

\n

You can add new text cells by holding down shift when you click to enter a new cell. Try doing this on the cell below.

\n

You can edit existing text cells by double clicking them.  Try modifying this text cell by double clicking on it.

"}︡ ︠60168b6c-b230-47c8-b1a5-b3245bfce0cd︠ 2+3 ︡3a9870ab-a4ab-403c-a379-f57817214e06︡{"stdout": "5"}︡ ︠6619f946-9513-4fd8-86a8-617cf8875a25i︠ %html

Now you're ready to begin.

Vectors

Use the vector command to enter vectors. Functions always require paranthesis ( ) and lists always require brackets [ ].

︡1e687f28-71b0-4764-b1e2-c1f401e7eba2︡{"html": "

Now you're ready to begin.

\n

Vectors

\n

Use the vector command to enter vectors. Functions always require paranthesis ( ) and lists always require brackets [ ].

"}︡ ︠2ae9b7d1-6372-4584-b74e-de823de0ba83︠ u=vector([1,2]) u ︡818226ef-5fe2-4cf7-b330-f476700ff27c︡{"stdout": "(1, 2)"}︡ ︠dbe654f4-d69d-451c-af54-96446775d41b︠ v=vector([3,1]) v ︡16b880ad-c965-4baf-985e-a29d81c4a22e︡{"stdout": "(3, 1)"}︡ ︠fdc996d2-a0f9-4f97-b95a-f0ce6f474c10i︠ %html

The magnitude of a vector is also called the length or norm.  Use .norm() to find lengths in Sage.

︡6d05d074-b793-4dd1-8570-91762baf79df︡{"html": "

The magnitude of a vector is also called the length or norm.  Use .norm() to find lengths in Sage.

"}︡ ︠efb2ce30-9f08-4752-9b27-6186cb92634f︠ u.norm() ︡f7b1fdc2-9d3b-4450-aa57-f21531cec8e5︡{"stdout": "sqrt(5)"}︡ ︠21229e71-2dcc-4ecb-88ca-c1c29b6f554fi︠ %html

The dot product is simply an asterisk *.

︡85a29f2c-0a61-4b8b-af6a-f6a98dd26932︡{"html": "

The dot product is simply an asterisk *.

"}︡ ︠ecc06a42-ddcf-494a-8bbb-f5c2caf5925a︠ u*v ︡667f92a8-ab9a-4cfa-9f80-d363159e55a1︡{"stdout": "5"}︡ ︠2e822934-cf34-4870-a62b-d85bb5a06136i︠ %html

Here is an example of how to plot vectors. This works in both 2D and 3D.

︡41818db1-4f06-4028-8f44-d7fdd8367363︡{"html": "

Here is an example of how to plot vectors. This works in both 2D and 3D.

"}︡ ︠599765df-0bc5-472d-abc8-58aee1267307︠ plot(u)+plot(v) ︡afdef4ab-62c5-4e1d-ab88-a408b010acfd︡{"html": ""}︡ ︠32217611-1ab4-467a-aa79-d84d3e4b14e0i︠ %html

To create an arrow with its base at $v$ and head at $u$, use arrow(v,u).  To get this to work in 3D, you have to use the command arrow3d(v,u) instead of just arrow(v,u).

︡bbc729c8-7123-453a-9f32-6def7223d4ee︡{"html": "

To create an arrow with its base at $v$ and head at $u$, use arrow(v,u).  To get this to work in 3D, you have to use the command arrow3d(v,u) instead of just arrow(v,u).

"}︡ ︠e405b67e-98f2-4c6b-86ad-783327530ba0︠ plot(u)+plot(v)+arrow(u,u+v)+plot(u+v,color='black') ︡4810a028-e4ab-4c3d-9f59-9f8c28dda15b︡{"html": ""}︡ ︠ca4993ab-d5ca-4219-a4ee-4ae245801a3b︠ plot(u)+plot(v)+plot(u-v,color='black')+arrow(v,u,color='black',linestyle='dashed') ︡6b68d26b-3337-4a09-89a6-dd5f58c05bd8︡{"html": ""}︡ ︠b6aa063a-c2d5-48d8-bcb3-d2531f8ad973i︠ %html

Here's the 3d code, put in a nice html table. If you get an error below, just reevaluate this line of code.

︡4f258bc9-e126-494d-919a-fd103ddc0188︡{"html": "

Here's the 3d code, put in a nice html table. If you get an error below, just reevaluate this line of code.

"}︡ ︠6299cf07-c005-4f2b-8fc4-1d73c4c23e38︠ u=vector([1,2,3]) v=vector([3,1,0]) html.table([ ["u", u], ["v", v], ["|u|", u.norm()], ["|v|", v.norm()], ["u.v", u*v], ]) plot(u)+plot(v) ︡cfda7fa4-9435-4ae1-8851-9bfcd98cd55a︡{"html": "\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
u\\left(1,2,3\\right)
v\\left(3,1,0\\right)
|u|\\sqrt{14}
|v|\\sqrt{10}
u.v5
\n
\n"}︡ ︠1313a2ee-3fde-4584-b1eb-39bf4fb02b41︠ plot(u)+plot(v)+arrow3d(u,u+v)+plot(u+v,color='black') ︡08c05448-5a3c-45e4-a1cb-f548b8189be5︡︡ ︠15f44dd7-d15b-470b-8433-7f35dcacb48e︠ plot(u)+plot(v)+plot(u-v,color='black')+arrow3d(v,u,color='black',linestyle='dashed') ︡56dc9612-4b62-4872-ab24-1bacdb8a94f9︡︡ ︠467ad781-439c-4af1-ad8b-ba39bbab94dbi︠ %html

Entering matrices - and rref (or echelon_form() )

To enter a matrix, use the matrix command. To store the matrix using a name, use A = matrix( ... ).  Here is an example. 

︡36cbc303-157f-438f-98fd-0745d7f5f731︡{"html": "

Entering matrices - and rref (or echelon_form() )

\n\n

To enter a matrix, use the matrix command. To store the matrix using a name, use A = matrix( ... ).  Here is an example. 

"}︡ ︠7fb0630d-c6df-424e-aa64-7c3da2a4150c︠ A = matrix([[-1,2,3,4],[4,5,6,0],[3,2,8,-4]]) A ︡14814759-d531-402f-ba50-a0081c0050ef︡{"stdout": "[-1 2 3 4]\n[ 4 5 6 0]\n[ 3 2 8 -4]"}︡ ︠6e890aca-7ceb-4354-867a-7f70d7a66a89i︠ %html

To row reduce the matrix, type the name of the matrix, a period, and then type echelon_form(). If you are familar with tab completion, Sage includes tab completion.  This means that you can type "A.ec" and then type "Tab" and it will show you all the commands that start with "ec" which can be applied to the matrix A. The command .rref() is availalbe in Sage version 4.3.4, so the next official release will allow us to type A.rref().

︡e8213185-c33c-45be-a350-55a05bbc4abc︡{"html": "

To row reduce the matrix, type the name of the matrix, a period, and then type echelon_form(). If you are familar with tab completion, Sage includes tab completion.  This means that you can type \"A.ec\" and then type \"Tab\" and it will show you all the commands that start with \"ec\" which can be applied to the matrix A. The command .rref() is availalbe in Sage version 4.3.4, so the next official release will allow us to type A.rref().

"}︡ ︠e7d90c1f-c2d7-429b-bbbf-2493b5b7a78b︠ A.echelon_form() ︡2af6ff5e-11cd-44ab-a99b-6e836b6d90a0︡{"stdout": "[ 1 0 59 -20]\n[ 0 1 31 -8]\n[ 0 0 77 -24]"}︡ ︠7cff6758-ec21-4a56-b563-1997f7120effi︠ %html

Notice that the 77 above is not a leading 1.  This is because Sage keeps track of the number system behind the matrix.  If you type in a matrix with all integer entries, then Sage will assume that all operations are to be done using only integers.  It will not fully reduce a matrix if the matrix requires division that will result in fractions.  To fix this, you can tell Sage that you want to work with fractions by typing QQ at the beginning of your matrix.  QQ is the Sage symbol for "Rational Numbers" or $\mathbb Q$ 

︡2c959c26-91d9-48b5-959b-ba852f941b75︡{"html": "

Notice that the 77 above is not a leading 1.  This is because Sage keeps track of the number system behind the matrix.  If you type in a matrix with all integer entries, then Sage will assume that all operations are to be done using only integers.  It will not fully reduce a matrix if the matrix requires division that will result in fractions.  To fix this, you can tell Sage that you want to work with fractions by typing QQ at the beginning of your matrix.  QQ is the Sage symbol for \"Rational Numbers\" or $\\mathbb Q$ 

"}︡ ︠2c0ceb8b-a430-4afc-993f-8099e2cd801f︠ A = matrix(QQ,[[-1,2,3,4],[4,5,6,0],[3,2,8,-4]]) A ︡4101c722-5433-43cf-96e4-7c8d8190e667︡{"stdout": "[-1 2 3 4]\n[ 4 5 6 0]\n[ 3 2 8 -4]"}︡ ︠f45ba0e1-2257-4014-b3b0-c9aef02659a1︠ A.echelon_form() ︡ddf209ab-dc5e-4854-ac65-9f13786b1236︡{"stdout": "[ 1 0 0 -124/77]\n[ 0 1 0 128/77]\n[ 0 0 1 -24/77]"}︡ ︠bc7ff38c-144c-4502-b352-82800a27e731i︠ %html

A quick way to enter matrices.

I prefer the following way to enter matrices.  Type QQ to allow fractional arithmetic, then the number of rows, numbers of columns, and then a list of the entries read left to right, top to bottom.  You can eliminate writing lots of brackets this way.

︡dadd70ef-bc7f-42e0-a4b5-12834703453f︡{"html": "

A quick way to enter matrices.

\n

I prefer the following way to enter matrices.  Type QQ to allow fractional arithmetic, then the number of rows, numbers of columns, and then a list of the entries read left to right, top to bottom.  You can eliminate writing lots of brackets this way.

"}︡ ︠c4c98811-2fba-4aab-b71c-1fd6e2709326︠ A=matrix(QQ,2,3,[1,2,3,4,5,6]) A ︡8885fb1d-af92-4e7f-bdc7-c7b20ad2e775︡{"stdout": "[1 2 3]\n[4 5 6]"}︡ ︠bee15349-24bd-4845-ae23-78c291300157i︠ %html

Making a matrix of column vectors - Using transpose()

If you want to make a matrix of vectors, Sage will quickly place the vectors into rows.  To get them to be columns, use the transpose command.

︡ed7aa8c5-edff-4fa8-baf6-a3311d7c5e3d︡{"html": "

Making a matrix of column vectors - Using transpose()

\n

If you want to make a matrix of vectors, Sage will quickly place the vectors into rows.  To get them to be columns, use the transpose command.

"}︡ ︠d19de006-f3b7-406b-82de-0cbda2cb812c︠ u=vector([1,2]) v=vector([3,4]) w=vector([5,6]) A=matrix(QQ,[u,v,w]) A ︡e3146ba8-f67e-4c04-b642-94541ddeb942︡{"stdout": "[1 2]\n[3 4]\n[5 6]"}︡ ︠fd2e1957-590c-4a26-b7d6-0b43b07a6670︠ B=A.transpose() B ︡a2d1b4e4-2cef-4915-bb60-131f36e9fc48︡{"stdout": "[1 3 5]\n[2 4 6]"}︡ ︠48a9f788-93b7-4223-8f70-b6d5511c3f97i︠ %html

Selecting entries of a matrix

You can access specific entries in a matrix by typing A[i,j].  However, note that Sage begins all numbering at 0 instead of 1.  So the $a_{11}$ spot in the matrix is $A[0,0]$.

︡bd145039-3052-47ed-a39a-5ad698fbc7c8︡{"html": "

Selecting entries of a matrix

\n

You can access specific entries in a matrix by typing A[i,j].  However, note that Sage begins all numbering at 0 instead of 1.  So the $a_{11}$ spot in the matrix is $A[0,0]$.

"}︡ ︠f5b4e06a-41b8-4c82-a153-90a12551c99f︠ A=matrix(QQ,2,3,[1,2,3,4,5,6]) A ︡49e4bf36-86e2-43a6-8dcf-5e08c76027ce︡{"stdout": "[1 2 3]\n[4 5 6]"}︡ ︠0e5da498-3ffd-41a3-857c-41ffef7432ce︠ A[0,0] ︡d03bf76a-50e4-4fc1-abef-4fedc9f61c08︡{"stdout": "1"}︡ ︠ccd48e3d-71d1-450f-8d9e-53d893593a91︠ A[0] ︡698b5dd4-e266-4ba4-8db6-505756651ef8︡{"stdout": "(1, 2, 3)"}︡ ︠bbb1aff5-c979-4f21-bb9c-49448b378aa2︠ A[1,2] ︡581b07af-624c-4cf5-a0fb-ba1616b6f785︡{"stdout": "6"}︡ ︠d8c5f79b-5540-487f-a038-b6420c7b9f22i︠ %html

Matrix Multiplication

Just enter both matrices, and then use an asterisk * to multiply them.

︡09133516-a34c-463d-97ed-dae3528d3056︡{"html": "

Matrix Multiplication

\n\n

Just enter both matrices, and then use an asterisk * to multiply them.

"}︡ ︠8d456070-8247-4829-9225-6fb4da51af95︠ A=matrix(QQ,2,3,[1,2,3,4,5,6,]) A ︡2a5e0a51-5599-4c40-adff-84eeee9cd931︡{"stdout": "[1 2 3]\n[4 5 6]"}︡ ︠f454800e-47d1-4361-a259-3171fc400f26︠ B=matrix(QQ,3,2,[1,2,3,4,5,6,]) B ︡6088d3cc-b5be-4f96-bfc9-5e78a54d5220︡{"stdout": "[1 2]\n[3 4]\n[5 6]"}︡ ︠3d571230-1e1a-4fb6-9ec4-e2d4ffda0395︠ A*B ︡38b39dab-8cd2-4286-a228-0478faa6c7ea︡{"stdout": "[22 28]\n[49 64]"}︡ ︠695c3092-f9cd-47b5-a4e5-8b35358cbeac︠ B*A ︡6106945e-f8ad-47dc-b1cc-d88f71f9e8b8︡{"stdout": "[ 9 12 15]\n[19 26 33]\n[29 40 51]"}︡ ︠02aa8b25-bc8e-42a3-bc51-d5e70ca1b696i︠ %html

You'll get an error if matrix multiplication doesn't make sense.

︡45b6e293-7b6d-4a47-a3b0-bd6c38afd3b6︡{"html": "

You'll get an error if matrix multiplication doesn't make sense.

"}︡ ︠e9e9f118-08a0-4e7a-96af-5f5e1c479154︠ A*A ︡062a3c48-208e-46f9-b3e3-6bb26b394d6e︡{"stderr": "Traceback (most recent call last):\n File \"\", line 1, in \n File \"_sage_input_27.py\", line 4, in \n exec compile(ur'A*A' + '\\n', '', 'single')\n File \"\", line 1, in \n \n File \"element.pyx\", line 2028, in sage.structure.element.Matrix.__mul__ (sage/structure/element.c:13799)\n File \"matrix_rational_dense.pyx\", line 1028, in sage.matrix.matrix_rational_dense.Matrix_rational_dense._matrix_times_matrix_ (sage/matrix/matrix_rational_dense.c:11135)\n File \"matrix_rational_dense.pyx\", line 2504, in sage.matrix.matrix_rational_dense.Matrix_rational_dense._multiply_pari (sage/matrix/matrix_rational_dense.c:21860)\nArithmeticError: self must be a square matrix"}︡ ︠4748cb1d-f27e-4e83-aaec-8cc1c8484a77i︠ %html

Rank, Determinants, Inverses, Eigenvalues, and Eigenvectors

Most commands we will need all semester long are available using tab completion.  First enter your matrix, then type A and a period, and then type the "Tab" key.  This will bring up a list of commands.  Try doing this now in the blank cell below (after evaluating the first cell by clicking in it and evaluating it (type Shift+Enter).

︡08a3698f-93d5-4f44-8df3-6133c250cdff︡{"html": "

Rank, Determinants, Inverses, Eigenvalues, and Eigenvectors

\n

Most commands we will need all semester long are available using tab completion.  First enter your matrix, then type A and a period, and then type the \"Tab\" key.  This will bring up a list of commands.  Try doing this now in the blank cell below (after evaluating the first cell by clicking in it and evaluating it (type Shift+Enter).

"}︡ ︠9b8d9a01-5cdb-4165-b87a-8d61fdf0e956︠ A=matrix(QQ,3,3,[2,1,0,1,2,0,0,0,1]) A ︡40406ea8-7d33-4df6-b705-12a5d1d3b903︡{"stdout": "[2 1 0]\n[1 2 0]\n[0 0 1]"}︡ ︠ce066e70-77ab-475c-82e7-0e0da20bd95ai︠ %html

We'll be using the rank(), det(), inverse(), eigenvalues(), and eigenvectors_right() commands.

︡f6d6d7cc-611c-4af8-85f7-790cda73d528︡{"html": "

We'll be using the rank(), det(), inverse(), eigenvalues(), and eigenvectors_right() commands.

"}︡ ︠c14a846a-aaf2-41f7-8ff1-2f5c753424c2︠ A.rank() ︡8509e8dc-2563-4c43-99a6-1a889423e4af︡{"stdout": "3"}︡ ︠317a3581-824f-4b43-aa1e-94ae727dd731︠ A.det() ︡92ec81f1-d522-4b73-9448-a76b6fac0f6b︡{"stdout": "3"}︡ ︠9bdcae94-05ec-426b-9a98-aab20c0f89ea︠ A.echelon_form() ︡ffe896b6-f275-4e49-90c3-9229517b150e︡{"stdout": "[1 0 0]\n[0 1 0]\n[0 0 1]"}︡ ︠b89a46e0-438e-4528-9fc6-8e74b7383fbb︠ A.inverse() ︡c3afb824-1fb1-4dcc-b899-da1dbdabfdb6︡{"stdout": "[ 2/3 -1/3 0]\n[-1/3 2/3 0]\n[ 0 0 1/3]"}︡ ︠d8489545-be59-434e-96b9-5e7e6ff57cc6︠ A.eigenvalues() ︡07694994-b4d6-4e99-8229-ef4876df1007︡{"stdout": "[3, 1, 1]"}︡ ︠429db4ea-fe86-4c6d-b8ec-f02c903976ed︠ A.eigenvectors_right() ︡f17fbf36-b96a-487b-bd3e-0ed467b3dc92︡{"stdout": "[(3, [\n(1, 1, 0)\n], 1), (1, [\n(1, -1, 0),\n(0, 0, 1)\n], 2)]"}︡ ︠aa522b13-fbf9-4123-9c1d-ab904ce36e00i︠ %html

Two questions:

  1. Why "right"?
  2. How do you read this?

We use "right" because we are trying to solve $A\vec x = \lambda \vec x$ instead of $\vec x A = \vec x \lambda$. The latter are called "left" eigenvectors.

To read the output, you are given a list of values for each eigenvalue.  In particular, you are given (the eigenvalue, a set of linearly independent eigenvectors whose span gives all the eigenvalues, and the multiplicity of the eigenvalue - how many times it is a root).

[(3, [             -the eigenvalue
(1, 1, 0) -an eigenvector whose span gives all the rest
], 1), -3 has multiplicity 1.
(1, [ -1 is the next eigenvalue
(1, -1, 0), -Here there are 2 independent eigenvectors whose span gives the rest
(0, 0, 1)
], 2)] -$\lambda=1$ has multiplicty 2
︡70ec249d-0aee-4fc8-8912-9fc68d85c55f︡{"html": "

Two questions:

\n
    \n
  1. Why \"right\"?
  2. \n
  3. How do you read this?
  4. \n
\n

We use \"right\" because we are trying to solve $A\\vec x = \\lambda \\vec x$ instead of $\\vec x A = \\vec x \\lambda$. The latter are called \"left\" eigenvectors.

\n

To read the output, you are given a list of values for each eigenvalue.  In particular, you are given (the eigenvalue, a set of linearly independent eigenvectors whose span gives all the eigenvalues, and the multiplicity of the eigenvalue - how many times it is a root).

\n
[(3, [             -the eigenvalue
(1, 1, 0) -an eigenvector whose span gives all the rest
], 1), -3 has multiplicity 1.
(1, [ -1 is the next eigenvalue
(1, -1, 0), -Here there are 2 independent eigenvectors whose span gives the rest
(0, 0, 1)
], 2)] -$\\lambda=1$ has multiplicty 2
"}︡ ︠d1fa1758-7740-4d3d-bbbe-4faa02eea304i︠ %html

Everything in a nice table format (provided the answers are nice)

This final example just computes all the values above for a square matrix, and displays them in an easy to read format. If the eigenvalues are rather complicated, then the display may get ugly and you are better off using the commands above.

︡5bbb54f5-6f07-4236-8a88-48881fc41b4d︡{"html": "

Everything in a nice table format (provided the answers are nice)

\n

This final example just computes all the values above for a square matrix, and displays them in an easy to read format. If the eigenvalues are rather complicated, then the display may get ugly and you are better off using the commands above.

"}︡ ︠4c85a11e-cd60-40a1-a7a4-4b7dac0603f1︠ A=matrix(QQ,3,3,[2,1,0,1,2,0,0,4,3]) EV=A.eigenvectors_right() if A.det()==0: inv="No Inverse Exists" ; else: inv=A.inverse(); list=([ ["Matrix",A], ["Transpose",A.transpose()], ["RREF",A.echelon_form()], ["Rank",A.rank()], ["Determinant",A.det()], ["Inverse",inv], ["Eigenvalues",A.eigenvalues()],[],[],[],[], ["Eigenvalue","Linearly Independent Eigenvectors", "Multiplicity of Eigenvalue"]]) for n in range(len(EV)): list.append(EV[n]) html.table(list) ︡bf01264c-33fe-4a4d-8e62-467e29492196︡{"html": "\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Matrix\\left(\\begin{array}{rrr}\n2 & 1 & 0 \\\\\n1 & 2 & 0 \\\\\n0 & 4 & 3\n\\end{array}\\right)
Transpose\\left(\\begin{array}{rrr}\n2 & 1 & 0 \\\\\n1 & 2 & 4 \\\\\n0 & 0 & 3\n\\end{array}\\right)
RREF\\left(\\begin{array}{rrr}\n1 & 0 & 0 \\\\\n0 & 1 & 0 \\\\\n0 & 0 & 1\n\\end{array}\\right)
Rank3
Determinant9
Inverse\\left(\\begin{array}{rrr}\n\\frac{2}{3} & -\\frac{1}{3} & 0 \\\\\n-\\frac{1}{3} & \\frac{2}{3} & 0 \\\\\n\\frac{4}{9} & -\\frac{8}{9} & \\frac{1}{3}\n\\end{array}\\right)
Eigenvalues\\left[1, 3, 3\\right]
EigenvalueLinearly Independent EigenvectorsMultiplicity of Eigenvalue
1\\left[\\left(1,-1,2\\right)\\right]1
3\\left[\\left(0,0,1\\right)\\right]2
\n
\n"}︡