Re: locally changing Options
- To: mathgroup at smc.vnet.net
- Subject: [mg108337] Re: locally changing Options
- From: Raffy <adraffy at gmail.com>
- Date: Sun, 14 Mar 2010 05:14:12 -0500 (EST)
- References: <hnakgk$5oa$1@smc.vnet.net> <hndb35$dee$1@smc.vnet.net>
On Mar 13, 4:59 am, Raffy <adra... at gmail.com> wrote: > On Mar 12, 4:12 am, Raffy <adra... at gmail.com> wrote: > > > > > > > On Mar 11, 3:34 am, hemmecke <hemme... at gmail.com> wrote: > > > > In a function I'd like to temporarily say > > > > foo[x_]:=Module[{...}, > > > ... > > > SetOptions[RowReduce, Method->OneStepRowReduction]; > > > ... > > > ] > > > > without affecting the global setting for RowReduce when I call foo. > > > > My experiments wrapping the SetOptions with a Block structure were not > > > fruitful so far. > > > The only thing I can think of is something of the form > > > > method = Options[RowReduce, Method]; > > > SetOptions[RowReduce, Method->OneStepRowReduction]; > > > ... do something ... > > > SetOptions[RowReduce,method] > > > > But that looks ugly. > > > > Any suggestions with built-in Mathematica features to achieve this kind= > o= > > f > > > locality. > > > > Ralf > > > Block[ > > {RowReduce = RowReduce[#1, Method -> "OneStepRowReduction", ##2] &}, > > (* your code here *) > > ] > > A few people emailed me today, noting that my solution above, while > simple, doesn't work and results in infinite recursion. I apologize > for not testing my solution. > > Anyway, after thinking about it, I felt that the simple fix would be: > Block[{RowReduce=System`RowReduce[#1,Method->...,##2]&}, ...] > > However, this also results in recursion. Something seems odd: how is > the blocked RowReduce scoping the fully qualified symbol > System`RowReduce? (Looking at the Trace, it appears that it is > dropping the System` context prior to evaluation...) > > This works: Block[{Print=System`Print},Print[1]]; > This doesn't: Block[{Print=System`Print[##] &}, Print[1]]; > > Really odd behavior can be found using the following: > Let $ContextPath = { ..., "Temp`", ..., "System`", ... } > Block[{Temp`Print=System`Print[1, ##] &}, Print[2]] will print "12" > and also cause Print to become shadowed. > > I guess the problem boils down to: Why does System`Print simplify to > Print? To make my point a bit more clear: Imagine your $ContextPath does not contain "Temp`" and then let Temp`f[x___] := {x}; Now, if I evaluate the following code, I get the expected result: Block[{f=Temp`f[1, ##]&},f[2]] === {1, 2} Notice: because Temp` is not on the $ContextPath, the fully qualified symbol is not being truncated from Temp`f to f. Instead, lets define System`f[x___] := {x}; The default notebook $ContextPath is similar to {..., "System`", "Global`"}; The same code above, Block[{f=System`f[1,##]&},f[2]], will now become recursive. For some reason, it's REMOVING the System` context on System`f during evaluation, which is causing it to get scoped by Block since obviously, Block[{f=f[1,##]&},f[1]], will be recursive.