dyadic bond can often be frustrating if used accidentally.  For those who don't 
know it applies power ^:[ to the bonded verb.

  2 (2&+) 1 
5 


the pad function below adds a border of 0's to a table.  unpad takes away the 
border.

unpad =: (1&(-@[ }."1 -@[ }. [ }."1 }.)) 
pad =: (0&([,.~[,.[,~,)) :. unpad 

notice the :. which defines an inverse to pad.

pad can be called dyadically to add more than 1 border layer. (dyadic bond rule)
 
  2 pad i.3 3 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 
0 0 0 1 2 0 0 
0 0 3 4 5 0 0 
0 0 6 7 8 0 0 
0 0 0 0 0 0 0 
0 0 0 0 0 0 0 

with the &. conjunction, 2 layers are also unpad'ed (though this example 
doesn't use the pad), adds 2 to each element.
 
  2&+&.(2&pad) i.3 3 
2 3  4 
5 6  7 
8 9 10 

adding 2 to each element (including pad) then taking a columnwise running sum 
of the matrix before unpad'ing

  +/\@:(2&+)&.(2&pad) i.3 3 
6  7  8 
11 13 15 
19 22 25

pad and unpad are mostly useful in conway life type algorithms or more 
generally, ;. 3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to