9
$\begingroup$

Something like x == (a|b|c) but that doesn't work.

$\endgroup$
1
  • 2
    $\begingroup$ In certain cases MemberQ[{a,b,c}, x] could be used but it does pattern matching/SameQ rather than EqualQ. Depends of your use case. $\endgroup$
    – Kuba
    May 26, 2021 at 4:39

2 Answers 2

9
$\begingroup$
AnyTrue[{a, b, c}, EqualTo[x]]

or

Or @@ Thread[x == {a, b, c}]
$\endgroup$
8
  • $\begingroup$ Those give ways to do it but they're longer! I was hoping for a concise syntax for "x equals any of these discrete values" $\endgroup$
    – JLo
    May 26, 2021 at 0:06
  • $\begingroup$ @JLo Element[x, {a, b, c}]? $\endgroup$
    – cvgmt
    May 26, 2021 at 0:10
  • $\begingroup$ Works: Simplify[x == 5, x == 1 || x == 2 || x == 3] Doesn't work: Simplify[x == 5, x \[Element] {1, 2, 3}] It knows the 1st one is false but not the 2nd. $\endgroup$
    – JLo
    May 26, 2021 at 0:18
  • $\begingroup$ @JLo {1,2,3} is not a valid domain for Element, so of course not... $\endgroup$
    – ciao
    May 26, 2021 at 0:25
  • $\begingroup$ Isn't that what you suggested? I'm confused. That's the same thing as Element[x, {a, b, c}] with values substituted. $\endgroup$
    – JLo
    May 26, 2021 at 0:27
7
$\begingroup$

You can make it work if you really want to! ;) (Though this would have to be re-evaluated every session—unless you put it in an init file or something.)

Unprotect[Or]

Or /: (x_ == HoldPattern[Or[ys__]]) := Thread[Unevaluated[x == Or[ys]], Or]

Protect[Or]

(* In:  *) x == (a || b || c)
(* Out: *) x == a || x == b || x == c

(Note that this uses Or (||), not Alternatives (|).)

Other possibilities (with a fresh Or):

(* Just use thread directly: *)

Thread[x == (a || b || c), Or]

(* Map the equality function: *)

x == # & /@ (a || b || c)

(* or equivalently: *)

EqualTo[x] /@ (a || b || c)

(* Define a new function: *)

OrEqual[x_][ys__] := Thread[x == Or[ys], Or]

OrEqual[x][a, b, c]

(* Use MemberQ, but this is pattern-based: *)

MemberQ[{a, b, c}, x]

MemberQ[x][{a, b, c}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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