Hello,
I think the problem is with your declaration of symb in
subExpres.degree(symb). Did you get it from a string "x" through a
parser? In this case it may be different from x in the poly. Then,
it is absent from a monomial and its degree is indeed zero.
Here is an output from a complete example code (see below):
----------------------------------------
Polynomial is x^2, it is a sum of monomials: false
x is polynomial=true and degree=1
2 is polynomial=true and degree=0
----------------------------------------
Polynomial is 1+3*x^2+2*x, it is a sum of monomials: true
3*x^2 is polynomial=true and degree=2
2*x is polynomial=true and degree=1
1 is polynomial=true and degree=0
Note that your polynomial is actually a monomial (but not a sum of
monomials) and this situation needs to be treated differently.
The complete code is:
#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
int main() {
realsymbol x("x");
lst expressions = lst{-4*(-3+x)*(-1+x)+numeric(9,2)*(-2+x)*(-1+x)+numeric(1,2)*(-2+x)*(-3+x),
3*pow(x,2)+2*x+1};
for ( auto poly : expressions) {
cout << endl << "----------------------------------------" << endl;
poly = poly.expand();
cout << "Polynomial is " << poly
<< ", it is a sum of monomials: " << boolalpha << is_a<add>(poly) <<endl;
for (size_t i = 0; i != poly.nops(); ++i) { // Here poly is polynomial as above
ex subExpres=poly.op(i); // I get individual terms
cout << subExpres<< " is polynomial="<< is_polynomial(subExpres,x) ;
// GiNaC::ex pow2=pow(symb,2);
cout << " and degree=" << subExpres.degree(x) << endl;
}
}
return 0;
}
Best wishes,
Vladimir
--
Vladimir V. Kisil
http://secure-web.cisco.com/1uavsA2G3CFg_3gcda5sStFEOvAflaJ2bvsS5fAGJLsvRhxmygxZpeJRcqg2QK_FVg34KGTN8REdXOdOooOeM8sgTWpZ3F67a-r3dX77zlhoiVmjogKWey3de4bHkriJs6vltoSgdKxnmVPR0q_Ciy5Yc8s7eUDeqz5rV8zpLyqHw7C6CzET3jV09f7-CfKMdNBBV1wIYsGFJcq3snZEYyonaLjZBVETNlCBifvTpvYyzGF5VGVLnOjgUg43ePKrNwYRJzHDL2LqYi2vnCvReRQ2yYcisllgfB6p2XaVaOApxnalBcI9HNzPeGWC5VyoF6psC0QWNquyN2LPh9zmQfht9vrx39ZgGFY0q9eBsWafhZyA7GjcOB4RciK_NThkyKIrQqs2y0tMsCz0f4uROcsslpK23m905FNZWgls-Zxyj3wn33HBUahBunSwQPFPw/http%3A%2F%2Fv-v-kisil.scienceontheweb.net
Book: Geometry of Mobius Maps
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fdoi.org%2F10.1142%2Fp835&data=05%7C02%7Csjha2%40gmu.edu%7Cab75458dc71e4de789db08dc9b3b92f9%7C9e857255df574c47a0c00546460380cb%7C0%7C0%7C638555927388803258%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=OY%2FslNRAJWreEeqwmKmcccmjoZKJoA%2FpqrqTdFEwvq0%3D&reserved=0
Soft: Geometry of cycles
https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fmoebinv.sourceforge.net%2F&data=05%7C02%7Csjha2%40gmu.edu%7Cab75458dc71e4de789db08dc9b3b92f9%7C9e857255df574c47a0c00546460380cb%7C0%7C0%7C638555927388811673%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=JRxAoUu7GIaMuB8QzE6%2BKzj2Ou72VMoV3TjAaifZWXk%3D&reserved=0
Jupyter notebooks:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fvvkisil%3Ftab%3Drepositories&data=05%7C02%7Csjha2%40gmu.edu%7Cab75458dc71e4de789db08dc9b3b92f9%7C9e857255df574c47a0c00546460380cb%7C0%7C0%7C638555927388816616%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=LGDJLxcJXIotHH%2FwrMMwQ37lWlA2EQcg1gvn3w0pfGk%3D&reserved=0
>>>>> On Wed, 3 Jul 2024 03:19:30 +0000, Santos Jha <sjha2@gmu.edu> said:
SI> Greetings,
SI> I am new to GiNac. I was trying to use the degree function, but
SI> I got a weird result.
SI> My initial expression is
SI> -4*(-3+x)*(-1+x)+9/2*(-2+x)*(-1+x)+1/2*(-2+x)*(-3+x)
SI> After the "expand" function call, it returns
SI> 9/2*x^2-5/2*x-4*x^2+16*x-27/2*x+1/2*x^2
SI> My goal is to add/subtract the coefficient of equal degree
SI> terms. I could not find any function for that. ( if it is
SI> already there, please point me) I wanted to write a function to
SI> achieve it. To do so. I am taking each term and try to see if
SI> for (size_t i = 0; i != poly.nops(); ++i) { // Here poly is
SI> polynomial as above
SI> ex subExpres=poly.op(i); // I get individual terms
SI> cout << "is polynomial="<< is_polynomial(subExpres,symb) <<endl;
SI> // GiNaC::ex pow2=pow(symb,2); cout << subExpres<< " degree=" <<
SI> subExpres.degree(symb) << endl;
SI> }
SI> It determines the subexpression as a polynomial but can not
SI> determine the degree. E.g
SI> is polynomial=1 9/2*x^2 degree=0 // Here degree returned is
SI> wrong.
SI> Your thoughts will be appreciated. Regards, Santos
SI> ----------------------------------------------------
SI> Alternatives:
SI> ----------------------------------------------------
SI> _______________________________________________ GiNaC-list
SI> mailing list GiNaC-list@ginac.de
SI>
https://secure-web.cisco.com/1W7ynipL-PtPXpNNCM8HaXVaflBXe5T6IsuNZG01-EjQ46nlW1KQgYXiDlPCSJ_dtrpcpYE-G1-aWiPc-PXi_V6ItAhj4t0vXSxiACgIo1xSacx78zQipu7HCrP0wbGGjFvCOCJvrxT-lnRljTV3oMQ8MLid7ALAUOSVtG38772FPluPMoU_LIA1rHb_CIPB8Mb5YKtEMincICyQoiBMs7UzsQsxSav45O1WlSdBCnz-6CYTOcQpRmMJkbngZpblNk4gOMZKi2RfVJ1k1vDYoPytTWhd9QIEshR2J45HiC5wfA-9GaJz8NxSS2cmE_ReERWgeUgyZrRXw0J1HaFppXQt9CqUmu_cdMPePOxT7wH1jxek0cP_1mfY-woY9T69Fg6OWFRP99PGliTHz0tPEuTiOP67hwV9TWy_b5Ir3i7OyE0bNLGlIDFRDis_D6ezY/https%3A%2F%2Fwww.ginac.de%2Fmailman%2Flistinfo%2Fginac-list