Skip to content

The Common Mean and APL

Vector 11:4, page 123

mathematics and statistics language design

Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 123

I read with interest Joseph De Kerf’s article "The Common Mean and APL" in the January 1995 Education Vector. The various computations described therein have terse restatements in J:

   a =. # %~ +/
   g =. # %: */
   h =. a&.%

The arithmetic mean is the number of items divided into the sum; the geometric mean is the n-th root of the product (n is the number of items); and the harmonic mean is the arithmetic mean under reciprocal, i.e. h v ←→ %a%v. All three verbs work on arrays of numbers.

   a 1 99                         a 1 10 100
50                             37
   g 1 99                         g 1 10 100
9.949874371                    10
   h 1 99                         h 1 10 100
1.98                           2.702702703

The common mean on a 2-element vector is an iteration where at each step, the elements are replaced by the geometric and arithmetic means. That is, at each step, v is replaced by (g,a)v, and the common mean is the first element of the limit (g,a)^:_

   c =. {. @ ((g,a)^:_)
   c 1 99
25.99812139

Since the power conjunction ^: accepts an array of powers, successive iteration values are produced readily:

   (g,a)^:(i.6) 1 99
          1          99
9.949874371          50
22.30456721 29.97493719
25.85687532  26.1397522
25.99792902 25.99831376
25.99812139 25.99812139

Finally, the "ubiquitous constant U", the common mean of 1 and the reciprocal of the square root of 2, is:

   c 1,%%:2
0.84721308479