The Common Mean and APL
Vector 11:3, page 21
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 21
The classical definitions of the mean of two non-negative real numbers are the harmonic mean \(h(x,y)\), the geometric mean \(g(x,y)\) and the arithmetic mean \(a(x,y)\):
with \(\min(x,y) \le h(x,y) \le g(x,y) \le a(x,y) \le \max(x,y)\). For example, let \(x=1\) and \(y=99\). We obtain respectively:
As we see, there may be a serious gap between the geometric mean \(g(x,y)\) and the arithmetic mean \(a(x,y)\). This gap may be filled by the concept of common mean [1] — a not very familiar concept from the literature. For convenience, let \(x_0\) be the smaller of two non-negative real numbers \(x_0\) and \(y_0\). The geometric mean \(x_1\) and arithmetic mean \(y_1\) are:
If this procedure of forming alternatively geometric and arithmetic means is repeated indefinitely:
one obtains:
\(x_i\) and \(y_i\) converging to the same value. We define this as the common mean \(c(x_0,y_0)\) of the numbers \(x_0\) and \(y_0\).
For the example \(x=1\) and \(y=99\) for instance, with an accuracy of 10 digits, we get successively:
| 9.94987437 | and | 50.00000000 |
| 22.30456721 | and | 29.97493719 |
| 25.85687532 | and | 26.13975220 |
| 25.99792902 | and | 25.99831376 |
| 25.99812139 | and | 25.99812139 |
such that \(c(x,y) = 25.99812139\).
with \(9.94987437 < 25.99812139 < 50.00000000\).
In fact the order in which \(x\) and \(y\) are treated and the order in which the sequences of geometric and arithmetic means are calculated is not relevant and \(c(x,y) = c(y,x)\) (commutativity). In addition \(c(x,x) = x\) (idempotency). Finally, \(c(x,y) = 0\) if and only if \(x = 0\) or \(y = 0\) (or both).
Programming the algorithm for calculating the common mean can be somewhat complicated in most programming languages. In APL however, it is very simple. A function to do the job is:
which for the chosen example gives:
Accuracy is determined by the current, i.e. the default value of comparison tolerance ⎕CT. It may be changed by defining the comparison tolerance as a global or local variable.
Note: a special case is the common mean of the numbers 1 and \(1\sqrt{2}\):
which is known in the literature as the "ubiquitous constant U" since it turns up all over the place. Finally, the common mean is very useful in the design of simple and efficient algorithms for calculating the complete elliptic integrals of the first kind \(K(p)\) and of the second kind \(E(p)\). More details may be found in [1].
Reference¶
[1] J Spanier and K B Oldham: An Atlas of Functions. Hemisphere Publishing Corporation, New York. New York 1987.