To enter a matrix into Mathematica, you need to use curly brackets, and only curly brackets. You enter the matrix by rows, with each row in curly brackets. Below I have entered the matrix .
1 | 2 |
3 | 4 |
matrix1={{1,2},{3,4}}
{{1,2},{3,4}}
You can also use the Pallettes to define a matrix. If you don’t see any screens off to the right, choose “Basic Math Assistant” from the Pallettes menu above. Scroll down to Typesetting, and there is a palette for a 2x2 matrix.
| |
| |
If you need to enter a bigger matrix, choose the “Classroom Assistant” palette, and choose the Advanced tab at the top. Click in an input cell, then start clicking “Row+“ and “Col+“ to add rows and columns.
| | | |
| | | |
| | | |
| | | |
Matrices entered using curly brackets can be a little hard to read, so the MatrixForm command can be helpful.
MatrixForm[matrix1]
1 | 2 |
3 | 4 |
I’ll enter another matrix to enable me to demonstrate matrix operations.
In[]:=
matrix2={{-1,3},{-2,4}}
Out[]=
{{-1,3},{-2,4}}
Adding matrices is as easy as typing a plus sign.
matrix1+matrix2
{{0,5},{1,8}}
MatrixForm[%](*The%giveyouthepreviousoutput.Typing%ngivesyouthenthoutputasnumberedontheleft.*)
0 | 5 |
1 | 8 |
Multiplying matrices takes a bit more care. You need to be sure to use a period to indicate matrix multiplication, instead of term-by-term multiplication. Check the computation below to make sure you can do matrix multiplication by hand.
matrix1.matrix2
{{-5,11},{-11,25}}
MatrixForm[matrix1.matrix2]
-5 | 11 |
-11 | 25 |
You can raise a square matrix to a power using the intuitively-named command, MatrixPower. Let’s raise matrix1 to the 20th power. We wouldn’t want to do that by hand! Putting the two backslashes then the command MatrixForm applies the command to the output of the operation.
MatrixPower[matrix1,20]//MatrixForm
95799031216999 | 139620104992450 |
209430157488675 | 305229188705674 |
Wow. Those are big numbers! Glad we had Mathematica to help us out! Let’s see which other matrix operations we learned in this section can be computed using Mathematica.
Diagonal[matrix1]
{1,4}
IdentityMatrix[3]
{{1,0,0},{0,1,0},{0,0,1}}
MatrixForm[%]
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 1 |
Transpose[matrix1]//MatrixForm
1 | 3 |
2 | 4 |
Inverse[matrix1]//MatrixForm
-2 | 1 |
3 2 | - 1 2 |
Inverse[matrix1].matrix1//MatrixForm
1 | 0 |
0 | 1 |
Almost all of them! And the commands are really easy to remember, too!
We do need to be able to make a zero matrix ourselves, but that’s pretty easy using the Table command. To make an nxn zero matrix, use the code below.
n=3;zeromatrix=Table[0,{i,1,n},{j,1,n}]
{{0,0,0},{0,0,0},{0,0,0}}
MatrixForm[%]
0 | 0 | 0 |
0 | 0 | 0 |
0 | 0 | 0 |
One more thing to notice. I’ve entered all of my comments in text cells. Doing so makes certain that your comments cannot be evaluated like functions. To make a text cell, you can click on the bracket of a cell on the right and type “Alt 7” or from the “Format” menu choose “Style - Text”. Or under the “Cell” menu choose “Convert To - Text Display”.
You should ALWAYS use text cells to enter your comments.
You should ALWAYS use text cells to enter your comments.