]> www.ginac.de Git - ginac.git/blobdiff - ginac/power.cpp
* Restore speed lost in GiNaC-1.3.2 [V. Kisil].
[ginac.git] / ginac / power.cpp
index 7929768541fd30b9f90fd5693f9776793f9213dc..f9dfafbfb80c55a5856195a4be9127a0f538c0fc 100644 (file)
@@ -381,6 +381,10 @@ ex power::eval(int level) const
        if (ebasis.is_equal(_ex1))
                return _ex1;
 
+       // power of a function calculated by separate rules defined for this function
+       if (is_exactly_a<function>(ebasis))
+               return ex_to<function>(ebasis).power(eexponent);
+
        if (exponent_is_numerical) {
 
                // ^(c1,c2) -> c1^c2  (c1, c2 numeric(),
@@ -448,7 +452,7 @@ 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).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));
                        }
                }
@@ -474,8 +478,8 @@ ex power::eval(int level) const
                                                return (new mul(power(*mulp,exponent),
                                                                power(num_coeff,*num_exponent)))->setflag(status_flags::dynallocated);
                                        } else {
-                                               GINAC_ASSERT(num_coeff.compare(_num0)<0);
-                                               if (!num_coeff.is_equal(_num_1)) {
+                                               GINAC_ASSERT(num_coeff.compare(*_num0_p)<0);
+                                               if (!num_coeff.is_equal(*_num_1_p)) {
                                                        mul *mulp = new mul(mulref);
                                                        mulp->overall_coeff = _ex_1;
                                                        mulp->clearflag(status_flags::evaluated);
@@ -704,7 +708,7 @@ ex power::expand_add(const add & a, int n, unsigned options) const
        const size_t m = a.nops();
        exvector result;
        // The number of terms will be the number of combinatorial compositions,
-       // i.e. the number of unordered arrangement of m nonnegative integers
+       // i.e. the number of unordered arrangements of m nonnegative integers
        // which sum up to n.  It is frequently written as C_n(m) and directly
        // related with binomial coefficients:
        result.reserve(binomial(numeric(n+m-1), numeric(m-1)).to_int());
@@ -806,7 +810,7 @@ ex power::expand_add_2(const add & a, unsigned options) const
                
                if (c.is_equal(_ex1)) {
                        if (is_exactly_a<mul>(r)) {
-                               sum.push_back(expair(expand_mul(ex_to<mul>(r), _num2, options, true),
+                               sum.push_back(expair(expand_mul(ex_to<mul>(r), *_num2_p, options, true),
                                                     _ex1));
                        } else {
                                sum.push_back(expair((new power(r,_ex2))->setflag(status_flags::dynallocated),
@@ -814,11 +818,11 @@ ex power::expand_add_2(const add & a, unsigned options) const
                        }
                } else {
                        if (is_exactly_a<mul>(r)) {
-                               sum.push_back(a.combine_ex_with_coeff_to_pair(expand_mul(ex_to<mul>(r), _num2, options, true),
-                                                    ex_to<numeric>(c).power_dyn(_num2)));
+                               sum.push_back(a.combine_ex_with_coeff_to_pair(expand_mul(ex_to<mul>(r), *_num2_p, options, true),
+                                                    ex_to<numeric>(c).power_dyn(*_num2_p)));
                        } else {
                                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)));
+                                                    ex_to<numeric>(c).power_dyn(*_num2_p)));
                        }
                }
 
@@ -826,7 +830,7 @@ ex power::expand_add_2(const add & a, unsigned options) const
                        const ex & r1 = cit1->rest;
                        const ex & c1 = cit1->coeff;
                        sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
-                                                                     _num2.mul(ex_to<numeric>(c)).mul_dyn(ex_to<numeric>(c1))));
+                                                                     _num2_p->mul(ex_to<numeric>(c)).mul_dyn(ex_to<numeric>(c1))));
                }
        }
        
@@ -836,10 +840,10 @@ ex power::expand_add_2(const add & a, unsigned options) const
        if (!a.overall_coeff.is_zero()) {
                epvector::const_iterator i = a.seq.begin(), end = a.seq.end();
                while (i != end) {
-                       sum.push_back(a.combine_pair_with_coeff_to_pair(*i, ex_to<numeric>(a.overall_coeff).mul_dyn(_num2)));
+                       sum.push_back(a.combine_pair_with_coeff_to_pair(*i, ex_to<numeric>(a.overall_coeff).mul_dyn(*_num2_p)));
                        ++i;
                }
-               sum.push_back(expair(ex_to<numeric>(a.overall_coeff).power_dyn(_num2),_ex1));
+               sum.push_back(expair(ex_to<numeric>(a.overall_coeff).power_dyn(*_num2_p),_ex1));
        }
        
        GINAC_ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
@@ -858,10 +862,13 @@ 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) {
+       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;
        }