# Get coefficients of specified variables of polynomial, including zeros

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.

• You probably shouldn't be using subscripts as variables. In Mathematica, subscripts are for display purposes, not calculations. Use arrays or lists. Jun 21 '18 at 13:47
• 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. Jun 21 '18 at 15:35
• I think it overcomplicated, have you tried the Coefficient function? reference.wolfram.com/language/ref/Coefficient.html Feb 15 at 20:39

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

• Your definition of vars can be shortened to vars = DeleteCases[Variables[myPolynomial], Except[Subscript[__]]] or vars = Cases[myPolynomial, Subscript[__], Infinity] Jun 21 '18 at 15:51
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.5051320791244507, "perplexity": 909.6218328057831}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-43/segments/1634323585121.30/warc/CC-MAIN-20211017052025-20211017082025-00335.warc.gz"}