Jsoftware
High-Performance Development Platform

Please help. Can it be simplified?

View: New views
4 Messages — Rating Filter:   Alert me  

Please help. Can it be simplified?

by Bo Jacoby :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

This code computes odds for a hypergeometric distribution
   u=.!/
   v=.i.@>:
   2(u&(-v)*u&v)4
6 3 1 0 0
0 3 4 3 0
0 0 1 3 6
Can the expression
   u&(-v)*u&v
be simplified?



      ___________________________________________________________
Skal du købe ny bil? Sammenlign priser på brugte biler med Kelkoo og find et godt tilbud! - Se mere her http://dk.yahoo.com/r/pat/mmb
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Re: Please help. Can it be simplified?

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bo Jacoby wrote:
> Can the expression
>   u&(-v)*u&v

This is an interesting question in the general case, but I'll return to
that another time.  

For your particular  u  and  v  the answer is yes:  

           2 (*];.0)@:(u&v)4
        6 3 1 0 0
        0 3 4 3 0
        0 0 1 3 6
           
The trick is  ];.0  which reverses all the axes of its input  (basically
it's  ($ $ |.@:,)  ).  This works in your case because that's the
relationship between  u&v  and  u&(-v)  : your  u  is a function table and
for your  v  the hook  (-v)  is just its reverse.

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

Re: Please help. Can it be simplified?

by Dan Bron :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bo Jacoby asked:
> Can the expression
>   u&(-v)*u&v
> be simplified?

I responded with:
>  (*];.0)@:(u&v)

I just realized that my reformulation is actually longer than your original:

           u     =.  !/
           v     =.  i.@:>:
       
           hg0   =.  u&(-v)*u&v
           hg1   =.  (*];.0)@u&v
           
           (-.&' ' ,&# ;:)@(5!:5) HGs =. hg0`hg1              NB.  # chars, # words
        10 10
        11 10

though not by much.  It's also somewhat faster:
       
           5j1 ": (%"1 <./)  TS =. 5 ts&> HGs ,L:0 '~ 2000'   NB. time-space
comparison on large dataset
          2.0  1.0
          1.0  1.2

But the question is whether you think the reformulation is worth it for
just a factor of 2.  I guess it depends on whether you think "slightly
longer but less redundant" is simpler than your symmetric formulation, and
whether you find  ];.0  obscure.

By the way, stylistically I prefer to put the more complex tine of a fork
on the right.  Often this allows me to elide parens, but I also think it
scans better:

           u&(-v) * u&v
vs
           u&v * u&(-v)

Here, we get a gentler introduction to the idea, and then we get to see how
the right tine changed from the "basis".  Another indication that a
stylistic rule has merit is if it increases or retains a formula's
simplicity after "algebraic reduction".  For example, considering only the
monads (though you're concerned with the dyads), we could express the
second formulation as  *&:u&v -  .  But for the first one we'd have to add
an extra reflex  *&:u&v~ -  .

-Dan

PS:  The fact that J permits algebraic manipulations without even a
reference to data is another reason I enjoy it.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Parent Message unknown Re: Please help. Can it be simplified?

by Bo Jacoby :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you, Dan, for your analysis and your solution.

When expanded, your version (hg1) is shorter than mine (hg0):
   hg1 f.
(* ];.0)@(!/)&(i.@>:)
   hg0 f.
!/&(- i.@>:) * !/&(i.@>:)

I do find ];.0  obscure, but only until I look ;. up in the dictionary.

I agree with you that manipulation of J-expressions is facinating. I just don't have much routine of doing it.

-Bo

--- Den tirs 24/11/09 skrev Dan Bron <j@...>:

Fra: Dan Bron <j@...>
Emne: Re: [Jprogramming] Please help. Can it be simplified?
Til: "Programming forum" <programming@...>
Dato: tirsdag 24. november 2009 16.55

Bo Jacoby asked:
> Can the expression
>   u&(-v)*u&v
> be simplified?

I responded with:
>  (*];.0)@:(u&v)

I just realized that my reformulation is actually longer than your original:

       u     =.  !/
       v     =.  i.@:>:
   
       hg0   =.  u&(-v)*u&v
       hg1   =.  (*];.0)@u&v
       
       (-.&' ' ,&# ;:)@(5!:5) HGs =. hg0`hg1              NB.  # chars, # words
    10 10
    11 10

though not by much.  It's also somewhat faster:
   
       5j1 ": (%"1 <./)  TS =. 5 ts&> HGs ,L:0 '~ 2000'   NB. time-space
comparison on large dataset
      2.0  1.0
      1.0  1.2

But the question is whether you think the reformulation is worth it for
just a factor of 2.  I guess it depends on whether you think "slightly
longer but less redundant" is simpler than your symmetric formulation, and
whether you find  ];.0  obscure.

By the way, stylistically I prefer to put the more complex tine of a fork
on the right.  Often this allows me to elide parens, but I also think it
scans better:

       u&(-v) * u&v
vs
       u&v * u&(-v)

Here, we get a gentler introduction to the idea, and then we get to see how the right tine changed from the "basis".  Another indication that a
stylistic rule has merit is if it increases or retains a formula's
simplicity after "algebraic reduction".  For example, considering only the monads (though you're concerned with the dyads), we could express the
second formulation as  *&:u&v -  .  But for the first one we'd have to add an extra reflex  *&:u&v~ -  .

-Dan

PS:  The fact that J permits algebraic manipulations without even a
reference to data is another reason I enjoy it.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm



      Find din nye laptop på kelkoo.dk. Se de gode tilbud her - http://dk.yahoo.com/r/pat/mm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm