# How can I enforce the use of Subtract and Divide?

It has been noted before, here and here, that when you enter a/b or a-b, Mathematica does not convert these to Subtract[a,b] or Divide[a,b]. It uses the rather long forms Times[a, Power[b,-1]] and Plus[a, Times[b,-1]], which are slower, since you do two operations instead of one.

Has anyone found a systematic way around that? I do MCMC sampling where you calculate a function many times, in which case these effects can accumulate and result in a sizable difference in run time. I could always manually implement Subtract and Divide when declaring functions, but right now I am also working on a problem where I have to take several gradients of multivariable functions and calculate the resulting object many times. Therefore, implementing Subtract and Divide by hand would be unfeasible, as the gradient calculations themselves are already automatic.

• Why don't you compile your code? Certainly, this will much more improve the performance than only converting things to Subtract and Divide. Commented Feb 14 at 16:33
• I work with a variety of input functions and some of them depend on special functions, which don't compile. Commented Feb 14 at 16:35
• (1) Any code evaluated symbolically before numerically will convert Subtract and Divide. So you have to avoid that. Somehow. (2) You could post-process generated code, making sure it was held (not evaluated) until numeric inputs are in place. (3) Subtractions and divisions within internal functions are probably unreachable. (4) Internal functions evaluated on numeric input sometimes perform different computations than what would be done by the expression returned when evaluated on symbolic input. (E.g. Eigenvalues.) Tricky to optimize in such a case. Commented Feb 14 at 16:53
• @Goofy What do you mean by (3)? If I generate a Cos[x/y] through some derivative it is not possible to convert to Cos[Divide[x,y]]? Convert using code, of course. Commented Feb 15 at 0:50
• "possible to convert to Cos[Divide[x,y]]?" -- That's what I mean in (2). In (3): A system function computes a value for you. How can you keep the function from using x * y^-1. For example, a symbolically computed jacobian in FindRoot or other function. Commented Feb 15 at 1:28