]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
* Fix typo in comment.
[ginac.git] / ginac / power.cpp
index 3dd0f48f487cc6c397ae13e70019f41e9fe2f7e9..fb5e351e7390204dbe6b024f5cc2f8a80c67625b 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's symbolic exponentiation (basis^exponent). */
 
 /*
- *  GiNaC Copyright (C) 1999-2005 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
@@ -40,6 +40,7 @@
 #include "lst.h"
 #include "archive.h"
 #include "utils.h"
+#include "relational.h"
 
 namespace GiNaC {
 
@@ -56,7 +57,7 @@ typedef std::vector<int> intvector;
 // default constructor
 //////////
 
-power::power() : inherited(TINFO_power) { }
+power::power() : inherited(&power::tinfo_static) { }
 
 //////////
 // other constructors
@@ -258,6 +259,15 @@ ex power::map(map_function & f) const
                return *this;
 }
 
+bool power::is_polynomial(const ex & var) const
+{
+       if (exponent.has(var))
+               return false;
+       if (!exponent.info(info_flags::nonnegint))
+               return false;
+       return basis.is_polynomial(var);
+}
+
 int power::degree(const ex & s) const
 {
        if (is_equal(ex_to<basic>(s)))
@@ -325,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)
@@ -385,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(),
@@ -541,6 +556,30 @@ ex power::evalm() const
        return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated);
 }
 
+bool power::has(const ex & other, unsigned options) const
+{
+       if (!(options & has_options::algebraic))
+               return basic::has(other, options);
+       if (!is_a<power>(other))
+               return basic::has(other, options);
+       if (!exponent.info(info_flags::integer)
+                       || !other.op(1).info(info_flags::integer))
+               return basic::has(other, options);
+       if (exponent.info(info_flags::posint)
+                       && other.op(1).info(info_flags::posint)
+                       && ex_to<numeric>(exponent).to_int()
+                                       > ex_to<numeric>(other.op(1)).to_int()
+                       && basis.match(other.op(0)))
+               return true;
+       if (exponent.info(info_flags::negint)
+                       && other.op(1).info(info_flags::negint)
+                       && ex_to<numeric>(exponent).to_int()
+                                       < ex_to<numeric>(other.op(1)).to_int()
+                       && basis.match(other.op(0)))
+               return true;
+       return basic::has(other, options);
+}
+
 // from mul.cpp
 extern bool tryfactsubs(const ex &, const ex &, int &, lst &);
 
@@ -581,13 +620,66 @@ ex power::conjugate() const
        return (new power(newbasis, newexponent))->setflag(status_flags::dynallocated);
 }
 
+ex power::real_part() const
+{
+       if (exponent.info(info_flags::integer)) {
+               ex basis_real = basis.real_part();
+               if (basis_real == basis)
+                       return *this;
+               realsymbol a("a"),b("b");
+               ex result;
+               if (exponent.info(info_flags::posint))
+                       result = power(a+I*b,exponent);
+               else
+                       result = power(a/(a*a+b*b)-I*b/(a*a+b*b),-exponent);
+               result = result.expand();
+               result = result.real_part();
+               result = result.subs(lst( a==basis_real, b==basis.imag_part() ));
+               return result;
+       }
+       
+       ex a = basis.real_part();
+       ex b = basis.imag_part();
+       ex c = exponent.real_part();
+       ex d = exponent.imag_part();
+       return power(abs(basis),c)*exp(-d*atan2(b,a))*cos(c*atan2(b,a)+d*log(abs(basis)));
+}
+
+ex power::imag_part() const
+{
+       if (exponent.info(info_flags::integer)) {
+               ex basis_real = basis.real_part();
+               if (basis_real == basis)
+                       return 0;
+               realsymbol a("a"),b("b");
+               ex result;
+               if (exponent.info(info_flags::posint))
+                       result = power(a+I*b,exponent);
+               else
+                       result = power(a/(a*a+b*b)-I*b/(a*a+b*b),-exponent);
+               result = result.expand();
+               result = result.imag_part();
+               result = result.subs(lst( a==basis_real, b==basis.imag_part() ));
+               return result;
+       }
+       
+       ex a=basis.real_part();
+       ex b=basis.imag_part();
+       ex c=exponent.real_part();
+       ex d=exponent.imag_part();
+       return
+               power(abs(basis),c)*exp(-d*atan2(b,a))*sin(c*atan2(b,a)+d*log(abs(basis)));
+}
+
+// protected
+
 // protected
 
 /** Implementation of ex::diff() for a power.
  *  @see ex::diff */
 ex power::derivative(const symbol & s) const
 {
-       if (exponent.info(info_flags::real)) {
+       if (is_a<numeric>(exponent)) {
                // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
                epvector newseq;
                newseq.reserve(2);
@@ -619,7 +711,7 @@ unsigned power::return_type() const
        return basis.return_type();
 }
 
-unsigned power::return_type_tinfo() const
+tinfo_t power::return_type_tinfo() const
 {
        return basis.return_type_tinfo();
 }
@@ -851,7 +943,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
 {
@@ -864,8 +956,11 @@ ex power::expand_mul(const mul & m, const numeric & n, unsigned options, bool fr
        // Leave it to multiplication since dummy indices have to be renamed
        if (get_all_dummy_indices(m).size() > 0 && n.is_positive()) {
                ex result = m;
+               exvector va = get_all_dummy_indices(m);
+               sort(va.begin(), va.end(), ex_is_less());
+
                for (int i=1; i < n.to_int(); i++)
-                       result *= rename_dummy_indices_uniquely(m,m);
+                       result *= rename_dummy_indices_uniquely(va, m);
                return result;
        }
 
@@ -876,19 +971,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;
        }