0
$\begingroup$

I am trying to extract coefficients of a complex expression which is a polynomial in two variables $x,y$ and has max order up to $x^{18},y^{18}$. I am trying to get the coefficient of say $x$ , $x^2$, $y^2$, $x^2 y^3$. I was usingCoefficient command but seems like I am facing the possible issues listed here.

For example, just a simple example (I was avoifding using CoeeficientLists as my expressions are big) -

A = Expand [(a*x + b*y + c*x^2)^2] gives

a^2 x^2 + 2 a c x^3 + c^2 x^4 + 2 a b x y + 2 b c x^2 y + b^2 y^2

Coefficient[A, x] gives

2 a b y

Coefficient[A, x^2] gives

a^2 + 2 b c y

I want purely the coefficient of $x^2$, that is I want the output to be $a^2$ only. Alos while searching for coefficient of $x$, it should return $0$. I am searching for commands which can do this? like ignore the associated variables with it? like solve the issues regarding the Coefficient command?

$\endgroup$
3
  • 1
    $\begingroup$ Coefficient[expr, x, 2] /. y -> 0 or Coefficient[Coefficient[expr, x, 2], y, 0] $\endgroup$
    – Bob Hanlon
    Sep 30, 2019 at 0:21
  • $\begingroup$ This works though! nice! $\endgroup$
    – BAYMAX
    Sep 30, 2019 at 0:44
  • 1
    $\begingroup$ A workaround is to multiply by all things you want to treat as "variables", and then extract the coefficient of x^2 times that same factor. In[7213]:= Coefficient[A*y, x^2*y] Out[7213]= a^2 This is to get around the issue that Coefficient does not allow one to provide a list specifying what is to be considered a variable vs what is a constant. $\endgroup$ Oct 2, 2019 at 16:52

3 Answers 3

2
$\begingroup$

Another possibility is to use SeriesCoefficient:

SeriesCoefficient[(a*x+b*y+c*x^2)^2, {x, 0, 2}, {y, 0, 0}]

a^2

$\endgroup$
1
$\begingroup$

Test this carefully and see if it consistently does what you expect

A = Expand [(a*x + b*y + c*x^2)^2];
coeff = A /. q_*x^2+r_ :> q

which assigns a^2 to coeff. q and r are any two symbols that do not appear anywhere in the expansion of A. You should be able to replace the *x^2 by things like *x^4 or *x*y or any other expression that appears in the expansion of A.

That /. is shorthand for ReplaceAll and the :> is shorthand for RuleDelayed. You should look up both those in the help system and see if you can understand what that perhaps cryptic expression is actually doing. After that you can experiment by making small changes in this and see if you can correctly predict what it is going to do for you. What that is intended to do is to replace your entire A expression with your desired coefficient and assign that to another variable.

$\endgroup$
1
  • $\begingroup$ Will try this and let u know some questions if i get it! Thanks! $\endgroup$
    – BAYMAX
    Sep 30, 2019 at 0:43
1
$\begingroup$

Why not using CoefficientList? In help page one can read (cl is CoefficientList[A]):

In cl the coefficient of x^a y^b is the element at position {a+1,b+1}

So, in your case if you want pure coefficient of x^2 you type cl[[2+1,0+1]] and get a^2, and for coefficient of x you need cl[[1+1,0+1]] which gives 0.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.