Something like x == (a|b|c) but that doesn't work.
2 Answers
AnyTrue[{a, b, c}, EqualTo[x]]
or
Or @@ Thread[x == {a, b, c}]
-
$\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$– JLoMay 26, 2021 at 0:06
-
-
$\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$– JLoMay 26, 2021 at 0:18 -
$\begingroup$ @JLo
{1,2,3}
is not a valid domain forElement
, so of course not... $\endgroup$– ciaoMay 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$– JLoMay 26, 2021 at 0:27
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}]
MemberQ[{a,b,c}, x]
could be used but it does pattern matching/SameQ rather than EqualQ. Depends of your use case. $\endgroup$