- Apparently, in ~ 30% of calls to mul::expand the expression is monomial.
authorJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Mon, 15 Oct 2007 23:40:30 +0000 (23:40 +0000)
committerJens Vollinga <vollinga@thep.physik.uni-mainz.de>
Mon, 15 Oct 2007 23:40:30 +0000 (23:40 +0000)
Expanding monomials should be done as fast as possible [Sheplyakov].

ginac/mul.cpp
ginac/power.cpp

index db8b9f14acd308d76344ed57bfc44857f63e4d24..298715b33fd2eabfd28ea9390229fb3f5b90b39d 100644 (file)
@@ -34,6 +34,7 @@
 #include "lst.h"
 #include "archive.h"
 #include "utils.h"
+#include "symbol.h"
 #include "compiler.h"
 
 namespace GiNaC {
@@ -992,6 +993,15 @@ bool mul::can_be_further_expanded(const ex & e)
 
 ex mul::expand(unsigned options) const
 {
+       {
+       // trivial case: expanding the monomial (~ 30% of all calls)
+               epvector::const_iterator i = seq.begin(), seq_end = seq.end();
+               while ((i != seq.end()) &&  is_a<symbol>(i->rest) && i->coeff.info(info_flags::integer))
+                       ++i;
+               if (i == seq_end)
+                       return (new mul(*this))->setflag(status_flags::dynallocated | status_flags::expanded);
+       }
+
        const bool skip_idx_rename = ! info(info_flags::has_indices);
        // First, expand the children
        std::auto_ptr<epvector> expanded_seqp = expandchildren(options);
index 10de528d06ad9cf09381b4376f61ff433ca414f6..1b28b509ac4bd35ef4bbcf7777e40069bcf056f2 100644 (file)
@@ -772,6 +772,9 @@ tinfo_t power::return_type_tinfo() const
 
 ex power::expand(unsigned options) const
 {
+       if (is_a<symbol>(basis) && exponent.info(info_flags::integer))
+               return (new power(*this))->setflag(status_flags::dynallocated | status_flags::expanded);
+
        if (options == 0 && (flags & status_flags::expanded))
                return *this;