first = false;
}
recombine_pair_to_ex(*it).print(c, precedence());
- it++;
+ ++it;
}
if (precedence() <= level) {
while (i != end) {
GINAC_ASSERT((!is_ex_exactly_of_type(i->rest, mul)) ||
(!(ex_to<numeric>(i->coeff).is_integer())));
- GINAC_ASSERT(!(cit->is_canonical_numeric()));
+ GINAC_ASSERT(!(i->is_canonical_numeric()));
if (is_ex_exactly_of_type(recombine_pair_to_ex(*i), numeric))
print(print_tree(std::cerr));
GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*i), numeric));
ex mul::expand(unsigned options) const
{
- if (flags & status_flags::expanded)
- return *this;
-
- exvector sub_expanded_seq;
-
+ // First, expand the children
epvector * expanded_seqp = expandchildren(options);
-
- const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
-
+ const epvector & expanded_seq = (expanded_seqp == NULL) ? seq : *expanded_seqp;
+
+ // Now, look for all the factors that are sums and multiply each one out
+ // with the next one that is found while collecting the factors which are
+ // not sums
int number_of_adds = 0;
+ ex last_expanded = _ex1();
epvector non_adds;
non_adds.reserve(expanded_seq.size());
- epvector::const_iterator cit = expanded_seq.begin();
- epvector::const_iterator last = expanded_seq.end();
- ex last_expanded = _ex1();
- while (cit!=last) {
- if (is_ex_exactly_of_type((*cit).rest,add) &&
- ((*cit).coeff.is_equal(_ex1()))) {
+ epvector::const_iterator cit = expanded_seq.begin(), last = expanded_seq.end();
+ while (cit != last) {
+ if (is_ex_exactly_of_type(cit->rest, add) &&
+ (cit->coeff.is_equal(_ex1()))) {
++number_of_adds;
- if (is_ex_exactly_of_type(last_expanded,add)) {
- // expand adds
+ if (is_ex_exactly_of_type(last_expanded, add)) {
const add & add1 = ex_to<add>(last_expanded);
- const add & add2 = ex_to<add>((*cit).rest);
+ const add & add2 = ex_to<add>(cit->rest);
int n1 = add1.nops();
int n2 = add2.nops();
exvector distrseq;
distrseq.reserve(n1*n2);
for (int i1=0; i1<n1; ++i1) {
for (int i2=0; i2<n2; ++i2) {
- distrseq.push_back(add1.op(i1)*add2.op(i2));
+ distrseq.push_back(add1.op(i1) * add2.op(i2));
}
}
- last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
+ last_expanded = (new add(distrseq))->
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
} else {
non_adds.push_back(split_ex_to_pair(last_expanded));
- last_expanded = (*cit).rest;
+ last_expanded = cit->rest;
}
} else {
non_adds.push_back(*cit);
if (expanded_seqp)
delete expanded_seqp;
- if (is_ex_exactly_of_type(last_expanded,add)) {
+ // Now the only remaining thing to do is to multiply the factors which
+ // were not sums into the "last_expanded" sum
+ if (is_ex_exactly_of_type(last_expanded, add)) {
add const & finaladd = ex_to<add>(last_expanded);
exvector distrseq;
int n = finaladd.nops();
for (int i=0; i<n; ++i) {
epvector factors = non_adds;
factors.push_back(split_ex_to_pair(finaladd.op(i)));
- distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
+ distrseq.push_back((new mul(factors, overall_coeff))->
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
}
return ((new add(distrseq))->
- setflag(status_flags::dynallocated | status_flags::expanded));
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
}
non_adds.push_back(split_ex_to_pair(last_expanded));
return (new mul(non_adds, overall_coeff))->
- setflag(status_flags::dynallocated | status_flags::expanded);
+ setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
}