Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Query regarding Function

37 views
Skip to first unread message

Rahul Chakraborty

unread,
Mar 12, 2013, 12:34:12 AM3/12/13
to
Dear all,

Case1: In the following code

Clear[f,x,y]
g=Function[{x,y},x^2+2xy][b,a]

The output is found to be b^2+2 xy

Case2: But for the code

Clear[f,x,y]
g=Function[{x,y},x^2+2y][b,a]

The output is 2 a+b^2

Hence my query is why in case 1 x*y is not replaced by b and a whereas in case 2 it got replaced ?

Regards,
rc

Oliver Roese

unread,
Mar 13, 2013, 4:17:53 AM3/13/13
to
"xy" is parsed as a single symbol in the former case. Try to put a space
between both variables:
Clear[f, x, y]
g = Function[{x, y}, x^2 + 2 x y][b, a]
My output is: 2 a b + b^2

Best regards
Oliver

David Bailey

unread,
Mar 13, 2013, 4:18:41 AM3/13/13
to

[This thread is now closed. There have been enough answers. - Moderator]
Variable names in Mathemnatica can be more than one character long -
remember that computer algebra can require a lot of variables! Thus xy
is a quite distinct variable - space the two characters out to get a
product: x y .

Note that it is always useful to examine dubious expressions using
FullForm (possibly with Hold to prevent evaluation) to determine their
true structure. This resolved a host of puzzles:

In[1]:= x y //FullForm
Out[1]//FullForm= Times[x,y]

In[2]:= xy //FullForm
Out[2]//FullForm= xy

In[3]:= x*y //FullForm
Out[3]//FullForm= Times[x,y]

David Bailey
http://www.dbaileyconsultancy.co.uk


Peter Pein

unread,
Mar 13, 2013, 4:17:47 AM3/13/13
to
Am 12.03.2013 05:34, schrieb Rahul Chakraborty:
Hi rc,

this occurs because you used a variable named "xy", not the product
"x*y" (or a space in place of the asterisk ("x y")).

Peter

Richard Fateman

unread,
Mar 13, 2013, 4:18:14 AM3/13/13
to
try using x^2+2*x*y

xy is not the same as x*y

Dr. Wolfgang Hintze

unread,
Mar 13, 2013, 4:17:58 AM3/13/13
to
On 12 Mrz., 05:34, Rahul Chakraborty <rahul.6s...@gmail.com> wrote:
> Dear all,
>
> Case1: In the following code
>
> Clear[f,x,y]
> g=Function[{x,y},x^2+2xy][b,a]
>
> The output is found to be b^2+2 xy
>
> Case2: But for the code
>
> Clear[f,x,y]
> g=Function[{x,y},x^2+2y][b,a]
>
> The output is 2 a+b^2
>
> Hence my query is why in case 1 x*y is not replaced by b and a whereas=
in case 2 it got replaced ?
>
> Regards,
> rc

It's a simple typo:
You must separate x from y in xy by * or space.
Regards,
Wolfgang

svke...@aol.com

unread,
Mar 13, 2013, 4:18:36 AM3/13/13
to
In case one x*y didn't get replaced because it wasn't there; instead the variable xy was. Put a space between the x and the y:

In[3]:= Clear[f, x, y]
g = Function[{x, y}, x^2 + 2 x y][b, a]

Out[4]= 2 a b+b^2


Frank K

unread,
Mar 13, 2013, 4:18:20 AM3/13/13
to
You need a space between the x and the y. What you have looks like a symbol xy, not x*y.

Alexei Boulbitch

unread,
Mar 14, 2013, 7:12:31 AM3/14/13
to
Dear all,

Case1: In the following code

Clear[f,x,y]
g=Function[{x,y},x^2+2xy][b,a]

The output is found to be b^2+2 xy

Case2: But for the code

Clear[f,x,y]
g=Function[{x,y},x^2+2y][b,a]

The output is 2 a+b^2

Hence my query is why in case 1 x*y is not replaced by b and a whereas in case 2 it got replaced ?

Regards,
rc

Hi, Rahul,

Mathematica understands xy as a single new symbol, and, therefore, it does not interpret it as a product. If you want it to be multiplication you should make a space between x and y.
Then it works. Compare: this is your function

Clear[f, x, y]
g = Function[{x, y}, x^2 + 2 xy][b, a]

b^2 + 2 xy

and this is the same, but with the space between x and y:

Clear[f, x, y]
g = Function[{x, y}, x^2 + 2 x y][b, a]

2 a b + b^2

However, I prefer to put the sign "*" between x and y. Since it becomes quite clear from the first glance, what is x*y. Like this:

Clear[f, x, y]
g = Function[{x, y}, x^2 + 2 x*y][b, a]

2 a b + b^2

Have fun, Alexei



Alexei BOULBITCH, Dr., habil.
IEE S.A.
ZAE Weiergewan,
11, rue Edmond Reuter,
L-5326 Contern, LUXEMBOURG

Office phone : +352-2454-2566
Office fax: +352-2454-3566
mobile phone: +49 151 52 40 66 44

e-mail: alexei.b...@iee.lu




0 new messages