1. Separating system of equations

Hi,

Given a matrix $A = \begin{bmatrix} B & 0 & 0 & -XB \\ 0 & B & 0 & -YB \\ 0 & 0 & B & -ZB \end{bmatrix}$

where $B$ is a non-negative m x n matrix and $X, Y, Z$ are m x m diagonal matrices.

How would I produce the following:

$\begin{bmatrix} B^T{B} & 0 & 0 & -B^{T}XB \\ 0 & B^T{B} & 0 & -B^{T}YB \\ 0 & 0 & B^{T}B & -B^{T}ZB \\ 0 & 0 & 0 & M \end{bmatrix}$

where $M = M_x + M_y + M_z$
$M_x = B^{T}X^{2}B - (B^{T}XB)(B^{T}B)^{-1}(B^{T}XB)$
$M_y = B^{T}Y^{2}B - (B^{T}YB)(B^{T}B)^{-1}(B^{T}YB)$
$M_z = B^{T}Z^{2}B - (B^{T}ZB)(B^{T}B)^{-1}(B^{T}ZB)$

Starting from:

$A^{T}A = \begin{bmatrix} B^T{B} & 0 & 0 & -B^{T}XB \\ 0 & B^T{B} & 0 & -B^{T}YB \\ 0 & 0 & B^{T}B & -B^{T}ZB \\ -B^{T}XB & -B^{T}YB & -B^{T}ZB & B^{T}X^{2}B + B^{T}Y^{2}B + B^{T}Z^{2}B \end{bmatrix}$

I'm also wondering if the inverses could be avoided somehow, since $B^{T}B$ may not be invertible.