enum=: ,@|:&.#:"1 @ (#: i.) @ (2&^) @ ,~

BOXC=: 9!:6 ''

worm=: 3 : 0
 select. y 
  case. 0 do. ,:_1{BOXC
  case. 1 do. BOXC{~ _1 _1 2,0 _1 8,: 6 _1 _1
  case.   do.
   n=. #t=. worm y-1
   v=. (0{BOXC),((   n-2)$_2{BOXC),8{BOXC
   h=. (0{BOXC),((_1+2*n)$_1{BOXC),8{BOXC
   (t,.v,.(2{BOXC) (<_1 _1)}t),h,((6{BOXC) (<0 0)}t),.v,.t
 end.
)

  enum 3
 0  1  4  5 16 17 20 21
 2  3  6  7 18 19 22 23
 8  9 12 13 24 25 28 29
10 11 14 15 26 27 30 31
32 33 36 37 48 49 52 53
34 35 38 39 50 51 54 55
40 41 44 45 56 57 60 61
42 43 46 47 58 59 62 63
   worm 3
──┐┌──┐┌──┐┌──┐
┌─┘│┌─┘│┌─┘│┌─┘
└──┘└─┐│└──┘└─┐
┌─────┘│┌─────┘
└─┐┌──┐│└─┐┌──┐
┌─┘│┌─┘│┌─┘│┌─┘
└──┘└──┘└──┘└─┐
┌─────────────┘
└─┐┌──┐┌──┐┌──┐
┌─┘│┌─┘│┌─┘│┌─┘
└──┘└─┐│└──┘└─┐
┌─────┘│┌─────┘
└─┐┌──┐│└─┐┌──┐
┌─┘│┌─┘│┌─┘│┌─┘
└──┘└──┘└──┘└──



----- Original Message -----
From: Henry Rich <[EMAIL PROTECTED]>
Date: Wednesday, May 21, 2008 7:12
Subject: RE: [Jprogramming] Enumerating the plane
To: 'Programming forum' <[email protected]>

> I have used interleaved binary representations to
> do this.  The even bits of the scalar come from
> the x coordinate of the pair, and the odd bits
> come from the y coordinate.
> 
> For each direction, you have translation tables
> to work on the input numbers a byte at a time, so
> it's pretty fast.
> 
> Henry Rich
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED] 
> > [mailto:[EMAIL PROTECTED] On Behalf Of John Randall
> > Sent: Wednesday, May 21, 2008 10:05 AM
> > To: [email protected]
> > Subject: [Jprogramming] Enumerating the plane
> > 
> > Suppose I want to set up an explicit correspondence between the
> > nonnegative integers and the gridded first quadrant (pairs 
> (x,y) where
> > x and y are nonnegative integers).
> > 
> > I can do this by picking a norm on Z^2, and taking successive shells
> > swept out in some order.
> > 
> > If I use the 1-norm |(x,y)|=|x|+|y|, the shells are reverse 
> diagonals:> 
> > NB. yth triangular number
> > T=:2&!@>:
> > 
> > NB. pair->integer
> > f=:({: + T@:(+/))"1
> > 
> > odometer=: #: i.@(*/)
> > 
> >    |.|: 5 5 $ f odometer 5 5
> > 14 19 25 32 40
> >  9 13 18 24 31
> >  5  8 12 17 23
> >  2  4  7 11 16
> >  0  1  3  6 10
> > 
> > To invert this, I have to find the largest triangular number 
> less than
> > or equal to y.
> > 
> > NB. "inverse" of T
> > TI=:13 : '<. >./ > {: p. (-y),0.5 0.5'
> > 
> > NB. integer->pair
> > g=:3 : 0"0
> > n=.TI y
> > b=.y-T n
> > a=.n-b
> > a,b
> > )
> > 
> > 
> > NB. Check that f and g are inverses.
> > 
> > integers=:1e3 ?. 1e3
> > pairs=:_2 ]\ integers
> > 
> >    (-:[EMAIL PROTECTED]) pairs
> > 1
> >    (-:[EMAIL PROTECTED]) integers
> > 1
> > 
> > 
> > I could use another norm.  Taking the infinity norm  
> x>./ y, I get
> > shells that are quarter-squares, and inversion is 
> easier.  Taking the
> > Euclidean norm gives quarter-circles, and enumerating the shell
> > involves finding Pythagorean triples.
> > 
> > Does anyone have any suggestions on this?  In particular 
> are there
> > better ways to go for the verb TI?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to