Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #8515

Re: Repackaging function arguments

From "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Repackaging function arguments
Date 2012-02-13 08:47 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jhaimj$576$1@smc.vnet.net> (permalink)
References <jh82ma$oh8$1@smc.vnet.net>

Show all headers | View raw


On Sun, 12 Feb 2012 10:01:46 -0000, Sam Takoy <sam.takoy@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).

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Repackaging function arguments Sam Takoy <sam.takoy@yahoo.com> - 2012-02-12 10:01 +0000
  Re: Repackaging function arguments "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-13 08:47 +0000

csiph-web