[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.8.3 Converting to a polynomial or rational expression
Some of the methods described so far only work on polynomials or rational functions. GiNaC provides a way to extend the domain of these functions to general expressions by using the temporary replacement algorithm described above. You do this by calling
ex ex::to_polynomial(exmap & m); ex ex::to_polynomial(lst & l); |
or
ex ex::to_rational(exmap & m); ex ex::to_rational(lst & l); |
on the expression to be converted. The supplied exmap
or lst
will be filled with the generated temporary symbols and their replacement
expressions in a format that can be used directly for the subs()
method. It can also already contain a list of replacements from an earlier
application of .to_polynomial()
or .to_rational()
, so it's
possible to use it on multiple expressions and get consistent results.
The difference between .to_polynomial()
and .to_rational()
is probably best illustrated with an example:
{ symbol x("x"), y("y"); ex a = 2*x/sin(x) - y/(3*sin(x)); cout << a << endl; lst lp; ex p = a.to_polynomial(lp); cout << " = " << p << "\n with " << lp << endl; // = symbol3*symbol2*y+2*symbol2*x // with {symbol2==sin(x)^(-1),symbol3==-1/3} lst lr; ex r = a.to_rational(lr); cout << " = " << r << "\n with " << lr << endl; // = -1/3*symbol4^(-1)*y+2*symbol4^(-1)*x // with {symbol4==sin(x)} } |
The following more useful example will print ‘sin(x)-cos(x)’:
{ symbol x("x"); ex a = pow(sin(x), 2) - pow(cos(x), 2); ex b = sin(x) + cos(x); ex q; exmap m; divide(a.to_polynomial(m), b.to_polynomial(m), q); cout << q.subs(m) << endl; } |