How to prove this identity nicely?

This was a pain, but here it is.

$\begin{array}\\ s(n) &=\sum_{1\le i<j\le n}\left((x_{j}-x_{i})-(x_{j}-x_{i})^2\right)\\ &=\sum_{i=1}^n \sum_{j=i+1}^n((x_{j}-x_{i})-(x_{j}-x_{i})^2)\\ &=\sum_{i=1}^n \sum_{j=i+1}^n(x_{j}-x_{i})-\sum_{i=1}^n \sum_{j=i+1}^n(x_{j}-x_{i})^2\\ &=s_1(n)-s_2(n)\\ s_1(n) &=\sum_{i=1}^n \sum_{j=i+1}^n(x_{j}-x_{i})\\ &=\sum_{i=1}^n \sum_{j=i}^n(x_{j}-x_{i})\\ &=\sum_{i=1}^n \sum_{j=i}^nx_{j}-\sum_{i=1}^n \sum_{j=i}^nx_{i}\\ &=\sum_{j=1}^n \sum_{i=1}^jx_{j}-\sum_{i=1}^n (n-i+1)x_{i}\\ &=\sum_{j=1}^n x_j\sum_{i=1}^j1-\sum_{i=1}^n (n-i+1)x_{i}\\ &=\sum_{j=1}^n jx_j-\sum_{i=1}^n (n-i+1)x_{i}\\ &=\sum_{i=1}^n ix_i-\sum_{i=1}^n (n-i+1)x_{i}\\ &=\sum_{i=1}^n (i-n+i-1)x_i\\ &=\sum_{i=1}^n (2i-n-1)x_i\\ s_2(n) &=\sum_{i=1}^n \sum_{j=i+1}^n(x_{j}-x_{i})^2\\ &=\sum_{i=1}^n \sum_{j=i}^n(x_{j}-x_{i})^2\\ &=\sum_{i=1}^n \sum_{j=i}^n(x_j^2-2x_jx_i+x_i^2)\\ &=\sum_{i=1}^n \sum_{j=i}^nx_j^2-2\sum_{i=1}^n \sum_{j=i}^nx_jx_i+\sum_{i=1}^n \sum_{j=i}^nx_i^2\\ &=s_3(n)-2s_4(n)+s_5(n)\\ s_3(n) &=\sum_{i=1}^n \sum_{j=i}^nx_j^2\\ &=\sum_{j=1}^n \sum_{i=1}^jx_j^2\\ &=\sum_{j=1}^n jx_j^2\\ s_4(n) &=\sum_{i=1}^n \sum_{j=i}^nx_jx_i\\ &=\sum_{i=1}^n x_i\sum_{j=i}^nx_j\\ &=\sum_{i=1}^n x_i(\sum_{j=1}^nx_j-\sum_{j=1}^{i-1}x_j)\\ &=\sum_{i=1}^n x_i\sum_{j=1}^nx_j-\sum_{i=1}^n \sum_{j=1}^{i-1}x_ix_j\\ &=(\sum_{i=1}^n x_i)^2-\sum_{j=1}^{n-1} \sum_{i=j+1}^{n}x_ix_j\\ &=(\sum_{i=1}^n x_i)^2-\sum_{j=1}^{n-1} x_j\sum_{i=j+1}^{n}x_i\\ &=(\sum_{i=1}^n x_i)^2-\sum_{j=1}^{n} x_j\sum_{i=j+1}^{n}x_i\\ &=(\sum_{i=1}^n x_i)^2-\sum_{j=1}^{n} x_j(\sum_{i=j}^{n}x_i-x_j)\\ &=(\sum_{i=1}^n x_i)^2-\sum_{j=1}^{n} x_j\sum_{i=j}^{n}x_i+\sum_{j=1}^{n} x_j^2\\ &=(\sum_{i=1}^n x_i)^2-\sum_{i=1}^{n} x_i\sum_{j=i}^{n}x_j+\sum_{j=1}^{n} x_j^2\\ &=(\sum_{i=1}^n x_i)^2-s_4(n)+\sum_{j=1}^{n} x_j^2\\ \text{so}\\ s_4(n) &=\frac12((\sum_{i=1}^n x_i)^2+\sum_{i=1}^{n} x_i^2)\\ s_5(n) &=\sum_{i=1}^n \sum_{j=i}^nx_i^2\\ &=\sum_{i=1}^n (n-i+1)x_i^2\\ \text{so}\\ s_2(n) &=s_3(n)-2s_4(n)+s_5(n)\\ &=\sum_{j=1}^n jx_j^2-((\sum_{i=1}^n x_i)^2+\sum_{i=1}^{n} x_i^2)+\sum_{i=1}^n (n-i+1)x_i^2\\ &=-((\sum_{i=1}^n x_i)^2+\sum_{i=1}^{n} x_i^2)+\sum_{i=1}^n (n+1)x_i^2\\ &=-(\sum_{i=1}^n x_i)^2+n\sum_{i=1}^n x_i^2\\ \text{so}\\ s(n) &=s_1(n)-s_2(n)\\ &=\sum_{i=1}^n (2i-n-1)x_i-(n\sum_{i=1}^n x_i^2-(\sum_{i=1}^n x_i)^2)\\ &=(\sum_{i=1}^n x_i)^2-n\sum_{i=1}^n x_i^2+\sum_{i=1}^n (2i-n-1)x_i\\ \end{array} $

