Robert Goudie wrote:
>
> Hi,
>
>
>
> In the essay on Sudoku solutions at
> http://www.jsoftware.com/jwiki/Essays/Sudoku the first line
>
>
>
> j =. (]/. i...@#) ,{;~3#i.3
>
>
>
> generates the indicies for each box. As a relative newcomer to J I am
> trying to figure out the relationship between the matrix created by the
> expression on the LHS of the ravel (,) and the paired box values on the
> RHS
> of this expression. I can "tune" the shape and contents of matrix
> created
> on the LHS to some degree by varying the paired values on the RHS.
> [...]
>
The expression above creates the matrix
0 1 2 9 10 11 18 19 20
3 4 5 12 13 14 21 22 23
6 7 8 15 16 17 24 25 26
27 28 29 36 37 38 45 46 47
30 31 32 39 40 41 48 49 50
33 34 35 42 43 44 51 52 53
54 55 56 63 64 65 72 73 74
57 58 59 66 67 68 75 76 77
60 61 62 69 70 71 78 79 80
Here you may evaluate the expression {;~3#i.3 to
see what it does--it creates coordinates of 3x3
submatrices to which each of 81 numbers belongs,
in the form of a matrix of boxes where each box contains
the coordinate of the submatrix.
, then ravels this matrix into an array of boxes,
each with one coordinate.
LHS of , then uses these boxed coordinates as
the keys (left argument of ]/.) to rearrange the
numbers from 0 to 80 (right argument of ]/. is
i...@# which is here just i.81) into the final shape.
As a simple example of workings of LHS, consider:
(0 0;0 0;0 1;0 1;0 0;0 0;0 1;0 1;1 0;1 0;1 1;1 1;1 0;1 0;1 1;1 1) ]/.
i.16
0 1 4 5
2 3 6 7
8 9 12 13
10 11 14 15
Here is important to notice that the keys, although
have something to do with coordinates, are not themselves
actual indices of anything, just a convenient way to group
items (in this case integers from 0 to 15) together.
In other words, yet another way to achieve the same
in this simple example could be:
(0;0;1;1;0;0;1;1;2;2;3;3;2;2;3;3) ]/. i.16
0 1 4 5
2 3 6 7
8 9 12 13
10 11 14 15
I would have used a different technique, namely block
matrices:
,./^:2 ] 9 (* +"0 2 ]) i.3 3
0 1 2 9 10 11 18 19 20
3 4 5 12 13 14 21 22 23
6 7 8 15 16 17 24 25 26
27 28 29 36 37 38 45 46 47
30 31 32 39 40 41 48 49 50
33 34 35 42 43 44 51 52 53
54 55 56 63 64 65 72 73 74
57 58 59 66 67 68 75 76 77
60 61 62 69 70 71 78 79 80
It's longer than Rogers code, but gives me a straightforward
generalization to nxm matrices (which might be useful if one
is considering generalizations of Sudoku):
3 ([: ,./^:2 * (* +"0 2 ]) i.@,) 2
0 1 6 7
2 3 8 9
4 5 10 11
12 13 18 19
14 15 20 21
16 17 22 23
24 25 30 31
26 27 32 33
28 29 34 35
I hope this helps!
Viktor
--
View this message in context:
http://old.nabble.com/Sudoku-solution-query-tp30431955s24193p30432824.html
Sent from the J Programming mailing list archive at Nabble.com.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm