[GiNaC-list] degree function
Vladimir V. Kisil
V.Kisil at leeds.ac.uk
Wed Jul 3 10:38:49 CEST 2024
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 at 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 at ginac.de
SI> https://www.ginac.de/mailman/listinfo/ginac-list
More information about the GiNaC-list
mailing list