Whew!


We take an algebraic approach and consider the expressions as quadratic polynomials in $n$ variables $x_1,x_2,\ldots,x_n$. We show equality by comparing the coefficients of corresponding terms.

It is convenient to use the coefficient of operator $[x^n]$ to denote the coefficient of $x^n$ of a series.

We introduce $f_1,f_2,f_3$ as \begin{align*} f_1(x_1,\ldots,x_n)&=\sum_{1\leq i<j\leq n}\left(\left(x_j-x_i\right)-\left(x_j-x_i\right)^2\right)\\ f_2(x_1,\ldots,x_n)&=\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\\ f_3(x_1,\ldots,x_n)&=-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2} \end{align*}

We start with the square terms.

We obtain for $1\leq k\leq n$: \begin{align*} \color{blue}{[x_k^2]f_1(x_1,\ldots,x_n)}&=[x_k^2]\sum_{1\leq i<j\leq n}\left(\left(x_j-x_i\right)-\left(x_j-x_i\right)^2\right)\\ &=-[x_k^2]\sum_{1\leq i<j\leq n}\left(x_j-x_i\right)^2\\ &=-[x_k^2]\sum_{1\leq i<k}\left(x_k-x_i\right)^2-[x_k^2]\sum_{k<j\leq n}\left(x_j-x_k\right)^2\\ &=-(k-1)-(n-k)\\ &\,\,\color{blue}{=1-n}\\ \color{blue}{[x_k^2]f_2(x_1,\ldots,x_n)}&=[x_k^2]\left(\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\right)\\ &=[x_k^2]\sum_{i=1}^nx_i^2-n[x_k^2]\sum_{i=1}^nx_i^2\\ &\,\,\color{blue}{=1-n}\\ \color{blue}{[x_k^2]f_3(x_1,\ldots,x_n)}&=[x_k^2]\left(-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\right.\\ &\qquad\qquad\qquad\left.+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\right)\\ &=-n[x_k^2]\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\\ &=-n[x_k^2]\sum_{i=1}^n\left(x_i^2-2x_i\cdot\frac{1}{n}\sum_{j=1}^n{x_j}+\left(\frac1n\sum_{j=1}^n{x_j}\right)^2\right)\\ &=-n[x_k^2]\sum_{i=1}^nx_i^2+2[x_k^2]\sum_{i=1}^nx_i\sum_{j=1}^nx_j-\frac{1}{n}[x_k^2]\sum_{i=1}^n\left(\sum_{j=1}^n{x_j}\right)^2\\ &=-n+2-1\\ &\,\,\color{blue}{=1-n} \end{align*}

