Solution (source code)

= Solution

Let $r=\sqrt{x_i^2+x_j^2}$. If $r>0$, choose $\theta$ so that
$$
\cos\theta=\frac{x_i}{r},\qquad \sin\theta=\frac{x_j}{r}.
$$
Such an angle exists because the two ratios have squared sum one. Then the changed coordinates are
$$
y_i=\frac{x_i^2+x_j^2}{r}=r,\qquad
y_j=-\frac{x_jx_i}{r}+\frac{x_ix_j}{r}=0.
$$
All other coordinates satisfy $y_k=x_k$. If $r=0$, both input coordinates are zero; take $\theta=0$, giving the <identity matrix> on this pair. In all cases,
$$
\boxed{y_i=\sqrt{x_i^2+x_j^2},\qquad y_j=0,\qquad y_k=x_k\ (k\ne i,j).}
$$
This fixes a nonnegative retained coordinate and handles division by zero explicitly. For floating-point computation, the <stable construction of a Givens rotation> uses scaled <Euclidean norm> evaluation to avoid unnecessarily overflowing or underflowing the squares.