Is there such a thing as short circuit multiplication?
Asked Answered
S

2

9

We all know about short circuiting in logical expressions, i.e. when

if ( False AND myFunc(a) ) then
...

doesn't bother executing myFunc() because there's no way the if condition can be true.

I was curious as to whether there is an equivalent for your everyday algebraic equation, say

result = C*x/y + z

If C=0 there is no point in evaluating the first term. It wouldn't matter much performance-wise if x and y were scalars, but if we pretend they are large matrices and the operations are costly (and applicable to matrices) then surely it would make a difference. Of course you could avoid such an extreme case by throwing in an if C!=0 statement.

So my question is whether such a feature exists and if it is useful. I'm not much of a programmer so it probably does under some name that I haven't come across; if so please enlighten me :)

Scanty answered 16/11, 2011 at 2:27 Comment(4)
Logical short-circuiting is an important concept from a functionality standpoint while "arithmetic short-circuiting" is merely an optimization at the compiler level with no functional difference. Your language of choice may already be doing it behind the scenes without you noticing.Arbitral
Someone who knows more than me should answer, but I'd imagine you'd run into problems if you short circuited the division. What would happen, for example, if y=0? If short circuited it would return 0 when the answer is actually an error.Bisk
@Arbitral Arithmetic short-circuiting would indeed have functional differences beyond optimization, just as logical short circuiting does. Consider result = C*myfunction(). If C==0, causing the arithmetic expression to short-circuit, then myfunction is never invoked, and whatever side-effects it might have had do not occur (just as with logical short-circuiting).Knoll
In theory if you're multiplying a series of values and encounter a 0; you can stop right there. I'm if/how real compilers implement this optimization. As @Knoll mentioned the compiler would have to ensure there are no side-effects.Motorcar
A
7

The concept you are talking about goes under different names: lazy evaluation, non-strict evaluation, call by need, to name a few and is actually much more powerful than just avoiding a multiplication here and there.

There are programming languages like Haskell or Frege whose evaluation model is non-strict. There it would be quite easy to write your "short circuiting" multiplication operator, for example you could write something like:

infixl 7 `*?`        -- tell compiler that ?* is a left associative infix operator
                     -- with precedence 7 (like the normal *)

0 *? x = 0           -- do not evaluate x
y *? x = y * x       -- fall back to standard multiplication
Alaster answered 16/11, 2011 at 11:16 Comment(1)
Thanks! Accepted for answering some follow-up questions as well.Scanty
I
1

If the data is large and/or complex and the operations are costly, then the implementation of the operation should perform appropriate shortcut checks before committing to the costly operation. It's an internal detail of the implementation of the operator (say, matrix *) but really has nothing to do with the language concept of "multiply" and should have little impact on how you write your calculations.

Imco answered 16/11, 2011 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.