10
$\begingroup$

To solve some PDE, I have to set NumericQ[n]=True on some symbol to get a solution, else Mathematica will not solve it.

When I tried ClearAll[n] later on, above command effect was not removed.

I need to clear it, so that later commands do not fail, since now doing n>0 will fail because I did NumericQ[n]=True before.

Doing ClearAll[n] have no effect. It still fails.

Here is a MWE

(*new kernel *)
NumericQ[n]=True;
n>0
   Greater::nord2: Comparison of n and 0 is invalid.

Ok, Mathematica does not like to compare n with zero. So I do

ClearAll[n];

And try again

n>0  (*still gives same error as above*)

I looked at OwnValues@n and UpValues@n and DownValues@n and SubValues@n and they all give {}

One way I found is to do NumericQ[n]=False; and this works. But I do not understand why ClearAll[n] does not work.

Thanks to comment below by Anjan Kumar Remove[n] also removes the effect.

My question is, why ClearAll[n] did not work?

Mathematica 11.3 on windows.

$\endgroup$
  • 1
    $\begingroup$ Perhaps Remove[n]? $\endgroup$ – Anjan Kumar Mar 15 '18 at 6:52
  • $\begingroup$ @AnjanKumar thanks, Remove works. But do you know why ClearAll does not work? May be I'll change the question to why clearAll does not work. $\endgroup$ – Nasser Mar 15 '18 at 6:54
  • 3
    $\begingroup$ Related: mathematica.stackexchange.com/a/128445/1871 $\endgroup$ – xzczd Mar 15 '18 at 6:58
8
$\begingroup$

You can undo this using

NumericQ[n] =.

(Note that there is no space between the = and the .)

ClearAll does not work because the definition is not associated with the symbol n. It seems that in this case it is not even associated with the symbol NumericQ. I believe that assignment operators are specially overloaded for NumericQ, and trying to make an assignment will cause the system to store away this information somewhere else.

Remove works because it effectively destroys the symbol n. So wherever this information about n was stored, n will be removed from there.


Here's an example of how I imagine NumericQ might work. This may of course be entirely wrong, but it illustrates one possible reason for why ClearAll wouldn't remove this setting.

Unprotect[spec];
ClearAll[spec, registry];
registry = <||>;
spec /: (Set | SetDelayed)[spec[sym_Symbol], val : True | False] := (AssociateTo[registry, sym -> val]; val)
spec /: Unset[spec[sym_Symbol]] := (KeyDropFrom[registry, sym];)
spec[sym_Symbol] := Lookup[registry, sym, False]
Protect[spec];

Demonstration:

spec[x] = True
(* True *)

ClearAll[x]

spec[x]
(* True *)

registry
(* <|x -> True|> *)
$\endgroup$
  • $\begingroup$ @Kuba How about now? $\endgroup$ – Szabolcs Mar 15 '18 at 10:34
  • $\begingroup$ Now it clearly is :) $\endgroup$ – Kuba Mar 15 '18 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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