And I never worry much about timing as J always beats me "big time", even
when poorly but correctly written.

Linda

-----Original Message-----
From: programming-boun...@forums.jsoftware.com
[mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Peter B.
Kessler
Sent: Tuesday, November 06, 2012 8:38 PM
To: programm...@jsoftware.com
Subject: Re: [Jprogramming] Arc consistency in J

I certainly looked at the difference in the resulting tacit forms.  I did
not look at the performance difference until you prompted me to

       NB. Modest values for to get numbers in the believable range.
       1000 (6!:2) 'f 1000'
    0.00125151
       1000 (6!:2) 'upperRightOnes 1000'
    0.00124929

       NB. 10^2 times as many inputs, comparisons, and results.
       100 (6!:2) 'f 10000'
    0.239497
       100 (6!:2) 'upperRightOnes 10000'
    0.237435

       NB.  Might as well measure the space, too.
       (7!:2) 'f 1000'
    1066624
       (7!:2) 'upperRightOnes 1000'
    1058816

but don't see a lot of difference.  Is that the kind of timing test you were
thinking of?  There's also the time taken to read the code, where not having
to think about hooks might balance out the time needed to recognize that
identical arguments are being supplied to both sides of the  </  .  I was
hoping that the hook (or the fork) would make it obvious to the reader and
the implementor that  i. y  only needed to be evaluated once.

I am delighted to discover that someone has taken the time to look at the
trees for these two forms and (apparently) discovered the common
sub-expression so that  i. y  is only evaluated once in each statement.  I'm
happy when I can write code the way I think of it and find that someone is
making it efficient behind my back.

                        ... peter

Linda Alvord wrote:
> Did you compare the tacit verbs of these two functions?
> 
>    f=: 13 :'(i.y)</i.y'
>    f
> i. </ i.
>    
>    5!:4 <'f'
>   -- i.     
> --+- / --- <
>   L- i.     
>             ^
>    upperRightOnes=: 13 : '(</ ]) i. y'
>    upperRightOnes
> [: (</ ]) i.
>    
>    5!:4 <'upperRightOnes'
>   -- [:          
>   │    -- / --- <
> --+----+- ]      
>   L- i.    
> 
> 
> In situations like this I guess timing would be the referee.
> 
> Linda
> 
> -----Original Message-----
> From: programming-boun...@forums.jsoftware.com
> [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of Peter B.
> Kessler
> Sent: Tuesday, November 06, 2012 2:26 PM
> To: programm...@jsoftware.com
> Subject: Re: [Jprogramming] Arc consistency in J
> 
> 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
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to