[GiNaC-list] how to drop terms in expression with small coefficients?
Alexei Sheplyakov
alexei.sheplyakov at gmail.com
Tue Sep 6 08:33:07 CEST 2011
Hello,
On Sat, Sep 03, 2011 at 10:22:18PM +1000, Tomasz Rudny wrote:
> I have the following problem - I have a polynomial P(x) and before I
> proceed with some calculations on it, I want to make sure it does
> not contain terms like for example:
>
> 10e-12 * x
>
> (in my case x is from (0, 1) so this is safe).
>
> I want to somehow get rid of such small terms.
> In another words for polynomial P(x) = 2.3 * x ^ 2 + 10e-12 * x + 3.76
>
> I want to obtain polynomial Q(x) = 2.3 * x ^ 2 + 3.76
#include <iostream>
#include <string>
#include <ginac/ginac.h>
using namespace GiNaC;
class drop_small_coeff : public map_function
{
const numeric small_val;
public:
ex operator()(const ex& e)
{
if (is_a<add>(e))
return e.map(*this);
numeric c = e.integer_content();
if (c < small_val)
return 0;
else
return e;
}
drop_small_coeff(const numeric& small_val_) : small_val(small_val_)
{ }
};
int main(int argc, char** argv)
{
parser p;
ex e = p(std::string("x^3 + 10*x^2 + 1/1000*x + 1/10"));
drop_small_coeff d(numeric("1/100"));
std::cout << e << " => " << d(e) << std::endl;
return 0;
}
By the way, the documentation [1] contains a similar example (look for
map_rem_quad).
[1] http://www.ginac.de/tutorial/Applying-a-function-on-subexpressions.html#Applying-a-function-on-subexpressions
Best regards,
Alexei
More information about the GiNaC-list
mailing list