## How About a Boxcar?

A Boxcar Filter is a gadget that runs (or maps) a function of four arguments over a rectangular grid of points. A rectangular grid is a data structure where each point has a pair of integer indices, i and j, say, and i runs from, say, 1, through M and j runs from 1 through N. The data structure is best viewed as a function from i and j to some value, which can be anything. Even better, let it be a list of lists, in the usual Mathematica way. The function of four arguments that the Boxcar filter maps over the grid expects four, nearest-neighbor points:

h[p1, p2, p3, p4],

p1=grid[i, j], p2=[i+1, j], p3[i, j+1], p4[i+1, j+1]

p1=grid[i, j], p2=[i+1, j], p3[i, j+1], p4[i+1, j+1]

The Boxcar filter mapper, then, must intelligently skip the last row and column. Enjoy:

fRows[opOf2Rows_,{a_,b_,c___}]:=

Join[{opOf2Rows[a,b]},fRows[opOf2Rows,{b,c}]];

fRows[_,{a_}]:={};

gCols[op4_,{a_,b_,c___},{d_,e_,f___}]:=

Join[{op4[a,b,d,e]},gCols[op4,{b,c},{e,f}]];

gCols[_,{a_},{d_}]:={};

boxCar[op4_,grid_]:=

fRows[gCols[op4[#1, #2, #3, #4]&, #1, #2]&, grid];

Here’s an application that draws criss-crosses in each box of a grid. Ampersand converts the previous expression into a function (a lambda expression), in which #1, #2, … denote positional arguments. Thus, {Line[{#1,#4}],Line[{#2,#3}]}& is a function of four arguments:

crissCross[grid_]:=boxCar[{Line[{#1,#4}],Line[{#2,#3}]}&,grid]

Advertisements