0
$\begingroup$

Struggling with something basic. Suppose when defining f[x_] the outcome depends on Sign[x]. When calling this function, how do I tell Mathematica the sign of the argument? My attempt:

f[x_]=x Sign[x];
Assuming[a>0, f[a]]

The output I get is

a Sign[a]

Obviously, I want to get simply a because I am trying to tell Mathematica a is positive

Additional details:

Thanks for the answers below. What I am really trying to do is as follows:

p = x /. Solve[(a - x)/y^2 == 1, x];
q = ((3 a - 2 p)/(4 y^2));
f[x_] = x Sign[x] ;
Assuming[q > 0, Simplify[f[q]]]

If I delete the first line in which p is defined via Solve, everything works fine. If, however, the first line is present, the output is:

{((a + 2 y^2) Sign[a + 2 y^2])/(4 y^2 Sign[y]^2)}

So, the Sign operator is still there.

What's wrong?

$\endgroup$
  • 2
    $\begingroup$ From the documentation for Assuming: "Assuming[assum, expr] evaluates expr with assum appended to $Assumptions, so that assum is included in the default assumptions used by functions such as Refine, Simplify, and Integrate." That is, the assumptions are only used by functions that take the option Assumptions. Your function f does not, so the assumption has no affect. Simplify[f[a], a > 0] $\endgroup$ – Bob Hanlon Jul 5 at 4:08
  • $\begingroup$ Thanks! I'd be grateful if you checked the update in the initial post $\endgroup$ – Dmitry Makarov Jul 5 at 10:46
  • $\begingroup$ Try using FullSimplify instead of Simplify. FullSimplify works a little harder to reduce things as much as possible. $\endgroup$ – Sjoerd Smit Jul 5 at 11:14
  • $\begingroup$ Sjoerd, this does not help - FullSimplify simply replaces Sign with Abs operator. $\endgroup$ – Dmitry Makarov Jul 5 at 11:17
  • $\begingroup$ Using $Assumptions={q>0}; right at the beginning does the job. Then instead of Assuming[q > 0, Simplify[f[q]]] use Simplify[f[q]]. $\endgroup$ – TheTwistedSector Aug 5 at 21:45
1
$\begingroup$
$Version

(* "12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)" *)

Clear["Global`*"]

p = x /. Solve[(a - x)/y^2 == 1, x];

q = ((3 a - 2 p)/(4 y^2));

f[x_] = x Sign[x];

Assuming[q > 0, FullSimplify[f[q]]]

(* {Abs[a + 2 y^2]/(4 y^2)} *)

Note the presence of List brackets that originate in your definition of p

Clear["Global`*"]

Use Part to remove the List brackets in the definition of p

p = x /. Solve[(a - x)/y^2 == 1, x][[1]];

q = ((3 a - 2 p)/(4 y^2));

f[x_] = x Sign[x];

Assuming[q > 0, Simplify[f[q]]]

(* 1/4 (2 + a/y^2) *)

EDIT: For the more complicated expression in your comment,

p = x /. Solve[(a - x)/(c y^2) + (b - x)/(d y^2) == 1, x][[1]];

q = ((3 a - 2 p)/(4 y^2))

(* (3 a - (2 (b c + a d - c d y^2))/(c + d))/(4 y^2) *)

f[x_] = x Sign[x];

If all the variables are real,

sol = Assuming[
  q > 0, (f[q] // ComplexExpand[#, TargetFunctions -> {Re, Im}] &)]

(* Sqrt[(3 a - (2 (b c + a d - c d y^2))/(c + d))^2]/(4 y^2) *)

This avoids Abs but is not the simplest form. Since the numerator is Sqrt[q^2] and q > 0

sol /. Sqrt[(z_)^2] :> z // Simplify

(* (3 a c - 2 b c + a d + 2 c d y^2)/(4 (c + d) y^2) *)
$\endgroup$
  • $\begingroup$ Hi Bob,thanks your solution works, but if I try to add another term to the first equation, so that the equation being solved is (a - x)/(c y^2) + (b - x)/(d y^2) == 1, and everything else is the same - including your new thing [[1]], then again sign operator appears in the outcome! $\endgroup$ – Dmitry Makarov Jul 6 at 13:24
  • $\begingroup$ Following up on my previous comment: if parameters c and d are removed from the denominators, there is no "Sign" in the answer. With c and d however, Sign pops up again $\endgroup$ – Dmitry Makarov Jul 6 at 13:31
  • $\begingroup$ @DmitryMakarov - If all the variables are real, Assuming[q > 0, f[q] // ComplexExpand[#, TargetFunctions -> {Re, Im}] &]. $\endgroup$ – Bob Hanlon Jul 6 at 13:47
  • $\begingroup$ Bob, this works, thanks! If I may ask, what was the obstacle that FullSimplify cannot overcome, but CompexExpand can? Is ComplexExpand the universally better approach to the broad question of Evaluating functions with a positive argument? Or if I keep increasing the complexity of the equation being solved, ComplexExpand will at some point also stop working, and then you'll recommend me another solution? If possible, I'd like as a takeaway to learn the most universal approach to this question. Thanks again! $\endgroup$ – Dmitry Makarov Jul 6 at 14:04
  • $\begingroup$ There is no universal approach since you are trying to avoid a particular form (Abs[ ]) that Mathematica finds perfectly acceptable. As general rules: include all available info about constraints in assumptions; and, if you want to specify particular forms ({RE, Im}, {Abs, Arg}, or Conjugate), you will probably need ComplexExpand. When using ComplexExpand either all variables must be real or you must explicitly identify which are complex. Some experimentation will often be required. See the See Also section of the documentation for Simplify for functions that may be useful. $\endgroup$ – Bob Hanlon Jul 6 at 14:31
0
$\begingroup$
f[x_]=x Sign[x];
Assuming[a>0,FunctionExpand[f[a]]]

returns a

Assuming[a>0,Simplify[f[a]]]

returns a

$\endgroup$
  • $\begingroup$ Thanks! I'd be grateful if you checked the update in the initial post $\endgroup$ – Dmitry Makarov Jul 5 at 10:46

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.