1

I have stumbled across some weird behaviour with Apart.

ans1 = Apart[(x1/r2 + x2/r1)/(1/r1 + 1/r2)]

Gives

(r1 x1)/(r1 + r2) + (r2 x2)/(r1 + r2)

But

ans2 = Apart[(b1/r2 + b2/r1)/(1/r1 + 1/r2)]

Despite being logically equivalent to ans1 gives

b2 + (b1 r1 - b2 r1)/(r1 + r2)

So, depending on the alphabetic letter of the variable i.e. x1, x2 versus b1, b2, Mathematica behaves differently. The two solutions are mathematically equivalent.

Apart[ans2 /. {b1 -> x1, b2 -> x2}]

Gives

(r1 x1)/(r1 + r2) + (r2 x2)/(r1 + r2)

But the behaviour just seems really counterintuitive. Note that if I use y1 and y2, I get the same behaviour as x1 and x2. But for any other letter of the alphabet I use so far (not tried all), I get the behaviour of b1 and b2.

Hopefully I am missing something obvious, but I cannot understand why the letter of the alphabet given to a variable should alter Mathematicas behaviour.

xzczd
  • 65,995
  • 9
  • 163
  • 468
flyingmind
  • 489
  • 2
  • 10
  • 3
    This is not a bug. It is known and came up before. If you want to get same output, specify the second argument var in the call. See help. So to get same output do Apart[(x1/r2 + x2/r1)/(1/r1 + 1/r2), x1] and Apart[(b1/r2 + b2/r1)/(1/r1 + 1/r2), b2] otherwise Mathematica picks the main var for you using some internal ordering. May be lexicographic letter ordering. From help on var it says treats all variables other than var as constants. – Nasser Mar 03 '22 at 17:33
  • 1
    Since Mathematica is fundamentally a pattern matching system, it needs a means of trying to normalize patterns. To this end, it puts the elements of input expressions into canonical order (for description of canonical order see documentation (Details) for Sort). Consequently, the canonical order of the variables used can affect the performance of internal algorithms and the subsequent form of output. – Bob Hanlon Mar 03 '22 at 18:13
  • Thanks all, I will have a look into this prior to accepting an answer. I kind of thought of a variable name as a "blob" as Wolfram has described it. So "x1" is the same kind of "blob" as "b1", i.e. it is a completely arbitrary variable name not related to a logical function, unless otherwise specified. Have I missed something fundamental. Also, "x" and "y" seem special compared to other letters. – flyingmind Mar 03 '22 at 21:34
  • Nothing special about x vs. a. As for that second argument, partial fraction decompositions are at heart a univariate operation. If you don't pick which is the variable, Mathematica will pick one for you based on which it first encounters in putting the rational function into an internal form. This might or might not correspond to the first symbol you see. – Daniel Lichtblau Mar 03 '22 at 23:17
  • 1
    You can also see this lexicographically-dependent behavior with Simplify. Quoting from the documentation (Simplify->Possible Issues): "Results of simplification may depend on the names of symbols: Simplify[(1 - a^2)/b^2, a^2 + b^2 == 1]1; Simplify[(1 - c^2)/b^2, c^2 + b^2 == 1](1 - c^2)/b^2" In the latter case, providing the variable names in reverse lexical order prevents Simplify from finding a solution, and it thus returns what you entered. You get the same behavior with Simplify[(1 - b^2)/a^2, b^2 + a^2 == 1]. I.e., it doesn't appear to be about... – theorist Mar 04 '22 at 09:23
  • 2
    ......the specific variable names per se, but rather about the order in which they are used relative to their lexical ordering. [If this is correct, it seems the documentation shouldn't be saying that 'results may depend on the names of the symbols', and should instead be saying that 'results may depend on the order in which the symbols are used relative to their lexical ordering'.] – theorist Mar 04 '22 at 09:25
  • @thorist The vague wording makes that documentation more future-proof. There will always be naming dependencies but they need not be tied to a specific feature such as lexicographic ordering. – Daniel Lichtblau Mar 04 '22 at 14:47
  • Rule of thumb: If a function takes, optionally, a variable as an argument, specify the variable of interest. That way there are fewer surprises as compared to the case where the function picks the variable for you. – Daniel Lichtblau Apr 14 '22 at 03:32

0 Answers0