Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: Evaluate, Replace, and Hold

  • To: mathgroup at smc.vnet.net
  • Subject: [mg77165] Re: [mg77118] Evaluate, Replace, and Hold
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Tue, 5 Jun 2007 06:37:23 -0400 (EDT)
  • References: <8194080.1180946234421.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

Here's a function that allows several different interfaces:

Clear[f]
f[a_, b_][x_] := a x + b;
f[{a_, b_}][x_] := f[a, b][x];
f[r : {_ -> a_, _ -> b_}][x_] := f[a, b][x]

p1 = {a -> 1, b -> 0};
p2 = {1, 1};
p3 = Sequence[1, 2];
pts = Range[10];

f[p1] /@ pts
f[p2] /@ pts
f[p3] /@ pts

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
{3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

f /@ {p1, p2}

{f[{a -> 1, b -> 0}], f[{1, 1}]}

# /@ pts & /@ (f /@ {p1, p2})

{{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}

(The Sequence interface doesn't work in the last two examples.)

Set also works (rather than SetDelayed):

Clear[f, a, b, x]
f[a_, b_][x_] = a x + b;
f[{a_, b_}][x_] = f[a, b][x];
f[r : {_ -> a_, _ -> b_}][x_] = f[a, b][x];

Set is faster, when it works at all for the application.

Bobby

On Mon, 04 Jun 2007 02:49:10 -0500, <grenander at gmail.com> wrote:

>
> This is a newbie question. I have a parametric form of a model and I
> would like to evaluate it with different parameters on a list of
> values.
>
> model = a x + b;
>
> Different sets of parameters:
>
> p1 = {a->1, b->0};
> p2 = {a->1, b->1};
>
> x for which I'd like to evaluate the model:
>
> pts = Range[10];
>
> I would like to define a function to evaluate the model at pts using
> any of the sets of paramters:
>
> f[pts, p1]
> f[pts, p2]
>
> I would also like it to work for single points:
>
> f[1, p1]
> f[2, p1]
>
> I've tried various forms for a function definition that dont work. I
> can do the following for a particular set of parameters:
>
> Function[{x}, Evaluate[model /. p1]] /@ pts
>
> but how can I turn it into a function of also the parameters. The
> below doesn't work, for example:
>
> f[pts_, p_]:=Function[{x}, Evaluate[model /. p] ] /@ pts
>
> What I know is from descending the documentation tree (v6.0) starting
> from the examples in FindFit. I have a fundamental misunderstanding of=

> replacement rules and delayed evaluation.
>
> Thanks.
>
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Evaluate, Replace, and Hold
  • Next by Date: Re: Aligning ListPlots
  • Previous by thread: Re: Evaluate, Replace, and Hold
  • Next by thread: Re: Evaluate, Replace, and Hold