# How do I extract terms from a complicated polynomial?

The polynomial is:

b^2 x1 y0 - b^2 x2 y0 + b^2 x0 y1 + b^2 x2 y1 - b^2 x0 y2 -
b^2 x1 y2 + a^2 x1 y0 λ - a^2 x2 y0 λ +
a^2 x0 y1 λ - a^2 x2 y1 λ - a^2 x0 y2 λ +
a^2 x1 y2 λ == 0


I want to:

1. Find terms that contain x1 y2 and x2 y1, and merge them separately into similar terms. The rest of the terms are not required, and the overall form of the result should be a x1 y2 + b x2 y1 + c.

2. Find terms that contain x1 y2 - x2 y1 or x2 y1 - x1 y2, and merge them into similar terms separately. The remaining terms are not required, and the results should be a (x1y2-x2y1)+b or a (x2y1-x1y2)+b.

I've tried this code, but it doesn't work.

b^2 (x1 y0 - x2 y0 + x0 y1 + x2 y1 - (x0 + x1) y2) +
a^2 (-x2 (y0 + y1) + x0 (y1 - y2) + x1 (y0 + y2)) λ ==
0 // Expand
% // Collect[#, {x1y2, x2y1}] &


The end result requirements are in the following form:

b^2 x1 y0 - b^2 x2 y0 + b^2 x0 y1 - b^2 x0 y2 + a^2 x1 y0 \[Lambda] -
a^2 x2 y0 \[Lambda] + a^2 x0 y1 \[Lambda] -
a^2 x0 y2 \[Lambda] + (x2 y1 - x1 y2) (b^2 - a^2 \[Lambda]) == 0

• 1. Check Collect. 2. What do you mean by "Find items that contain x1y2 x2y1 or x2y1 x1y2"? Terms involving x1 y2 x2 y1 or x2 y1 x1 y2? If so, I don't find such term in the given equation. Aug 18, 2023 at 8:22
• If you've really understood the answers under your previous questions e.g. this one, this should not be a problem for you. Once again (I can't remember how many times I've typed this line), please put more effort in understanding the answers you've obtained. Aug 18, 2023 at 8:41

Try this:

Let us introduce the following function:

f[expr_, factor1_, factor2_] :=
Select[Select[expr, MemberQ[#, factor1] &], MemberQ[#, factor2] &];


Here is your expression:

eq = b^2 x1 y0 - b^2 x2 y0 + b^2 x0 y1 + b^2 x2 y1 - b^2 x0 y2 -
b^2 x1 y2 + a^2 x1 y0 \[Lambda] - a^2 x2 y0 \[Lambda] +
a^2 x0 y1 \[Lambda] - a^2 x2 y1 \[Lambda] - a^2 x0 y2 \[Lambda] +
a^2 x1 y2 \[Lambda] == 0;


Step 1:

expr1 = f[eq[[1]], x1, y2] + f[eq[[1]], y1, x2]

(*  b^2 x2 y1 - b^2 x1 y2 - a^2 x2 y1 \[Lambda] + a^2 x1 y2 \[Lambda]  *)


Step 2:

Collect[expr1, {x1*y2, x2*y1}]

(*  x2 y1 (b^2 - a^2 \[Lambda]) + x1 y2 (-b^2 + a^2 \[Lambda])  *)


Step 3:

expr1 // Simplify

(*  (x2 y1 - x1 y2) (b^2 - a^2 \[Lambda])  *)


If you need the rest of expression, do the following:

eq[[1]] - expr1 // Simplify

(*  (x1 y0 - x2 y0 + x0 (y1 - y2)) (b^2 + a^2 \[Lambda])  *)


Have fun!

• The result should be displayed completely, which is equal to the original formula. Aug 18, 2023 at 11:50
• @csn It should be displayed completely? Then display it! Aug 18, 2023 at 12:13
expr = b^2 x1 y0 - b^2 x2 y0 + b^2 x0 y1 + b^2 x2 y1 - b^2 x0 y2 -
b^2 x1 y2 + a^2 x1 y0 λ - a^2 x2 y0 λ +
a^2 x0 y1 λ - a^2 x2 y1 λ - a^2 x0 y2 λ +
a^2 x1 y2 λ;

factorsOfInterest = {x1 y2 , x2 y1};
t1 = factorsOfInterest . Coefficient[expr, factorsOfInterest]
t2 = Collect[(expr - t1 ) // Simplify, {x0, y0}]
t1 + t2


x2 y1 (b^2 - a^2 λ) + x1 y2 (-b^2 + a^2 λ) + (x1 - x2) y0 (b^2 + a^2 λ) + x0 (y1 - y2) (b^2 + a^2 λ)

(t1 + t2 // Expand) === expr


True