# Finding most general matrices $A$ and $B$ such that $A\cdot B=1$

I would like to find the most general shape of matrices $A$ and $B$ such that $A\cdot B=1_{4\times4}$. Naively, I just define for example

AA = Table[Subscript[aa, i, j], {i, 1, 4}, {j, 1, 5}];
BB = Table[Subscript[bb, i, j], {i, 1, 5}, {j, 1, 4}];


And look for the solution set of

Solve[(AA.BB // Flatten) == (IdentityMatrix[4] // Flatten), Variables[{AA, BB}]]


However, unfortunately this calculation goes on indefinitely, never producing a result for some reason. Therefore, I'd like to ask if there is a more clever way to do it? Note that I just want $A$ to be the left inverse of $B$, while $B\cdot A$ does not necessarily equal $1$. Thanks for any suggestion!

• AA and Inverse[AA] would be an admissible pair, no? Feb 20, 2016 at 15:50
• I believe that would be too restrictive, since these two are simultaneously left and right inverses of each other. Feb 20, 2016 at 15:51
• I see, so your matrices can be singular, then? Feb 20, 2016 at 15:53
• Then, have you looked into the Moore-Penrose conditions? Feb 20, 2016 at 16:02
• @J.M., Kagaratsch, If A is square, then A.B = I implies A is nonsingular and has a two-sided inverse, and it is given by B, no? So J.M.'s first comment is the answer. If Kagaratsch is really interested in rectangular matrices, then giving only a square example and not mentioning the full scope of the question seems misleading. It misled me. Feb 20, 2016 at 16:54

Problem. Let $A$ be an $n \times m$ matrix and $B$. To find all $m \times n$ matrices $B$ satisfying $AB = I$.

Let $p,q$ be the rank, nullity (respectively) of $A$ so that $p+q=m$. The condition $AB = I$ implies $p \ge n$ and the dimensions of $A$ imply $p \le n$; therefore the rank of $A$ being $p = n$ is a necessary condition. Let $A^+$ be any left inverse of $A$, such as the Moore-Penrose pseudoinverse, so that $AA^+=I$. Let $C$ be an $m \times n$ matrix whose columns are chosen from the null space of $A$. Then if $B = A^+ + C$, then $AB = I$.

Conversely, if $AB_1 = AB_2 = I$, then $A(B_1-B_2) = 0$ and the columns of $B_1-B_2$ are elements of the null space of $A$.

Therefore, if the rank of $A$ is less than $n$, there are no such $B$; if the rank is $n$, then the general solution is given by $B = A^+ +C$, where the columns of $C$ belong to the null space of $A$.

Remark. If $A$ is square and there is a matrix $B$ such that $AB=I$, then $A$ is invertible and $B = A^{-1}$ is the only solution.

It seems more like a mathematics quesion, but here is some code to find the general left inverse for a made-up matrix. (In Mathematica, the pseudoinverse is computed with PseudoInverse.)

b = PseudoInverse[
a = {{1, 1, 2, 0, 1},
{0, 2, 2, 2, 0},
{0, 0, 0, 1, 1} }];
With[{nsb = NullSpace[a]},
c = Transpose@ Table[Sum[C[i, j] nsb[[i]], {i, Length@nsb}], {j, Length@a}]]
b + c // MatrixForm
a.(b + c) // Simplify // MatrixForm


This seems to give a reasonable answer in n=2 and n=3 dimensions:

n = 2; aa = Array[a, {n, n}]; bb = Array[b, {n, n}];
Reduce[(aa.bb // Flatten) == (IdentityMatrix[n] // Flatten), Variables[bb]]


Thanks to a hint by J.M. I looked up the Moore-Penrose conditions. They describe a unique generalization of the inverse to singular matrices, which reduces to the regular inverse for non-singular matrices. Property 3) and 4) state that the multiplication of pseudoinverse $A^+$ and matrix $A$ has to be hermitian. If we want this multiplication to definitely yield the unit matrix (and not just a hermitian matrix), we have to assume that either rows or columns of $A$ are linearly independent. I.e. for linearly independent rows we get

$$A^+=A^*(AA^*)^{-1}$$

since then $AA^+=1$. Therefore, in the language of the question above

BB=Conjugate[AA].Inverse[AA.Conjugate[AA]]


is the most general right inverse of a matrix AA with linearly independent rows. Similarly

AA=Inverse[Conjugate[BB].BB].Conjugate[BB]


is the most general left inverse of a matrix BB with linearly independent columns.