From f8c2455fbbd34dbbfb72ac96eec0f45fd453bade Mon Sep 17 00:00:00 2001 From: Richard Kreckel Date: Wed, 6 Jan 2016 22:34:23 +0100 Subject: [PATCH] Avoid x^0 and Order(x^0) terms together in series expansion. At the branch cut, the series expansions of log(), atan(), and atanh() assembled pseries objects which contained both x^0 and Order(x^0) terms. This patch removes the extra x^0 term if the order is 0. It also adds a GINAC_ASSERT for these kinds of invariants to the pseries ctor and simplifies the loops in pseries' degree(), ldegree(), eval() and evalf() member functions. --- ginac/inifcns_trans.cpp | 24 +++++++--- ginac/pseries.cpp | 104 +++++++++++++++++++++------------------- 2 files changed, 73 insertions(+), 55 deletions(-) diff --git a/ginac/inifcns_trans.cpp b/ginac/inifcns_trans.cpp index 99828539..98e0caff 100644 --- a/ginac/inifcns_trans.cpp +++ b/ginac/inifcns_trans.cpp @@ -265,8 +265,12 @@ static ex log_series(const ex &arg, const ex &point = rel.rhs(); const symbol foo; const ex replarg = series(log(arg), s==foo, order).subs(foo==point, subs_options::no_pattern); - epvector seq { expair(-I*csgn(arg*I)*Pi, _ex0), - expair(Order(_ex1), order) }; + epvector seq; + if (order > 0) { + seq.reserve(2); + seq.push_back(expair(-I*csgn(arg*I)*Pi, _ex0)); + } + seq.push_back(expair(Order(_ex1), order)); return series(replarg - I*Pi + pseries(rel, std::move(seq)), rel, order); } throw do_taylor(); // caught by function::series() @@ -928,8 +932,12 @@ static ex atan_series(const ex &arg, Order0correction += log((I*arg_pt+_ex_1)/(I*arg_pt+_ex1))*I*_ex_1_2; else Order0correction += log((I*arg_pt+_ex1)/(I*arg_pt+_ex_1))*I*_ex1_2; - epvector seq { expair(Order0correction, _ex0), - expair(Order(_ex1), order) }; + epvector seq; + if (order > 0) { + seq.reserve(2); + seq.push_back(expair(Order0correction, _ex0)); + } + seq.push_back(expair(Order(_ex1), order)); return series(replarg - pseries(rel, std::move(seq)), rel, order); } throw do_taylor(); @@ -1534,8 +1542,12 @@ static ex atanh_series(const ex &arg, Order0correction += log((arg_pt+_ex_1)/(arg_pt+_ex1))*_ex1_2; else Order0correction += log((arg_pt+_ex1)/(arg_pt+_ex_1))*_ex_1_2; - epvector seq { expair(Order0correction, _ex0), - expair(Order(_ex1), order) }; + epvector seq; + if (order > 0) { + seq.reserve(2); + seq.push_back(expair(Order0correction, _ex0)); + } + seq.push_back(expair(Order(_ex1), order)); return series(replarg - pseries(rel, std::move(seq)), rel, order); } throw do_taylor(); diff --git a/ginac/pseries.cpp b/ginac/pseries.cpp index 12f4fede..2d0b7adc 100644 --- a/ginac/pseries.cpp +++ b/ginac/pseries.cpp @@ -71,6 +71,19 @@ pseries::pseries() { } pseries::pseries(const ex &rel_, const epvector &ops_) : seq(ops_) { +#ifdef DO_GINAC_ASSERT + auto i = seq.begin(); + while (i != seq.end()) { + auto ip1 = i+1; + if (ip1 != seq.end()) + GINAC_ASSERT(!is_order_function(i->rest)); + else + break; + GINAC_ASSERT(is_a(i->coeff)); + GINAC_ASSERT(ex_to(i->coeff) < ex_to(ip1->coeff)); + ++i; + } +#endif // def DO_GINAC_ASSERT GINAC_ASSERT(is_a(rel_)); GINAC_ASSERT(is_a(rel_.lhs())); point = rel_.rhs(); @@ -79,6 +92,19 @@ pseries::pseries(const ex &rel_, const epvector &ops_) pseries::pseries(const ex &rel_, epvector &&ops_) : seq(std::move(ops_)) { +#ifdef DO_GINAC_ASSERT + auto i = seq.begin(); + while (i != seq.end()) { + auto ip1 = i+1; + if (ip1 != seq.end()) + GINAC_ASSERT(!is_order_function(i->rest)); + else + break; + GINAC_ASSERT(is_a(i->coeff)); + GINAC_ASSERT(ex_to(i->coeff) < ex_to(ip1->coeff)); + ++i; + } +#endif // def DO_GINAC_ASSERT GINAC_ASSERT(is_a(rel_)); GINAC_ASSERT(is_a(rel_.lhs())); point = rel_.rhs(); @@ -290,25 +316,17 @@ ex pseries::op(size_t i) const * series is examined termwise. */ int pseries::degree(const ex &s) const { - if (var.is_equal(s)) { - // Return last exponent - if (seq.size()) - return ex_to((seq.end()-1)->coeff).to_int(); - else - return 0; - } else { - epvector::const_iterator it = seq.begin(), itend = seq.end(); - if (it == itend) - return 0; - int max_pow = std::numeric_limits::min(); - while (it != itend) { - int pow = it->rest.degree(s); - if (pow > max_pow) - max_pow = pow; - ++it; - } - return max_pow; - } + if (seq.empty()) + return 0; + + if (var.is_equal(s)) + // Return last/greatest exponent + return ex_to((seq.end()-1)->coeff).to_int(); + + int max_pow = std::numeric_limits::min(); + for (auto & it : seq) + max_pow = std::max(max_pow, it.rest.degree(s)); + return max_pow; } /** Return degree of lowest power of the series. This is usually the exponent @@ -318,25 +336,17 @@ int pseries::degree(const ex &s) const * I.e.: (1-x) + (1-x)^2 + Order((1-x)^3) has ldegree(x) 1, not 0. */ int pseries::ldegree(const ex &s) const { - if (var.is_equal(s)) { - // Return first exponent - if (seq.size()) - return ex_to((seq.begin())->coeff).to_int(); - else - return 0; - } else { - epvector::const_iterator it = seq.begin(), itend = seq.end(); - if (it == itend) - return 0; - int min_pow = std::numeric_limits::max(); - while (it != itend) { - int pow = it->rest.ldegree(s); - if (pow < min_pow) - min_pow = pow; - ++it; - } - return min_pow; - } + if (seq.empty()) + return 0; + + if (var.is_equal(s)) + // Return first/smallest exponent + return ex_to((seq.begin())->coeff).to_int(); + + int min_pow = std::numeric_limits::max(); + for (auto & it : seq) + min_pow = std::min(min_pow, it.rest.degree(s)); + return min_pow; } /** Return coefficient of degree n in power series if s is the expansion @@ -389,15 +399,13 @@ ex pseries::eval() const if (flags & status_flags::evaluated) { return *this; } - + // Construct a new series with evaluated coefficients epvector new_seq; new_seq.reserve(seq.size()); - epvector::const_iterator it = seq.begin(), itend = seq.end(); - while (it != itend) { - new_seq.push_back(expair(it->rest, it->coeff)); - ++it; - } + for (auto & it : seq) + new_seq.push_back(expair(it.rest, it.coeff)); + return dynallocate(relational(var,point), std::move(new_seq)).setflag(status_flags::evaluated); } @@ -413,11 +421,9 @@ ex pseries::evalf(int level) const // Construct a new series with evaluated coefficients epvector new_seq; new_seq.reserve(seq.size()); - epvector::const_iterator it = seq.begin(), itend = seq.end(); - while (it != itend) { - new_seq.push_back(expair(it->rest.evalf(level-1), it->coeff)); - ++it; - } + for (auto & it : seq) + new_seq.push_back(expair(it.rest, it.coeff)); + return dynallocate(relational(var,point), std::move(new_seq)).setflag(status_flags::evaluated); } -- 2.44.0