5

How do I use relational operators on vectors?

{1, 2, 3} > {0, 1, 2}
(*outputs {1, 2, 3} > {0, 1, 2}*)

More generally, I want {a,b}>{c,d} to be equivalent to a>c && b>d. Or, I would want to say vars>0 where vars is a vector of my variables and that one constraint forces them all to be strictly positive. How might I achieve that?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Shane
  • 1,003
  • 1
  • 6
  • 18

3 Answers3

8
And @@ Thread[{a, b} > {c, d}]

(* a > c && b > d *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Great! And copying that for the vars>0 example: And @@ Thread[vars > Flatten[ConstantArray[0, {Length[vars], 1}]]] works. – Shane Mar 22 '16 at 20:11
5

Here is a new operator ( ,alias \[NestedGreaterGreater] ) that is a generalisation of the built-in > :

NestedGreaterGreater[x___] := And @@ Thread[Greater[x]]

This permits interesting operations :

  • {a, b} ⪢ {c, d}

(a > c) && (b > d)

  • {a, b} ⪢ {c, d} ⪢ {e, f}

(a > c > e) && (b > d > f)

  • {a, b} ⪢ c ⪢ {d, e}

(a > c > d) && (b > c > e)

For clearity some parenthesis have been added in the previous results.

andre314
  • 18,474
  • 1
  • 36
  • 69
3

You can also use the BoolEval package. You'd use it like this:

Needs["BoolEval`"]
FreeQ[BoolEval[{1, 2, 3} > {0, 1, 2}], 0]
(* True *)

Or

Times @@ BoolEval[{1, 2, 3} > {0, 1, 2}] == 1

The point being that BoolEval returns a 1 or a 0 for each comparison, representing true or false. If there are no zeroes that means that all comparisons were true.

How do I use relational operators on vectors?

BoolEval is a general answer to this question.

If you want a more compact syntax you could implement BoolEvalAnd, BoolEvalOr etc. along the lines of the solution above.

C. E.
  • 70,533
  • 6
  • 140
  • 264