1

I want to get a general formula for an expression, so instead of numbers like 5 or Pi I want to insert variables into an expression. But the functions involved check NumericQ on the arguments and do not evaluate for a symbolic variable. Can I force Mathematica to return NumericQ as True for a set of variables?

I tried:

  • NumericQ[a]=True - no error but does not work

  • The same but with Unprotect[NumericQ] - no effect

  • $Assumptions=a \[Element] Reals - no effect

What else should I try?

Anixx
  • 3,585
  • 1
  • 20
  • 32

1 Answers1

3
$Version

(* "13.2.1 for Mac OS X ARM (64-bit) (January 27, 2023)" *)

Clear["Global`*"]

Use TagSet or TagSetDelayed

a /: NumericQ[a] = True;

Then

f[x_?NumericQ] := x^2

f[a]

(* a^2 *)

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
  • For some reason this does not work for variables with lower index – Anixx May 01 '23 at 06:14
  • I do not understand your comment. What do you mean by "variables with lower index"? – Bob Hanlon May 01 '23 at 06:32
  • Such as $a_1$, $b_2$. – Anixx May 01 '23 at 06:33
  • As a workaround Format[a1] = Subscript[a, 1]; a1 /: NumericQ[a1] = True; f[a1] – Bob Hanlon May 01 '23 at 06:53
  • See here: 3. Avoid using subscripted symbols in your code. While it can be done, it causes a lot of confusion and is harder to use than just sym[j] or whatever your symbol might be. The reason is that subscripted symbols are not plain symbols, so you can’t assign values (strictly speaking, DownValues) to them directly. See also general discussion about "indexed variables". – Roman May 01 '23 at 08:42