A Useful Trick for Generating 
Symbolic Matrices

Visualizing matrix operations on arbitrary symbolic matrices with "standard" subscripts on the elements is a helpful learning and discovery tool.

by Danny W. Turner and Robert Piziak

With Mathematica 3.0 manipulation and visualization of matrices have never been easier.  Here we provide a simple way to generate arbitrary size matrices containing subscripted symbolic entries.  The output form will look like that of a standard linear algebra text.  The reader should consult Wagner[1997] for related material.

The ability to produce and manipulate the usual textbook form of a matrix will make it easy to "track" the actions of various matrix operations and will be especially useful for those who are first learning the basics of matrix theory.  A number of illustrations are provided.

THE MATRIX GENERATION COMMAND

Here is the simple procedure to create our command genmat.

Clear[genmat,sub,m,n,i,j,b]sub[i_,j_,b_] :=HoldForm[Subscript[b,i*j]]genmat[m_,n_,b_] := Table[sub[i,j,b],{i,m},{j,n}]

We can now produce a general symbolic matrix with the "usual" subscripted entries.

genmat[2,3,s]//MatrixForm

Let's create a column vector and a row vector.

Clear[mata,matb]mata =genmat[5,1,a];matb = genmat[1,5,b];
mata//MatrixFormmatb//MatrixForm

MATRIX OPERATION ILLUSTRATIONS

Now we can illustrate the multiplication of these two vectors and see that the "pattern" of the general 
result of such an operation is clear.

mata.matb//MatrixForm

It is an easy matter to illustrate the pattern for matrix multiplication.

Clear[A,B];
A =genmat[3,3,a];
B = genmat[3,2,b];
(A//MatrixForm).(B//MatrixForm)A.B//MatrixForm%[[1,2]]

The following definition will simplify using the //MatrixForm operation in the remainder of our examples.

Clear[mf]mf=MatrixForm;

We all know what happens to a given matrix when it is multiplied (on the left for example) by an identity matrix.  But what happens if we permute the rows of our identity matrix and then multiply?  Let's investigate.

i3 = IdentityMatrix[3];
permi3={i3[[2]],i3[[1]],i3[[3]]};
mf[permi3].mf[A]mf[permi3.A]

Well, it looks like by interchanging the first two rows of a 3×3 identity matrix and multiplying on the left of A, we've interchanged the first two rows of A!  Have fun discovering other general results about elementary matrix operations.

Here is a more complicated illustration involving the pseudoinverse(Moore–Penrose inverse) of a 
rectangular matrix.  First we look at a familiar problem--the inverse of a 2×2 matrix.  The use
of the function ComplexExpand carries out an expansion assuming that all variables are real.

Of course symbolically we see that Apseudo is the same as the usual inverse A-1 and not surprisingly we observe that AA-1 = (Subscript[I, 2×2]) .

Clear[A,Apseudo,a]A=genmat[2,2,a];
mf[A]Apseudo=PseudoInverse[A]//Simplify//ComplexExpand;
mf[Apseudo]mf[A].mf[Apseudo]mf[A.Apseudo//Simplify]

Now, let's look at the "inverse" of a rectangular matrix.  We define the general 2×3 matrix B, find
Bpseudo, the Moore–Penrose inverse, and "clean" it up a bit.  The output is suppressed due to the size of the full matrix.  The dimensions of Bpseudo are output to check for correctness.

Clear[B,Bpseudo,b]B=genmat[2,3,b];
mf[B]Bpseudo=PseudoInverse[B]//Simplify//ComplexExpand;
Print["Bpseudo has dimensions:  ",Dimensions[Bpseudo]]

In the next cell we look at one(simplified) entry from  Bpseudo.

mf[{{Bpseudo[[1,2]]//Simplify//Together}}]

Let's check the conditions that guarantee we have indeed found the correct pseudoinverse of B.

mf[B.Bpseudo.B//Simplify](Bpseudo.B.Bpseudo==Bpseudo)//Simplify//Together(Transpose[B.Bpseudo]==B.Bpseudo)//Simplify//Together(Transpose[Bpseudo.B]==Bpseudo.B)//Simplify//Together

All defining conditions are seen to hold and thus we have obtained the Moore–Penrose generalized inverse of B.  

It is also very interesting to look at the following matrices.  Due to the size of the expressions, only the entry in row 1 and column 2 is viewed for the matrix Bpseudo.B.

mf[(B.Bpseudo)//Simplify]mf[{{(Bpseudo.B)[[1,2]]//Simplify}}]

An obvious theoretical question is generated by looking at the above results.  Is the appearance of the 2×2 identity matrix a "fluke" or is there a deeper result waiting to be discovered?  (There is in fact a deeper result and the interested reader should consult an advanced matrix theory text.)

In conclusion, we hope these examples will inspire readers to make use of our tips/tricks to help elucidate some of the many aspects of linear algebra and matrix theory.

REFERENCES

Wagner, David B.  1997.  Functional iteration.  The Mathematica Journal  7 (1) 42–53.

ABOUT THE AUTHORS

Danny W. Turner received a Ph.D. from Clemson University in 1973 and is currently Professor of Mathematics at Winthrop University.  He enthusiastically uses Mathematica in both his teaching and research.

Danny W. Turner
Department of Mathematics
Winthrop University
Rock Hill, SC 29733
turnerd@winthrop.edu

Robert Piziak received his Ph.D. from the University of Massachusetts in 1969 and is currently a Professor in the mathematics department at Baylor University, where he has been since 1981.  He has extensive experience with Mathematica and has developed a large collection of classroom materials to guide his students through many aspects of the efficient use of this powerful software.

Robert Piziak
Department of Mathematics
Baylor University
P.O. Box 97328
Waco, TX 76798-7328
robert_piziak@baylor.edu

ELECTRONIC SUBSCRIPTIONS

Included in the distribution for each electronic subscription is the file MatrixNotation.nb containing Mathematica code for the material described in this article.