MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: And, Or, Intersection, Union - no Orderless attribute

  • To: mathgroup at
  • Subject: [mg24019] Re: [mg23994] And, Or, Intersection, Union - no Orderless attribute
  • From: Andrzej Kozlowski <andrzej at>
  • Date: Tue, 20 Jun 2000 03:07:34 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

First of all, one should not identify the Orderless attribute with
commutativity. Rather, the Orderless attribute is one of the ways in which
commutativity is implemented in Mathematica. There are however, other ways,
and there are cases when having the Orderless attribute would be wasteful or
have undesirable side-effects. The cases you mention belong to this

When a function has the Orderless attribute its arguments are sorted before
the function is applied. For various reasons this may be inconvenient, even
when the function is "commutative". The cases you mention belong to two
different groups, and the reasons are somwhat different.

First, the logical functions, And and Or. Let's see what the mathematica
book says about And:

"e1 && e2 && ...  is the logical AND function. It evaluates its arguments in
order, giving False immediately if any of them are False, and True if they
are all True."

Note carefully "evaluates its arguments in order...". A brief reflection
shows why this may be desirable. Suppose for example you want to check a
condition which can be represented in the form p && q, where p takes
potentially a long time to check while q can be verified quickly. Naturally,
you should enter this as q && p . If q turns out to be false Mathematica
won't bother working out p at all. This can be a great time saving. If And
had the Orderless attribute the arguments p and q would have to be sorted
according to the built-in sorting rules and you would loose control over the
order in which this evaluation is performed. Thus the Orderless attribute
would be a disadvantage in this case. In any case, it is not needed, since
commutativity arises merely from the way Truth tables for And and Or work,
and having the Orderless attribute would contribute nothing here. (By the
way, you should not conuse the sorting out output, which Mathematica does
when you apply LogicalExpand, with the sorting of the arguments.)

In the case of the other two functions, Union and Interestion the
explanation is similar. Again having the Orderless attribute would simply
add unnecessary "prior sorting" of arguments, to the working of these
functions. But in any case, these functions have to at some point sort the
elements of list that you get at the output and sorting the arguments first
would not, in general,  help in this task. Sorting the arguments of Union or
Intersection would simply add an additional computational task, which would
have no effect at all on the final result! Again, in this case the
commutativity comes as a by-product of the sorting of elements in the
output, and not through giving the function and the Orderless attribute.


Andrzej Kozlowski
Toyama International University

on 6/19/00 2:45 PM, Drago Ganic at drago.ganic at wrote:

> Hi Mathgroup !!
> Is there any reason that the symbols
> And, 
> Or, 
> Intersection, 
> Union 
> do not have the Orderless attribute (but do have the Flat attribute).
> These operations are commutative (as far as I know). We can see this fo And
> and Or if we use LogicalExpand
> In[1]:        b && c && a
> Out[1]:     b && c && a
> In[2]:        LogiclaExpand[%]
> Out[2]:     a && b && c
> Why, why ??
> Drago Ganic
> Croatia

  • Prev by Date: Findminimum computation time (ignore my previous posting)
  • Next by Date: Re: I wish Descending Order Result
  • Previous by thread: Re: And, Or, Intersection, Union - no Orderless attribute
  • Next by thread: formatting questions