...Set...
functions are overridden for NumericQ
. That's why your assignment g /: NumericQ[g[_]] = True
does not work as expected.
I guess that the fact that above assignment doesn't complain with an error message, and just does nothing, might be considered a bug.
You could try to manually add necessary UpValues
to your g
:
g // ClearAll
g // UpValues = {HoldPattern@NumericQ@g@_ :> True};
This will work when NumericQ
is called directly on g[...]
.
NumericQ@g (* False *)
NumericQ@g[1] (* True *)
NumericQ@g[1, 2] (* False *)
But it will not work when g[...]
is deeper in some other expression:
NumericQ@Sin@g[1] (* False *)
For NumericQ
to recognize g[...]
with numeric arguments arbitrarily deep inside other numeric functions you could set NumericFunction attribute (as suggested in comment by J. M.), but this does not give you precise control on pattern for which g[...]
will be considered numeric:
g // ClearAll
g // Attributes = NumericFunction;
NumericQ@g (* False *)
NumericQ@g[a] (* False *)
NumericQ@g[1] (* True *)
NumericQ@g[1, 2] (* True *)
NumericQ@Sin@g[1, Pi] (* True *)
Overridden assignments involving NumericQ
allow defining custom symbols representing numerical constants like E
, or Pi
, in a way that Mathematica will recognize them as numerical. E.g:
NumericQ@Sin@myNumConst (* False *)
NumericQ@myNumConst = True;
NumericQ@Sin@myNumConst (* True *)
NumericQ@myNumConst = False;
NumericQ@Sin@myNumConst (* False *)
You can also force Mathematica to forget that a built-in constant is numerical:
NumericQ@Sin@E (* True *)
NumericQ@E = False;
NumericQ@Sin@E (* False *)
Mathematica seems to store some internal collection of such numerical symbols since results of such assignments are not visible neither in Language`ExtendedDefinition@NumericQ
nor in Language`ExtendedDefinition@myNumConst
. Unfortunately I don't know how to access it.
SetAttributes[g, NumericFunction]
instead. $\endgroup$NumericQ[g]
andNumericQ[g[1]]
evaluate toTrue
and does not offer the flexibility of leavingNumericQ[q]
asFalse
, should one ever want that... I don't but worth mentioning for sake of generality. :-) Thanks again! $\endgroup$