Linda Alvord wrote:
This sequence has a few problems:
A=. ? (2$n)$2 NB. generate random matrix of [0,1]
A=. A *. (i.n) </ i.n NB. make it upper diagonal, zeros on diagonal
] A=. A + |: 2*A NB. make it symmetrix referencing transpose in C.
All the ideas you used are correct. Maybe something like this is simpler:
f=: 13 :'(i.y)</i.y'
(f n)+|:2*f n=:4
0 1 1 1
2 0 1 1
2 2 0 1
2 2 2 0
Linda
I have no idea where this fits in the scheme of sudoku solvers, but the repeated use of "i. y" in
your definition of "f" seems awkward, and the repeated use of "f n" in your last
statement seems awkward. An alternative to duplicating an expression is to evaluate it once and use it as an
argument to a monadic fork or hook, since those implicitly reference their right argument for both the left
and right verbs. So you could write
NB. The hook references i. y twice.
upperRightOnes=: 13 : '(</ ]) i. y'
upperRightOnes 4
0 1 1 1
0 0 1 1
0 0 0 1
0 0 0 0
NB. The outer fork references upperRightOnes y twice.
upperRightOnesLowerLeftTwos=: 13 : '(] + ([: |: (2&*))) upperRightOnes y'
upperRightOnesLowerLeftTwos 4
0 1 1 1
2 0 1 1
2 2 0 1
2 2 2 0
I am a total novice at J, so you can ignore my observations on programming
style.
... peter
P.S. I can't believe that I'm actually advocating the use of a monadic hook,
since they are among the things I find most confusing, because of their
asymmetry. If you have trouble reading the hook, you can rewrite it as a
monadic fork
upperRightOnesFork=: 13 : '([ </ ]) (i. y)'
upperRightOnesFork 4
0 1 1 1
0 0 1 1
0 0 0 1
0 0 0 0
which seems sort of redundant with the definition of hook[1]. The use of "[" instead of
"]" for the left verb of the fork is due to Raul Miller[2], and serves to remind us to
think about what this verb would mean if used as a dyad.
[1] http://www.jsoftware.com/help/dictionary/dictf.htm
[2] http://jsoftware.com/pipermail/programming/2012-August/028995.html
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm