4
$\begingroup$

I would like to compare variables t and u and print either Yes or No depending one which is larger.

My code is simply the following:

Assuming[t < u, If[t - u < 0, Print[Yes], Print[No]]]

Since t < u is assumed, t - u < 0 holds. So I should get Yes. But what I get is a repetition of If term. That is,

If[t - u < 0, Print[Yes], Print[No]]

Am I missing something?

$\endgroup$
10
  • $\begingroup$ If[t < u, Print[Yes], Print[No]] $\endgroup$
    – paw
    Sep 6, 2014 at 18:04
  • 2
    $\begingroup$ You need a Simplify (Assuming[t < u, Simplify[If[t - u < 0, Print[Yes], Print[No]]]]) otherwise the Assumpition has no effect. $\endgroup$
    – Karsten 7.
    Sep 6, 2014 at 18:07
  • 1
    $\begingroup$ If has no Assumptions option. $\endgroup$
    – Karsten 7.
    Sep 6, 2014 at 18:08
  • 2
    $\begingroup$ Assuming only has an effect on an expression, if the expression has the option Assumptions, like Simplify has. $\endgroup$
    – Karsten 7.
    Sep 6, 2014 at 18:11
  • 1
    $\begingroup$ @ppp I noticed you never accepted any answer to your previous questions. Maybe you would get more responses if you checked "Accept" on some of the earlier answers that you apparently liked. $\endgroup$
    – Jens
    Sep 7, 2014 at 3:35

1 Answer 1

6
$\begingroup$

What you need to use here is Refine:

Refine[If[t - u < 0, Print[Yes], Print[No]], t < u]

Yes

Ad if you need to work with $Assumptions as well, then you could do this:

$Assumptions = t < u;

Refine[If[t - u < 0, Print[Yes], Print[No]], $Assumptions]

Yes

The last line is also the way it would look if you were to wrap your If statement in Assuming. However, since $Assumptions is also used by default, you can simply do this:

Assuming[t < u, Refine[If[t - u < 0, Print[Yes], Print[No]]]]

Yes
$\endgroup$
3
  • $\begingroup$ This is very helpful. Thanks a lot! $\endgroup$
    – ppp
    Sep 6, 2014 at 18:25
  • $\begingroup$ what about in case of multiple assumptions? Can I do add them with &&? $\endgroup$
    – ppp
    Sep 6, 2014 at 18:47
  • $\begingroup$ @ppp Yes, the docs have many examples. $\endgroup$
    – Jens
    Sep 6, 2014 at 18:54

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.