2

I have the following vector:

t ← 1 2 2 3 4 5 5 5 6 6

and would like to sum each pair of it (similar to 2+/vec, which sums every 2 adjacent elements).

What is the most efficient way to do this in APL?

syntagma
  • 23,346
  • 16
  • 78
  • 134

2 Answers2

2

If I understand your question, you are trying sum every two items. The easiest way to do this is to reshape into a matrix and then sum across the rows:

    t←1 2 2 3 4 5 5 5 6 6
       5 2⍴t
1 2
2 3
4 5
5 5
6 6
       +/5 2⍴t
3 5 9 10 12

This easily generalizes for triplets, etc. You can obviously write a little function to determine the shape of the resulting matrix.

Paul Mansour
  • 1,436
  • 9
  • 11
0

Try

2 +/ 1 drop (-1) drop 2 / x

Your timings will vary, by APL implementation, and by the datatype and width of the integer. Most APLs support at least 2 types of numbers, 2 or 4 byte integers, 8 byte floating point, not to mention booleans. Dyalog APL uses three different integer widths, the speed would be different for each of them. Experiment.

Lobachevsky
  • 1,222
  • 9
  • 17