Applying rules to functions with non numeric arguments

I am trying to do the following (it's a simplified version):

In[1]:= rulepositive = { f[a_?Positive]:> f[a] };
In[2]:= rulenegative = { f[a_?Negative]:> 0 };

In[3]:= $Assumptions = Elements[w,Positive]; In[4]:= f[w]/.rulepositive In[5]:= f[w]/.rulenegative  where I expect Out[4]:= f[w] Out[5]:= 0  But it doesn't work. In words I want to apply a set of mapping rules in functions with non numeric arguments, which nevertheless have definite nature (e.g. Positive/Negative). How could I do it? • Why you can't simply use Piecewise? Commented Mar 22, 2020 at 10:00 • f[a_]:=Piecewise[{{Unevaluated[f[a]],a > 0},{0, a < 0}}] Commented Mar 22, 2020 at 13:48 2 Answers I don't follow what you expect, but you can use Simplify to respect $Assumptions:

rulepositive = {f[a_] /; Simplify[Positive@a] :> 1};   (* modified from your example *)
rulenegative = {f[a_] /; Simplify[Negative@a] :> 0};

$Assumptions = {w > 0}; f[w] /. rulepositive f[w] /. rulenegative   1 f[w]  $Assumptions = {w < 0};

f[w] /. rulepositive
f[w] /. rulenegative

 f[w]

0


Reference:

Positive is not a set.

    ?? Positive
(*Positive[x] gives True if x is a positive number.*)


Compare to

    ?? Integers
(*Integers represents the domain of integers, as in x∈Integers. *)


Besides, your expectations seem to be off. Why would you expect '0' out of f[w]/.rulenegative? Moreover, was zero (which is neither positive nor negative) considered in your logic?

In any case, the code below might help you. The basic idea is to assign the information to w instead. See TagSetDelayed help "f/:lhs:=rhs assigns rhs to be the delayed value of lhs, and associates the assignment with the symbol f."

   rulepositive = {f[a_?Positive] :> "yeah"};
rulenegative = {f[a_?Negative] :> "bummer"};
w /: Positive[w] = True;
w /: Negative[w] = False;
f[w] /. {{rulepositive}, {rulenegative}}
(*{{"yeah"}, {f[w]}}*)

• No, zero is not a case due to the nature of the problem. Also w shouldn't be fixed positive or negative
– hal
Commented Mar 20, 2020 at 11:55