]> www.ginac.de Git - ginac.git/blobdiff - ginac/ex.cpp
I really think that changes to top-level files should go to ginac-cvs
[ginac.git] / ginac / ex.cpp
index 77c7983300ed10edc670ea076be17a481be8e313..0d0657a52302df50ca2092c288fb56560695be41 100644 (file)
@@ -29,8 +29,8 @@
 #include "ncmul.h"
 #include "numeric.h"
 #include "power.h"
+#include "lst.h"
 #include "relational.h"
-#include "indexed.h"
 #include "input_lexer.h"
 #include "debugmsg.h"
 #include "utils.h"
@@ -43,18 +43,6 @@ namespace GiNaC {
 
 // none (all inlined)
 
-//////////
-// functions overriding virtual functions from bases classes
-//////////
-
-// none
-
-//////////
-// new virtual functions which can be overridden by derived classes
-//////////
-
-// none
-
 //////////
 // non-virtual functions in this class
 //////////
@@ -115,7 +103,7 @@ void ex::dbgprinttree(void) const
 ex ex::expand(unsigned options) const
 {
        GINAC_ASSERT(bp!=0);
-       if (bp->flags & status_flags::expanded)
+       if (options == 0 && (bp->flags & status_flags::expanded)) // The "expanded" flag only covers the standard options; someone might want to re-expand with different options
                return *bp;
        else
                return bp->expand(options);
@@ -136,26 +124,30 @@ ex ex::diff(const symbol & s, unsigned nth) const
                return bp->diff(s, nth);
 }
 
-/** Simplify/canonicalize expression containing indexed objects. This
- *  performs contraction of dummy indices where possible and checks whether
- *  the free indices in sums are consistent.
- *
- *  @return simplified expression */
-ex ex::simplify_indexed(void) const
+/** Check whether expression matches a specified pattern. */
+bool ex::match(const ex & pattern) const
 {
-       return GiNaC::simplify_indexed(*this);
+       lst repl_lst;
+       return bp->match(pattern, repl_lst);
 }
 
-/** Simplify/canonicalize expression containing indexed objects. This
- *  performs contraction of dummy indices where possible, checks whether
- *  the free indices in sums are consistent, and automatically replaces
- *  scalar products by known values if desired.
- *
- *  @param sp Scalar products to be replaced automatically
- *  @return simplified expression */
-ex ex::simplify_indexed(const scalar_products & sp) const
+/** Find all occurrences of a pattern. The found matches are appended to
+ *  the "found" list. If the expression itself matches the pattern, the
+ *  children are not further examined. This function returns true when any
+ *  matches were found. */
+bool ex::find(const ex & pattern, lst & found) const
 {
-       return GiNaC::simplify_indexed(*this, sp);
+       if (match(pattern)) {
+               found.append(*this);
+               found.sort();
+               found.unique();
+               return true;
+       }
+       bool any_found = false;
+       for (unsigned i=0; i<nops(); i++)
+               if (op(i).find(pattern, found))
+                       any_found = true;
+       return any_found;
 }
 
 ex ex::operator[](const ex & index) const
@@ -199,25 +191,6 @@ ex ex::rhs(void) const
        return (*static_cast<relational *>(bp)).rhs();
 }
 
-/** Used internally by operator+() to add two ex objects together. */
-ex ex::exadd(const ex & rh) const
-{
-       return (new add(*this,rh))->setflag(status_flags::dynallocated);
-}
-
-/** Used internally by operator*() to multiply two ex objects together. */
-ex ex::exmul(const ex & rh) const
-{
-       // Check if we are constructing a mul object or a ncmul object.  Due to
-       // ncmul::eval()'s rule to pull out commutative elements we need to check
-       // only one of the elements.
-       if (rh.bp->return_type()==return_types::commutative ||
-           bp->return_type()==return_types::commutative)
-               return (new mul(*this,rh))->setflag(status_flags::dynallocated);
-       else
-               return (new ncmul(*this,rh))->setflag(status_flags::dynallocated);
-}
-
 // private
 
 /** Make this ex writable (if more than one ex handle the same basic) by 
@@ -241,14 +214,12 @@ void ex::makewriteable()
  *  @see ex::ex(const basic &) */
 void ex::construct_from_basic(const basic & other)
 {
-       if ((other.flags & status_flags::evaluated)==0) {
-               // cf. copy ctor
+       if (!(other.flags & status_flags::evaluated)) {
                const ex & tmpex = other.eval(1); // evaluate only one (top) level
                bp = tmpex.bp;
-               GINAC_ASSERT(bp!=0);
                GINAC_ASSERT(bp->flags & status_flags::dynallocated);
                ++bp->refcount;
-               if ((other.flags & status_flags::dynallocated)&&(other.refcount==0))
+               if ((other.refcount==0) && (other.flags & status_flags::dynallocated))
                        delete &const_cast<basic &>(other);
        } else {
                if (other.flags & status_flags::dynallocated) {