I am studying Mathematica myself and doing some exercises for improvement.

Question: Try to guess the internal forms of b/c and c/b. Verify your answer using FullForm.

Here is what I got by using FullForm:


As you can see from the full form of b/c and c/b, the order of two terms in function Times is different. I am wondering why does this happen? What is the purpose of this? For the full form of c/b, why not Times[c,Power[b,-1]]? Just curious, however this doesn't seem to be important.


1 Answer 1


Times has the Attribute Orderless:

{Flat, Listable, NumericFunction, OneIdentity, Orderless, Protected}

As the documentation states:

Orderless is an attribute that can be assigned to a symbol f to indicate that the elements ei in expressions of the form f[e1, e2, ...] should automatically be sorted into canonical order. This property is accounted for in pattern matching.

Observe it acting on an arbitrary user head:

Attributes[head] = {Orderless};
head[b, c]
head[c, b]
head[b, c]

head[b, c]

Among other things this helps to put expressions into a canonical form, allowing e.g. head[b, c] == head[c, b] to return True, as it should be for a commutative operator.

Recommended reading:

May also be of interest:

  • $\begingroup$ Thanks. I still wonder why the form of b/c is sorted (canonical order) but c/b is not? $\endgroup$
    – emnha
    Sep 29, 2017 at 8:01
  • 1
    $\begingroup$ @anhnha It is sorted according to the canonical ordering function of Mathematica, e.g. Sort[{Power[b, -1], a, c}] outputs {a, 1/b, c}. Sadly that ordering remains poorly documented. $\endgroup$
    – Mr.Wizard
    Sep 29, 2017 at 8:07
  • 1
    $\begingroup$ Well, in this case, it's simple: What comes first in the alphabet? $\endgroup$
    – halirutan
    Sep 29, 2017 at 8:08
  • 3
    $\begingroup$ @anhnha Only for simple symbols. You can look up the documentation of sort and find in the details section: "Sort orders symbols by their names, and in the event of a tie, by their contexts." and "Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner. " and "Sort treats powers and products specially, ordering them to correspond to terms in a polynomial." As pointed out by Mr.Wizard, things get more involved as soon as you have complex expressions. $\endgroup$
    – halirutan
    Sep 29, 2017 at 8:28
  • 1
    $\begingroup$ @halirutan You and I have different definitions for nice. ;-p $\endgroup$
    – Mr.Wizard
    Sep 29, 2017 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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