[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
5.8.1 The normal
method
Some basic form of simplification of expressions is called for frequently.
GiNaC provides the method .normal()
, which converts a rational function
into an equivalent rational function of the form ‘numerator/denominator’
where numerator and denominator are coprime. If the input expression is already
a fraction, it just finds the GCD of numerator and denominator and cancels it,
otherwise it performs fraction addition and multiplication.
.normal()
can also be used on expressions which are not rational functions
as it will replace all non-rational objects (like functions or non-integer
powers) by temporary symbols to bring the expression to the domain of rational
functions before performing the normalization, and re-substituting these
symbols afterwards. This algorithm is also available as a separate method
.to_rational()
, described below.
This means that both expressions t1
and t2
are indeed
simplified in this little code snippet:
{ symbol x("x"); ex t1 = (pow(x,2) + 2*x + 1)/(x + 1); ex t2 = (pow(sin(x),2) + 2*sin(x) + 1)/(sin(x) + 1); std::cout << "t1 is " << t1.normal() << std::endl; std::cout << "t2 is " << t2.normal() << std::endl; } |
Of course this works for multivariate polynomials too, so the ratio of
the sample-polynomials from the section about GCD and LCM above would be
normalized to P_a/P_b
= (4*y+z)/(y+3*z)
.