3
$\begingroup$

I have a function (deep inside my package) that looks like

signedPart =
  Function[{expr, inf},
    Assuming[Join[#>0&/@(Variables[expr]), {expr<0}], Simplify[Sign[inf]]]
  ]

This function is supposed to return the sign of the input inf assuming that all variables given in expr are (real) positive and expr is negative.

Some examples that work as intended:

signedPart[-x^2-y^2,x]
(*1*)

signedPart[-x^2+y^2,x-y]
(*Sign[x-y]*)

However, the following

signedPart[x^2 + y^2, -x]

gives a warning that the assumptions are contradictory (because x^2 + y^2 can't be negative), and arbitrarily returns -1.

Question: What can I do to suppress Simplify from issuing warning messages and instead have it return a default of +1 if contradictory assumptions are encountered?

What I prefer: For the sake of speed, I prefer a solution that is as low-level as possible. For example, inside Simplify there may be a low-level function ContradictoryAssumptionsQ that checks for this. If I had access to this function I would wrap my code to test and return the appropriate result. I would prefer not to use Simplify, FullSimplify, Reduce or Refine on the assumptions to test for a contradiction since these are much too general-purpose (overkill) for the task. Also, my function signedPart will be used many times, so I'd like it to be fast and not issue messages.

$\endgroup$
5
  • 1
    $\begingroup$ See Check[ ]... $\endgroup$ Commented Nov 23, 2015 at 17:56
  • $\begingroup$ Why do you say that returning $-1$ in your last case is arbitrary? Your assumptions in that case end up including $x>0$, so $-x<0$ and the sign of $-x$ is indeed $-1$. What am I missing? $\endgroup$
    – MarcoB
    Commented Nov 23, 2015 at 18:03
  • $\begingroup$ @MarcoB Well, I guess that example is not a good one. If you try Assuming[x < 0 && x > 0, Simplify[Sign[x]]], you get 1. It has (arbitrarily) picked the second assumption and dropped the first. $\endgroup$
    – QuantumDot
    Commented Nov 23, 2015 at 20:04
  • 2
    $\begingroup$ @MarcoB In general, of course, you'd expect arbitrary output to be possible. If $1<0$ then I am a fish. $\endgroup$ Commented Nov 23, 2015 at 20:27
  • $\begingroup$ Here may be a solution: mathematica.stackexchange.com/questions/267089/… $\endgroup$
    – rnotlnglgq
    Commented Apr 20, 2022 at 9:59

1 Answer 1

3
$\begingroup$

The message name is $Assumptions::cas so it is coming from the setting of $Assumptions rather than from within Simplify. You can confirm this with:

$Assumptions = x < 0 && x > 0

During evaluation of In[111]:= $Assumptions::cas: Warning: contradictory assumption(s) x<0&&x>0 encountered. >>

Don't forget to reset $Assumptions after this!

$Assumptions = True;

There don't appear to be any accessible internals behind this mechanism, so I suspect it will be difficult to find anything lower-level to shortcut it. However, as belisarius commented, you can use Check to detect the message and return 1.

You are interested in speed, so it will be a good idea to interrupt the evaluation as soon as possible rather than wait for Simplify to run - I suggest using Throw/Catch for that purpose. This requires setting $Assumptions explicitly rather than via Assuming so that Throw can be evaluated before Simplify.

Finally you want to suppress the message output, so use Quiet to wrap the whole thing.

The finished function is:

signedPart = Function[{expr, inf},
  Quiet[
   Catch[
    Block[{$Assumptions},
         Check[$Assumptions = Join[# > 0 & /@ (Variables[expr]), {expr < 0}], Throw[1]];
     Simplify[Sign[inf]]]],
   $Assumptions::cas]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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