2
$\begingroup$

I am trying to get my head around Options, OptionsPattern, etc. - I have looked e.g. here and here but am still struggling.

Say I want to create a "customised" version of FullSimplify, (call it fs) which is supposed to

  1. always use a certain set of assumptions
  2. allow for the input of additional assumptions.

Example: always assume a<0, sometimes add b>0. So the desired result would be:

Column[{
FullSimplify[Sqrt[a^2]],
fs[Sqrt[a^2]],
fs[Sqrt[a^2 b^2],
fs[Sqrt[a^2 b^2],{b>0}]
}]

Sqrt[a]
-a
-a Sqrt[b]
-a b

Now my question is how to define fs(and the "default" assumption) properly. My (lousy) current attempt is something like

std:={a<0}; (* or "=" instead? *)
Options[fs]=OptionsPattern[Assumptions -> {a < 0}];
(* do I need to parse the other options of FullSimplify as well? *)
fs[x_,opts:OptionsPattern[]]:=FullSimplify[x,...]
(* how do I add the option {b>0} s.t. it becomes an _additional_ assumption? *)
$\endgroup$
3
$\begingroup$

If you don't need to specify default options for fs independently from those of FullSimplify, you could do

Options[fs] = {Assumptions :> a < 0 && $Assumptions};

fs[expr_, assum_ : {}, opts : OptionsPattern[]] := 
    FullSimplify[expr, assum, Join[{opts}, Options[fs]]]

{FullSimplify[Sqrt[a^2]], fs[Sqrt[a^2]], fs[Sqrt[a^2 b^2]], fs[Sqrt[a^2 b^2], {b > 0}]}
(* {Sqrt[a^2], -a, -a Sqrt[b^2], -a b} *)

If you do want to be able to define all of the default options of fs independently, you can instead set

Options[fs] = Options[FullSimplify] /. 
                (Assumptions :> _) -> (Assumptions :> a < 0 && $Assumptions)
(* {Assumptions :> a < 0 && $Assumptions, ComplexityFunction -> Automatic, ExcludedForms -> {}, TimeConstraint -> \[Infinity], TransformationFunctions -> Automatic, Trig -> True} *)

with the other function definitions as before. Then you could have something like

SetOptions[fs, TimeConstraint -> 1]
(* {Assumptions :> a < 0 && $Assumptions, ComplexityFunction -> Automatic, ExcludedForms -> {}, 
TimeConstraint -> 1, TransformationFunctions -> Automatic, Trig -> True} *)
$\endgroup$
  • $\begingroup$ Thank you! Since I don't need to specify further options, your first solution does just fine. Just to be sure: I don't need to add anything like SetAttributes[fs,HoldRest] to ensure the assumption assum doesn't get evaluated too early? $\endgroup$ – Bernd Nov 16 '16 at 9:25
  • 1
    $\begingroup$ Well, FullSimplify doesn't have HoldRest, so I don't think adding it to fs will make any difference. If you're wondering about the use of RuleDelayed in the Assumptions option, I think that's to allow the value of the variable $Assumptions to be changed after the default option is defined. $\endgroup$ – Simon Rochester Nov 16 '16 at 10:10
  • 1
    $\begingroup$ Edited my answer to shorten the function definition using a default argument. $\endgroup$ – Simon Rochester Dec 1 '16 at 1:32

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.