Thank you Raul for response.  This optimization can be implemented as a customized table with computational time saving of a factor of 2.  Does the pattern occur often enough to be worthwhile?
Is a factor of 2 sufficient to trouble with?
I forget Roger's rule, he took care to change the source if the savings were a factor of 10, or a factor of 2 or greater?

NB. CustomTable u/!.n  for n is 0 or 1

CustomTable =: conjunction define
 NB. insert is unaffected
 u/ y
:
 NB. customized table   u/!.n
 x ((<:`<@.n)/&:(i.@:#) * u/) y
)


Where n is n of the conjunction,
instead of for (i=0;i<N;++i) for (j=0;j<m;++j) statement;
we implement for (i=0;i<N;++i) for (j=i+n;j<m;++j) statement;
The remainder of the table is filled.

Here's an example.

NB. r finds the radius of the intersection of n-spheres
NB. x (r -: r~) y
r=: radius@:[ -&.:*: ex

radius=: {:
coordinate=: }:
length=: +/&.:*: :($:@:-)
d=: length&:coordinate
ex=: ((+*-)&:radius + *:@:d)%(+:@:d)


TESTS=:((4#10)#:".);._2]0 :0
 1111 1454  NB. tangent 3 4 5 triangle
    1   11  NB. normal contact
    1   31  NB. not touching
    1    2  NB. enclosed
)

assert '0 0.866025 0j1.11803 0j_' -: ": r/"2 TESTS


   r"1 CustomTable 1~ ~. ,/ TESTS
0 0       0.5  0.707107 0j0.707107         1
0 0 0j1.82737 0j1.20605   0.755929 0j1.16496
0 0         0  0.866025  0j1.11803       0j_
0 0         0         0          0         0
0 0         0         0          0         0
0 0         0         0          0         0


   NB. r is a horrible monster
   r f.
{:@:[ -&.:*: ((+ * -)&:{: + *:@:(3 : '+/&.:*: :($:@:-) y' :(4 : 'x +/&.:*: :($:@:-) y')&:}:)) % +:@:(3 : '+/&.:*: :($:@:-) y' :(4 : 'x +/&.:*: :($:@:-) y')&:}:)

Preselecting the argument pairs is an alternative with  2 combinations # SPHERES

Date: Fri, 29 Jun 2018 12:34:09 -0400
From: Raul Miller<[email protected]>
To: Programming forum<[email protected]>
Subject: Re: [Jprogramming] Proposal for a new table adverb
Message-ID:
        <CAD2jOU8O7bjBM2uRkTD5fTDX9a6bahQ_h8zUyw82wWejBK=b...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

Here's a tentative implementation of part of what I think you have described:

commute=:3 :0
   assert.2=#$y
   assert.=/$y
   assert. 0={.u b. 0
   mask=. <:/~i.#y
   inds=. (>. |:)mask*i.$y
   inds{u mask #&, y
)

That said, I don't think this would work for you - this winds up
testing each item independently so there's no comparison involved.

Perhaps you could flesh out an example to better show what you mean?

Thanks,

-- Raul On Fri, Jun 29, 2018 at 12:04 PM David Lambert <[email protected]> wrote:
I have an equality test.  If a=b then b=a .  (In my case "find overlapping
spheres", in other words the equality test may be non-trivial.)  The new
adverb would compute only the upper triangular set of cases and thereby
save about half the time.

    NB. assuming the main diagonal could be interesting
    NB. the new adverb would compute these cases
    <:/~ i.7
1 1 1 1 1 1 1
0 1 1 1 1 1 1
0 0 1 1 1 1 1
0 0 0 1 1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 1 1
0 0 0 0 0 0 1
----------------------

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

Reply via email to