You are on page 1of 2

COMBINATION$IN APL and the remaining 2 planes a l l zeros°

This matrix preserves the t r i a n g u l a r


The problem section in the issue of June structure of the 2-dimensional Upper
1971 invited readers to submit an Triangular matrix in each of i t s planes
expression which returned a l l the while at the same time the rows of the
combinations of 2 integers selected from various planes are progressively masked
the f i r s t N integers. The published out, 1 row in the i s t plane° 2 in the
solution ( s l i g h t l y rewritten) was second and so on: This masking out
process is performed by
~(N,N)T(2 ~N)+=I+~,(IN)o. <iN

Question: can this techniquebe used to (14)o.< 1 1 1 1


obtain combinations of M(>2) integers 2 2 2 2
from the f i r s t N integers? 3 3 3 3
4 4 4 4
Consideration of the above expression
shows that the 2 is fundamentally the while the preservation of the t r i a n g u l a r
dimensionality of the l e f t argument of structure is achieved by
encode. To extend the algorithm we must premultip]ication by the next lower
find an outer product yielding an order Upper Triangular matrix~ The
M-dimensional result. The heart of the result is the following d e f i n i t i o n of
2-D algorithm consists of grading down the general i zed Upper Tri angu]ar
the ones in the Upper Triangular matrix Matri x :-
(~N)o.<~N. To be specific consider N =
4, M = 2. The ones in
v R÷UT K
[I] ÷LI×~K=2
0111 [2] i~.(~N)o.<((¢tK-I)~ ((K-I)pN)
O011 piN)xUT K-I
0001 [3]
0000 [4] LI:R÷(~N)o.<IN
are graded down to give 2 3 4 7 8 12, I
is subtracted and the result expressed and the general expression for the
is arithmetic modulo 4 - 01, 02, 03, 12 combinations of M integers out of N is
13, 23 - with a final addition of I i f
the result is required in origin I I~(MpN)T(M~N)÷-I+~,UT M
notation.
I t is useful i n p r a c t i c e to embody this
To extend the algorithm to M = 3 we have in a function which allows M to be 1,
to seek a generalized 3-dimensional thus:
Upper Triangular matrix which identifies
012,013,023,123. Regarding these as 3 V R+M COM N
dimensional co-ordinates in origin 0, [1] -~LlxIM~i
the matrix we seek has as i t s f i r s t two [2] R~-(N,I)p iN
planes:- [3] +o
[4] L1 :R÷I+~(MpN)T(M:I~)
÷-I+~,UT M
00OO
0011
0001
0OOO I t remains to compare this with other
means of obtaining the required
combinations. The following pairs of
0000 interconnected functions have the same
0000 effect as COM - the combinations are
0001 generated in odometer fashion using a
0000 recursion from l e f t to right through the
columns of the result.

2 .....
V M COMBS N
[ I] Y~Ip i
[2 ] NEXTCOL I
V
The figures speak for themselves - for
V NEXTCOL K
small H, the dimensional method is the
[1] ÷L1×lK=1
more e f f i c i e n t , but space limitations
[2] z[x]÷YEX-1]+1 increase rapidly with M, The following
[3] LI:÷L3xlK=pY table shows the maximumvalue of N which
[4] L2:NEXTCOL K+I
can be achieved for given M in the 48K
[5] Y[K]÷Y[K]+i
work-space:-
[6] ÷L2xY[K]~N-M-K
[7] LS:Y
[8] Y[K]+Y[K]+i
[9] ~ L4:÷LSxY[K]~N

M 2 3 4 5
The following table gives CPU times (in Maximum N 60 19 9 6
1/60th sec) for the 2 algorithms on an -L---- J

IBM 360/50 with a 48K work-space.

Norman Thompson
C~ IBM
United Kingdom Laboratories
2 3 4 5 6 7 8 9 Hursley Park Winchester
Hampshire SO21 2JN
2 5 5 4 4 6 7 7 7

3 7 8 10 12 14 19 25 34

11 14 18 30 49 79 126 197

5 15 24 48 108 236 * * *

• = WS FULL

2 3 4 5 6 7 8 9
, , ..... ~, ,,,, , ,

2 i 8 13 22 28 37 48 60 72

3 12 10 29 47 78 115 171 226

14 16 18 ~2 90 170 288 464

5 18 19 20 18 63 155 329 611

23

You might also like