Now we check the mixed quadratic terms.

We obtain for $1\leq k<l\leq n$: \begin{align*} \color{blue}{[x_kx_l]f_1(x_1,\ldots,x_n)}&=[x_kx_l]\sum_{1\leq i<j\leq n}\left(\left(x_j-x_i\right)-\left(x_j-x_i\right)^2\right)\\ &=-[x_kx_l]\sum_{1\leq i<j\leq n}\left(x_j-x_i\right)^2\\ &=2[x_kx_l]\sum_{1\leq i<j\leq n}x_ix_j\\ &\,\,\color{blue}{=2}\\ \color{blue}{[x_kx_l]f_2(x_1,\ldots,x_n)}&=[x_kx_l]\left(\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\right)\\ &=[x_kx_l]\left(\sum_{i=1}^n{x_i}\right)^2\\ &=[x_kx_l]\left(\sum_{i=1}^n{x_i}^2+2\sum_{1\leq i<j\leq n}x_ix_j\right)\\ &\,\,\color{blue}{=2}\\ \color{blue}{[x_kx_l]f_3(x_1,\ldots,x_n)}&=[x_k^2]\left(-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\right.\\ &\qquad\qquad\qquad\left.+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\right)\\ &=-n[x_kx_l]\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\\ &=[x_kx_l]\sum_{i=1}^n\left(2x_i\sum_{j=1}^n{x_j}\right)\\ &\,\,\color{blue}{=2} \end{align*}

... the linear terms ...

We obtain for $ 1\leq k\leq n$: \begin{align*} \color{blue}{[x_k]f_1(x_1,\ldots,x_n)}&=[x_k]\sum_{1\leq i<j\leq n}\left(\left(x_j-x_i\right)-\left(x_j-x_i\right)^2\right)\\ &=[x_k]\sum_{1\leq i<j\leq n}\left(x_j-x_i\right)\\ &=[x_k]\sum_{1\leq i<k}\left(x_k-x_i\right)+[x_k]\sum_{k<j\leq n}\left(x_j-x_k\right)\\ &=(k-1)-(n-k)\\ &\,\,\color{blue}{=2k-n-1}\\ \color{blue}{[x_k]f_2(x_1,\ldots,x_n)}&=[x_k]\left(\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\right)\\ &=[x_k]\left(-\sum_{i=1}^n{(n-2i+1)x_i}\right)\\ &\,\,\color{blue}{=2k-n-1}\\ \color{blue}{[x_k]f_3(x_1,\ldots,x_n)}&=[x_k]\left(-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\right.\\ &\qquad\qquad\qquad\left.+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\right)\\ &=-n[x_k]\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\\ &=-n[x_k]\sum_{i=1}^n\left(2x_i\cdot\frac{n-2i+1}{2n}\right)\\ &\qquad\qquad\qquad-n[x_k]\sum_{i=1}^n\left(-2\cdot\frac1n\sum_{j=1}^n{x_j}\cdot\frac{n-2i+1}{2n}\right)\\ &=-[x_k]\sum_{i=1}^nx_i(n-2i+1)+[x_k]\sum_{i=1}^n\frac{1}{n}\sum_{j=1}^n{x_j}(n-2i+1)\\ &=-(n-2k+1)+\sum_{i=1}^n\frac{1}{n}(n-2i+1)\\ &=-(n-2k+1)+(n+1-n-1)\\ &\,\,\color{blue}{=2k-n-1} \end{align*}

and finally the constant term .

