% maple lab 5 Linear Algebra S2015
\documentclass[leqno]{article}
\input{linalg.sty}
\def\MAPLE{{\tt Maple}}
\def\rf#1{(\ref{#1})}
\def\Prob#1#2{\par\bigskip\par\noindent
{\Large\sf Problem #1}. {\bf #2}\par}
\parskip=3pt
\def\MAPLE{{\tt maple}}
\def\RHS{\mbox{RHS}}
\def\LHS{\mbox{LHS}}
% \begin{topics}{template} ... \end{topics}
% used like enumerate, itemize, description environments
% The item[] is never optional!
%
\newcommand{\topicslabel}[1]{\mbox{#1}\hfil}
\newenvironment{topics}[1]{
    \begin{list}{}{%
      \let\makelabel\topicslabel
      \settowidth{\labelwidth}{\topicslabel{#1}}
      \setlength{\leftmargin}{\labelwidth}
      \advance\leftmargin by 1.5em}
}{\end{list}}
 \pagestyle{plain}
 \setlength{\textheight}{10in}
\begin{document}
\thispagestyle{empty} {\bf
Name~}\underline{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}\hfill
 {Class Time~}\underline{~~~~~~~~}\newline

\begin{center}
\bf Math 2250
\bf Extra Credit Maple Project 5: Linear Algebra \\
\bf S2015
\end{center}


Extra credit Maple lab 5 has four
problems: L5-1, L5-2, L5-3, L5-4. Examples of the {\tt maple} coding required
appears in five examples at the end of this document.

{\bf References}: %Edwards-Penney, pages 55--57.
Code in \MAPLE{} appears in \verb|2250mapleL5-S2015.txt| at URL
\verb|http://www.math.utah.edu/~gustafso/|. This document:
\verb|2250mapleL5-S2015.pdf|.

\smallskip

\Prob{L5-1}{(Matrix Algebra)} Define $\dd
A=\left(\begin{array}{rrr} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9
\end{array}\right)$,
$\dd B=\left(\begin{array}{rrr} 2 & 1 & 0 \\ 1 & 2 & 1 \\ 0 & 1 & 2
\end{array}\right)$, $\.v=\left(\begin{array}{r} 1\\ 2\\ 3
\end{array}\right)$ and $\.w=\left(\begin{array}{r} -1\\ 4 \\ 1 \end{array}\right)$.
Create a worksheet in \MAPLE{} which states this problem in text,
then defines the four objects. The worksheet should contain text,
\MAPLE{} code and displays. Continue with this worksheet to answer
{\bf (1)}--{\bf (7)} below. Submit problem L5.1 as a worksheet
printed on 8.5 by 11 inch paper. See Example 1 for maple commands.
\begin{itemize}
 \item[{\bf (1)}]  Compute $AB$ and $BA$.  Are they the same?
 \item[{\bf (2)}]   Compute $A+B$ and $B+A$.  Are they the same?
 \item[{\bf (3)}]   Let $C=A+B$. Compare $C^2$ to $A^2 +
 2AB + B^2$. Explain why they are different.
 \item[{\bf (4)}] Compute transposes $C_1=(AB)^T$, $C_2=A^T$ and
 $C_3=B^T$. Find a matrix equation for $C_1$ in terms of $C_2$ and
 $C_3$. Verify the equation.
 \item[{\bf (5)}] Solve for $\.X$ in $B\.X=\.v$ by maple commands {\tt
 linalg[rref], linalg[linsolve]}, then by inversion $\.X=A^{-1}\.b$.
 \item[{\bf (6)}] Solve $A\.Y=\.v$ for $\.Y$.  Do an answer check using
 {\tt linalg[linsolve]}.
 \item[{\bf (7)}] Solve $A\.Z=\.w$. Explain your answer using the three
 possibilities for a linear system. Discuss the possible maple reports
 for (1) no solution case, (2) unique solution, (3) infinitely many
 solutions.
\end{itemize}

\Prob{L5-2}{(Independent Columns)}
%A:=matrix([[ 1, 1, 1, 2, 6],
%           [ 2, 3,-2, 1,-3],
%           [ 0, 1,-4,-3,-15],
%           [ 1, 2,-3,-1,-9]]);
Let $A=\left(\begin{array}{rrrrr}
 1    & 1    &  1    &  2     &  6 \\
 2    & 3    & -2    &  1     & -3 \\
 0    & 1    & -4    & -3     & -15 \\
 1    & 2    & -3    & -1     & -9
\end{array}\right)$.

 Find independent vectors which have the same span as the columns of $A$ using the
following methods.

\begin{description}
 \item[{\bf Method 1}.] Find the pivot columns of $A$. See Example 2.
 \item[{\bf Method 2}.] The \MAPLE{} command \fbox{\tt linalg[colspace](A)}.
\end{description}

The first method is equivalent to finding a largest set of
independent vectors from the list of $5$ vectors formed from the
columns of $A$. The answer is a basis of $2$ vectors. The span of
these $2$ vectors equals the span of the $5$ column vectors of $A$.
The second method finds another basis of $2$ vectors, which is
generally different, but equivalent in the sense described in the
next part.

\Prob{L5-3}{(Equivalent Bases)}
Let
$$                      \.v_1=\colvector{1\\ 2\\ 0\\ 1}, \quad
                        \.v_2 = \colvector{1\\ 3\\ 1\\ 2}, \quad
                        \.w_1 = \colvector{1\\ 0\\ -2\\ -1},\quad
                       \.w_2 = \colvector{0 \\ 1\\ 1\\ 1}.
                       $$
Verify that the
two bases ${\cal B}_1=\{\.v_1,\.v_2\}$ and ${\cal
B}_2=\{\.w_1,\.w_2\}$ are {\bf equivalent}. This means that each
vector in ${\cal B}_1$ is a linear combination of the vectors in
${\cal B}_2$, and conversely, that each vector in ${\cal B}_2$ is a
linear combination of the vectors in ${\cal B}_1$. Briefly,
$$\span\{\.v_1,\.v_2\} = \span\{\.w_1,\.w_2\}.$$

\vfill \underline{~~~~~~~~~~~~~~~~~~~~~~~~~~}\newline{{\sf Staple
this page on top of the maple work sheets.}}\newpage


% A := matrix([[8, 10, 3], [-3, -5, -3], [-4, -4, 1]]);
% P:=matrix([[1,-1,1],[-1,1,0],[-1,0,1]]);
% T:=matrix([[1,0,0],[0,-2,0],[0,0,5]]);

\Prob{L5-4}{(Matrix Equations)}
%A:=matrix([[ 1, 1, 1, 2, 6],
%           [ 2, 3,-2, 1,-3],
%           [ 0, 1,-4,-3,-15],
%           [ 1, 2,-3,-1,-9]]);
Let $A=
 \left(\begin{array}{rrrr}
 8 & 10 & 3 \\
  -3 & -5 & -3 \\
 -4 & -4 & 1
 \end{array}\right)$, $T=
 \left(\begin{array}{rrrr}
 1 & 0 & 0\\
 0 & -2 & 0 \\
 0 & 0 & 5
 \end{array}\right)$. Let $P$ denote a $3\times 3$ matrix. Define $\lambda_1=1$,  $\lambda_2=-2$ and $\lambda_3=5$.
Assume the following result:
\begin{quote}\sf
 {\bf Lemma 1}. The equality $AP=PT$ holds if and only if the columns
 $\.v_1$, $\.v_2$, $\.v_3$ of $P$ satisfy $A\.v_1=\lambda_1\.v_1$,
 $A\.v_2=\lambda_2\.v_2$, $A\.v_3=\lambda_3\.v_3$. [proved after Example 5]
\end{quote}
(a) Determine three specific columns for $P$ such that $\det(P)\ne
0$ and $AP=PT$. These columns contain only numbers -- no symbols allowed!
 Infinitely
 many answers are possible. See Example 5 for the maple method
 that determines a column of $P$.

 (b) After reporting the three
 columns, check the answer by computing $AP-PT$ (it should be
 zero) and $\det(P)$ (it should be nonzero).

{\bf Example 1}. Let $\dd A=\left(\begin{array}{rrr} 1 & 2 & 3\\ 2
& -1 & 1\\ 3 & 0 & -1 \end{array}\right)$ and $\dd
\.b=\left(\begin{array}{r} 9 \\ 8 \\ 3 \end{array}\right)$. Create
a \MAPLE{} work sheet. Define and display matrix $A$ and vector
$\.b$. Then compute
\begin{itemize}
 \item[{\bf (1)}] The inverse of $A$.
 \item[{\bf (2)}] The augmented matrix $C=\AUG(A,\.b)$.
 \item[{\bf (3)}] The reduced row echelon form $R=\rref(C)$.
 \item[{\bf (4)}] The column $\.X$ of $R$ which solves $A\.X=\.b$.
 \item[{\bf (5)}] The matrix $A^3$.
 \item[{\bf (6)}] The transpose of $A$.
 \item[{\bf (7)}] The matrix $A-3A^2$.
 \item[{\bf (8)}] The solution $\.X$ of $A\.X=\.b$ by two methods different than (4).
 \item[{\bf (9)}] Find a matrix $F$ such that $F\.x=\.b$ has no solution. Explain why {\tt linsolve} prints nothing.
 \item[{\bf (10)}] Compute $A^TA$, $(A^TA)^{-1}$, $A^{-1}(A^{-1})^T$.
\end{itemize}

{\bf Solution}: A lab instructor or classmate can help you to create a blank
work sheet in \MAPLE{}, enter code and print the work sheet. The
code to be entered into the work sheet appears below. 
\begin{verbatim}
 A:=Matrix([[1,2,3],[2,-1,1],[3,0,-1]]); 
 b:=Vector([9,8,3]); 
 print("(1)"); A^(-1);
 print("(2)"); C:=<A|b>;
 print("(3)"); R:=linalg[rref](C); 
 print("(3) Alternate"); 
 R:=LinearAlgebra[ReducedRowEchelonForm](C); 
 print("(4)"); R[1..3,4]; # Select col=4 from 3x4 matrix R
 print("(4) Alternate"); X:=LinearAlgebra[Column](R,4); 
 print("(5)"); A^3; 
 print("(6)"); A^+;
 print("(7)"); A-3*A^2;
 print("(8)"); X:=A^(-1).b;X:=LinearAlgebra[LinearSolve](A,b); 
 # (9): linsolve prints nothing: a signal equation "0=3".
 print("(9)"); F:=Matrix([[1,2,3],[2,-1,1],[0,0,0]]);linalg[linsolve](F,b); 
 <F|b>; # Visual explanation of the signal equation
 print("(10)"); A^+ . A; (A^+.A)^(-1); A^(-1) . (A^(-1))^+;
\end{verbatim}

\medskip

{\bf Example 2}.
Let $A=\left(\begin{array}{rrrrr}
 1    & 1     & 1    & 2     & 6 \\
 2    & 3    & -2    & 1    & -3 \\
 3    & 5    & -5    & 1    & -8 \\
 4    & 3     & 8    & 2     & 3
\end{array}\right)$.
\begin{itemize}
 \item[{\bf (1)}] Find a basis for the column space of $A$. This means: find a largest list of independent columns of $A$.
 \item[{\bf (2)}] Find a basis for the row space of $A$.
 \item[{\bf (3)}] Find a basis for the nullspace of $A$. This is the list of vector partials $\partial_{t_1}\.x$, $\partial_{t_2}\.x$, \ldots applied to the general solution $\.x$ of $A\.x=\.0$, which is obtained from the {\em last frame algorithm}.
 \item[{\bf (4)}] Find $\rank(A)$ and $\nullity(A)$. They are the number of lead variables and the number of free variables for the problem $A\.x=\.0$, respectively.
 \item[{\bf (5)}] Find the dimensions of the nullspace, row space and
 column space of $A$.
\end{itemize}

{\bf Solution}: The theory applied: {\em The columns of $B$
corresponding to the leading ones in $\rref(B)$ are independent and form
a basis for the column space of $B$}. These columns are called the {\bf
pivot columns} of $B$. The meaning is
$$\span\{ \mbox{all columns of $B$}\} = \span\{\mbox{pivot columns of
$B$}\}.$$ 
A list of vectors is called a {\bf basis} provided it is {\bf
independent} and {\bf spans}.

Results for the row space of $A$ are obtained by replacing $B$ by the
transpose of $A$. In particular, the row space of $A$ is spanned by the
pivot columns of $B=A^T$.

The \MAPLE{} code which applies is
\begin{verbatim}
 A:=Matrix([[ 1, 1, 1, 2, 6],
            [ 2, 3,-2, 1,-3],
            [ 3, 5,-5, 1,-8],
            [ 4, 3, 8, 2, 3]]);
 print("(1)"); C:=linalg[rref](A); # leading ones in columns 1,2,4              
               BASIScolumnspace=A[1..4,1],A[1..4,2],A[1..4,4];
 print("(2)"); F:=linalg[rref](A^+); # leading ones in columns 1,2,3
               BASISrowspace=row(A,1),row(A,2),row(A,3);
 print("(3)"); linalg[nullspace](A); linalg[linsolve](A,Vector([0,0,0,0]));

 print("(4)"); RANK=linalg[rank](A); 
               NULLITY=linalg[coldim](A)-linalg[rank](A);
 
 print("(5)"); DIMnullspace=linalg[coldim](A)-linalg[rank](A); 
               DIMrowspace=linalg[rank](A);
               DIMcolumnspace=linalg[rank](A);
\end{verbatim}



\medskip

{\bf Example 3}. Let $A=\left(\begin{array}{rrrrr}
 1    & 1     & 1    & 2     & 6 \\
 2    & 3    & -2    & 1    & -3 \\
 3    & 5    & -5    & 1    & -8 \\
 4    & 3     & 8    & 2     & 3
\end{array}\right)$.
Verify that the following column space bases of $A$ are equivalent.
$$
 \.v_1=\left(\begin{array}{r} 1 \\ 2 \\ 3 \\ 4 \end{array}\right),\quad
 \.v_2=\left(\begin{array}{r} 1 \\ 3 \\ 5 \\ 3 \end{array}\right),\quad
 \.v_3=\left(\begin{array}{r} 2 \\ 1 \\ 1 \\ 2 \end{array}\right),
$$
$$
 \.w_1=\left(\begin{array}{r} 1\\ 0\\ 0\\ -3 \end{array}\right),\quad
 \.w_2=\left(\begin{array}{r} 0\\ 1\\ 0\\ 17 \end{array}\right),\quad
 \.w_3=\left(\begin{array}{r} 0\\ 0\\ 1\\ -9 \end{array}\right).
$$

{\bf Solution}: We will use this result:

{\bf Lemma 2}. \sf Bases
 $\{ \.v_1, \.v_2, \.v_3\}$ and $\{ \.w_1, \.w_2, \.w_3\}$ are
 equivalent bases if and only if the augmented matrices $F=\AUG(\.v_1, \.v_2,
 \.v_3)$, $G=\AUG(\.w_1, \.w_2, \.w_3)$ and $H=\AUG(F,G)$ satisfy
 the rank condition $\rank(F)=\rank(G)=\rank(H)=3$. \rm

The proof appears below.

The \MAPLE{} code which applies is
\begin{verbatim}
 A:=Matrix([[ 1, 1, 1, 2, 6],
            [ 2, 3,-2, 1,-3],
            [ 3, 5,-5, 1,-8],
            [ 4, 3, 8, 2, 3]]);
 v1:=Vector([1,2,3,4]); v2:=Vector([1,3,5,3]); v3:=Vector([2,1,1,2]);
 w1:=Vector([1,0,0,-3]); w2:=Vector([0,1,0,17]); w3:=Vector([0,0,1,-9]);
 F:=<v1|v2|v3>;
 G:=<w1|w2|w3>;
 H:=<F|G>;
 linalg[rank](F); linalg[rank](G); linalg[rank](H);
\end{verbatim}
{\bf Example 4}. The two bases in the example were discovered from the
\MAPLE{} code
\begin{verbatim}
 A:=Matrix([[ 1, 1, 1, 2, 6],
            [ 2, 3,-2, 1,-3],
            [ 3, 5,-5, 1,-8],
            [ 4, 3, 8, 2, 3]]);
 linalg[rref](A); # pivot cols 1,2,4
 v1:=A[1..4,1]; v2:=A[1..4,2]; v3:=A[1..4,4];# Name the columns
 linalg[rref](A^+); # pivot cols 1,2,3
 w1:=A[1,1..5]; w2:=A[2,1..5]; w3:=A[3,1..5];# Name the rows
\end{verbatim}

{\bf Proof of Lemma 2}.


{\bf Proof}:  Let's justify part of the test, showing
only half the proof: $\rank(F)=\rank(G)=\rank(H)=n$ implies the
bases are equivalent.

The equation $\rref(F)=EF$ holds for $E$ a product of elementary
matrices. Then $\rref(H)=EH$ has to have $n$ leading ones, because of $F$
in the first $n$ columns, and the remaining rows of $\rref(H)$ are zero, because
$\rank(H)=n$. Therefore, the first $n$ columns of $H=\AUG(F,G)$ are the pivot
columns of $H$. The non-pivots of $H$ are just the columns of $G$,
and by the pivot theory, they are linear combinations of the pivot
columns, namely, the columns of $F$. We can multiply $H$ by a
permutation matrix $P$ which effectively swaps $F$ and $G$. Already,
$HP$ has the $n$ independent columns of $F$, so its rank is at least
$n$. But its other columns are linear combinations of these columns,
so the rank is exactly $n$. Now we argue by symmetry that the
columns of $F$ are linear combinations of the columns of $G$, using
$HP$ instead of $H$.

The first half of the proof is complete. The other half is left to the reader.



{\bf Example 5}. Let $\dd A=\left(\begin{array}{rrr} 1 & 2 & 3\\ 2
& -1 & 1\\ 3 & 0 & 0 \end{array}\right)$. Solve the equation $A\.x
= -3\.x$ for $\.x$.

% A:=matrix([[1,2,3],[2,-1,1],[3,0,0]]);

{\bf Solution}. Let $\lambda=-3$. The idea is to write the equation
$A\.x=\lambda\.x$ as a homogeneous problem $(A-\lambda I)\.x=\.0$.

The trick is to move
$\lambda\.x$ from the $\RHS$ to the $\LHS$ of the equation, then re-write $\lambda\.x$ as
$\lambda I\.x$, where $I$ is the identity matrix. Then $\.x$ is a common factor, and the
matrix equation can be written as $(A\.x-\lambda I\.x=\.0$. Then $(A-\lambda I)\.x=\.0$.

Define $B=A-\lambda I$. The homogeneous equation $B\.x=\.0$ always
has the solution $\.x=\.0$. It has a nonzero solution $\.x$ if and
only if there are infinitely many solutions, in which case the
solutions are found by a frame sequence to $\rref(B)$. The \MAPLE{}
details appear below. The basis vectors for $B\.x=\.0$ are obtained
in the usual way, by taking partial derivatives on the general
solution with respect to the symbols $t_1$, $t_2$, \ldots. In this
case, there is just one basis vector $$\colvector{-2\\ 1\\ 2}.$$
\begin{verbatim}
 A:=Matrix([[1,2,3],[2,-1,1],[3,0,0]]);
 B:=A-(-3)*<1,0,0|0,1,0|0,0,1>;
 linalg[linsolve](B,Vector([0,0,0]));
 # ans: Vector([-2*t,1*t,2*t]), maple replaces t by _t_1
 # Basis == partial on t == Vector([-2,1,2])
\end{verbatim}



{\bf Proof of Lemma 1}. Define $r_1=1$, $r_2=-2$, $r_3=5$. Assume
$AP=PT$, $P=\AUG(\.v_1,\.v_2,\.v_3)$ and $T=\diag(r_1,r_2,r_3)$.
The definition of matrix multiplication implies that
$AP=\AUG(A\.v_1,A\.v_2,A\.v_3)$ and
$PT=\AUG(r_1\.v_1,r_2\.v_2,r_3\.v_3)$. Then $AP=PT$ holds if and
only if the columns of the two matrices match, which is equivalent
to the three equations $A\.v_1=r_1\.v_1$, $A\.v_2=r_2\.v_2$,
$A\.v_3=r_3\.v_3$. The proof is complete.


{\bf End of Maple Lab 5 Linear Algebra.}

\end{document}










