Group Abstract Group Abstract

Message Boards Message Boards

0
|
44 Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Coefficient extraction for x^2, y^2,xy and x,y

Posted 2 days ago

For the following equation:

40 \[Lambda]1 + x (-15 - 13 \[Lambda]1 - 24 \[Lambda]2) + 
  y^2 (2 - 2 \[Lambda]1 - \[Lambda]2) + 
  x y (-7 - \[Lambda]1 + 2 \[Lambda]2) + 
  x^2 (3 + \[Lambda]1 + 3 \[Lambda]2) + 
  y (5 + 11 \[Lambda]1 + 8 \[Lambda]2) == 0

How to extract the coefficients for x², y², xy, x, y terms and the constant term from the equation to obtain results in the following format?

{3 + \[Lambda]1 + 3 \[Lambda]2, 
 2 - 2 \[Lambda]1 - \[Lambda]2, -7 - \[Lambda]1 + 2 \[Lambda]2, -15 - 
  13 \[Lambda]1 - 24 \[Lambda]2, 5 + 11 \[Lambda]1 + 8 \[Lambda]2, 
 40 \[Lambda]1}

Why does the following code produce incorrect results, and how can I fix it to get the expected output?

eq = 40 \[Lambda]1 + x (-15 - 13 \[Lambda]1 - 24 \[Lambda]2) + 
    y^2 (2 - 2 \[Lambda]1 - \[Lambda]2) + 
    x y (-7 - \[Lambda]1 + 2 \[Lambda]2) + 
    x^2 (3 + \[Lambda]1 + 3 \[Lambda]2) + 
    y (5 + 11 \[Lambda]1 + 8 \[Lambda]2) == 0;
coefficients = {x^2 -> Coefficient[eq[[1]], x, 2], 
  y^2 -> Coefficient[eq[[1]], y, 2], x*y -> Coefficient[eq[[1]], x*y],
   x -> Coefficient[eq[[1]], x, 1], y -> Coefficient[eq[[1]], y, 1], 
  "Constant" -> eq[[1]] /. {x -> 0, y -> 0}}
POSTED BY: Wen Dao
4 Replies

Another way is with CoefficientRules:

CoefficientRules[eq[[1]], {x, y}]
% /. {{0, 0} -> "constant", {a_Integer, b_} :> x^a y^b}
POSTED BY: Gianluca Gorni
Posted 23 hours ago

Well done! Thanks!

POSTED BY: Wen Dao
Posted 23 hours ago

Great!Thanks!

POSTED BY: Wen Dao
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard