Some questions about the pseudoinverse of a matrix For every mxn-matrix A with real entries, there exist a unique nxm-matrix B, also 
 with real entries, such that
$$ABA = A$$
$$BAB = B$$
$$AB = (AB)^T$$
$$BA = (BA)^T$$
B is called the pseudoinverse of A.
There is also a complex version, but I am only interested in the real one.
Now my questions :


*

*If A has rational entries, must B also have rational entries ?

*How can I calculate the pseudoinverse of a matrix with PARI/GP ?

*Is there a simple method to calculate the pseudoinverse by hand for
small matrices ?

*Under which condition are the entries of the pseudoinverse integers ?


I know some special properties, for instance, that for invertible square 
matrices A, the pseudoinverse is simply $A^{-1}$ , or that the pseudoinverse
of a zero-matrix is its transposition, but I have not much experience with
general pseudoinverses.
 A: Edit. The old answer is wrong. Here is a corrected one.
For your first question, since $A$ has rational entries, it has a rank decomposition $A=XY$ such that $X$ is a tall matrix with full column rank and rational elements and $Y$ is fat matrix with full row rank and rational elements. One may verify, using the four defining properties of Moore-Penrose pseudoinverse, that $G=Y^+X^+$ is identical to $A^+$. Indeed,
$$
\begin{aligned}
&AGA=X(YY^+)(X^+X)Y=XY=A,\\
&GAG=Y^+(X^+X)(YY^+)X^+=Y^+X^+=G,\\
&AG=XYY^+X^+=XX^+\text{ is Hermitian},\\
&GA=Y^+X^+XY=Y^+Y\text{ is Hermitian}.\\
\end{aligned}
$$
Since $X^+=(X^TX)^{-1}X^T$ and $Y^+=Y^T(YY^T)^{-1}$, they have rational entries. In turn, so does $A^+$.
For your third question, if $A$ is at most $3\times3$, you may try the formula
$$
A^+ = \lim_{\delta \searrow 0} (A^\top A + \delta I)^{-1} A^\top
= \lim_{\delta \searrow 0} A^\top (A A^\top + \delta I)^{-1}.
$$
For your last question, I'm not sure if there are any nice and general sufficient conditions.
A: Regarding the first question: Let $\mathbb F$ be a field (not necessarily $\mathbb R$ or $\mathbb C$), and let $m,n$ be nonnegative integers. Given an involutory field automorphism $a:\mathbb F\to\mathbb F$ (think complex conjugation), we can define a sort of "conjugate transpose" $\mathbb F^{m\times n}\to\mathbb F^{n\times m}$ via $A\mapsto A^*:=\left(a(A_{j,i})\right)_{i\in n,j\in m}$. This "conjugate transpose" lets us define what it means for a matrix $X\in\mathbb F^{n\times m}$ to be a pseudoinverse of $A.$ It can then be shown that every $A\in\mathbb F^{m\times n}$ has at most one pseudoinverse $X\in\mathbb F^{n\times m}$ (the usual proof carries over). The topic of existence is more delicate; it can be shown that an arbitrary $A\in\mathbb F^{m\times n}$ has a pseudoinverse belonging to $\mathbb F^{n\times m}$ if and only if  we have the equalities $\text{rank}(A^*A)=\text{rank }A=\text{rank}(AA^*)$ [Bot13].
Now, consider the case where we have two fields $\mathbb E,\mathbb F$, and an involutory field automorphism $a:\mathbb F\to\mathbb F,$ where $\mathbb E\subseteq\mathbb F$ and $a|_{\mathbb E}:\mathbb E\to\mathbb E$ has range $\text{range}(a|_{\mathbb E})\subseteq\mathbb E.$ It is not difficult to see that $a|_{\mathbb E}$ will be an involutory field automorphism on $\mathbb E$, so, given two nonnegative integers $m,n$, we can apply the above theory to $\mathbb E^{m\times n}:$ Any $A\in\mathbb E^{m\times n}$ has a pseudoinverse belonging to $\mathbb E^{n\times m}$ iff $\text{rank}_{\mathbb E}(A^*A)=\text{rank}_{\mathbb E}A=\text{rank}_{\mathbb E}(AA^*).$ This string of equalities is in turn equivalent to $\text{rank}_{\mathbb F}(A^*A)=\text{rank}_{\mathbb F}A=\text{rank}_{\mathbb F}(AA^*)$ (rank does not change under field extension). This second string of equalities is equivalent to $A$ having a pseudoinverse belonging to $\mathbb F^{n\times m}$. Thus, $A$ has a pseudoinverse belonging to $\mathbb F^{n\times m}$ iff $A$ has a pseudoinverse belonging to $\mathbb E^{n\times m}$. (One direction is almost obvious, from the set inclusion $\mathbb E^{n\times m}\subseteq \mathbb F^{n\times m}$, but the converse might have been not obvious.)
Now, let us specialize to the case where $\mathbb F=\mathbb C$, $a=(\bar z)_{z\in\mathbb C}$ is complex conjugation, and $\mathbb E$ is any subfield of $\mathbb C$ such that $a|_{\mathbb E}:\mathbb E\to\mathbb E$. (Examples: $\mathbb C$ itself; the algebraic numbers $\mathbb A$; or any subfield of $\mathbb R$, such as $\mathbb R$ itself, $\mathbb Q$, or $\mathbb Q(\sqrt2)$.) We know that every $A\in\mathbb E^{m\times n}\subseteq\mathbb C^{m\times n}$ has a pseudoinverse, which we denote $A^+$, belonging to $\mathbb C^{n\times m}.$ Thus, from the above paragraph, $A$ has a pseudoinverse in $\mathbb E^{n\times m}\subseteq\mathbb C^{n\times m}$, and so from uniqueness in $\mathbb C^{n\times m}$ we have $A^+\in\mathbb E^{n\times m}$. Thus, the answer is yes.
In fact, for any $A\in\mathbb Z^{m\times n}$, the least common denominator of the image of $A^+$,
$$\ell:=\min\{d\in\mathbb Z_{\ge1}:\forall i\in m~\forall j\in n\quad d(A^+)_{i,j}\in\mathbb Z\}$$ will divide (in $\mathbb Z$) $(\text{vol}A)^2$, where we define the volume
$$\text{vol}~A:=\sqrt{\sum_{I,J}\det(A|_{I\times J})^2},$$
where the summation ranges over all cardinality-$r$ subsets $I,J\subseteq A$, where $r=\text{rank}~A$ [BhR02 p.81 Theorem 6.7, BouVre20]. (If $A$ is square and invertible, then $\text{vol}~A=|\det A|$.) In fact, based on a few examples I computed (with noninvertible integer-valued matrices whose nonzero entries are relatively prime), it seems we "often" have $\ell=(\text{vol}~A)^2$. For instance, in the example $[1;2]^+=5^{-1}[1,2]$ we have $\ell=5$.
Regarding the fourth question: We also have a necessary and sufficient condition when the entries of $A$ itself are integers: if $A\in\mathbb Z^{m\times n}$, then $A^+\in\mathbb Z^{n\times m}$ iff $\text{vol}~A=1$ [BhR02 Theorem 6.7]. As for arbitrary $A\in\mathbb Q^{m\times n}$, I'm unsure.
[BhR02] Bhaskara Rao, K. (2002). Theory of generalized inverses over commutative rings. CRC Press.
[Bot13] Botha, J. D. (2013). Matrices over finite fields. In: Hogben, L. (ed.). Handbook of Linear Algebra (2nd ed.). CRC Press. Section 31.5.
[BouVre20] Bouman, N. J., and de Vreede, N. (2020). A practical approach to the secure computation of the Moore–Penrose pseudoinverse over the rationals. In International Conference on Applied Cryptography and Network Security (pp. 398-417). Springer, Cham.
A: This is not a proper definition of the pseudoinverse. There are two common definitions, the first concerning matrices where the Singular Value Decomposition is used, and the more general (which I prefer) in Hilbert spaces where we consider the minimum of $\left\|Ax-y \right\|$ over $X$ and then we define as $A^{+}y$ the minimizing $x$ with the minimum norm. All the condition you use are obtained as propositions by this definition of the pseudoinverse!
