Re: Transform list of numbers into pure functions using replace rule
- To: mathgroup at smc.vnet.net
- Subject: [mg89503] Re: Transform list of numbers into pure functions using replace rule
- From: "David Park" <djmpark at comcast.net>
- Date: Wed, 11 Jun 2008 03:17:54 -0400 (EDT)
- References: <g2lb8t$964$1@smc.vnet.net>
Mac,
It is often easier to use actual Function expressions than to use slot
notation. So you could try something like this:
{1, 2, f[x]} /. x_?NumberQ -> Function[x]
{1 &, 2 &, f[x]}
But there is some danger in that because I don't know what f[x] is. It looks
like it is not a function but an expression. Is 'f' a function you have
already predefined? If it is an expression then it may contain numbers that
would also be replaced by the rule. The following might be safer:
{1, 2, f[x]} /.
data : List[args__] :> (If[NumberQ[#], Function[x, #], #] & /@ data)
Function[x, 1], Function[x, 2], f[x]}
If f[x] is actually an expression in x then why not use the following, which
does give everything as a Function?
{1, 2, 2 x + x^2} /. data : List[args__] :> (Function[x, #] & /@ data)
{Function[x, 1], Function[x, 2], Function[x, 2 x + x^2]}
And why use a replacement rule at all, rather than just mapping directly
onto the data list?
--
David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/
"Mac" <mwjdavidson at googlemail.com> wrote in message
news:g2lb8t$964$1 at smc.vnet.net...
> Hello,
>
> I'm developed a program to invert satellite observations into
> geophysical variables such as forest biomass. One of the input
> parameters are the errors in the observation channels which can be
> either constant or a function. To be consistent the program expects
> all errors to be functions but they can also be constant functions
> e.g. if the error is 4 then the input parameter is "4&".
>
> I've however hit a problem with the replacement rule that allows me to
> transform all input errors into a function. The following works as
> expected in that constants in the list are transformed into virtual
> functions and functions in the list are left alone.
>
> In[40]:= If[NumberQ[#], ToExpression[ToString[#] <> "&"], #] & /@ {1,
> 2, 3 &, f[x]}
>
> Out[40]= {1 &, 2 &, 3 &, f[x]}
>
> However, I cannot find a replacement rule to do the same job. Here is
> one try:
>
> In[39]:= {1, 2, f[x]} /. x_ /; NumberQ -> Hold[x &]
>
> Out[39]= {1, 2, f[x]}
>
> Any help would be appreciated.
>
> Mac
>
>