I suggest that as usual, you should write the expression 
in the "best" way, and then wait for the implementation
to catch up.

What might the "best" way be?  Perhaps:

({...@[ f ]) *. ({:@[ g ])

where f and g are one of < <: , used as
(a,b) expression x  where a and b are atoms
and x is an array.  If the interval endpoints 
are known, I might write it as  (y1&<: *. <&y2) .

If/when the implementation catches up,
the time should be the same as in C and the
space should be optimal (just whatever space
is needed to hold the overall results).



----- Original Message -----
From: Boyko Bantchev <[email protected]>
Date: Wednesday, October 28, 2009 10:13
Subject: Re: [Jprogramming] x ∈ [y1,y2)
To: Programming forum <[email protected]>

> Hi Kenneth,
> 
> 2009/10/28 Lettow, Kenneth <[email protected]>:
> > The equivalent phrase is:
> > CO=: ({...@] <: [)*.([ < {:@])
> > A quick test shows that it is faster and almost as lean.
> > ...
> 
> Thank you for pointing out this, and strange that CO is so
> much faster.  Would anybody knowledgeable explain why?
> 
> As for ‘lean’ … well, CO is a direct expression of what is wanted
> (y1<=x and x<y2), so nothing to be surprised of.  
> Plus, it needs
> operations for explicitly (albeit tacitly) accessing each of x, y1
> and y2, including extracting y1 and y2 from a vector.  I wouldn't
> – in an array language – call this lean :)  In fact, it 
> would have
> been more straightforward to express the same in C.
> 
> The first variant I came up was
> 
>    w1=. (>:{.)*.(<{:)
> 
> which is not unlike CO but simpler.  Then I had
> 
>    w2=. (<{.)<(<{:)
> 
> which is simpler yet.  I was finally satisfied with
> 
>    w3=. </@:<
> 
> Each of w1, w2, and w3 is simpler than CO, and w1 and w2
> are as fast.  Only w3 is slower, for a reason that is mysterious
> to me.
> 
> I should also add that, in case it is not known which end of the
> interval is the lower one, we could use a very similar verb:
> 
>    w4=. ~:/@:<
> 
> in place of w3.
> 
>    2.4 (~:/@:<) 2 3
> 1
>    2.4 (~:/@:<) 3 2
> 1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to