1
$\begingroup$

In general I want to extract coefficients of specific variables of a polynomial (even of variables whose coefficients are zero).

I try to explain it step by step:

1) I have a first polynomial. Exemplary extract:

myPolynomial = Subscript[a, d, 0, 0, 0] + Iq^2*Subscript[a, d, 0, 0, 2] + Id*Subscript[a, d, 0, 1, 0] 

(...and so on)

2) Each Subscript[a, d, x, y, z] is a variable. x, y, and z are indices of a summation. Id, Iq are the coefficients.

3) At first, I want to collect all coefficients of that first polynomial. In the example, this should lead to a list or something that contains {1, Iq^2, Id}. The corresponding variables are {Subscript[a, d, 0, 0, 0], Subscript[a, d, 0, 0, 2], Subscript[a, d, 0, 1, 0]}.

At this point I want to mention that I have to extract the final results to Matlab. For this step I use the "ToMatlab"-function. Code:

myPolynomial /. Plus -> List /. c_.*Subscript[__] :> c // ToMatlab; 

With it, my exported Matlab result has following form: [1, Iq^2, Id];

Ok.. this was the first part, which is already running.

5) Now I want to differentiate the just mentioned polynomial respective (e.g.) Id. This leads to a second polynomial. In the example this would lead to

myPolynomial2 = 0 + 0 + Id*Subscript[a, d, 0, 1, 0] 

Now I want to automatically extract the coefficients of this polynomial. Again Subscript[a, d, x, y, z] with different values of x, y and z are the variables. I want to extract the coefficients in such a way, that each coefficient is at the same place in the resulting list, as the coefficient from the first polynomial. E.g.: For the first list, the coefficients of the variables were listed in following order:

{Subscript[a, d, 0, 0, 0], Subscript[a, d, 0, 0, 2], Subscript[a, d, 0, 1, 0]} 

Therefore the result for the coefficients of the second polynomial should have following form: {0, 0, Id}.

However, I am only able to collect the coefficients without the zeros, so the first coefficient of the second list does not belong to the same variable, as the first coefficient of the first list.

$\endgroup$
3
  • 4
    $\begingroup$ You probably shouldn't be using subscripts as variables. In Mathematica, subscripts are for display purposes, not calculations. Use arrays or lists. $\endgroup$ Commented Jun 21, 2018 at 13:47
  • $\begingroup$ As it is, your question is almost unanswerable. Not only Subscript is really bad practice, it makes your code hard to read. Also, you don't give a concrete example , coded polynomial, over which to test different methods. I guess you should look at CoefficientList, hard to tell. If you expect to inspire anybody to volunteer their time to look at your problem, then start by doing your part. Get the Informed badge by taking the tour. Please edit your question and give a concrete example we can copy&paste and the expected outcome. $\endgroup$ Commented Jun 21, 2018 at 15:35
  • $\begingroup$ I think it overcomplicated, have you tried the Coefficient function? reference.wolfram.com/language/ref/Coefficient.html $\endgroup$ Commented Feb 15, 2021 at 20:39

1 Answer 1

1
$\begingroup$

I think I could solve it myself. Maybe not on a "nice" way, but it works:

I can get the coefficients of polynomial one as follows:

vars = DeleteCases[Variables[myPolynomial], Except[Subscript[_, _, _, _, _]]]; 

Subsequently, I can get the coefficients of the second polynomial in the form as I wanted:

Total[Last@CoefficientArrays[myPolynomial2 /. Plus -> List, vars], 1] // ToMatlab 

I'm sure that this is not the best solution, but as I said, for me it works for the moment.

But I'm open-minded for any better solutions!

Best regards

$\endgroup$
1
  • $\begingroup$ Your definition of vars can be shortened to vars = DeleteCases[Variables[myPolynomial], Except[Subscript[__]]] or vars = Cases[myPolynomial, Subscript[__], Infinity] $\endgroup$ Commented Jun 21, 2018 at 15:51

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.