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.
Subtract
andDivide
. $\endgroup$Subtract
andDivide
. 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. $\endgroup$x * y^-1
. For example, a symbolically computed jacobian inFindRoot
or other function. $\endgroup$