static ex log_expand(const ex & arg, unsigned options) { if ((options & expand_options::expand_transcendental) && is_exactly_a<mul>(arg) && !arg.info(info_flags::indefinite)) { exvector sumseq; exvector prodseq; sumseq.reserve(arg.nops()); prodseq.reserve(arg.nops()); bool possign=true; // searching for positive/negative factors for (const_iterator i = arg.begin(); i != arg.end(); ++i) { ex e; if (options & expand_options::expand_function_args) e=i->expand(options); else e=*i; if (e.info(info_flags::positive)) sumseq.push_back(log(e)); else if (e.info(info_flags::negative)) { sumseq.push_back(log(-e)); possign = !possign; } else prodseq.push_back(e); } if (sumseq.size() > 0) { ex newarg; if (options & expand_options::expand_function_args) newarg=((possign?_ex1:_ex_1)*mul(prodseq)).expand(options); else { newarg=(possign?_ex1:_ex_1)*mul(prodseq); ex_to<basic>(newarg).setflag(status_flags::purely_indefinite); } return add(sumseq)+log(newarg); } else { if (!(options & expand_options::expand_function_args)) ex_to<basic>(arg).setflag(status_flags::purely_indefinite); } } if (options & expand_options::expand_function_args) return log(arg.expand(options)).hold(); else return log(arg).hold(); }
void Cancellator::convert(const ex &poly, Polynomial *result) { const unsigned numSymbols(ginacSymbols->size()); vector<ex> toSort; if (!is_a<add>(poly)) { toSort.push_back(poly); } else { for (const_iterator i = poly.begin(); i != poly.end(); i++) { toSort.push_back(*i); } } struct CompCf compCf; compCf.symbols = ginacSymbols; sort(toSort.begin(), toSort.end(), compCf); delete[] result->monomials; for (unsigned termNr(0); termNr < result->numTerms; termNr++) { mpz_clear(result->coefficients[termNr]); } delete[] result->coefficients; result->numTerms = toSort.size(); result->monomials = new unsigned[result->numTerms * numSymbols]; result->coefficients = new mpz_t[result->numTerms]; for (unsigned termNr(0); termNr < result->numTerms; termNr++) { for (unsigned symNr(0); symNr < numSymbols; symNr++) { const int degr(degree(toSort[termNr], (*ginacSymbols)[symNr])); result->monomials[termNr * numSymbols + symNr] = degr; } ex coefff(1); if (is_a<numeric>(toSort[termNr])) { coefff = toSort[termNr]; } else if (is_a<mul>(toSort[termNr])) { for (const_iterator i = toSort[termNr].begin(); i != toSort[termNr].end(); i++) { if (is_a<numeric>(*i)) { coefff = *i; } } } stringstream sstream; sstream << coefff; std::string coeffStr = sstream.str(); mpz_class coeffMpz(coeffStr); mpz_init(result->coefficients[termNr]); mpz_set(result->coefficients[termNr], coeffMpz.get_mpz_t()); } }
static void collect_vargs(ex_collect_priv_t& ec, ex e, const exvector& vars) { e = e.expand(); if (e.is_zero()) { ec.clear(); return; } if (!is_a<add>(e)) { collect_term(ec, e, vars); return; } for (const_iterator i = e.begin(); i != e.end(); ++i) collect_term(ec, *i, vars); wipe_out_zeros(ec); }
static ex abs_expand(const ex & arg, unsigned options) { if ((options & expand_options::expand_transcendental) && is_exactly_a<mul>(arg)) { exvector prodseq; prodseq.reserve(arg.nops()); for (const_iterator i = arg.begin(); i != arg.end(); ++i) { if (options & expand_options::expand_function_args) prodseq.push_back(abs(i->expand(options))); else prodseq.push_back(abs(*i)); } return dynallocate<mul>(prodseq).setflag(status_flags::expanded); } if (options & expand_options::expand_function_args) return abs(arg.expand(options)).hold(); else return abs(arg).hold(); }