There is an attribute NumericFunction
in Mathematica that can do the following job:
Attributes[f] = {NumericFunction};
NumericQ[f[1,2,3,4]]
then returns true. However, the following return false
NumericQ[f[{1,2,3,4}]]; NumericQ[f[1,{2},3,{4}]]
which makes sense by Documentation on NumericFunction
. However, I want to define a function $f$ that returns True even in above cases, how can this be achieved?
Curiously, functions like HypergeometricPFQ
has Attributes NumericFunction
, but NumericQ[HypergeometricPFQ[{1, Sqrt[2]}, {Pi}, 1/3]]
still returns true despite having lists in its arguments, why?
My goal is to define an f
such that all expressions like f[1,{2},3,{4}]
, 1+5*f[1,{2},3,{4}]
, Sin[1+5*f[1,{2},3,{4}]]
returns true upon NumericQ
. How can this be done?
Thank you.
Update: After much thoughts, I think I found a solution:
SetAttributes[f, NumericFunction]; Unprotect[NumericQ];
$numericflag = 0;
NumericQ[expr_] /; FreeQ[expr, f] == False && $numericflag == 0 :=
Block[{bool}, $numericflag = 1;
bool = NumericQ[expr /. f[x__] :> f @@ Flatten[{x}]]; $numericflag =
0; Return[bool]]; Protect[NumericQ];
it works as expected: NumericQ[Pi + 58 + f[2, 3, {4, 4}]]
, NumericQ[Sin[f[2,{3,{0,0}}]]]
all return true.
f[l___]:=1
the result ofNumericQ@f[{1},2,3]
becomeTrue
$\endgroup$f
evaluates to1
first?NumericQ
doesn't have anyHold*
attributes. $\endgroup$