In your example 0 is always divided by 0.

   x=:6 6$4 0 0 0 0 0 0 5 0 0 0 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   y=:6 6$1 0 0 0 0 0 0 2 0 0 0 0 0 0 3 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0
   x%y
4   0 0    0 0 0
0 2.5 0    0 0 0
0   0 2    0 0 0
0   0 0 1.75 0 0
0   0 0    0 0 0
0   0 0    0 0 0
   
   0%0
0
   
As a math teacher, I would be leery a about writing a way divide most numbers 
by 0 to get 0.  It seems like writing code to have  14 % 2  result  in a  5 or 
an 8.

Linda

-----Original sMessage-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of P T
Sent: Friday, May 17, 2013 10:13 PM
To: [email protected]
Subject: Re: [Jprogramming] newbie help: how to avoid division by zero

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

Reply via email to