Prev: algebraic numbers
Next: More /.{I->-1} craziness
From: Sam Takoy on 30 Dec 2009 04:18 Hi, Is it possible to make an Assumption for the duration of a block of code? In other words, to say "In all of the following code, Assume[x>0]". I appreciate that it is possible to use, Assuming[x>0, a whole lot of code ] but that's quite awkward. Looking for a better alternative. Thanks, Sam
From: Leonid Shifrin on 31 Dec 2009 03:17 Sam, you can use the global variable $Assumptions - in particular, temporarily redefine it as a logical And of what it is currently and the conditions you wish to hold for your block of code. Here is one possible way to do this using Block (reprinting from one of the past threads where a similar question was asked) ClearAll[ computeUnderAssumptions]; SetAttributes[computeUnderAssumptions, HoldFirst]; computeUnderAssumptions[expr_, assumptions_List] := Block[{$Assumptions = And[$Assumptions, Sequence @@ assumptions]}, expr]; Example: In[1] = computeUnderAssumptions[ FullSimplify[Im[(a + I*b)^2]] + FullSimplify[Re[(a + I*c)^2], ComplexityFunction -> (Count[#, _Complex, Infinity] &)], {Element[{a, b, c}, Reals]}] Out[1] = 2 a b + (a - c) (a + c) Hope this helps. Regards, Leonid On Wed, Dec 30, 2009 at 1:15 AM, Sam Takoy <samtakoy(a)yahoo.com> wrote: > Hi, > > Is it possible to make an Assumption for the duration of a block of > code? In other words, to say "In all of the following code, Assume[x>0]". > > I appreciate that it is possible to use, > > Assuming[x>0, > > a whole lot of code > > ] > > but that's quite awkward. Looking for a better alternative. > > Thanks, > > Sam > >
From: Bob Hanlon on 31 Dec 2009 03:20 $Assumptions = {x > 0}; Simplify[Sqrt /@ {x^2, y^2}] {x, Sqrt[y^2]} $Assumptions =. Bob Hanlon ---- Sam Takoy <samtakoy(a)yahoo.com> wrote: ============= Hi, Is it possible to make an Assumption for the duration of a block of code? In other words, to say "In all of the following code, Assume[x>0]". I appreciate that it is possible to use, Assuming[x>0, a whole lot of code ] but that's quite awkward. Looking for a better alternative. Thanks, Sam
|
Pages: 1 Prev: algebraic numbers Next: More /.{I->-1} craziness |