Calculates the sum of a series.
SUM(series, "naflag", "method")
series |
- |
A series. |
||||||
"naflag" |
- |
Optional. A string, the NA handling method.
|
||||||
"method" |
- |
Optional. A string, the summation method.
|
A real or complex scalar.
sum(1..10)
returns 55
sum((1..10) + i * (1..10))
returns 55 + 55i
W1: 1..1000
W2: {sum(w1)}
W3: cumsum(w1)
W4: w2 == w3[end]
W1 contains the series of the first 1000 positive integers;
W2 computes the sum
W3 computes the cumulative sum.
W4 verifies that the last point of the cumulative sum is equal to the total sum.
n := 1156287;
W1: 1..n
W2: {sum(w1)}
W3: {n * (n + 1) / 2}
W4: w2 == w3
Variable n is defined as a hot variable.
W1 creates a list of sequential integers where n is the length.
W2 computes the sum.
W3 computes the analytical sum.
W4 verifies the computed sum is identical to the analytical sum.
Changing n, for example n = 10_000_000, automatically updates the worksheet.
W1: {1, 2, nan, 3, 4, 5}
W2: {sum(w1)}
W3: {sum(w1, "omitnan")}
W4: {sum(w1, "includenan")}
W1 creates a series with a NaN value.
W2 computes the sum. The NaN value is skipped and the result is 15.
W3 is identical to W2, the NaN is skipped,
W4 includes the NaN in the sum. The result is NaN.
W1: {1e16, 1, -1e16, -1}
W2: {sum(w1)}
W3: {sum(w1, "neumaier")}
W4: {sum(w1, "direct")}
The series in W1 contains numbers that differ by many orders of magnitude. Mathematically, the exact sum of W1 should be 0.
The Neumaier compensation summation used in W2 and W3 track precision loss during intermediate steps and accurately return a total sum of 0.
The direct summation method in W4 uses standard floating point addition and suffers from catastrophic cancellation leading to an incorrect result of -1.
By default, NA values in a series are skipped over by SUM. Set naflag to "includenan" to include NaN values.
The default Neumaier method is a compensated summation algorithm that extends Kahan summation to accurately add floating-point numbers of vastly different magnitudes, minimizing catastrophic cancellation and rounding errors.
The less accurate "direct" method may be faster for very large series or arrays.
See SUMS to compute the point by point sum of multiple input series.
See VSUM to compute the sum of input values that consist of scalars and/or series.
See CUMSUM to compute the cumulative sum.