1
$\begingroup$

If I clear the commutativity of *:

ClearAttributes[Times, Orderless]

I get that ab and ba are different now. Why is it that when I order Mathematica to compute:

(a + b) c // Expand

It yields ca + cb instead of ac + bc? How can I make it work so that it yields the latter? It seems to me that this happens because the way Expand works.

$\endgroup$
9
  • 1
    $\begingroup$ I think it is just the final display is based on letters chronological ordering which Front end uses for display. If you do ClearAttributes[Times, Orderless]; (a + b) c // Expand // TraditionalForm then you get same result as before which is ac + bc but no time to investigate more. $\endgroup$
    – Nasser
    Commented May 23 at 1:24
  • 4
    $\begingroup$ As stated in the documentation, "Orderless is an attribute that can be assigned to a symbol f to indicate that the elements Subscript[e, i] in expressions of the form f[Subscript[e, 1], Subscript[e, 2], [Ellipsis]] should automatically be sorted into canonical order." Consequently, if you remove the Orderless attribute, the sorting does not occur. $\endgroup$
    – Bob Hanlon
    Commented May 23 at 1:31
  • 1
    $\begingroup$ Expand might be using internal code that bypasses Times, hence fails to respect the changed attribute. $\endgroup$ Commented May 23 at 10:58
  • 1
    $\begingroup$ (a + b) c // Distribute? $\endgroup$
    – Goofy
    Commented May 23 at 14:46
  • 1
    $\begingroup$ Actually, I sometimes get a c + b c: i.sstatic.net/4aJN4RfL.png - Is it a bug? $\endgroup$
    – Goofy
    Commented May 23 at 14:55

1 Answer 1

3
$\begingroup$

I recommend using ** (NonCommutativeMultiply), instead of modifying the attribute of Times.

(a + b) ** c

(* (a + b) ** c *)

Distribute[%]

(* a ** c + b ** c *)

c ** (a + b) 

(* c ** (a + b) *)

Distribute[%]

(* c ** a + c ** b *)
$\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.