# Numerically robust Cauchy-Schwarz?

Is it well-known by the Cauchy-Schwarz inequality that $$\langle x, x\rangle \langle y, y\rangle - \langle x, y\rangle^2 \geq 0$$ for any $x,y\in H$, $H$ being a Hilbert space with real-valued scalars.

When computing the above expression numerically, sometimes it will be negative (in the order of machine precision) due to round-off errors in the subtraction.

Is there an equivalent expression that is always numerically positive? (E.g., something to the power of 2, a norm of a vector etc.)

• I take it that an absolute value will not suffice? You do know that the quantity is nonnegative, so taking an absolute value will not change its true value, but will result in a nonnegative number on the computer. – RideTheWavelet Jul 19 '18 at 8:56
• Or taking the maximum of the computed value and 0? – Mees de Vries Jul 19 '18 at 9:45
• Would taking the logarithm work? $\log(\langle x, x\rangle )+ \log(\langle y, y\rangle) - 2\log(\langle x, y\rangle) \geq 0$. This help when the numbers get really small and avoids floating point errors because it is addition and subtraction. – Sonal_sqrt Jul 19 '18 at 9:55
• Why do you care? Is this computation an end in itself, or is it part of a larger computation? – awkward Jul 19 '18 at 12:17
• @RideTheWavelet: taking the absolute value is not the best idea. You know that you are tampering the value, i.e. replacing $-\epsilon$ by $\epsilon$, twice the correction achieved with $0$. – user65203 Jul 21 '18 at 14:12

$$\langle x,x \rangle \langle y,y\rangle - \langle x,y \rangle^2 = \sum_{1 \le i < j \le n} (x_i y_j - x_j y_i)^2$$
for $x, y \in \mathbb{R}^n$.