0

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?

hal
  • 783
  • 3
  • 14

2 Answers2

1

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:

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
0

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]}}*)
SolutionExists
  • 234
  • 1
  • 6
  • No, zero is not a case due to the nature of the problem. Also w shouldn't be fixed positive or negative – hal Mar 20 '20 at 11:55