index 25ffca31d45023f63eaf45374f3002c29714d94d..c4cfe69cb2a93fd3b8afe03f2e571c9a1ba0e34e 100644 (file)
@@ -147,69 +147,69 @@ typedef std::vector<int> intvector;

ex ncmul::expand(unsigned options) const
{
-       exvector sub_expanded_seq;
-
-       exvector expanded_seq=expandchildren(options);
-
-
-       int number_of_expanded_terms=1;
-
-       unsigned current_position=0;
-       exvector::const_iterator last=expanded_seq.end();
+       // First, expand the children
+       exvector expanded_seq = expandchildren(options);
+
+       // Now, look for all the factors that are sums and remember their
+       // position and number of terms. One remark is in order here: we do not
+       // take into account the overall_coeff of the add objects. This is
+       // because in GiNaC, all terms of a sum must be of the same type, so
+       // a non-zero overall_coeff (which can only be numeric) would imply that
+       // the sum only has commutative terms. But then it would never appear
+       // as a factor of an ncmul.
+
+       int number_of_expanded_terms = 1;
+
+       unsigned current_position = 0;
+       exvector::const_iterator last = expanded_seq.end();
for (exvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
}
current_position++;
}

-               return (new ncmul(expanded_seq,1))->setflag(status_flags::dynallocated ||
-                                                                                                       (options == 0 ? status_flags::expanded : 0));
-       }
+       // If there are no sums, we are done
+               return (new ncmul(expanded_seq, true))->
+                       setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));

+       // Now, form all possible products of the terms of the sums with the
+       // remaining factors, and add them together
exvector distrseq;
distrseq.reserve(number_of_expanded_terms);

-       intvector k;
-
-       int l;
-       for (l=0; l<number_of_adds; l++) {
-               k[l]=0;
-       }

-       while (1) {
-               exvector term;
-               term=expanded_seq;
-               for (l=0; l<number_of_adds; l++) {
+       while (true) {
+               exvector term = expanded_seq;
+               for (int i=0; i<number_of_adds; i++) {
}
-               distrseq.push_back((new ncmul(term,1))->setflag(status_flags::dynallocated |
-                                                                                                               (options == 0 ? status_flags::expanded : 0)));
+               distrseq.push_back((new ncmul(term, true))->
+                                   setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));

// increment k[]
-                       k[l]=0;
+               while ((l>=0) && ((++k[l]) >= number_of_add_operands[l])) {
+                       k[l] = 0;
l--;
}
-               if (l<0) break;
+               if (l<0)
+                       break;
}