An Axis Operator


Depending on your viewpoint, bracket indexing is either an anomoly or an abomination.  Aside from Keyed Properties in Classes (whatever were they thinking) we can pretty much stop using them in Dyalog APL from Version 11 onward.

But that leaves the axis operator still something of a syntactic oddity.  Can we do anything about it ourselves?

The "obvious" approach is via a defined operator...

       ⎕cr 'axis'
 z←a(aa axis ww)w
 z←a aa[ww]w    
       1 2 3 (+ axis 1) 3 3⍴⍳9
SYNTAX ERROR
axis[1] z←a aa[ww]w
       ∧

Which - of course - we'd have expected if we'd bothered to read the manual.

But, ever the optimist, what if we define a dynamic operator?

       axis←{⍺ ⍺⍺[⍵⍵] ⍵}
       1 2 3 (+ axis 1) 3 3⍴⍳9
1 3  5
4 6  8
7 9 11  

So it seems that sometimes optimism is the best policy.

But being tidy souls, we'd like to tuck this away into a namespace where we keep all our other twiddly bits.

      )ns #.EXTEND
#.EXTEND
      #.EXTEND.axis←{⍺ ⍺⍺[⍵⍵] ⍵}
      1 2 3 (+ #.EXTEND.axis 1) 3 3⍴⍳9
SYNTAX ERROR
axis[0] #.EXTEND.axis←{⍺ ⍺⍺[⍵⍵]⍵}
                      ∧ 

Dang, as they say in the cowboy comics.

So lets do a little syntactic tweaking (because we know it works "where we are")...

       1 2 3 #.EXTEND.(+ axis 1) 3 3⍴⍳9
1 3  5
4 6  8
7 9 11

Yikes (as they also say).

More typing, or more syntactic anomolies?  Up to you.

The above is axis at its simplest (monadic only), more generally useful if we extend the definition to also handle dyadic usage...

    axis←{
 ⍝ Axis operator, examples: 2 (, axis 0) 3 4⍴1 or  (⊂ axis 0)3 4⍴2
     0=⎕NC'⍺':⍺⍺[⍵⍵]⍵
     ⍺ ⍺⍺[⍵⍵]⍵}

Page updated 18 January 2010.
Copyright © Dogon Research 2009-2010.