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


Groups > comp.soft-sys.math.mathematica > #8494 > unrolled thread

Repackaging function arguments

Started bySam Takoy <sam.takoy@yahoo.com>
First post2012-02-12 10:01 +0000
Last post2012-02-13 08:47 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  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

#8494 — Repackaging function arguments

FromSam Takoy <sam.takoy@yahoo.com>
Date2012-02-12 10:01 +0000
SubjectRepackaging function arguments
Message-ID<jh82ma$oh8$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#8515

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-02-13 08:47 +0000
Message-ID<jhaimj$576$1@smc.vnet.net>
In reply to#8494
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).

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web