MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Repackaging function arguments

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124950] Re: Repackaging function arguments
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at ymail.com>
  • Date: Mon, 13 Feb 2012 03:44:02 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jh82ma$oh8$1@smc.vnet.net>

On Sun, 12 Feb 2012 10:01:46 -0000, Sam Takoy <sam.takoy at yahoo.com> wrote:

> Hi,
>
> Suppose I have a variable
>
> params = {AccuracyGoal -> 30, PrecisionGoal -> 30, WorkingPrecision ->  
> 50}
>
> How do I "repackage" it so I can pass it to functions such as
> FindRoot?
>
> Thank you in advance,
>
> Sam
>

These would usually be considered options rather than arguments, although  
of course one could use the same approach with arguments proper. Anyway,  
the solution is:

FindRoot[Sin[x] + Exp[x], {x, 0}, Evaluate[Sequence@@params]]

Note that Evaluate is essential here because FindRoot has the attribute  
HoldAll. You have to be a little careful with evaluation control when  
using this technique; for example when x has a value:

x = 1;

params = {Sin[x] + Exp[x], {x, 0}, AccuracyGoal -> 30, PrecisionGoal ->  
30, WorkingPrecision -> 50};
FindRoot@Evaluate[Sequence @@ params]

does not work, but

params = {Unevaluated[Sin[x] + Exp[x]], Unevaluated[{x, 0}], AccuracyGoal  
-> 30, PrecisionGoal -> 30, WorkingPrecision -> 50};
Block[{Unevaluated = Sequence},
  FindRoot@Evaluate[Sequence @@ params]
]

does (albeit with a misleading message and strange-looking output because  
of inappropriate substitutions for x during the evaluation).



  • Prev by Date: Re: Setting global graphics options
  • Next by Date: surprising timings for multiplication of diagonalmatrix and full matrix
  • Previous by thread: Re: Repackaging function arguments
  • Next by thread: simple PDE?