Intro14


14. Partitions


The function sum=: +/ applies to an entire list argument;
to compute partial sums or subtotals,
it is necessary to apply it to each prefix of the argument. For example:

   sum=: +/ 
   a=: 1 2 3 4 5 6
   (sum a) ; (sum a)
+--+--------------+
|21|1 3 6 10 15 21|
+--+--------------+

The symbol denotes the prefix adverb,
which applies its argument (in this case sum)
to each prefix of the eventual argument. The adverb . applies
similarly to suffixes:

   sum. a
21 20 18 15 11 6

The monad < simply boxes its arguments,
and the verbs < and <. therefore show
the effects of partitions with great clarity. For example:

   <1 2 3
+-----+
|1 2 3|
+-----+
   (<1),(<1 2),(<1 2 3)
+-+---+-----+
|1|1 2|1 2 3|
+-+---+-----+
   < a
+-+---+-----+-------+---------+-----------+
|1|1 2|1 2 3|1 2 3 4|1 2 3 4 5|1 2 3 4 5 6|
+-+---+-----+-------+---------+-----------+
   <. a
+-----------+---------+-------+-----+---+-+
|1 2 3 4 5 6|2 3 4 5 6|3 4 5 6|4 5 6|5 6|6|
+-----------+---------+-------+-----+---+-+

The oblique adverb /. partitions a table
along diagonal lines. Thus:

   </. t=: 1 2 1 */ 1 3 3 1
+-+---+-----+-----+---+-+
|1|3 2|3 6 1|1 6 3|2 3|1|
+-+---+-----+-----+---+-+
   t ; (sum/. t) ; (10 #. sum/. t) ; (121*1331)
+-------+-------------+------+------+
|1 3 3 1|1 5 10 10 5 1|161051|161051|
|2 6 6 2|             |      |      |
|1 3 3 1|             |      |      |
+-------+-------------+------+------+


Exercises

14.1   Define programs analogous to sum=:+/ for
progressive products, progressive maxima, and progressive minima.

14.1   Treat the following programs and comments like those of
Section 12, that is, as exercises in reading and writing.
Experiment with expressions such as c pol x and c pp d
and (c pp d) pol x with c=:1 3 3 1
and d=:1 2 1 and x=:i.5 .
See the dictionary or Section 20
for the use of rank:

pol=: +/@([*]^i.@#@[)"1 0   Polynomial
pp=: +//.@(*/) Polynomial product
pp11=: 1 1&pp Polynomial product by 1 1
pp11 d
pp11^:5 (1)
ps=: +/@,: Polynomial sum