The answer is no. That's because this wouldn't actually be an
optimization: the number of "wasted" cycles in adding two floating point
numbers is one (assuming an FPU, which is a pretty good assumption). The
number of cycles to check whether either of the operands is infinity is
three: check whether the left operand is infinity, check the right
operand, and or them together. After that, you have to set the answer to
infinity and exit, which is another two cycles. And with every test you
tack on (if x is zero return y?) is another few cycles in the operation.

The reason you made this mistake (and it's an easy mistake to make) is
that humans are bad at arithmetic and good at tricks. Therefore, we use
a lot of tricks in order to do less arithmetic. Computers, on the other
hand, are good at arithmetic and bad at tricks--they've been heavily
optimized for arithmetic, and they execute every instruction in one
cycle (okay, not really--but close) so that even things that are easier
than arithmetic operations take the same amount of time. Therefore it's
best to just have them do the math like they were made to.

You can check that there's no optimization just by comparing adding a
random list of floating point numbers to another random list and then to
a list of infinities. The timings are the same.

Marshall

On Sat, May 18, 2013 at 02:13:28AM -0400, Robert Knight wrote:
> Thanks, Henry.  I also defer to Devon's more expert solution for a similar
> approach (which I had not seen while composing my reply to PT).
> 
> Your advice about premature optimization strategy is cogent:  I can't help
> wonder whether J's low-level arithmetic routines have "common sense"
> optimizations already built in.
> 
> For example:  Does J's "addition" procedure *automatically* return an
> infinity result when either of the addends is infinity -- rather than
> wasting cycles on the logically needless floating point addition?
> 
> 
> *-Robert Knight
> *
> *
> *
> *
> *
> On Fri, May 17, 2013 at 10:28 PM, Henry Rich <[email protected]> wrote:
> 
> 
> > Devon's & Robert's ideas are good.  Don't worry about performance until
> > you get something working.  Then use the performance monitor to tune.
> > Almost certainly you will be fixing the wrong thing if you try to optimize
> > first.
> >
> > Henry Rich
> >
> >
> > On 5/17/2013 10:13 PM, P T 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 <http://www.jsoftware.com/forums.htm>
> >>>>
> >>>>  ------------------------------**------------------------------**
> >>> ----------
> >>> For information about J forums see 
> >>> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
> >>>
> >>>  ------------------------------**------------------------------**
> >> ----------
> >> For information about J forums see 
> >> http://www.jsoftware.com/**forums.htm<http://www.jsoftware.com/forums.htm>
> >>
> >>  ------------------------------**------------------------------**
> > ----------
> > For information about J forums see 
> > http://www.jsoftware.com/**forums.htm<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