$A,B$ be $n\times n$ semipositive-definite, rank $A=r.$ Find an invertible $P$, such $P^{-1}A(P^{-1})^T=diag(I_r,0)$, $P^T BP=diag(\lambda_i)$

$A,B$ be $n\times n$ semipositive-definite, $\operatorname{rank} A=r$. Find an invertible $P$, such $P^{-1}A(P^{-1})^T=\operatorname{diag}(I_r,0)$, $P^T BP=\operatorname{diag}(\lambda_i)$

Here, $I_r$ is the $r\times r$ identity matrix, $\lambda_i$ are the eigenvalues of $B$, $P^T$ is the transpose of $P$.

Terribly sorry. I only have known that if $A$ is positive definite, then $P^T AP=I_n$, and then $P^T BP$ is also semipositive-definite. Then some orthogonal matrix $Q$ exists, which let $Q^T P^T APQ=I_n$, and $Q^T P^T BPQ=\operatorname{diag}(\lambda_i)$, but $\lambda_i$ is eigenvalue of $P^T BP$!


This is not always possible. Consider the case where $A$ is a positive definite matrix of determinant $\ne1$. Since $P^{-1}A(P^{-1})^T=I$, we have $\det(P)^2=\det(A)\ne1$. This is inconsistent with the condition that $P^TBP=\operatorname{diag}(\lambda_1(B),\ldots,\lambda_n(B))$ when $\det(B)=\prod_i\lambda_i(B)$ is nonzero.