Question:
The floating point sum of x + y x+y x+y has a relative error ϵ < ϵ m \epsilon < \epsilon_{m} ϵ<ϵm. ϵ m \epsilon_{m} ϵm is the machine epsilon.
double compute_sum(double x[], int n)
{
double S = 0;
for (int i = 0; i < n; ++i)
S += x[i];
return S;
}
Show that the absolute error in the sum computed in the above code is no worse than ( n − 1 ) ⋅ ϵ m ∑ i = 0 n − 1 ∣ x i ∣ (n-1)\cdot \epsilon_{m}\sum_{i=0}^{n-1}|x_i| (n−1)⋅ϵm∑i=0n−1∣xi∣.
Answer:
S = x 0 + x 1 + … + x n − 1 S=x_0+x_1+\ldots+x_{n-1} S=x0+x1+…+xn−1
Considering the error ϵ \epsilon ϵ , let:
S 0 = ( x 0 + x 1 ) ⋅ ( 1 + ϵ 0 ) S_0 = (x_0+x_1)\cdot(1+\epsilon_0) S0=(x0+x1)⋅(1+ϵ0)
S 1 = ( S 0 + x 2 ) ⋅ ( 1 + ϵ 1 ) S_1 =(S_0+x_2)\cdot(1+\epsilon_1) S1=(S0+x2)⋅(1+ϵ1)
S 2 = ( S 1 + x 3 ) ⋅ ( 1 + ϵ 2 ) S_2=(S_1+x_3)\cdot(1+\epsilon_2) S2=(S1+x3)⋅(1+ϵ2)
⋮ \vdots ⋮
S n − 2 = ( S n − 3 + x n − 1 ) ⋅ ( 1 + ϵ n − 2 ) S_{n-2} = (S_{n-3}+x_{n-1})\cdot(1+\epsilon_{n-2}) Sn−2=(Sn−3+xn−1)⋅(1+ϵn−2)
Considering S 0 S_0 S0 & S 1 S_1 S1, we have:
S 0 = ( x 0 + x 1 ) ⋅ ( 1 + ϵ 0 ) = ( x 0 + x 1 ) + ( x 0 + x 1 ) ⋅ ϵ 0 \begin{aligned} S_0 &= (x_0+x_1)\cdot(1+\epsilon_0) \\ &=(x_0+x_1)+(x_0+x_1)\cdot\epsilon_0 \end{aligned} S0=(x0+x1)⋅(1+ϵ0)=(x0+x1)+(x0+x1)⋅ϵ0
S 1 = ( S 0 + x 2 ) ⋅ ( 1 + ϵ 1 ) = ( ( x 0 + x 1 + x 2 ) + ( x 0 + x 1 ) ⋅ ϵ 0 ) ( 1 + ϵ 1 ) = ( x 0 + x 1 + x 2 ) + ( x 0 + x 1 ) ⋅ ϵ 0 + ( x 0 + x 1 + x 2 ) ϵ 1 + ( x 0 + x 1 ) ⋅ ϵ 0 ⋅ ϵ 1 = ( x 0 + x 1 + x 2 ) + ( x 0 + x 1 ) ⋅ ϵ 0 + ( x 0 + x 1 + x 2 ) ϵ 1 \begin{aligned} S_1 &=(S_0+x_2)\cdot(1+\epsilon_1) \\ &=((x_0+x_1+x_2)+(x_0+x_1)\cdot\epsilon_0)(1+\epsilon_1) \\ &=(x_0+x_1+x_2)+(x_0+x_1)\cdot\epsilon_0 +(x_0+x_1+x_2)\epsilon_1+(x_0+x_1)\cdot\epsilon_0\cdot\epsilon_1 \\ &= (x_0+x_1+x_2)+(x_0+x_1)\cdot\epsilon_0 +(x_0+x_1+x_2)\epsilon_1\end{aligned} S1=(S0+x2)⋅(1+ϵ1)=((x0+x1+x2)+(x0+x1)⋅ϵ0)(1+ϵ1)=(x0+x1+x2)+(x0+x1)⋅ϵ0+(x0+x1+x2)ϵ1+(x0+x1)⋅ϵ0⋅ϵ1=(x0+x1+x2)+(x0+x1)⋅ϵ0+(x0+x1+x2)ϵ1
Hence,
S n − 2 − S = x 0 ⋅ ( ϵ 0 + … + ϵ n − 2 ) + x 1 ⋅ ( ϵ 0 + … + ϵ n − 2 ) + x 2 ⋅ ( ϵ 1 + … + ϵ n − 2 ) + … + x ( n − 1 ) ⋅ ϵ n − 2 < ∣ x 0 ∣ ⋅ ( ϵ 0 + … + ϵ n − 2 ) + ∣ x 1 ∣ ⋅ A + … + ∣ x n − 1 ∣ ⋅ A < ∣ x 0 ∣ ⋅ ( ( n − 1 ) ϵ m ) + ∣ x 1 ∣ ⋅ B + … + ∣ x n − 1 ∣ ⋅ B = ( n − 1 ) ⋅ ϵ m ⋅ ∑ i = 0 n − 1 ∣ x i ∣ \begin{aligned} S_{n-2}-S &= x_0\cdot(\epsilon_0+\ldots+\epsilon_{n-2})+x_1\cdot(\epsilon_0+\ldots+\epsilon_{n-2}) \\ &+x_2\cdot(\epsilon_1+\ldots+\epsilon_{n-2})+\ldots+x(n-1)\cdot\epsilon_{n-2} \\ &<|x_0|\cdot(\epsilon_0+\ldots+\epsilon_{n-2})+|x_1|\cdot A+\ldots+|x_{n-1}|\cdot A \\ &< |x_0|\cdot((n-1)\epsilon_m)+|x_1|\cdot B+\ldots+|x_{n-1}|\cdot B \\&= (n-1)\cdot\epsilon_m\cdot\sum_{i=0}^{n-1}|x_i|\end{aligned} Sn−2−S=x0⋅(ϵ0+…+ϵn−2)+x1⋅(ϵ0+…+ϵn−2)+x2⋅(ϵ1+…+ϵn−2)+…+x(n−1)⋅ϵn−2<∣x0∣⋅(ϵ0+…+ϵn−2)+∣x1∣⋅A+…+∣xn−1∣⋅A<∣x0∣⋅((n−1)ϵm)+∣x1∣⋅B+…+∣xn−1∣⋅B=(n−1)⋅ϵm⋅i=0∑n−1∣xi∣