Re: Controlled evaluation of functions
- To: mathgroup at smc.vnet.net
- Subject: [mg56809] Re: [mg56763] Controlled evaluation of functions
- From: yehuda ben-shimol <bsyehuda at gmail.com>
- Date: Fri, 6 May 2005 03:00:55 -0400 (EDT)
- References: <200505051002.GAA22030@smc.vnet.net>
- Reply-to: yehuda ben-shimol <bsyehuda at gmail.com>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
you can always use an undefined function that will serve as a dummy
head for your expressions and then use replacement rule to replace
this "dummy head" with your true function.
k[x_] := x^2; (*you original function*)
f[i_, x_] := dh[i x]; (* dh stands for Dummy Head *)
g[x_]:= Table[f[i, x], {i, 3}]; (*use SetDelayed in your code and not Set *)
Then
g[y] returns
{dh[y], dh[2 y], dh[3 y]}
and
{3, 0, 1}.g[y] returns
3 dh[y] + dh[3 y]
No its time to use pattern matching (or just replacing heads)
first option
g[x]/.dh[x_]->k[x]
second option
g[x]/.dh->k
third option
Map[Apply[k,#]&,g[x]]
or shortly
Apply[k,#]&/@g[x]
yehuda
On 5/5/05, Brett Patterson <muckle.moose at gmail.com> wrote:
> Consider the following behaviour:
>
> In[1]:= f[i_, x_] := Sin[i x]
>
> In[2]:= g[x_] = Table[f[i, x], {i, 3}]
>
> Out[2]= {Sin[x], Sin[2 x], Sin[3 x]}
>
> In[3]:= {3, 0, 1} . g[y]
>
> Out[3]= 3 Sin[y] + Sin[3 y]
>
> This is what I want to do, but using my own function instead of Sin.
> However, this is the result:
>
> In[4]:= k[x_] := x^2 (* This is my alternative to Sin *)
>
> In[5]:= f[i_, x_] := k[i x]
>
> In[6]:= g[x_] = Table[f[i, x], {i, 3}]
>
> Out[6]= {x^2, 4 x^2, 9 x^2} (* I want {k[x], k[2 x], k[3 x]} *)
>
> In[7]:= {3, 0, 1} . g[y]
>
> Out[7]= 12 y^2 (* I want 3 k[y] + k[3 y] *)
>
> How can I get the function k to behave like Sin, so that it is not
> evaluated?
>
> Note that in my real application, k is a lot more complex and has
> conditions on its arguments, etc.
>
> Thanks!
>
> Brett Patterson
>
>
- References:
- Controlled evaluation of functions
- From: "Brett Patterson" <muckle.moose@gmail.com>
- Controlled evaluation of functions