Re: Taking a function as a parameter of a function
- To: mathgroup at smc.vnet.net
- Subject: [mg41861] Re: Taking a function as a parameter of a function
- From: eduault <eduault at yahoo.com>
- Date: Sat, 7 Jun 2003 00:08:51 -0400 (EDT)
- References: <bbq8jj$d7i$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"JustNImge" <justnimge at angelfire.com> a écrit dans le message news:
bbq8jj$d7i$1 at smc.vnet.net...
> Hi everyone I'm new to this group and new to Mathematica in general.
> Actually I'm taking a class in it but we've had very little hands on
> experience.
>
> Anyway, I was wondering if it was possible to take a function as a
> parameter of another function. What I've been trying to do is make a
> list of x^2+y^2 but increase x and y by one each time so I would have
> (x+1)^2+(y+1)^2 etc. Actually the function I'm trying to write for my
> class is more complicated than that but I'm trying to figure this out
> first. When I try to do this, I get:
> In[5]:= test[y1_[x_,y_]]:=Table[y1[x+i,y+i],{i,3}]
>
> In[6]:= test[x^2+y^2]
>
> 2 2 2 2 2 2
> Out[6]= {2 + x + y , 4 + x + y , 6 + x + y }
>
> Is it just not possible to take x and y as their own parameters like
> this?
> I'm sorry if this has been addressed before but when I browsed
> subjects last night there didn't seem to be anything within the first
> 100.
> Thank you very much for any and all help.
> JustNImge
>
In your example, x^2+y^2 is considered by Mathematica as the expression
"Plus[Power[x, 2], Power[y, 2]" ,
so the y1 parameter will be mapped to the "Plus" (+) function, x to
"Power[x, 2]", and y to "Power[y, 2]".
y1[x+i,y+i] is evaluated as Plus[ Power[x, 2]+i , Power[y, 2]+i ], or in a
simpler form as x^2+y^2+2*i.
You should try :
In[46]:= f[x_, y_] := x^2 + y^2;
In[47]:= test[f_, x_, y_] := Table[f[x + i, y + i], {i, 3}];
In[48]:= test[f, x, y]
Out[49]= {(1 + x)^2 + (1 + y)^2, (2 + x)^2 + (2 + y)^2, (3 + x)^2 + (3 +
y)^2}
ou with a pure function (#1^2 + #2^2 &) instead of f:
In[50]:= test[f_, x_, y_] := Table[f[x + i, y + i], {i, 3}];
In[51]:= test[#1^2 + #2^2 &, x, y]
Out[52]= {(1 + x)^2 + (1 + y)^2, (2 + x)^2 + (2 + y)^2, (3 + x)^2 + (3 +
y)^2}
In[53]:= Expand[%]
Out[54]= {2 + 2*x + x^2 + 2*y + y^2, 8 + 4*x + x^2 + 4*y + y^2, 18 + 6*x +
x^2 + 6*y + y^2}
If this code is not what you expect, you should give a complete example,
saying what what the "y1_[x_,y_]]" you really use is, what the y1, x and y
are in this expression, and what the expected transformation / result is.