]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
Additional transformations for mul and power [Sheplyakov].
[ginac.git] / ginac / power.cpp
index 75fd2ff51a9d12bd09d867d30fdf50b860ec6240..8829bbae597788f6908e2c4739d68da24ea42619 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's symbolic exponentiation (basis^exponent). */
 
 /*
- *  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -335,6 +335,7 @@ ex power::coeff(const ex & s, int n) const
  *  - ^(0,c) -> 0 or exception  (depending on the real part of c)
  *  - ^(1,x) -> 1
  *  - ^(c1,c2) -> *(c1^n,c1^(c2-n))  (so that 0<(c2-n)<1, try to evaluate roots, possibly in numerator and denominator of c1)
+ *  - ^(^(x,c1),c2) -> ^(x,c1*c2)  if x is positive and c1 is real.
  *  - ^(^(x,c1),c2) -> ^(x,c1*c2)  (c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
  *  - ^(*(x,y,z),c) -> *(x^c,y^c,z^c)  (if c integer)
  *  - ^(*(x,c1),c2) -> ^(x,c2)*c1^c2  (c1>0)
@@ -395,6 +396,10 @@ ex power::eval(int level) const
        if (is_exactly_a<function>(ebasis))
                return ex_to<function>(ebasis).power(eexponent);
 
+       // Turn (x^c)^d into x^(c*d) in the case that x is positive and c is real.
+       if (is_exactly_a<power>(ebasis) && ebasis.op(0).info(info_flags::positive) && ebasis.op(1).info(info_flags::real))
+               return power(ebasis.op(0), ebasis.op(1) * eexponent);
+
        if (exponent_is_numerical) {
 
                // ^(c1,c2) -> c1^c2  (c1, c2 numeric(),
@@ -462,8 +467,9 @@ ex power::eval(int level) const
                        if (is_exactly_a<numeric>(sub_exponent)) {
                                const numeric & num_sub_exponent = ex_to<numeric>(sub_exponent);
                                GINAC_ASSERT(num_sub_exponent!=numeric(1));
-                               if (num_exponent->is_integer() || (abs(num_sub_exponent) - (*_num1_p)).is_negative())
+                               if (num_exponent->is_integer() || (abs(num_sub_exponent) - (*_num1_p)).is_negative()) {
                                        return power(sub_basis,num_sub_exponent.mul(*num_exponent));
+                               }
                        }
                }
        
@@ -471,7 +477,31 @@ ex power::eval(int level) const
                if (num_exponent->is_integer() && is_exactly_a<mul>(ebasis)) {
                        return expand_mul(ex_to<mul>(ebasis), *num_exponent, 0);
                }
-       
+
+               // (2*x + 6*y)^(-4) -> 1/16*(x + 3*y)^(-4)
+               if (num_exponent->is_integer() && is_exactly_a<add>(ebasis)) {
+                       const numeric icont = ebasis.integer_content();
+                       const numeric& lead_coeff = 
+                               ex_to<numeric>(ex_to<add>(ebasis).seq.begin()->coeff).div_dyn(icont);
+
+                       const bool canonicalizable = lead_coeff.is_integer();
+                       const bool unit_normal = lead_coeff.is_pos_integer();
+
+                       if (icont != *_num1_p) {
+                               return (new mul(power(ebasis/icont, *num_exponent), power(icont, *num_exponent))
+                                      )->setflag(status_flags::dynallocated);
+                       }
+
+                       if (canonicalizable && (! unit_normal)) {
+                               if (num_exponent->is_even()) {
+                                       return power(-ebasis, *num_exponent);
+                               } else {
+                                       return (new mul(power(-ebasis, *num_exponent), *_num_1_p)
+                                              )->setflag(status_flags::dynallocated);
+                               }
+                       }
+               }
+
                // ^(*(...,x;c1),c2) -> *(^(*(...,x;1),c2),c1^c2)  (c1, c2 numeric(), c1>0)
                // ^(*(...,x;c1),c2) -> *(^(*(...,x;-1),c2),(-c1)^c2)  (c1, c2 numeric(), c1<0)
                if (is_exactly_a<mul>(ebasis)) {
@@ -938,7 +968,7 @@ ex power::expand_add_2(const add & a, unsigned options) const
        return (new add(sum))->setflag(status_flags::dynallocated | status_flags::expanded);
 }
 
-/** Expand factors of m in m^n where m is a mul and n is and integer.
+/** Expand factors of m in m^n where m is a mul and n is an integer.
  *  @see power::expand */
 ex power::expand_mul(const mul & m, const numeric & n, unsigned options, bool from_expand) const
 {
@@ -966,19 +996,13 @@ ex power::expand_mul(const mul & m, const numeric & n, unsigned options, bool fr
        epvector::const_iterator last = m.seq.end();
        epvector::const_iterator cit = m.seq.begin();
        while (cit!=last) {
-               if (is_exactly_a<numeric>(cit->rest)) {
-                       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
-                       numeric new_coeff = ex_to<numeric>(cit->coeff).mul(n);
-                       if (from_expand && is_exactly_a<add>(cit->rest) && new_coeff.is_pos_integer()) {
-                               // this happens when e.g. (a+b)^(1/2) gets squared and
-                               // the resulting product needs to be reexpanded
-                               need_reexpand = true;
-                       }
-                       distrseq.push_back(expair(cit->rest, new_coeff));
+               expair p = m.combine_pair_with_coeff_to_pair(*cit, n);
+               if (from_expand && is_exactly_a<add>(cit->rest) && ex_to<numeric>(p.coeff).is_pos_integer()) {
+                       // this happens when e.g. (a+b)^(1/2) gets squared and
+                       // the resulting product needs to be reexpanded
+                       need_reexpand = true;
                }
+               distrseq.push_back(p);
                ++cit;
        }