0
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Why you can't simply use Piecewise? $\endgroup$ – vi pa Mar 22 '20 at 10:00
  • $\begingroup$ f[a_]:=Piecewise[{{Unevaluated[f[a]],a > 0},{0, a < 0}}] $\endgroup$ – vi pa Mar 22 '20 at 13:48
0
$\begingroup$

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

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:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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