From: Camille on
Dear All,

I am fairly new to mathematica. I am stuck with a problem I cannot solve. I would like to call a previous equation into the function Function. If a type directly the expression or copy paste it, it works. However, when I call the expression by its name it does not.
Here is the code for a more precise explanation:

In: ll
Out: d + a x + h x^2 + b y + e x y + c z + j z^2

In: Function[##, a x + b y + c z + d + e x y + h x^2 + j z^2] & @@ {Listp}
Out: Function[{a, b, c, d, e, h, j},
a x + b y + c z + d + e x y + h x^2 + j z^2]

It works well and I can use it to generate as many equations I want by replacing the variables a,b,c,d,e,h,j.
But if I do:

In:Function[##, ll] & @@ {Listp}
Out:Function[{a, b, c, d, e, h, j}, ll]

And I cannot use it.

Any suggestions?

Thanks in advance. Do not hesitate to post if you need some further information. Sorry for my bad english.

Camille

From: Peter Pein on
Am Sun, 1 Aug 2010 08:58:26 +0000 (UTC)
schrieb Camille <camille.segarra(a)gmail.com>:

> Dear All,
>
....
> Here is the code for a more precise explanation:
>
> In: ll
> Out: d + a x + h x^2 + b y + e x y + c z + j z^2
>
> In: Function[##, a x + b y + c z + d + e x y + h x^2 + j z^2] & @@
> {Listp} Out: Function[{a, b, c, d, e, h, j},
> a x + b y + c z + d + e x y + h x^2 + j z^2]
>
> It works well and I can use it to generate as many equations I want
> by replacing the variables a,b,c,d,e,h,j. But if I do:
>
> In:Function[##, ll] & @@ {Listp}
> Out:Function[{a, b, c, d, e, h, j}, ll]
>
> And I cannot use it.
>
> Any suggestions?
>
> Thanks in advance. Do not hesitate to post if you need some further
> information. Sorry for my bad english.
>
> Camille
>
Salut Camille,


In[1]:= ll=d+a x+h x^2+b y+e x y+c z+j z^2;
Listp=Variables[ll]~Complement~{x,y,z};

Function has the attribute HoldAll. The function Function does not
evaluate its parameters.

In[3]:= Attributes(a)Function
Out[3]= {HoldAll,Protected}

But you can force evaluation:

In[4]:= Function[##,Evaluate(a)ll]&@@{Listp}
Out[4]= Function[{a,b,c,d,e,h,j},d+a x+h x^2+b y+e x y+c z+j z^2]

Peter

P.S.: if you like unusal ways to define functions, try:

In[7]:= Evaluate[f@@(Pattern[#,Blank[]]&/@Listp)]=ll
Out[7]= d+a x+h x^2+b y+e x y+c z+j z^2
In[8]:= ??f
:-)


From: Bill Rowe on
On 8/1/10 at 4:58 AM, camille.segarra(a)gmail.com (Camille) wrote:


>I am fairly new to mathematica. I am stuck with a problem I cannot
>solve. I would like to call a previous equation into the function
>Function. If a type directly the expression or copy paste it, it
>works. However, when I call the expression by its name it does not.
>Here is the code for a more precise explanation:
>
>In: ll Out: d + a x + h x^2 + b y + e x y + c z + j z^2

>In: Function[##, a x + b y + c z + d + e x y + h x^2 + j z^2] & @@
>{Listp} Out: Function[{a, b, c, d, e, h, j}, a x + b y + c z +
>d + e x y + h x^2 + j z^2]

>It works well and I can use it to generate as many equations I want
>by replacing the variables a,b,c,d,e,h,j. But if I do:

>In:Function[##, ll] & @@ {Listp} Out:Function[{a, b, c, d, e, h, j},
>ll]

>And I cannot use it.

>Any suggestions?

It would be helpful to know what you want to do after you have
created a function. I am guessing, you have an expression (ll)
and a list of parameters (Listp) in that expression. Then with
this you want to be able to substitute particular values for the
parameters and arrive at a specific instance of your expression
with those values. I would do this as follows:

In[22]:= ll = d + a x + h x^2 + b y + e x y + c z + j z^2;
Listp = {a, b, c, d, e, h, j};
test = RandomInteger[{1, 10}, 7]

Out[24]= {7,6,1,3,4,5,3}

Here, ll is your expression and Listp is your list of
parameters. I've created the variable test to be specific values
for the parameters. I can substitute the values in test for the
corresponding parameters using ReplaceAll as follows:

In[25]:= ll /. Thread[Listp -> test]

Out[25]= 5*x^2 + 4*x*y + 7*x + 6*y + 3*z^2 + z + 3



From: Sseziwa Mukasa on


On Aug 1, 2010, at 4:58 AM, Camille <camille.segarra(a)gmail.com> wrote:

> Dear All,
>
> I am fairly new to mathematica. I am stuck with a problem I cannot solve.=
I would like to call a previous equation into the function Function. If a =
type directly the expression or copy paste it, it works. However, when I ca=
ll the expression by its name it does not.
> Here is the code for a more precise explanation:
>
> In: ll
> Out: d + a x + h x^2 + b y + e x y + c z + j z^2
>
> In: Function[##, a x + b y + c z + d + e x y + h x^2 + j z^2] & @@ {Listp=
}
> Out: Function[{a, b, c, d, e, h, j},
> a x + b y + c z + d + e x y + h x^2 + j z^2]
>
> It works well and I can use it to generate as many equations I want by re=
placing the variables a,b,c,d,e,h,j.
> But if I do:
>
> In:Function[##, ll] & @@ {Listp}
> Out:Function[{a, b, c, d, e, h, j}, ll]
>
> And I cannot use it.
>
> Any suggestions?

Use evaluate, Function[##,Evaluate[ ll]]& @@ {Listp}

Regards,
Ssezi
>

From: Albert Retey on
Am 01.08.2010 10:58, schrieb Camille:
> Dear All,
>
> I am fairly new to mathematica. I am stuck with a problem I cannot solve. I would like to call a previous equation into the function Function. If a type directly the expression or copy paste it, it works. However, when I call the expression by its name it does not.
> Here is the code for a more precise explanation:
>
> In: ll
> Out: d + a x + h x^2 + b y + e x y + c z + j z^2
>
> In: Function[##, a x + b y + c z + d + e x y + h x^2 + j z^2] & @@ {Listp}
> Out: Function[{a, b, c, d, e, h, j},
> a x + b y + c z + d + e x y + h x^2 + j z^2]
>
> It works well and I can use it to generate as many equations I want by replacing the variables a,b,c,d,e,h,j.
> But if I do:
>
> In:Function[##, ll] & @@ {Listp}
> Out:Function[{a, b, c, d, e, h, j}, ll]
>
> And I cannot use it.
>
> Any suggestions?

Function @@ {##, ll} & @@ {Listp}

or

Function[##, Evaluate[ll]] & @@ {Listp}

should do what you want. The deeper reason is the HoldAll-Attribute that
Function has set:

In[9]:= Attributes[Function]

Out[9]= {HoldAll, Protected}

The two examples above make sure the body of the function is evaluated.
Some other possibilities will not work, since Function is also a scoping
construct and some renaming might not turn out as intended:

In[6]:= With[{l = ll}, Function[##, l]] & @@ {Listp}

Out[6]= Function[{a$, b$, c$, d$, e$, h$, j$},
d + a x + h x^2 + b y + e x y + c z + j z^2]

hth,

albert