]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
- introduced info_flag::algebraic.
[ginac.git] / ginac / power.cpp
index cbe0c65bb9f023d19e94381a38843fcbd763bb1f..8ec1945f718ea833571b978448ceee7d68469f3f 100644 (file)
 #include "add.h"
 #include "mul.h"
 #include "numeric.h"
+#include "inifcns.h"
 #include "relational.h"
 #include "symbol.h"
 #include "archive.h"
 #include "debugmsg.h"
 #include "utils.h"
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC
 
 GINAC_IMPLEMENT_REGISTERED_CLASS(power, basic)
 
@@ -249,17 +250,20 @@ void power::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) co
 
 bool power::info(unsigned inf) const
 {
-    if (inf==info_flags::polynomial ||
-        inf==info_flags::integer_polynomial ||
-        inf==info_flags::cinteger_polynomial ||
-        inf==info_flags::rational_polynomial ||
-        inf==info_flags::crational_polynomial) {
-        return exponent.info(info_flags::nonnegint);
-    } else if (inf==info_flags::rational_function) {
-        return exponent.info(info_flags::integer);
-    } else {
-        return inherited::info(inf);
+    switch (inf) {
+        case info_flags::polynomial:
+        case info_flags::integer_polynomial:
+        case info_flags::cinteger_polynomial:
+        case info_flags::rational_polynomial:
+        case info_flags::crational_polynomial:
+            return exponent.info(info_flags::nonnegint);
+        case info_flags::rational_function:
+            return exponent.info(info_flags::integer);
+        case info_flags::algebraic:
+            return (!exponent.info(info_flags::integer) ||
+                    basis.info(inf));
     }
+    return inherited::info(inf);
 }
 
 unsigned power::nops() const
@@ -318,7 +322,7 @@ ex power::eval(int level) const
 {
     // simplifications: ^(x,0) -> 1 (0^0 handled here)
     //                  ^(x,1) -> x
-    //                  ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
+    //                  ^(0,c1) -> 0 or exception (depending on real value of c1)
     //                  ^(1,x) -> 1
     //                  ^(c1,c2) -> *(c1^n,c1^(c2-n)) (c1, c2 numeric(), 0<(c2-n)<1 except if c1,c2 are rational, but c1^c2 is not)
     //                  ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
@@ -327,50 +331,54 @@ ex power::eval(int level) const
     //                  ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
     
     debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
-
-    if ((level==1)&&(flags & status_flags::evaluated)) {
+    
+    if ((level==1) && (flags & status_flags::evaluated))
         return *this;
-    } else if (level == -max_recursion_level) {
+    else if (level == -max_recursion_level)
         throw(std::runtime_error("max recursion level reached"));
-    }
     
     const ex & ebasis    = level==1 ? basis    : basis.eval(level-1);
     const ex & eexponent = level==1 ? exponent : exponent.eval(level-1);
-
-    bool basis_is_numerical=0;
-    bool exponent_is_numerical=0;
+    
+    bool basis_is_numerical = 0;
+    bool exponent_is_numerical = 0;
     numeric * num_basis;
     numeric * num_exponent;
-
+    
     if (is_exactly_of_type(*ebasis.bp,numeric)) {
-        basis_is_numerical=1;
-        num_basis=static_cast<numeric *>(ebasis.bp);
+        basis_is_numerical = 1;
+        num_basis = static_cast<numeric *>(ebasis.bp);
     }
     if (is_exactly_of_type(*eexponent.bp,numeric)) {
-        exponent_is_numerical=1;
-        num_exponent=static_cast<numeric *>(eexponent.bp);
+        exponent_is_numerical = 1;
+        num_exponent = static_cast<numeric *>(eexponent.bp);
     }
-
+    
     // ^(x,0) -> 1 (0^0 also handled here)
     if (eexponent.is_zero())
-        return _ex1();
-
+        if (ebasis.is_zero())
+            throw (std::domain_error("power::eval(): pow(0,0) is undefined"));
+        else
+            return _ex1();
+    
     // ^(x,1) -> x
     if (eexponent.is_equal(_ex1()))
         return ebasis;
-
-    // ^(0,x) -> 0 (except if x is real and negative)
-    if (ebasis.is_zero()) {
-        if (exponent_is_numerical && num_exponent->is_negative()) {
-            throw(std::overflow_error("power::eval(): division by zero"));
-        } else
+    
+    // ^(0,c1) -> 0 or exception (depending on real value of c1)
+    if (ebasis.is_zero() && exponent_is_numerical) {
+        if ((num_exponent->real()).is_zero())
+            throw (std::domain_error("power::eval(): pow(0,I) is undefined"));
+        else if ((num_exponent->real()).is_negative())
+            throw (std::overflow_error("power::eval(): division by zero"));
+        else
             return _ex0();
     }
-
+    
     // ^(1,x) -> 1
     if (ebasis.is_equal(_ex1()))
         return _ex1();
-
+    
     if (basis_is_numerical && exponent_is_numerical) {
         // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
         // except if c1,c2 are rational, but c1^c2 is not)
@@ -387,10 +395,10 @@ ex power::eval(int level) const
         if (basis_is_crational && exponent_is_crational
             && num_exponent->is_real()
             && !num_exponent->is_integer()) {
-            numeric r, q, n, m;
-            n = num_exponent->numer();
-            m = num_exponent->denom();
-            q = iquo(n, m, r);
+            numeric n = num_exponent->numer();
+            numeric m = num_exponent->denom();
+            numeric r;
+            numeric q = iquo(n, m, r);
             if (r.is_negative()) {
                 r = r.add(m);
                 q = q.sub(_num1());
@@ -398,29 +406,22 @@ ex power::eval(int level) const
             if (q.is_zero())  // the exponent was in the allowed range 0<(n/m)<1
                 return this->hold();
             else {
-                epvector res(2);
+                epvector res;
                 res.push_back(expair(ebasis,r.div(m)));
-                res.push_back(expair(ex(num_basis->power(q)),_ex1()));
-                return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
-                /*return mul(num_basis->power(q),
-                           power(ex(*num_basis),ex(r.div(m)))).hold();
-                */
-                /* return (new mul(num_basis->power(q),
-                   power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
-                */
+                return (new mul(res,ex(num_basis->power(q))))->setflag(status_flags::dynallocated | status_flags::evaluated);
             }
         }
     }
-
+    
     // ^(^(x,c1),c2) -> ^(x,c1*c2)
     // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
-    // case c1=1 should not happen, see below!)
+    // case c1==1 should not happen, see below!)
     if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
-        const power & sub_power=ex_to_power(ebasis);
-        const ex & sub_basis=sub_power.basis;
-        const ex & sub_exponent=sub_power.exponent;
+        const power & sub_power = ex_to_power(ebasis);
+        const ex & sub_basis = sub_power.basis;
+        const ex & sub_exponent = sub_power.exponent;
         if (is_ex_exactly_of_type(sub_exponent,numeric)) {
-            const numeric & num_sub_exponent=ex_to_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)<1) {
                 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
@@ -433,7 +434,7 @@ ex power::eval(int level) const
         is_ex_exactly_of_type(ebasis,mul)) {
         return expand_mul(ex_to_mul(ebasis), *num_exponent);
     }
-
+    
     // ^(*(...,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 (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
@@ -482,13 +483,16 @@ ex power::evalf(int level) const
     ex eexponent;
     
     if (level==1) {
-        ebasis=basis;
-        eexponent=exponent;
+        ebasis = basis;
+        eexponent = exponent;
     } else if (level == -max_recursion_level) {
         throw(std::runtime_error("max recursion level reached"));
     } else {
-        ebasis=basis.evalf(level-1);
-        eexponent=exponent.evalf(level-1);
+        ebasis = basis.evalf(level-1);
+        if (!is_ex_exactly_of_type(eexponent,numeric))
+            eexponent = exponent.evalf(level-1);
+        else
+            eexponent = exponent;
     }
 
     return power(ebasis,eexponent);
@@ -514,6 +518,21 @@ ex power::simplify_ncmul(const exvector & v) const
 
 // protected
 
+/** Implementation of ex::diff() for a power.
+ *  @see ex::diff */
+ex power::derivative(const symbol & s) const
+{
+    if (exponent.info(info_flags::real)) {
+        // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
+        return mul(mul(exponent, power(basis, exponent - _ex1())), basis.diff(s));
+    } else {
+        // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
+        return mul(power(basis, exponent),
+                   add(mul(exponent.diff(s), log(basis)),
+                       mul(mul(exponent, basis.diff(s)), power(basis, -1))));
+    }
+}
+
 int power::compare_same_type(const basic & other) const
 {
     GINAC_ASSERT(is_exactly_of_type(other, power));
@@ -539,36 +558,41 @@ unsigned power::return_type_tinfo(void) const
 
 ex power::expand(unsigned options) const
 {
-    ex expanded_basis=basis.expand(options);
-
-    if (!is_ex_exactly_of_type(exponent,numeric)||
+    if (flags & status_flags::expanded)
+        return *this;
+    
+    ex expanded_basis = basis.expand(options);
+    
+    if (!is_ex_exactly_of_type(exponent,numeric) ||
         !ex_to_numeric(exponent).is_integer()) {
         if (are_ex_trivially_equal(basis,expanded_basis)) {
             return this->hold();
         } else {
             return (new power(expanded_basis,exponent))->
-                    setflag(status_flags::dynallocated);
+                setflag(status_flags::dynallocated |
+                        status_flags::expanded);
         }
     }
-
+    
     // integer numeric exponent
-    const numeric & num_exponent=ex_to_numeric(exponent);
+    const numeric & num_exponent = ex_to_numeric(exponent);
     int int_exponent = num_exponent.to_int();
-
+    
     if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
         return expand_add(ex_to_add(expanded_basis), int_exponent);
     }
-
+    
     if (is_ex_exactly_of_type(expanded_basis,mul)) {
         return expand_mul(ex_to_mul(expanded_basis), num_exponent);
     }
-
+    
     // cannot expand further
     if (are_ex_trivially_equal(basis,expanded_basis)) {
         return this->hold();
     } else {
         return (new power(expanded_basis,exponent))->
-               setflag(status_flags::dynallocated);
+               setflag(status_flags::dynallocated |
+                       status_flags::expanded);
     }
 }
 
@@ -582,15 +606,14 @@ ex power::expand(unsigned options) const
 // non-virtual functions in this class
 //////////
 
+/** expand a^n where a is an add and n is an integer.
+ *  @see power::expand */
 ex power::expand_add(const add & a, int n) const
 {
-    // expand a^n where a is an add and n is an integer
-
-    if (n==2) {
+    if (n==2)
         return expand_add_2(a);
-    }
     
-    int m=a.nops();
+    int m = a.nops();
     exvector sum;
     sum.reserve((n+1)*(m-1));
     intvector k(m-1);
@@ -599,39 +622,45 @@ ex power::expand_add(const add & a, int n) const
     int l;
     
     for (int l=0; l<m-1; l++) {
-        k[l]=0;
-        k_cum[l]=0;
-        upper_limit[l]=n;
+        k[l] = 0;
+        k_cum[l] = 0;
+        upper_limit[l] = n;
     }
-
+    
     while (1) {
         exvector term;
         term.reserve(m+1);
         for (l=0; l<m-1; l++) {
-            const ex & b=a.op(l);
+            const ex & b = a.op(l);
             GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
             GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
-                   !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
-                   !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
+                         !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
+                         !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer()||
+                         !is_ex_exactly_of_type(ex_to_power(b).basis,add)||
+                         !is_ex_exactly_of_type(ex_to_power(b).basis,mul)||
+                         !is_ex_exactly_of_type(ex_to_power(b).basis,power));
             if (is_ex_exactly_of_type(b,mul)) {
                 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
             } else {
                 term.push_back(power(b,k[l]));
             }
         }
-
-        const ex & b=a.op(l);
+        
+        const ex & b = a.op(l);
         GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
         GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
-               !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
-               !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
+                     !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
+                     !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer()||
+                     !is_ex_exactly_of_type(ex_to_power(b).basis,add)||
+                     !is_ex_exactly_of_type(ex_to_power(b).basis,mul)||
+                     !is_ex_exactly_of_type(ex_to_power(b).basis,power));
         if (is_ex_exactly_of_type(b,mul)) {
             term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
         } else {
             term.push_back(power(b,n-k_cum[m-2]));
         }
-
-        numeric f=binomial(numeric(n),numeric(k[0]));
+        
+        numeric f = binomial(numeric(n),numeric(k[0]));
         for (l=1; l<m-1; l++) {
             f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
         }
@@ -675,13 +704,15 @@ ex power::expand_add(const add & a, int n) const
             upper_limit[i]=n-k_cum[i-1];
         }   
     }
-    return (new add(sum))->setflag(status_flags::dynallocated);
+    return (new add(sum))->setflag(status_flags::dynallocated |
+                                   status_flags::expanded );
 }
 
+
+/** Special case of power::expand_add. Expands a^2 where a is an add.
+ *  @see power::expand_add */
 ex power::expand_add_2(const add & a) const
 {
-    // special case: expand a^2 where a is an add
-
     epvector sum;
     unsigned a_nops=a.nops();
     sum.reserve((a_nops*(a_nops+1))/2);
@@ -738,21 +769,21 @@ ex power::expand_add_2(const add & a) const
         
     GINAC_ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
     
-    return (new add(sum))->setflag(status_flags::dynallocated);
+    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
+ *  @see power::expand */
 ex power::expand_mul(const mul & m, const numeric & n) const
 {
-    // expand m^n where m is a mul and n is and integer
-
-    if (n.is_equal(_num0())) {
+    if (n.is_equal(_num0()))
         return _ex1();
-    }
     
     epvector distrseq;
     distrseq.reserve(m.seq.size());
-    epvector::const_iterator last=m.seq.end();
-    epvector::const_iterator cit=m.seq.begin();
+    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));
@@ -765,7 +796,7 @@ ex power::expand_mul(const mul & m, const numeric & n) const
         ++cit;
     }
     return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
-                 ->setflag(status_flags::dynallocated);
+        ->setflag(status_flags::dynallocated);
 }
 
 /*
@@ -789,9 +820,8 @@ ex power::expand_commutative_3(const ex & basis, const numeric & exponent,
         distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
                            power(last_operand,numeric(n-k)));
     }
-    return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
-                                           status_flags::expanded |
-                                           status_flags::dynallocated  )).
+    return ex((new add(distrseq))->setflag(status_flags::expanded |
+                                           status_flags::dynallocated )).
            expand(options);
 }
 */
@@ -814,7 +844,7 @@ ex power::expand_noncommutative(const ex & basis, const numeric & exponent,
 
 // protected
 
-unsigned power::precedence=60;
+unsigned power::precedence = 60;
 
 //////////
 // global constants
@@ -830,6 +860,6 @@ ex sqrt(const ex & a)
     return power(a,_ex1_2());
 }
 
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
 } // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC