]> www.ginac.de Git - ginac.git/blobdiff - ginac/ncmul.cpp
- Changed policy: look for cln/cln.h instead of CLN/cln.h, reflecting an
[ginac.git] / ginac / ncmul.cpp
index 7d2364e2e6107650473707347fa48b524087786a..d7d97b957fa7d517abd10804c4bdb74e44df53c3 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's non-commutative products of expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 #include "ex.h"
 #include "add.h"
 #include "mul.h"
+#include "archive.h"
 #include "debugmsg.h"
+#include "utils.h"
 
+#ifndef NO_GINAC_NAMESPACE
 namespace GiNaC {
+#endif // ndef NO_GINAC_NAMESPACE
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
 
 //////////
 // default constructor, destructor, copy constructor assignment operator and helpers
@@ -70,12 +76,12 @@ ncmul const & ncmul::operator=(ncmul const & other)
 
 void ncmul::copy(ncmul const & other)
 {
-    exprseq::copy(other);
+    inherited::copy(other);
 }
 
 void ncmul::destroy(bool call_parent)
 {
-    if (call_parent) exprseq::destroy(call_parent);
+    if (call_parent) inherited::destroy(call_parent);
 }
 
 //////////
@@ -85,28 +91,28 @@ void ncmul::destroy(bool call_parent)
 // public
 
 ncmul::ncmul(ex const & lh, ex const & rh) :
-    exprseq(lh,rh)
+    inherited(lh,rh)
 {
     debugmsg("ncmul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
 
 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3) :
-    exprseq(f1,f2,f3)
+    inherited(f1,f2,f3)
 {
     debugmsg("ncmul constructor from 3 ex",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
 
 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
-      ex const & f4) : exprseq(f1,f2,f3,f4)
+      ex const & f4) : inherited(f1,f2,f3,f4)
 {
     debugmsg("ncmul constructor from 4 ex",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
 
 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
-      ex const & f4, ex const & f5) : exprseq(f1,f2,f3,f4,f5)
+      ex const & f4, ex const & f5) : inherited(f1,f2,f3,f4,f5)
 {
     debugmsg("ncmul constructor from 5 ex",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
@@ -114,23 +120,46 @@ ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
 
 ncmul::ncmul(ex const & f1, ex const & f2, ex const & f3,
       ex const & f4, ex const & f5, ex const & f6) :
-    exprseq(f1,f2,f3,f4,f5,f6)
+    inherited(f1,f2,f3,f4,f5,f6)
 {
     debugmsg("ncmul constructor from 6 ex",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
 
-ncmul::ncmul(exvector const & v, bool discardable) : exprseq(v,discardable)
+ncmul::ncmul(exvector const & v, bool discardable) : inherited(v,discardable)
 {
     debugmsg("ncmul constructor from exvector,bool",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
 
-ncmul::ncmul(exvector * vp) : exprseq(vp)
+ncmul::ncmul(exvector * vp) : inherited(vp)
 {
     debugmsg("ncmul constructor from exvector *",LOGLEVEL_CONSTRUCT);
     tinfo_key = TINFO_ncmul;
 }
+
+//////////
+// archiving
+//////////
+
+/** Construct object from archive_node. */
+ncmul::ncmul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
+{
+    debugmsg("ncmul constructor from archive_node", LOGLEVEL_CONSTRUCT);
+}
+
+/** Unarchive the object. */
+ex ncmul::unarchive(const archive_node &n, const lst &sym_lst)
+{
+    return (new ncmul(n, sym_lst))->setflag(status_flags::dynallocated);
+}
+
+/** Archive the object. */
+void ncmul::archive(archive_node &n) const
+{
+    inherited::archive(n);
+}
+
     
 //////////
 // functions overriding virtual functions from bases classes
@@ -144,6 +173,39 @@ basic * ncmul::duplicate() const
     return new ncmul(*this);
 }
 
+void ncmul::print(ostream & os, unsigned upper_precedence) const
+{
+    debugmsg("ncmul print",LOGLEVEL_PRINT);
+    printseq(os,'(','%',')',precedence,upper_precedence);
+}
+
+void ncmul::printraw(ostream & os) const
+{
+    debugmsg("ncmul printraw",LOGLEVEL_PRINT);
+
+    os << "%(";
+    for (exvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
+        (*it).bp->printraw(os);
+        os << ",";
+    }
+    os << ",hash=" << hashvalue << ",flags=" << flags;
+    os << ")";
+}
+
+void ncmul::printcsrc(ostream & os, unsigned upper_precedence) const
+{
+    debugmsg("ncmul print csrc",LOGLEVEL_PRINT);
+    exvector::const_iterator it;
+    exvector::const_iterator itend = seq.end()-1;
+    os << "ncmul(";
+    for (it=seq.begin(); it!=itend; ++it) {
+        (*it).bp->printcsrc(os,precedence);
+        os << ",";
+    }
+    (*it).bp->printcsrc(os,precedence);
+    os << ")";
+}
+
 bool ncmul::info(unsigned inf) const
 {
     throw(std::logic_error("which flags have to be implemented in ncmul::info()?"));
@@ -198,7 +260,7 @@ ex ncmul::expand(unsigned options) const
         exvector term;
         term=expanded_seq;
         for (l=0; l<number_of_adds; l++) {
-            ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
+            GINAC_ASSERT(is_ex_exactly_of_type(expanded_seq[positions_of_adds[l]],add));
             add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]]);
             term[positions_of_adds[l]]=addref.recombine_pair_to_ex(addref.seq[k[l]]);
         }
@@ -267,7 +329,7 @@ ex ncmul::coeff(symbol const & s, int const n) const
 
     if (coeff_found) return (new ncmul(coeffseq,1))->setflag(status_flags::dynallocated);
     
-    return exZERO();
+    return _ex0();
 }
 
 unsigned ncmul::count_factors(ex const & e) const
@@ -275,9 +337,9 @@ unsigned ncmul::count_factors(ex const & e) const
     if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
         (is_ex_exactly_of_type(e,ncmul))) {
         unsigned factors=0;
-        for (int i=0; i<e.nops(); i++) {
+        for (unsigned i=0; i<e.nops(); i++)
             factors += count_factors(e.op(i));
-        }
+        
         return factors;
     }
     return 1;
@@ -287,9 +349,9 @@ void ncmul::append_factors(exvector & v, ex const & e) const
 {
     if ((is_ex_exactly_of_type(e,mul)&&(e.return_type()!=return_types::commutative))||
         (is_ex_exactly_of_type(e,ncmul))) {
-        for (int i=0; i<e.nops(); i++) {
+        for (unsigned i=0; i<e.nops(); i++)
             append_factors(v,e.op(i));
-        }
+        
         return;
     }
     v.push_back(e);
@@ -339,7 +401,7 @@ ex ncmul::eval(int level) const
     if (assocseq.size()==1) return *(seq.begin());
 
     // ncmul() -> 1
-    if (assocseq.size()==0) return exONE();
+    if (assocseq.size()==0) return _ex1();
 
     // determine return types
     unsignedvector rettypes;
@@ -364,7 +426,7 @@ ex ncmul::eval(int level) const
         }
         ++i;
     }
-    ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
+    GINAC_ASSERT(count_commutative+count_noncommutative+count_noncommutative_composite==assocseq.size());
 
     // ncmul(...,c1,...,c2,...) ->
     //     *(c1,c2,ncmul(...)) (pull out commutative elements)
@@ -391,7 +453,7 @@ ex ncmul::eval(int level) const
     if (count_noncommutative_composite==0) {
         // there are neither commutative nor noncommutative_composite
         // elements in assocseq
-        ASSERT(count_commutative==0);
+        GINAC_ASSERT(count_commutative==0);
 
         exvectorvector evv;
         unsignedvector rttinfos;
@@ -416,15 +478,15 @@ ex ncmul::eval(int level) const
             }
         }
 
-#ifdef DOASSERT
-        ASSERT(evv.size()==rttinfos.size());
-        ASSERT(evv.size()>0);
+#ifdef DO_GINAC_ASSERT
+        GINAC_ASSERT(evv.size()==rttinfos.size());
+        GINAC_ASSERT(evv.size()>0);
         unsigned s=0;
         for (i=0; i<evv.size(); ++i) {
             s += evv[i].size();
         }
-        ASSERT(s==assocseq.size());
-#endif // def DOASSERT
+        GINAC_ASSERT(s==assocseq.size());
+#endif // def DO_GINAC_ASSERT
         
         // if all elements are of same type, simplify the string
         if (evv.size()==1) {
@@ -478,7 +540,7 @@ ex ncmul::thisexprseq(exvector * vp) const
 
 int ncmul::compare_same_type(basic const & other) const
 {
-    return exprseq::compare_same_type(other);
+    return inherited::compare_same_type(other);
 }
 
 unsigned ncmul::return_type(void) const
@@ -509,7 +571,7 @@ unsigned ncmul::return_type(void) const
         }
     }
     // all factors checked
-    ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
+    GINAC_ASSERT(!all_commutative); // not all factors should commute, because this is a ncmul();
     return all_commutative ? return_types::commutative : return_types::noncommutative;
 }
    
@@ -583,7 +645,7 @@ ex nonsimplified_ncmul(exvector const & v)
 ex simplified_ncmul(exvector const & v)
 {
     if (v.size()==0) {
-        return exONE();
+        return _ex1();
     } else if (v.size()==1) {
         return v[0];
     }
@@ -591,4 +653,6 @@ ex simplified_ncmul(exvector const & v)
                                    status_flags::evaluated);
 }
 
+#ifndef NO_GINAC_NAMESPACE
 } // namespace GiNaC
+#endif // ndef NO_GINAC_NAMESPACE