I think Chris's solution is excellent and what Henry has to say about
premature optimization is also relevant to the bigger picture.


On Fri, May 17, 2013 at 11:20 PM, chris burke <[email protected]> wrote:

> > Sure, we can replace zeros with infinity and vice versa before and after
> > division. But, can it be avoided? What I am looking for is conditional
>
> Why do you want to avoid it? I suggest simply:
>
>    f=: %*0~:]
>
>
>    x f y
>
> 0 2
>
> 1 0.666667
>
>
>
> On Sat, May 18, 2013 at 10:13 AM, P T <[email protected]> wrote:
>
> > Thanks Devon and Robert.
> >
> > Sure, we can replace zeros with infinity and vice versa before and after
> > division. But, can it be avoided? What I am looking for is conditional
> > division without writing an explicit loop. My intention is to represent
> an
> > electrical transmission network with matrices and they can potentially be
> > large. I am trying to minimizing the number of operations.
> >
> > I am reading on sparse matrices in "Learning J" by Roger Stokes and looks
> > like it can do what I am looking for. But, I am not sure if it actually
> > does not perform the unnecessary operations (i.e. division by zeros) or
> > just a display issue.
> >
> > x =: 1 $. 6 6                                    NB. an empty 6x6 matrix
> > x =: 4 5 6 7 ( 0 0 ; 1 1; 2 2; 3 3) } x   NB. insert some data
> > x
> > 0 0 │ 4
> > 1 1 │ 5
> > 2 2 │ 6
> > 3 3 │ 7
> >
> > y =: 1 $. 6 6
> > y =: 1 2 3 4 ( 0 0 ; 1 1; 2 2; 3 3) } y
> > y
> > 0 0 │ 1
> > 1 1 │ 2
> > 2 2 │ 3
> > 3 3 │ 4
> >
> > x%y
> > 0 0 │    4
> > 1 1 │  2.5
> > 2 2 │    2
> > 3 3 │ 1.75
> >
> > Thanks,
> > PT
> >
> >
> >
> > On Fri, May 17, 2013 at 6:45 PM, Robert Knight <
> [email protected]
> > >wrote:
> >
> > > *PT-*
> > >
> > > To modify the (infinite) "zero-division" result from *infinity* to
> > > *zero*...
> > >
> > > How about adding infinity to the divisor's zero-elements?
> > >
> > > *z =: x%y+_*y=0*
> > >
> > >
> > >    ]x =. 2 2 $ 2
> > >
> > > 2 2
> > > 2 2
> > >
> > >    ]y =. 2 2 $ i.4
> > >
> > > 0 1
> > > 2 3
> > >
> > >    ]z =. x%y+_*y=0
> > >
> > > 0        2
> > > 1 0.666667
> > >
> > > *-Robert Knight*
> > > (Also a J-newbie)
> > >
> > >
> > > On Fri, May 17, 2013 at 6:44 PM, P T <[email protected]> wrote:
> > >
> > >
> > > > I am learning J (J602) and dividing one table with another. When
> > division
> > > > by zero occurs, I want the the value to be zero instead of infinity.
> >  For
> > > > example, in the results below, I want the first element to be 0.0
> > instead
> > > > of _
> > > >
> > > > ]x =. 2 2 $ 2
> > > > 2 2
> > > > 2 2
> > > >
> > > > ]y=. 2 2 $ i.4
> > > > 0 1
> > > > 2 3
> > > >
> > > > x%y
> > > > _        2
> > > > 1 0.666667
> > > >
> > > >
> > > > May be I can replace all occurrences of _ with 0.0. But, can I avoid
> > this
> > > > additional step?
> > > >
> > > > Thanks,
> > > > PT
> > > >
> ----------------------------------------------------------------------
> > > > 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
>



-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to