With NumericQ[symbol] = True, I can declare that a symbol is numeric. I want the expressions matching: $$e_{\text{i$\_$}?\text{IntegerQ}}^2$$ to be treated as numerical expressions too.
e /: NumericQ[Subscript[e, i_?IntegerQ]^2] = True;
doesn't work. Also, I can't use NumericFunction because it's too restrictive. Is there something like NumericPattern?
Answer
Here is another way: you can fool the depth-1 tag rule for UpValues with a few temporary symbols. Here is an example:
ClearAll[e];
e /: Subscript[e, i_?IntegerQ] := e /: Subscript[e, i] =
Module[{el},
el /: el^p_ := el /: el^p =
Module[{elp},
elp /: NumericQ[elp] = True;
Format[elp] := TraditionalForm[Subscript["e", i]^p];
elp
];
el /: NumericQ[el] = True;
Format[el] := TraditionalForm[Subscript["e", i]];
el]
What this does is to substitute Subscript[e, i_?IntegerQ] by some symbol, which will print just as the original one, but will have some rules attached which will do what you need. Now,
NumericQ[Subscript[e,1]]
(* True *)
NumericQ[Subscript[e,1]^2]
(* True *)
The advantage of this method is that it is flexible. You are not tied to just powers of your subscript, it can be easily generalized to other functions.
Comments
Post a Comment