We obtain \begin{align*} \color{blue}{[x_0]f_1(x_1,\ldots,x_n)}&=[x_0]\sum_{1\leq i<j\leq n}\left(\left(x_j-x_i\right)-\left(x_j-x_i\right)^2\right)\\ &\,\,\color{blue}{=0}\\ \color{blue}{[x_0]f_2(x_1,\ldots,x_n)}&=[x_0]\left(\left(\sum_{i=1}^n{x_i}\right)^2-n\sum_{i=1}^n{x_i^2}-\sum_{i=1}^n{(n-2i+1)x_i}\right)\\ &\,\,\color{blue}{=0}\\ \color{blue}{[x_0]f_3(x_1,\ldots,x_n)}&=[x_0]\left(-n\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\right.\\ &\qquad\qquad\qquad\left.+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\right)\\ &=-n[x_0]\sum_{i=1}^n\left(x_i-\frac1n\sum_{j=1}^n{x_j}+\frac{n-2i+1}{2n}\right)^2\\ &\qquad\qquad\qquad+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\\ &=-n[x_0]\sum_{i=1}^n\left(\frac{n-2i+1}{2n}\right)^2+\frac1{4n}{\sum_{i=1}^n(n-2i+1)^2}\\ &\,\,\color{blue}{=0} \end{align*}

We observe the coefficients of terms with equal powers are equal.

Conclusion:

We obtain by collecting the results from above \begin{align*} \color{blue}{f_1(x_1,\ldots,x_n)}&\color{blue}{=f_2(x_1,\ldots,x_n)=f_3(x_1,\ldots,x_n)}\\ &\,\,\color{blue}{=(1-n)\sum_{i=1}^nx_i^2+2\sum_{1\leq i<j\leq n}x_ix_j+\sum_{i=1}^n(2i-n-1)x_i} \end{align*}


$\def\peq{\mathrel{\phantom{=}}{}}$For the first identity, because$$ \sum_{i < j} (x_j - x_i) = \sum_{k = 1}^n x_k \left( \sum_{l < k} 1 + \sum_{l > k} (-1) \right) = \sum_{k = 1}^n (2k - n - 1) x_k, $$\begin{gather*} \sum_{i < j} (x_j - x_i)^2 = \sum_{k = 1}^n x_k^2 \left( \sum_{l < k} 1 + \sum_{l > k} 1 \right) - 2 \sum_{i < j} x_i x_j\\ = (n - 1) \sum_{k = 1}^n x_k^2 - \left( \left( \sum_{k = 1}^n x_k \right)^2 - \sum_{k = 1}^n x_k^2 \right) = n \sum_{k = 1}^n x_k^2 - \left( \sum_{k = 1}^n x_k \right)^2, \end{gather*} then$$ \sum_{i < j} ((x_j - x_i) - (x_j - x_i)^2) = \left( \sum_{k = 1}^n x_k \right)^2 - n \sum_{k = 1}^n x_k^2 - \sum_{k = 1}^n (n - 2k + 1) x_k. $$

For the second identity, denoting $\bar{x} = \dfrac{1}{n} \sum\limits_{k = 1}^n x_k$,\begin{align*} &\peq -n \sum_{k = 1}^n \left( x_k - \bar{x} + \frac{1}{2n} (n - 2k + 1) \right)^2 + \frac{1}{4n} \sum_{k = 1}^n (n - 2k + 1)^2\\ &= -\sum_{k = 1}^n \left( n(x_k - \bar{x})^2 + (n - 2k + 1)(x_k - \bar{x}) + \frac{1}{4n} (n - 2k + 1)^2 \right) + \frac{1}{4n} \sum_{k = 1}^n (n - 2k + 1)^2\\ &= -\sum_{k = 1}^n \left( n(x_k - \bar{x})^2 + (n - 2k + 1)(x_k - \bar{x}) \right)\\ &= -n \sum_{k = 1}^n (x_k - \bar{x})^2 - \sum_{k = 1}^n (n - 2k + 1) x_k + \bar{x} \sum_{k = 1}^n (n - 2k + 1)\\ &= -n \left( \sum_{k = 1}^n x_k^2 - n\bar{x}^2 \right) - \sum_{k = 1}^n (n - 2k + 1) x_k + 0\\ &= \left( \sum_{k = 1}^n x_k \right)^2 - n \sum_{k = 1}^n x_k^2 - \sum_{k = 1}^n (n - 2k + 1) x_k. \end{align*}