Right inverses
The goal is to work out a problem similar to 4.5.20: given a rectangular (possibly non-square) matrix $A$, find a right inverse or several, if possible. Recall from Definition 4.46 in our textbook that a right inverse $B$ of $A$ is a matrix such that $AB=I$ (this last one is the identity matrix).
If $A$ is of size $m\times n$ ($m$ rows, $n$ columns) then $B$ must be of size $n\times m$, in which case the identity matrix $I$ in $AB=I$ is of size $m\times m$.
I propose to do this for, say, $$ A= \left[ \begin{array}{rrr} 3&0&5\\ 0&1&1 \end{array} \right]. $$ Since $A$ is $2\times 3$, any right inverses we might find have to be of size $3\times 2$.
Now, how would you go about doing this sort of thing? One way is to look at the equation $AB=I$ (which we are solving for $B$) one column at a time. It then reads \begin{align*} A\cdot(\text{first column of }B) &= (\text{first column of }I)\\ A\cdot(\text{second column of }B) &= (\text{second column of }I)\\ &\vdots \end{align*}
In our case $B$ has two columns, so we have two equations: if, say, $$ B= \left[ \begin{array}{rrr} x&p\\ y&q\\ z&r \end{array} \right], $$
then we have to solve
$$ A\cdot \left[ \begin{array}{r} x\\y\\z \end{array} \right] = \left[ \begin{array}{r} 1\\0 \end{array} \right] \quad\text{and}\quad A\cdot \left[ \begin{array}{r} p\\q\\r \end{array} \right] = \left[ \begin{array}{r} 0\\1 \end{array} \right] $$
Note, now, that these are just two linear systems with unknowns $x$, $y$ and $z$ (for one system) and $p$, $q$ and $r$ (for the other): this is explained in Definition 4.20 from the textbook. The first one for instance, written as a system, reads
\begin{equation*} \begin{aligned} 3x \phantom{+ 0y} + 5z &= 1\\ \phantom{0x+}y +z &= 0\\ \end{aligned} \end{equation*} In any case, we know how to solve these: collect the values on the right-hand side of the equal signs into a column, append that column to $A$ to create an augmented matrix (see Definition 1.13), and proceed as usual.
In this specific case the two systems have corresponding augmented matrices
$$ \left[ \begin{array}{rrr|r} 3&0&5&1\\ 0&1&1&0 \end{array} \right] $$
and
$$ \left[ \begin{array}{rrr|r} 3&0&5&0\\ 0&1&1&1 \end{array} \right]. $$
These we can now feed into SymPy for solving, as seen before.
from sympy import *
init_printing(use_latex='mathjax')
from sympy.solvers.solveset import linsolve
x, y, z = symbols('x, y, z')
p, q, r = symbols('p, q, r')
linsolve(Matrix(([3, 0, 5, 1], [0, 1, 1, 0])), (x, y, z))
That's the first column of $B$. We can choose the last entry $z$ arbitrarily, and then the first two entries are expressible in terms of $z$. Now on to the second column:
linsolve(Matrix(([3, 0, 5, 0], [0, 1, 1, 1])), (p, q, r))
That's $B$'s second column. Gathering it all up, we have now found all right inverses of $A$; they are the matrices $$ B= \left[ \begin{array}{rr} \frac 13-\frac{5z}3 & -\frac{5r}3\\ -z & 1-r\\ z&r \end{array} \right] $$ for arbitrary choices of $r$ and $z$.
Let's check that indeed $AB=I$ upon plugging in specific values for $r$ and $z$. We could do this "by hand", i.e. by simply plugging in specific matrices $B$ obtained by giving $r$ and $z$ specific numerical values, but it will be more flexible to regard $B$ as a function of two variables $r$ and $z$, and then have Python compute the matrix as we enter various $r$s and $z$s. Here's how to set up a function that takes $r$ and $z$ as variables and returns the corresponding matrix $B(r,z)$:
def B(r,z):
return Matrix([[S(1)/3-z*S(5)/3, r*S(-5)/3],[-z, 1-r],[z,r]])
The notation $S(\text{u})/\text{v}$ is meant to tell SymPy to leave fractions alone and not try to round them. Without that we might run into trouble due to computer arithmetic:
1/3==S(1)/3
False
Anyway, we can now simply plug in the desired values for $r$ and $z$ and get back the corresponding $B(r,z)$:
B(1,1)
B(3,19)
Finally, let's try out some multiplications of the form $A\cdot B(r,z)$, to check that indeed all $B(r,z)$ are right inverses of $A$ (I haven't actually defined $A$ yet in SymPy, so I'll start with that):
A=Matrix([[3,0,5],[0,1,1]])
A*B(1,1)
A*B(3,5)
and so on.