]> www.ginac.de Git - ginac.git/blobdiff - ginac/ncmul.cpp
Added complex conjugation methods and GiNaC function "conjugate".
[ginac.git] / ginac / ncmul.cpp
index dd0c7bdc07d0fee84c03ade49504b13375c16a3c..a00bff1e385f2016228f4dd850ce55757a93263f 100644 (file)
@@ -3,7 +3,7 @@
  *  Implementation of GiNaC's non-commutative products of expressions. */
 
 /*
- *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2004 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 "add.h"
 #include "mul.h"
 #include "matrix.h"
-#include "print.h"
 #include "archive.h"
 #include "utils.h"
 
 namespace GiNaC {
 
-GINAC_IMPLEMENT_REGISTERED_CLASS(ncmul, exprseq)
+GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(ncmul, exprseq,
+  print_func<print_context>(&ncmul::do_print).
+  print_func<print_tree>(&basic::do_print_tree).
+  print_func<print_csrc>(&ncmul::do_print_csrc).
+  print_func<print_python_repr>(&ncmul::do_print_csrc))
+
 
 //////////
 // default constructor
@@ -85,7 +89,7 @@ ncmul::ncmul(const exvector & v, bool discardable) : inherited(v,discardable)
        tinfo_key = TINFO_ncmul;
 }
 
-ncmul::ncmul(exvector * vp) : inherited(vp)
+ncmul::ncmul(std::auto_ptr<exvector> vp) : inherited(vp)
 {
        tinfo_key = TINFO_ncmul;
 }
@@ -102,26 +106,15 @@ DEFAULT_ARCHIVING(ncmul)
 
 // public
 
-void ncmul::print(const print_context & c, unsigned level) const
+void ncmul::do_print(const print_context & c, unsigned level) const
 {
-       if (is_a<print_tree>(c)) {
-
-               inherited::print(c, level);
-
-       } else if (is_a<print_csrc>(c) || is_a<print_python_repr>(c)) {
-
-               c.s << class_name() << "(";
-               exvector::const_iterator it = seq.begin(), itend = seq.end()-1;
-               while (it != itend) {
-                       it->print(c, precedence());
-                       c.s << ",";
-                       it++;
-               }
-               it->print(c, precedence());
-               c.s << ")";
+       printseq(c, '(', '*', ')', precedence(), level);
+}
 
-       } else
-               printseq(c, '(', '*', ')', precedence(), level);
+void ncmul::do_print_csrc(const print_context & c, unsigned level) const
+{
+       c.s << class_name();
+       printseq(c, '(', ',', ')', precedence(), precedence());
 }
 
 bool ncmul::info(unsigned inf) const
@@ -427,7 +420,7 @@ ex ncmul::eval(int level) const
 ex ncmul::evalm() const
 {
        // Evaluate children first
-       exvector *s = new exvector;
+       std::auto_ptr<exvector> s(new exvector);
        s->reserve(seq.size());
        exvector::const_iterator it = seq.begin(), itend = seq.end();
        while (it != itend) {
@@ -446,7 +439,6 @@ ex ncmul::evalm() const
                        prod = prod.mul(ex_to<matrix>(*it));
                        it++;
                }
-               delete s;
                return prod;
        }
 
@@ -459,11 +451,30 @@ ex ncmul::thiscontainer(const exvector & v) const
        return (new ncmul(v))->setflag(status_flags::dynallocated);
 }
 
-ex ncmul::thiscontainer(exvector * vp) const
+ex ncmul::thiscontainer(std::auto_ptr<exvector> vp) const
 {
        return (new ncmul(vp))->setflag(status_flags::dynallocated);
 }
 
+ex ncmul::conjugate() const
+{
+       if (return_type() != return_types::noncommutative) {
+               return exprseq::conjugate();
+       }
+
+       if (return_type_tinfo() & 0xffffff00U != TINFO_clifford) {
+               return exprseq::conjugate();
+       }
+
+       exvector ev;
+       ev.reserve(nops());
+       for (const_iterator i=end(); i!=begin();) {
+               --i;
+               ev.push_back(i->conjugate());
+       }
+       return (new ncmul(ev, true))->setflag(status_flags::dynallocated).eval();
+}
+
 // protected
 
 /** Implementation of ex::diff() for a non-commutative product. It applies