]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
under certain conditions, power::expand_add_2() could produce non-canonical
[ginac.git] / ginac / power.cpp
index a6decf3cb39804eb1c255dd64d2ecebb317861ad..85e5bd6c9a92e76c470dc83c527a4d6eaa85a0d0 100644 (file)
@@ -33,6 +33,7 @@
 #include "constant.h"
 #include "inifcns.h" // for log() in power::derivative()
 #include "matrix.h"
+#include "indexed.h"
 #include "symbol.h"
 #include "print.h"
 #include "archive.h"
@@ -169,39 +170,27 @@ void power::print(const print_context & c, unsigned level) const
 
        } else {
 
+               bool is_tex = is_a<print_latex>(c);
+
                if (exponent.is_equal(_ex1_2)) {
-                       if (is_a<print_latex>(c))
-                               c.s << "\\sqrt{";
-                       else
-                               c.s << "sqrt(";
+                       c.s << (is_tex ? "\\sqrt{" : "sqrt(");
                        basis.print(c);
-                       if (is_a<print_latex>(c))
-                               c.s << '}';
-                       else
-                               c.s << ')';
+                       c.s << (is_tex ? '}' : ')');
                } else {
-                       if (precedence() <= level) {
-                               if (is_a<print_latex>(c))
-                                       c.s << "{(";
-                               else
-                                       c.s << "(";
-                       }
+                       if (precedence() <= level)
+                               c.s << (is_tex ? "{(" : "(");
                        basis.print(c, precedence());
                        if (is_a<print_python>(c))
                                c.s << "**";
                        else
                                c.s << '^';
-                       if (is_a<print_latex>(c))
+                       if (is_tex)
                                c.s << '{';
                        exponent.print(c, precedence());
-                       if (is_a<print_latex>(c))
+                       if (is_tex)
                                c.s << '}';
-                       if (precedence() <= level) {
-                               if (is_a<print_latex>(c))
-                                       c.s << ")}";
-                               else
-                                       c.s << ')';
-                       }
+                       if (precedence() <= level)
+                               c.s << (is_tex ? ")}" : ")");
                }
        }
 }
@@ -775,10 +764,10 @@ ex power::expand_add_2(const add & a) const
                        }
                } else {
                        if (is_ex_exactly_of_type(r,mul)) {
-                               sum.push_back(expair(expand_mul(ex_to<mul>(r),_num2),
+                               sum.push_back(a.combine_ex_with_coeff_to_pair(expand_mul(ex_to<mul>(r),_num2),
                                                     ex_to<numeric>(c).power_dyn(_num2)));
                        } else {
-                               sum.push_back(expair((new power(r,_ex2))->setflag(status_flags::dynallocated),
+                               sum.push_back(a.combine_ex_with_coeff_to_pair((new power(r,_ex2))->setflag(status_flags::dynallocated),
                                                     ex_to<numeric>(c).power_dyn(_num2)));
                        }
                }
@@ -822,16 +811,16 @@ ex power::expand_mul(const mul & m, const numeric & n) const
        epvector::const_iterator last = m.seq.end();
        epvector::const_iterator cit = m.seq.begin();
        while (cit!=last) {
-               if (is_ex_exactly_of_type((*cit).rest,numeric)) {
-                       distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
+               if (is_ex_exactly_of_type(cit->rest,numeric)) {
+                       distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit, n));
                } else {
                        // it is safe not to call mul::combine_pair_with_coeff_to_pair()
                        // since n is an integer
-                       distrseq.push_back(expair((*cit).rest, ex_to<numeric>((*cit).coeff).mul(n)));
+                       distrseq.push_back(expair(cit->rest, ex_to<numeric>(cit->coeff).mul(n)));
                }
                ++cit;
        }
-       return (new mul(distrseq,ex_to<numeric>(m.overall_coeff).power_dyn(n)))->setflag(status_flags::dynallocated);
+       return (new mul(distrseq, ex_to<numeric>(m.overall_coeff).power_dyn(n)))->setflag(status_flags::dynallocated);
 }
 
 } // namespace GiNaC