# What function can I use to evaluate $(x+y)^2$ to $x^2 + 2xy + y^2$?

What function can I use to evaluate $(x+y)^2$ to $x^2 + 2xy + y^2$?

I want to evaluate It and I've tried to use the most obvious way: simply typing and evaluating $(x+y)^2$, But it gives me only $(x+y)^2$ as output. I've been searching for it in the last minutes but I still got no clue, can you help me?

-

 Expand[(x + y)^2]


x^2 + 2 x y+ y^2

But I recommend you to look at the following tutorials.

And of course a super tutorial:

Also this palette maybe really useful: Top Menu >> Palettes >> Other >> Algebraic Manipulation

-
I've found it in the same time you answered. Haha – Voyska Aug 5 '12 at 6:10
@GustavoBandeira Nope. Vitaliy won by 6 seconds – Dr. belisarius Aug 5 '12 at 6:11
@belisarius I feel Olympics spirit ;-) – Vitaliy Kaurov Aug 5 '12 at 6:13
Congratulations! – Voyska Aug 5 '12 at 6:15
@OleksandrR. Maybe to evaluate sub-expressions? One can select self-sufficient part of expression and evaluate it leaving the larger context untouched. – Vitaliy Kaurov Aug 6 '12 at 17:59

## Collect

Since it hasn't been mentioned (and one can interpret the question in another way) I'd recommend to use also Collect (it can be applied not only to polynomials) :

Collect[(x + y)^2, x]

x^2 + 2 x y + y^2


In more general cases it would be handy to use the second argument in the form of List, e.g. Collect[(x + y)^2, {x, y}]. Comparing it to Expand let's try Collect with PolynomialForm :

Collect[(x + y + z)^3, x] // PolynomialForm[ #, TraditionalOrder -> True] &

x^3 + (3 y + 3 z) x^2 + (3 y^2 + 6 z y + 3 z^2) x + y^3 + z^3 + 3 y z^2 + 3 y^2 z


it collects terms with various powers of x only, while this expands terms with positive integer power in the expression :

Expand[(x + y + z)^3]

x^3 + 3 x^2 y + 3 x y^2 + y^3 + 3 x^2 z + 6 x y z + 3 y^2 z + 3 x z^2 + 3 y z^2 + z^3


## Expand

It could be useful to take a look at the second argument of Expand e.g.

Expand[(x + y)^2 + (y + z)^2, x]

x^2 + 2 x y + y^2 + (y + z)^2


it leaves unexpanded terms free of x.

Edit

Let's add another functions which can also expand polynomials ( they serve different purposes though ) like :

## GroebnerBasis

GroebnerBasis[(x + y)^2, x][[1]]

x^2 + 2 x y + y^2

And @@ ( GroebnerBasis[(x + y + w + z)^#, x][[1]] == Expand[(x + y + w + z)^#] & /@ Range[2, 10])

True


## PolynomialReduce

PolynomialReduce[(x + y)^2, 1, x][[1, 1]]

x^2 + 2 x y + y^2

And @@ ( PolynomialReduce[(x + y + w + z)^#, 1, {x, y, w, z}][[1,1]]
== Expand[(x + y + w + z)^# ] & /@ Range[2, 10])

True

-

You might also try:

Apart[(x + y)^2]


x^2 + 2 x y + y^2

-