Greetings, I am new to GiNac. I was trying to use the degree function, but I got a weird result. My initial expression is -4*(-3+x)*(-1+x)+9/2*(-2+x)*(-1+x)+1/2*(-2+x)*(-3+x) After the "expand" function call, it returns 9/2*x^2-5/2*x-4*x^2+16*x-27/2*x+1/2*x^2 My goal is to add/subtract the coefficient of equal degree terms. I could not find any function for that. ( if it is already there, please point me) I wanted to write a function to achieve it. To do so. I am taking each term and try to see if 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 << "is polynomial="<< is_polynomial(subExpres,symb) <<endl; // GiNaC::ex pow2=pow(symb,2); cout << subExpres<< " degree=" << subExpres.degree(symb) << endl; } It determines the subexpression as a polynomial but can not determine the degree. E.g is polynomial=1 9/2*x^2 degree=0 // Here degree returned is wrong. Your thoughts will be appreciated. Regards, Santos
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://v-v-kisil.scienceontheweb.net Book: Geometry of Mobius Maps https://doi.org/10.1142/p835 Soft: Geometry of cycles http://moebinv.sourceforge.net/ Jupyter notebooks: https://github.com/vvkisil?tab=repositories
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://www.ginac.de/mailman/listinfo/ginac-list
Thank you, Vladimir. It was a great suggestion. Your comments reflect the statement in section 4.5 of the tutorial. "GiNaC will assign the symbol an internal, unique name of the form symbolNNN. This won’t affect the usability of the symbol but the output of your calculations will become more readable if you give your symbols sensible names....." My context was a bit different. I was trying to use the Lagrange interpolation theorem generically. I created three methods for basis, interpolation, and utility. It was my ignorance to assume symbols are more like literals. After your guidance, I passed the symbol from one function to another so that it could be used. Beauty, now I do not have to add/subtract the coefficient of equal degree terms for which I was planning to write a method. Thanks again. BTW, I am reading your paper "CLASSICAL/QUANTUM=COMMUTATIVE/NONCOMMUTATIVE?" Regards, Santos ________________________________ From: Vladimir V. Kisil <V.Kisil@leeds.ac.uk> Sent: Wednesday, July 3, 2024 4:38 AM To: GiNaC discussion list <ginac-list@ginac.de>; Santos Jha <sjha2@gmu.edu> Cc: Vladimir V. Kisil <V.Kisil@leeds.ac.uk> Subject: Re: [GiNaC-list] degree function 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_3gcda5sStFEOvAflaJ2bvsS5fAGJLsvRhxm... 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<https://doi.org/10.1142/p835> 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<http://moebinv.sourceforge.net/> 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<https://github.com/vvkisil?tab=repositories>
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-EjQ46n...
participants (2)
-
Santos Jha
-
Vladimir V. Kisil