I have quadratic equation like $eq = x2-7*x5^2$ and vector $xx=\{x0,x1,x2,x3,x4,x5,x6,x7,...\}$. I need to do some nice calculations which will give me coefficient $k=7$, $y=x2$ and $x=x5$, so that equation $y=k*x^2$ is the same as my original equation. Eq is a good equation, so finding $k$, $y$ and $x$ is possbile. How can I do this?


I'm not sure what exactly you want to achieve, but let it be for a start.

eq = x2-7 x5^5

One can extract the list of variables by

xx = Variables[eq]

{x2, x5}

Then, find the list of exponents and coefficients:

rules = CoefficientRules[eq, xx]

{{1, 0} -> 1, {0, 2} -> -7}

The output means that the first coordinate, x2, has power 1 (that comes from the {1,0} part) and a coefficient 1; the second coordinate, x5, has power 2 and coefficient -7. (You can add a term like x2 x5 to see how it works.)

That gives a list of rules that one can use to reproduce the initial polynomial eq with arbitrary variables:

eqNew = FromCoefficientRules[rules, {y, x}]

-7 x^2 + y

I understand that your eq==0, so

Simplify[eqNew == 0]

7 x^2 == y


y = y /. First@Solve[eqNew == 0, y]

7 x^2

assigns 7 x^2 to y:


7 x^2

but I don't know how general you want it to be and what purposes should it serve.

  • $\begingroup$ Just a note that Variables will only return the right variables under certain assumptions (like the expression being a polynomial). If you have more complicated expressions then you'd need to do something along the lines of Cases[N[expr], _Symbol, Infinity] to retrieve the variables $\endgroup$
    – gpap
    Sep 19 '16 at 9:57
  • $\begingroup$ @gpap That's not ultimately general; consider Cases[Exp[Sin[\[Pi] x]], _Symbol, Infinity]. One should somehow use also Except[E,Pi]. $\endgroup$
    – corey979
    Sep 20 '16 at 9:31
  • $\begingroup$ That's why I used N, otherwise all these are read off as symbols. It's still not failsafe but at least picks out "more" variables $\endgroup$
    – gpap
    Sep 20 '16 at 9:33
  • $\begingroup$ Yes, it works with N. In case one wants to avoid it, maybe use Cases twice: Cases[Cases[Exp[Sin[Pi x]], _Symbol, Infinity], Except[E | Pi]], or with DeleteCases. $\endgroup$
    – corey979
    Sep 20 '16 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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