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?
Best wishes,
John
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm