15

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?

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
comco
  • 253
  • 1
  • 4

4 Answers4

16

Everything depends on what you try to do with this. First you could use Subsuperscript and get rid of one level which is introduced by your Power

Subscript[e,3]^2//FullForm
(* Power[Subscript[e,3],2] *)

Subsuperscript[e,3,2]//FullForm
(* Subsuperscript[e,3,2] *)

Both forms look equally in the front-end, but now you can use TagSet to transform all e with integer-subscript to a different form which displays as it would still be the normal e:

e/:Subsuperscript[e,i_?IntegerQ,2]:=eNumeric[i,2];
e/:Subsuperscript[e,i_,2]:=eNonNumeric[i,2];
SetAttributes[eNumeric,{NumericFunction}];
(Format[#[i_,2]]:=TraditionalForm[Subsuperscript["e",i,2]])&/@{eNumeric,eNonNumeric};

Mathematica graphics

halirutan
  • 112,764
  • 7
  • 263
  • 474
12

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.

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420
7

You can use Notation package to treat anything like function and then set whatever attributes to this function:

enter image description here

The only problem is that Notation don't support test patterns, so to make an expression numeric with only integer indexes:

enter image description here

swish
  • 7,881
  • 26
  • 48
  • Please try to include the code in textual form for two reasons: 1) Other users may want to copy/paste it and 2) Your answer will be better indexed for web searches. Thnx! – Dr. belisarius Dec 27 '12 at 13:25
  • 1
    @belisarius I didn't make it textual because those boxes inside Notation are very messy and unreadable. – swish Dec 27 '12 at 13:46
  • Yes, I know. I'm not suggesting to replace the images. Anyway, your choice :) – Dr. belisarius Dec 27 '12 at 13:52
1

Maybe you can make e NumericQ and also give Subscript the NumericFunction attribute:

NumericQ[e] = True;
SetAttributes[Subscript, NumericFunction]

Then:

NumericQ /@ {Subscript[e, 2]^2, Sin[Subscript[e, 1]^3 + Subscript[e, 2]], Log[Subscript[e, 1]]

{True, True, True}

Carl Woll
  • 130,679
  • 6
  • 243
  • 355