5
$\begingroup$

I spotted this under \[Element] in the Wolfram documentation: "x ∈ y is by default interpreted as Element[x, y]."

By default? So can it be changed? I'd like to be able to use ∈ and ∉ (\[NotElement]) as shorthand to check list membership. Even better, can I have ∈ mean MemberQ or Element depending on whether it's used on a List expression? Ideally I'd like it by default in every notebook.

$\endgroup$
3
  • 6
    $\begingroup$ You might find more success in defining a similarly looking undefined operator to do such a thing. I’m not sure I would advise making the change to a defined operator as you desire, but it is possible. $\endgroup$ Dec 21, 2021 at 13:18
  • 6
    $\begingroup$ I suggest using one the similar-looking Unicode characters, such as ⋹, ∊, ⋳, ⋿, ⋲ ... $\endgroup$
    – Domen
    Dec 21, 2021 at 13:46
  • 3
    $\begingroup$ A lot of the calculus and region functionality rest on the already built-in evaluation rules for Element[], so don't be surprised if they break because of what you want. $\endgroup$ Dec 21, 2021 at 14:54

4 Answers 4

4
$\begingroup$

By default? So can it be changed?

No, it cannot be changed. I believe the description is incorrect.

Some of the existing answers change the meaning of the Element function, not the interpretation of the \[Element] operator. That is not the same thing. Also, it will almost certainly break things very badly.

Michael's answer is the only one which actually changes the interpretation of \[Element], but only within notebooks, i.e. only in StandardForm. This is all that the notation package can do. It won't change the interpretation in InputForm, i.e. the plain text interpretation that applies when using Get. I believe that is hard-wired into the parser.

The only reasonable answer here is that no, it is not possible to change the interpretation of \[Element]. If you try, you will likely seriously break things. Don't do this. If you really want an operator for MemberQ, use one of the operators without built-in meaning.

$\endgroup$
6
$\begingroup$

Although you can do it, I would advice against it because you may upset something unexpected.

Anyway to do this you may first unprotect the command, clear possible definitions and redefine it. Finally you protect it again.

If you want this as default, you put it in the "init.m" file (look it up in the help)

Here is the code:

Unprotect[Element, NotElement];
ClearAll[Element, NotElement];
Element[x_?AtomQ, y_] := MemberQ[y, x];
NotElement[x_, y_] := ! MemberQ[y, x];
Element[x_List, y_] := AllTrue[x, MemberQ[y, #] &];
NotElement[x_List, y_] := NoneTrue[x, MemberQ[y, #] &];
Protect[Element, NotElement];

To test the MemberQ functionality:

1 ∈ {1, 2}
1 ∉ {1, 2}
True
False

And the MemberQ functionality:

{1, 2} ∈ {1, 2, 3}
{4, 5} ∉ {1, 2, 3}
True
True
$\endgroup$
5
$\begingroup$

You can use the Notation package to make the input x ∈ y be interpreted as MemberQ[x, y]. Besides being a literal interpretation of the question in the OP, this way does not override Element or MemberQ, only input syntax and optionally output-formatting.

Load the package with Get or Needs:

Needs["Notation`"]

Pick the input form from the Notation palette and enter:

One can use the input-output form to have MemberQ displayed as \[Element] if desired; however, you cannot stop MemberQ from evaluating to True or False except by holding it:

Note ToExpression["x\[Element]{1,2,3}", StandardForm] is converted to MemberQ[{1, 2, 3}, x], but I doubt that will cause trouble for built-in functions.

$\endgroup$
1
$\begingroup$

I give you a version that is only used when MemberQ returns True or False.

Element[form_,lst_List]:=With[{result=MemberQ[lst,form]},
  result/;Or[result,Not@result]
];

Then

Element[3,{1,2,3,4,5}]
(* True *)

Using Block you can do things that are otherwise not possible such as this:

Unprotect[SetPrecision];
$ModifySetPrecision=True;

SetPrecision[x_Real,p_Real?Positive]/;$ModifySetPrecision:=
  Block[{$ModifySetPrecision=False},SetPrecision[x,p+15]];

SetPrecision[x_Real,p_Integer?Positive]/;$ModifySetPrecision:=
  Block[{$ModifySetPrecision=False},SetPrecision[x,p+15]];

Then

x=SetPrecision[2.2`17,20];
Precision[x]

(* 35. *)

My changes above will certainly slow down the affected functions, and it is conceivable this code will not work in a future version of Wolfram Language. It is conceivable you have another use for $ModifySetPrecision. Other than that, what unexpected changes could it have?

$\endgroup$

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.