Ordering multivariate polynomials by increasing higher order terms

6 vues (au cours des 30 derniers jours)
Mohammad Shojaei Arani
Mohammad Shojaei Arani le 13 Août 2024
Modifié(e) : John D'Errico le 13 Août 2024
Hello,
(I did my best to fix this but it failed. Otherwise, I would not bother you)
Consider a multivariate polynomial. It can have constant, lineaer, quadratic, cubic, etc. terms. What I need is to order the terms based on 1) an increasing total degree 2) terms with the same total degree should be ordered by their lexicographical (or alphabetiacal) order. This means that 1) First constants apear, then quadratic terms, then cubic terms, etc 2) (if we have three variables x1,x2,x3) x1*x1 comes first, then x1*x2, then x1*x3, then x2*x2, then x2*x3, and finally x3*x3. I give an exampl in below:
syms x1 x2 x3
F1 = 1.4*x1 - 0.38*x2 - 0.45*x3 - 0.059*x1*x2 - 0.012*x1*x3 + 0.032*x2*x3 - 0.069*x1^2 + 0.037*x2^2 - 7.2e-3*x3^2 + 0.34;
F2 = 2.6*x2 - 0.23*x1 - 0.42*x3 - 0.014*x1*x2 + 7.7e-3*x1*x3 + 1.1e-3*x2*x3 - 2.6e-3*x1^2 - 0.22*x2^2 + 4.4e-4*x3^2 - 0.1;
F3 = 3.6*x3 - 0.77*x2 - 0.23*x1 + 0.049*x1*x2 - 0.059*x1*x3 - 0.18*x2*x3 - 0.012*x1^2 + 0.032*x2^2 - 0.32*x3^2 + 3.5;
The order I prefer is as follow:
syms x1 x2 x3
F1_new = 0.34+1.4*x1 - 0.38*x2 - 0.45*x3- 0.069*x1^2- 0.059*x1*x2 - 0.012*x1*x3+ 0.037*x2^2+ 0.032*x2*x3 - 7.2e-3*x3^2 ;
F2_new = - 0.1- 0.23*x1+2.6*x2- 0.42*x3- 2.6e-3*x1^2- 0.014*x1*x2+7.7e-3*x1*x3- 0.22*x2^2 + 1.1e-3*x2*x3+ 4.4e-4*x3^2 ;
F3_new = 3.5- 0.23*x1- 0.77*x2+3.6*x3- 0.012*x1^2 + 0.049*x1*x2 - 0.059*x1*x3+ 0.032*x2^2 - 0.18*x2*x3- 0.32*x3^2;
I played with the command [C, T] = coeffs (F1) and tried its name-value pairs but unfortunatley it had a very different ordering system which confused me and ChatGPT :-) .I also tried the commands tylor(F1, 'ascend') and gbasis(F1, ...) (and their different name-values pairs). Unfortunately, none of them could generate what i need. I hope you know how to restore the order I want and also you know an efficient way to reorder not just a single eqaution (like F1 above) but a system of multivariate polynomial system at once (lF1, F2, F3).
The ordering I need is of course, not important for calculations. In the command [C, T] = coeffs (F1), the inner product of C and T will reproduce the polynomial no matter what is the order. However, what I need is important for 'representation' of the results and in many mathematical/statistical textbooks people are interested in the ordering system I discussed.
Thanks for your kind response in advance!
Babak

Réponses (1)

John D'Errico
John D'Errico le 13 Août 2024
Modifié(e) : John D'Errico le 13 Août 2024
The problem is, the symbolic toolbox does not use the term sequence you personally prefer. In fact, you can't control the sequence of the terms it generates, and there is no command to tell it to do what you want here.
I suppose you could write code, to extract each term into a cell array. Write more code to check the total polynomial order for each cell, then sort the cells in the sequence you prefer. Then display them as you wish, fussing around with the formatting to get it right. An hour or several later...
However, nothing stops you from using a simple text editor to reorder the terms, in the sequence you prefer. About 30 seconds with an editor using cut and paste will give you what you want. Intead, you have already spent far more time than that playing around with many different options, then asking this question and waiting for a response.
Choose your battles wisely. :)

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by