1
$\begingroup$

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?

$\endgroup$
0

1 Answer 1

3
$\begingroup$
$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 *)
$\endgroup$
5
  • $\begingroup$ For some reason this does not work for variables with lower index $\endgroup$
    – Anixx
    May 1 at 6:14
  • $\begingroup$ I do not understand your comment. What do you mean by "variables with lower index"? $\endgroup$
    – Bob Hanlon
    May 1 at 6:32
  • $\begingroup$ Such as $a_1$, $b_2$. $\endgroup$
    – Anixx
    May 1 at 6:33
  • $\begingroup$ As a workaround Format[a1] = Subscript[a, 1]; a1 /: NumericQ[a1] = True; f[a1] $\endgroup$
    – Bob Hanlon
    May 1 at 6:53
  • $\begingroup$ 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". $\endgroup$
    – Roman
    May 1 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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