5
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ Related question $\endgroup$ Commented Mar 23, 2016 at 11:34

3 Answers 3

8
$\begingroup$
And @@ Thread[{a, b} > {c, d}] (* a > c && b > d *) 
$\endgroup$
1
  • $\begingroup$ Great! And copying that for the vars>0 example: And @@ Thread[vars > Flatten[ConstantArray[0, {Length[vars], 1}]]] works. $\endgroup$ Commented Mar 22, 2016 at 20:11
5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ by the way, I'm discovering that 1>x>2 gives False (with x symbolic) $\endgroup$ Commented Mar 22, 2016 at 21:25
  • $\begingroup$ I have tried this operator on a lot of special cases : it seems to be robust. If someone find a problem, I'm interested. Thanks. $\endgroup$ Commented Mar 22, 2016 at 21:47
  • $\begingroup$ I have not studied the precedence of the operator. Don't hesitate to use parenthesis. $\endgroup$ Commented Mar 22, 2016 at 21:50
3
$\begingroup$

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.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.