Matrices and Linear Algebra

The Mathematica front end provides an Insert->Table/Matrix submenu for creating and editing arrays with any specified number of rows and columns. Once you have such an array, you can edit it to fill in whatever elements you want.

Some examples of vectors and matrices:

Mathematica treats a matrix like a list of lists. To see it in standard matrix form, use the command MatrixForm[..]

A=(({{1, 0, 0}, {1, 1, 1}}))

IdentityMatrix[3]
MatrixForm[%]

A vector can be given as a list. Column vectors and row vectors are represented identically.

w={1,2,3}
MatrixForm[w]

We can also use the Insert->Table/Matrix submenu to create a column vector with a given number of elements, as a matrix with n rows and one column(({{□}, {□}, {□}})). If we use this template, the vector is interpreted as a list of lists.

v=(({{1}, {2}, {3}}))

The inverse of a matrix

Inverse[A] computes the inverse of an nxn square matrix A, if the matrix is invertible, and displays an error message otherwise. A matrix whose determinant is different from 0 is invertible. Its inverse is the unique nxn square matrix B wih the property that AB = BA = IdentityMatrix[n].

M={{1,2},{3,4}};
Inverse[M]
Print[MatrixForm[M]MatrixForm[Inverse[M]],"=",MatrixForm[Inverse[M]]MatrixForm[M],"=",MatrixForm[IdentityMatrix[2]]]

Matrix and Vector Operations

https : // reference.wolfram.com/language/guide/MatrixOperations.html

We can perform matrix addition A+B and scalar multiplication 3A in the usual way. However, to do matrix multiplication we cannot use the symbol * , instead we have to use a dot! If two matrices A and B cannot be multiplied together (because of incompatible sizes), you will receive an error message like “Tensors A and B have incompatible shapes”.

A=(({{1, 0, 0}, {1, 1, 1}}));
A+A; MatrixForm[%]
MatrixForm[3A]
A.A
A.Transpose[A]
MatrixForm[%]

A.w
A.v
MatrixForm[A.w]
MatrixForm[A.v]

Transpose[matrix] -- Computes the transpose
MatrixPower[matrix, power] -- For square matrices, we can compute the power of a matrix (A^n) 
Eigenvalues[matrix] -- Finds the eigenvalues of a square matrix
Eigensystem[matrix] -- Finds finds both the eigenvalues and a complete linearly independent set of eigenvectors for each eigenvalue.
Eigenvectors[matrix] -- Finds the eigenvectors of a square matrix
Inverse[matrix] -- Finds the inverse of a square matrix

Linear Algebra - Eigenvectors and Eigenvalues

The function Eigenvalues[A] returns a list with the eigenvalues of the square matrix A. Recall that a number λ is called an eigenvalue of A if there exists a nonzero vector v such that Av=λv. The function Eigensystem[A] finds both the eigenvalues and a complete linearly independent set of eigenvectors for each eigenvalue.  The output is given in the form {list of eigenvalues, list of eigenvectors}. If the geometric multiplicity of an eigenvalue is less than its algebraic multiplicity, then the list of eigenvectors is completed with 0 vectors.

Consider an nxn matrix A. The Jordan Decomposition Theorem says there exists an invertible matrix S such that (S^(-1))A S=J , where J is called the Jordan canonical form of A (J is an upper triangular matrix whose diagonal entries are the eigenvalues of A, and immediately above the diagonal some entries are equal to 1). The function JordanDecomposition[A] returns a list of the form {S, J}, where J is the Jordan canonical form of A. S is one choice of matrix satisfying (S^(-1))A S=J, although it’s not the usually the one that we would get when doing the computations by hand.

M = (({{2, 0, 1}, {0, 1, -1}, {-1, 0, 0}}));
Eigenvalues[M]
Eigensystem[M]
{S,J}=JordanDecomposition[M];
Print["The Jordan canonical form of M is ",MatrixForm[J]]
Print["The result of the matrix Multiplication (SMS^(-1)) is the Jordan canonical form J"]
MatrixForm[Inverse[S].M.S]

Exercise 1 : Find the eigenvalues and eigenvectors of matrix B.

B={{1,1},{1,1}};

Changing Elements

To access different matrix or vector elements use [[..]]

w={10,20,30}; w[[1]]

B={{10,20},{30,40}};
B[[1]]

To access the  element on row i and column j of matrix B, we use B[[i, j]] or B[[ i ]] [[ j ]]. Remember to always use double brackets, as simple brackets are reserved for functions.

B[[1]][[1]]
B[[1,1]]

To change elements of a matrix or vector:

B[[2,1]]=2017;
MatrixForm[B]
w[[1]]=2017; w

A lot of vector operations are already built-in

Length[..] -- returns the number of elements in a list.
Sum[..] -- returns the sum of elements in a list.
Product[..] -- returns the product of elements in a list.

w={1,2,3};
w[[1]]+w[[2]]+w[[3]]
Sum[w[[i]],{i,1,Length[w]}]

Loops and Vectors/Matrices

Find the sum of the elements of a square matrix.

A={{1,2,3},{4,5,6},{7,8,9}};

We can use the built - in command Sum[ ..]

Sum[A[[i]][[j]],{i,1,Length[A]},{j,1,Length[A]}]

We can use the Do[...] loop. The variable sumElements is initially set to 0. We then add each (i,j) element of the matrix A to the variable sumElements.

sumElements=0;
Do[sumElements=sumElements+A[[i]][[j]], 
{i,1,Length[A]},{j,1,Length[A]} ];
Print[sumElements]

We can use two For[...] loops. The second For loop is highlighted in LightYellow.

sumElements =0;
For[i=1,i≤Length[A],i++, For[j=1,j≤Length[A],j++,sumElements=sumElements + A[[i]][[j]] ] ];
Print[sumElements]

Similarly, we can use two While[...] loops. The second While loop is highlighted in LightYellow.

sumElements =0;
i=1;
While[i≤Length[A], j=1; While[j≤Length[A], sumElements=sumElements+A[[i]][[j]];j++ ];
i++];
Print[sumElements]

Other ways of generating a matrix:

Table[f, {i,m}, {j,n}] builds an mxn matrix by evaluating the function f with arguments i and j, where i ranges from 1 to m and j ranges from 1 to n. The lower bound is implicitly 1, so we only specify the upper bound.

The following commands generate random 4x4 matrix with entries between 0 and 1:

Rn=Table[Random[],{i,4},{j,4}]

Ri=Table[RandomInteger[],{4},{4}]

We may also want to define our own function f, and use it to define a matrix with a given pattern. For example, the code below generates a matrix whose (i,j)-entry is equal to i+j.

f[i_,j_]:=i+j
NewM= Table[f[i,j],{i,4},{j,4}];
MatrixForm[NewM]

Exercise 2: Generate a 7x7 matrix B 
                with Bij=i if i>=j  and 
                       Bij=0 otherwise

Exercise 3: Find the sum of the elements on the 5th row of the matrix B that you have generated in exercise 2. Use your favorite loop (Do, While, For).