5

In the standard evaluation process, innermost parts are evaluated first. For example:

In[0]:= (a/a + 1) * 0

The evaluation process gives:

(a/a + 1) * 0 = (1 + 1) * 0 = 2 * 0 = 0

In that case, this evaluation doesn't make sense because the result will always be 0. I'd like to write a rule where the left member "x" is never evaluated:

Multiply[x_, 0] := 0

For example:

In[1]:= Multiply[Simplify[D[Cos[x]^(x + 1)/x^4, {x, 5}], 0]
Out[1]:= 0

without evaluating Simplify[D[Cos[x]^(x + 1)/x^4, {x, 5}] which is time consuming.

The built-in rule of Mathematica is very slow too (about 2s on my machine), so it means innermost parts are evaluated first:

In[2]:= 0 * Simplify[D[Cos[x]^(x + 1)/x^4, {x, 5}]]
Out[2]:= 0

I'm not sure this kind of concept exists in Mathematica. Any suggestions?

Thanks for the help!

BP75
  • 51
  • 3

2 Answers2

6

We can use the attribute HoldAll:

SetAttributes[Multiply, HoldAll]

Multiply[___, 0, ___] = 0;
Multiply[args___] := Times[args]

Multiply[Pause[1000], 0] // AbsoluteTiming
{4.*10^-6, 0}
Greg Hurst
  • 35,921
  • 1
  • 90
  • 136
  • Funny, I read this as wanting to modify the behavior of Times, but reading it again that may not have been the intent at all. +1 I think you have an error though, with HoldFirst. – Mr.Wizard May 23 '20 at 23:18
2

Interesting question. I don't have time today to address this in depth but this kind of evaluation can be controlled by attributes, specifically HoldAll. One cannot change the attributes of core functions without risking serious problems however, but just to illustrate:

Internal`InheritedBlock[{Times},
  SetAttributes[Times, HoldAll];
  0*Simplify[D[Cos[x]^(x + 1)/x^4, {x, 5}]]
]

This is basically like using Unevaluated (below) but it does not require foreknowledge of which part to apply it to.

0*Unevaluated[Simplify[D[Cos[x]^(x + 1)/x^4, {x, 5}]]]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Thank you for these answers.
    But how to override the Times function globally for all my computations (not locally with InheritedBlock) without risking problems?
    – BP75 May 24 '20 at 10:23
  • @BP75 I don't believe you can as modifying a deeply integrated System function could have many unforeseen consequences. The best I can offer would be an automatic replacement of all Times expressions in user input with a separate function like Chip Hurst's Multiply, but I don't know what your actual application is and this may not help. See: (117), (7223), (16652), (63147) – Mr.Wizard May 24 '20 at 12:23