3
$\begingroup$

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.

$\endgroup$
5
  • 4
    $\begingroup$ Why don't you compile your code? Certainly, this will much more improve the performance than only converting things to Subtract and Divide. $\endgroup$
    – Domen
    Commented Feb 14 at 16:33
  • $\begingroup$ I work with a variety of input functions and some of them depend on special functions, which don't compile. $\endgroup$
    – Felipe
    Commented Feb 14 at 16:35
  • 2
    $\begingroup$ (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. $\endgroup$
    – Goofy
    Commented Feb 14 at 16:53
  • $\begingroup$ @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. $\endgroup$
    – Felipe
    Commented Feb 15 at 0:50
  • $\begingroup$ "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. $\endgroup$
    – Goofy
    Commented Feb 15 at 1:28

2 Answers 2

2
$\begingroup$

The Mathematica kernel machinery relies in the first place on pattern matching.

The problem of coding Mathematica is not time but unique identification of terms and lookup tables for typical simplifications to be done on each input, intermediary and output results.

Any ambiguity in notation of products of positive and negative powers and the order of sums would boost term identifcation time to infinity.

For the beginner its hard to see the negative parts of a sum to appear in front and to identify a denominator as a negative power in front of a product. But this notation has been evolved as the fastest to identify terms, that need special attention.

$\endgroup$
1
  • $\begingroup$ Even if it is faster in some cases it was also pointed out, in the previous posts, that the precision is not the same, which also bothers me. Also, I don't know if speed is the only factor on why they keep it that way. In one of the previous posts someone from wolfram research commented that fixing this was considered and rejected because it would generate backward incompatibility. $\endgroup$
    – Felipe
    Commented Feb 16 at 9:10
1
$\begingroup$

Mathematica provides no way to modify the parser. Therefore the choice between, for example, writing 'a/b' and 'Divide[a,b]' is mutually exclusive, as the former will always be parsed as 'Times[a, Power[b,-1]]' instead. This may seem disappointing, but it is a consequence of the typical use case of the software.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.