I want to write a function MySimplify
(like built-in Simplify
) that takes a second argument to specify additional $Assumptions
and that also has an overriding Assumptions
option. Here's how I start:
MySimplify::usage = "MySimplify[expr] simplifies expr using QuantumDot's code.
MySimplify[expr, assum] does simplification using assumptions.";
Options[MySimplify] = {Assumptions :> $Assumptions};
SyntaxInformation[MySimplify] = {"ArgumentsPattern" -> {_, _., OptionsPattern[]}};
Question How do I write the boiler-plate code to mimic the behavior of built-in Simplify
? Here's my attempt:
MySimplify[e_, o:OptionsPattern[]] := MySimplify[e, True, o];
MySimplify[e_, a_, OptionsPattern[]] :=
Assuming[a,
Block[{$Assumptions=OptionValue[Assumptions]},
Null
(*QuantumDot's code for simplification goes here*)
]
];
The outer Assuming
appends any assumptions in the second argument to $Assumptions
, and the inner Block
uses $Assumptions
, just like built-in Simplify
.
Problem If I supply assumptions that are contradicting or false, it throws $Assumptions::cas
instead of MySimplify::cas
, or $Assumptions::fas
instead of MySimplify::fas
. How do I make the following cases
$Assumptions = True;
MySimplify[Sin[x], x > 0 && x < 0];
or
$Assumptions = a > 0;
MySimplify[Sin[x], x < 0]
or
MySimplify[Sin[x], Assumptions->False]
generate a MySimplify::cas
and MySimplify::fas
errors instead of a $Assumptions::cas
and $Assumptions::fas
error?
Is this the correct way to build a function that uses assumptions?
WithMessageHandler
to catch messages and reemit them with the appropriate head or use a different handler built at theInternal`HandlerBlock
level. $\endgroup$WithMessageHandler
... you can look here I guess, although that's a bit ugly and autobuilt. $\endgroup$