expand_indexed expands the base expression
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 28 Jan 2004 19:34:55 +0000 (19:34 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Wed, 28 Jan 2004 19:34:55 +0000 (19:34 +0000)
ginac/indexed.cpp

index 0bb34d8a578a4f42315aaf7b0d13a854b74cefe8..40a6232c1ecdcb02f5f678acd616b79d21b76138 100644 (file)
@@ -325,20 +325,24 @@ ex indexed::expand(unsigned options) const
 {
        GINAC_ASSERT(seq.size() > 0);
 
-       if ((options & expand_options::expand_indexed) && is_exactly_a<add>(seq[0])) {
-
-               // expand_indexed expands (a+b).i -> a.i + b.i
-               const ex & base = seq[0];
-               ex sum = _ex0;
-               for (size_t i=0; i<base.nops(); i++) {
+       if (options & expand_options::expand_indexed) {
+               ex newbase = seq[0].expand(options);
+               if (is_exactly_a<add>(newbase)) {
+                       ex sum = _ex0;
+                       for (size_t i=0; i<newbase.nops(); i++) {
+                               exvector s = seq;
+                               s[0] = newbase.op(i);
+                               sum += thiscontainer(s).expand(options);
+                       }
+                       return sum;
+               }
+               if (!are_ex_trivially_equal(newbase, seq[0])) {
                        exvector s = seq;
-                       s[0] = base.op(i);
-                       sum += thiscontainer(s).expand();
+                       s[0] = newbase;
+                       return ex_to<indexed>(thiscontainer(s)).inherited::expand(options);
                }
-               return sum;
-
-       } else
-               return inherited::expand(options);
+       }
+       return inherited::expand(options);
 }
 
 //////////