X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fpower.cpp;h=14878a9187f40a588b0ea498336793fd5f1de943;hp=2b6049ba4f3eb90dbf787ddcb6818c6d889de7c1;hb=93e491cf586b2e16854c56a9c71196711b1cd889;hpb=703c6cebb5d3d395437e73e6935f3691aed68e0a diff --git a/ginac/power.cpp b/ginac/power.cpp index 2b6049ba..14878a91 100644 --- a/ginac/power.cpp +++ b/ginac/power.cpp @@ -58,7 +58,7 @@ power::power() : basic(TINFO_power) power::~power() { debugmsg("power destructor",LOGLEVEL_DESTRUCT); - destroy(0); + destroy(false); } power::power(const power & other) @@ -71,7 +71,7 @@ const power & power::operator=(const power & other) { debugmsg("power operator=",LOGLEVEL_ASSIGNMENT); if (this != &other) { - destroy(1); + destroy(true); copy(other); } return *this; @@ -211,9 +211,8 @@ void power::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedenc debugmsg("power print csrc", LOGLEVEL_PRINT); // Integer powers of symbols are printed in a special, optimized way - if (exponent.info(info_flags::integer) && - (is_ex_exactly_of_type(basis, symbol) || - is_ex_exactly_of_type(basis, constant))) { + if (exponent.info(info_flags::integer) + && (is_ex_exactly_of_type(basis, symbol) || is_ex_exactly_of_type(basis, constant))) { int exp = ex_to_numeric(exponent).to_int(); if (exp > 0) os << "("; @@ -283,9 +282,12 @@ ex & power::let_op(int i) int power::degree(const symbol & s) const { if (is_exactly_of_type(*exponent.bp,numeric)) { - if ((*basis.bp).compare(s)==0) - return ex_to_numeric(exponent).to_int(); - else + if ((*basis.bp).compare(s)==0) { + if (ex_to_numeric(exponent).is_integer()) + return ex_to_numeric(exponent).to_int(); + else + return 0; + } else return basis.degree(s) * ex_to_numeric(exponent).to_int(); } return 0; @@ -294,9 +296,12 @@ int power::degree(const symbol & s) const int power::ldegree(const symbol & s) const { if (is_exactly_of_type(*exponent.bp,numeric)) { - if ((*basis.bp).compare(s)==0) - return ex_to_numeric(exponent).to_int(); - else + if ((*basis.bp).compare(s)==0) { + if (ex_to_numeric(exponent).is_integer()) + return ex_to_numeric(exponent).to_int(); + else + return 0; + } else return basis.ldegree(s) * ex_to_numeric(exponent).to_int(); } return 0; @@ -306,17 +311,27 @@ ex power::coeff(const symbol & s, int n) const { if ((*basis.bp).compare(s)!=0) { // basis not equal to s - if (n==0) { + if (n == 0) return *this; - } else { + else return _ex0(); + } else { + // basis equal to s + if (is_exactly_of_type(*exponent.bp, numeric) && ex_to_numeric(exponent).is_integer()) { + // integer exponent + int int_exp = ex_to_numeric(exponent).to_int(); + if (n == int_exp) + return _ex1(); + else + return _ex0(); + } else { + // non-integer exponents are treated as zero + if (n == 0) + return *this; + else + return _ex0(); } - } else if (is_exactly_of_type(*exponent.bp,numeric)&& - (static_cast(*exponent.bp).compare(numeric(n))==0)) { - return _ex1(); } - - return _ex0(); } ex power::eval(int level) const @@ -450,8 +465,7 @@ ex power::eval(int level) const mulp->clearflag(status_flags::evaluated); mulp->clearflag(status_flags::hash_calculated); return (new mul(power(*mulp,exponent), - power(num_coeff,*num_exponent)))-> - setflag(status_flags::dynallocated); + power(num_coeff,*num_exponent)))->setflag(status_flags::dynallocated); } else { GINAC_ASSERT(num_coeff.compare(_num0())<0); if (num_coeff.compare(_num_1())!=0) { @@ -460,8 +474,7 @@ ex power::eval(int level) const mulp->clearflag(status_flags::evaluated); mulp->clearflag(status_flags::hash_calculated); return (new mul(power(*mulp,exponent), - power(abs(num_coeff),*num_exponent)))-> - setflag(status_flags::dynallocated); + power(abs(num_coeff),*num_exponent)))->setflag(status_flags::dynallocated); } } } @@ -532,9 +545,9 @@ ex power::derivative(const symbol & s) const return mul(newseq, exponent); } 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)))); + return mul(*this, + add(mul(exponent.diff(s), log(basis)), + mul(mul(exponent, basis.diff(s)), power(basis, _ex_1())))); } } @@ -573,9 +586,7 @@ ex power::expand(unsigned options) const if (are_ex_trivially_equal(basis,expanded_basis)) { return this->hold(); } else { - return (new power(expanded_basis,exponent))-> - setflag(status_flags::dynallocated | - status_flags::expanded); + return (new power(expanded_basis,exponent))->setflag(status_flags::dynallocated | status_flags::expanded); } } @@ -595,9 +606,7 @@ ex power::expand(unsigned options) const if (are_ex_trivially_equal(basis,expanded_basis)) { return this->hold(); } else { - return (new power(expanded_basis,exponent))-> - setflag(status_flags::dynallocated | - status_flags::expanded); + return (new power(expanded_basis,exponent))->setflag(status_flags::dynallocated | status_flags::expanded); } } @@ -638,12 +647,12 @@ ex power::expand_add(const add & a, int n) const for (l=0; lsetflag(status_flags::dynallocated), - _ex1())); + _ex1())); } } else { if (is_ex_exactly_of_type(r,mul)) { sum.push_back(expair(expand_mul(ex_to_mul(r),_num2()), - ex_to_numeric(c).power_dyn(_num2()))); + ex_to_numeric(c).power_dyn(_num2()))); } else { sum.push_back(expair((new power(r,_ex2()))->setflag(status_flags::dynallocated), - ex_to_numeric(c).power_dyn(_num2()))); + ex_to_numeric(c).power_dyn(_num2()))); } } @@ -758,7 +768,7 @@ ex power::expand_add_2(const add & a) 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().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1)))); } } @@ -774,8 +784,7 @@ 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 | - status_flags::expanded ); + 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 @@ -795,18 +804,16 @@ ex power::expand_mul(const mul & m, const numeric & n) const } 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); } /* ex power::expand_commutative_3(const ex & basis, const numeric & exponent, - unsigned options) const + unsigned options) const { // obsolete @@ -822,12 +829,10 @@ ex power::expand_commutative_3(const ex & basis, const numeric & exponent, int n=exponent.to_int(); for (int k=0; k<=n; k++) { - distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))* - power(last_operand,numeric(n-k))); + 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::expanded | - status_flags::dynallocated )). - expand(options); + return ex((new add(distrseq))->setflag(status_flags::expanded | status_flags::dynallocated)).expand(options); } */ @@ -835,11 +840,11 @@ ex power::expand_commutative_3(const ex & basis, const numeric & exponent, ex power::expand_noncommutative(const ex & basis, const numeric & exponent, unsigned options) const { - ex rest_power=ex(power(basis,exponent.add(_num_1()))). - expand(options | expand_options::internal_do_not_expand_power_operands); + ex rest_power = ex(power(basis,exponent.add(_num_1()))). + expand(options | expand_options::internal_do_not_expand_power_operands); return ex(mul(rest_power,basis),0). - expand(options | expand_options::internal_do_not_expand_mul_operands); + expand(options | expand_options::internal_do_not_expand_mul_operands); } */ @@ -856,7 +861,7 @@ unsigned power::precedence = 60; ////////// const power some_power; -const type_info & typeid_power=typeid(some_power); +const std::type_info & typeid_power=typeid(some_power); // helper function