2
$\begingroup$
vars = Table[Subscript[a, j], {j, 5}]

We can do the total

Total[vars]

and produce

Times@@vars

What's the most efficient way to get this

{Subscript[a, 1]<=Subscript[a, 2]<=Subscript[a, 3]<=Subscript[a, 4]<=Subscript[a, 5]}

I can't seem to find a proper way to map this.

$\endgroup$
1
  • 6
    $\begingroup$ It's just LessEqual @@ vars :-) $\endgroup$
    – Mr.Wizard
    Commented Jul 24, 2015 at 11:40

1 Answer 1

5
$\begingroup$

Rather than considering this question "too simple" and closing it I tried to think of a way to make it or its answer of wider interest. It seems to me that for this question to have been asked it must not be clear that there is equivalence between <= and LessEqual, and/or that LessEqual is not a binary function but can receive many arguments.

One can see with FullForm that operators such as <= are represented by a common Head such as LessEqual. Some other examples:

expr = {
  a < b < c,
  a <= b <= c,
  a > b > c,
  a >= b >= c,
  a == b == c,
  a != b != c,
  a && b && c,
  a | b | c,
  a.b.c
};

FullForm /@ expr // Column
Less[a, b, c]
LessEqual[a, b, c]
Greater[a, b, c]
GreaterEqual[a, b, c]
Equal[a, b, c]
Unequal[a, b, c]
And[a, b, c]
Alternatives[a, b, c]
Dot[a, b, c]

Therefore these and other heads may be used to construct such expressions:

operators = {Less, LessEqual, Greater, GreaterEqual, Equal, Unequal, Dot, And, Or, Nand, 
   Nor, Xor, Alternatives, Equivalent, Proportional};

list = {a, b, c, d, e};

Table[
 op @@ list,
 {op, operators}
]
{a < b < c < d < e, a <= b <= c <= d <= e, a > b > c > d > e, a >= b >= c >= d >= e, 
 a == b == c == d == e, a != b != c != d != e, a.b.c.d.e, a && b && c && d && e, 
 a || b || c || d || e, a ⊼ b ⊼ c ⊼ d ⊼ e, 
 a ⊽ b ⊽ c ⊽ d ⊽ e, a ⊻ b ⊻ c ⊻ d ⊻ e, 
 a | b | c | d | e, a ⧦ b ⧦ c ⧦ d ⧦ e, 
 a ∝ b ∝ c ∝ d ∝ e}

Not included in the example are SameQ and UnsameQ only because those evaluate on symbolic arguments:

SameQ @@ list
UnsameQ @@ list
False
True

With holding to prevent that evaluation:

SameQ @@@ HoldForm @@ {list}
UnsameQ @@@ HoldForm @@ {list}
a === b === c === d === e
a =!= b =!= c =!= d =!= e

All of the operators shown above are of the kind that work with multiple arguments. Not all operators are like this. For other operators one may refer to the Operator Precedence Table. Note that something like Map parses differently:

HoldForm @ FullForm[a /@ b /@ c /@ d /@ e]
Map[a, Map[b, Map[c, Map[d, e]]]]

(I believe this is known as a right-associative operator.) One therefore cannot construct an equivalent expression with Apply:

 Map @@ {a, b, c, d, e}

Map::nonopt: Options expected (instead of e) beyond position 3 in Map[a,b,c,d,e]. An option must be a rule or a list of rules. >>

Map[a, b, c, d, e]

Recommended reading:

$\endgroup$
4
  • 1
    $\begingroup$ Another possibility: Inequality @@ Riffle[list, LessEqual]. This allows for slightly more flexibility, e.g. Inequality @@ Riffle[list, {LessEqual, Less}]. $\endgroup$ Commented Jul 24, 2015 at 12:27
  • $\begingroup$ @Guess Would you care to discuss Inequality in a separate answer? I had overlooked it and as I recall it is somewhat complex in its interaction with the other operators. $\endgroup$
    – Mr.Wizard
    Commented Jul 24, 2015 at 12:29
  • $\begingroup$ I was hoping you would :D, but I guess I can try writing a demo later… $\endgroup$ Commented Jul 24, 2015 at 12:31
  • $\begingroup$ @Guess OK, maybe if I have time. $\endgroup$
    – Mr.Wizard
    Commented Jul 24, 2015 at 12:32

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.