]> www.ginac.de Git - ginac.git/blobdiff - ginac/lorentzidx.cpp
* New funny timing added: compute an antipode in Yukawa theory.
[ginac.git] / ginac / lorentzidx.cpp
index af35a457d5fa22a668d67aec3d4b473c64cec353..783316c48b9f173547abcdd16cc1d70ac5baf948 100644 (file)
@@ -1,8 +1,9 @@
 /** @file lorentzidx.cpp
  *
 /** @file lorentzidx.cpp
  *
- *  Implementation of GiNaC's lorentz indices.
- *
- *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ *  Implementation of GiNaC's Lorentz indices. */
+
+/*
+ *  GiNaC Copyright (C) 1999-2001 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
  *
  *  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 <stdexcept>
 
 
 #include <stdexcept>
 
-#include "ginac.h"
+#include "lorentzidx.h"
+#include "lst.h"
+#include "symbol.h"
+#include "archive.h"
 #include "utils.h"
 #include "utils.h"
+#include "debugmsg.h"
+
+namespace GiNaC {
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(lorentzidx, idx)
 
 //////////
 // default constructor, destructor, copy constructor assignment operator and helpers
 
 //////////
 // default constructor, destructor, copy constructor assignment operator and helpers
 
 lorentzidx::lorentzidx() : orthogonal_only(false), dim_parallel_space(0)
 {
 
 lorentzidx::lorentzidx() : orthogonal_only(false), dim_parallel_space(0)
 {
-    debugmsg("lorentzidx default constructor",LOGLEVEL_CONSTRUCT);
-    // serial is incremented in idx::idx()
-    name="mu"+ToString(serial);
-    tinfo_key=TINFO_LORENTZIDX;
-}
-
-lorentzidx::~lorentzidx() 
-{
-    debugmsg("lorentzidx destructor",LOGLEVEL_DESTRUCT);
-    destroy(0);
-}
-
-lorentzidx::lorentzidx(lorentzidx const & other)
-{
-    debugmsg("lorentzidx copy constructor",LOGLEVEL_CONSTRUCT);
-    copy(other);
-}
-
-lorentzidx const & lorentzidx::operator=(lorentzidx const & other)
-{
-    debugmsg("lorentzidx operator=",LOGLEVEL_ASSIGNMENT);
-    if (this != &other) {
-        destroy(1);
-        copy(other);
-    }
-    return *this;
+       debugmsg("lorentzidx default constructor",LOGLEVEL_CONSTRUCT);
+       // serial is incremented in idx::idx()
+       name = "mu" + ToString(serial);
+       tinfo_key = TINFO_lorentzidx;
 }
 
 // protected
 
 }
 
 // protected
 
-void lorentzidx::copy(lorentzidx const & other)
+void lorentzidx::copy(const lorentzidx & other)
 {
 {
-    idx::copy(other);
-    orthogonal_only=other.orthogonal_only;
-    dim_parallel_space=other.dim_parallel_space;
+       inherited::copy(other);
+       orthogonal_only=other.orthogonal_only;
+       dim_parallel_space=other.dim_parallel_space;
 }
 
 void lorentzidx::destroy(bool call_parent)
 {
 }
 
 void lorentzidx::destroy(bool call_parent)
 {
-    if (call_parent) idx::destroy(call_parent);
+       if (call_parent) inherited::destroy(call_parent);
 }
 
 //////////
 }
 
 //////////
@@ -80,141 +67,202 @@ void lorentzidx::destroy(bool call_parent)
 
 // public
 
 
 // public
 
-lorentzidx::lorentzidx(bool cov, bool oonly, unsigned dimp) :
-    idx(cov), orthogonal_only(oonly), dim_parallel_space(dimp)
+/** Construct symbolic Lorentz index, using an automatically generated unique name.
+ *
+ *  @param cov Index is covariant (contravariant otherwise)
+ *  @param oonly Index only lives in orthogonal space
+ *  @param dimp Dimension of parallel space
+ *  @return newly constructed index */
+lorentzidx::lorentzidx(bool cov, bool oonly, unsigned dimp)
+  : idx(cov), orthogonal_only(oonly), dim_parallel_space(dimp)
 {
 {
-    debugmsg("lorentzidx constructor from bool",LOGLEVEL_CONSTRUCT);
-    // serial is incremented in idx::idx(bool)
-    if (oonly) {
-        name="muorth"+ToString(serial);
-    } else {
-        name="mu"+ToString(serial);
-    }
-    tinfo_key=TINFO_LORENTZIDX;
+       debugmsg("lorentzidx constructor from bool,bool,unsigned",LOGLEVEL_CONSTRUCT);
+       // serial is incremented in idx::idx(bool)
+       if (oonly) {
+               name="muorth"+ToString(serial);
+       } else {
+               name="mu"+ToString(serial);
+       }
+       tinfo_key=TINFO_lorentzidx;
 }
 
 }
 
-lorentzidx::lorentzidx(string const & n, bool cov, bool oonly, unsigned dimp)
-    : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
+/** Construct symbolic Lorentz index with specified name.
+ *
+ *  @param n Symbolic index name
+ *  @param cov Index is covariant (contravariant otherwise)
+ *  @param oonly Index only lives in orthogonal space
+ *  @param dimp Dimension of parallel space
+ *  @return newly constructed index */
+lorentzidx::lorentzidx(const std::string & n, bool cov, bool oonly, unsigned dimp)
+  : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
 {
 {
-    debugmsg("lorentzidx constructor from string,bool,bool,unsigned",
-             LOGLEVEL_CONSTRUCT);
-    tinfo_key=TINFO_LORENTZIDX;
+       debugmsg("lorentzidx constructor from string,bool,bool,unsigned",
+                        LOGLEVEL_CONSTRUCT);
+       tinfo_key=TINFO_lorentzidx;
 }
 
 }
 
-lorentzidx::lorentzidx(char const * n, bool cov, bool oonly, unsigned dimp)
-    : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
+/** Construct symbolic Lorentz index with specified name.
+ *
+ *  @param n Symbolic index name
+ *  @param cov Index is covariant (contravariant otherwise)
+ *  @param oonly Index only lives in orthogonal space
+ *  @param dimp Dimension of parallel space
+ *  @return newly constructed index */
+lorentzidx::lorentzidx(const char * n, bool cov, bool oonly, unsigned dimp)
+  : idx(n,cov), orthogonal_only(oonly), dim_parallel_space(dimp)
 {
 {
-    debugmsg("lorentzidx constructor from char*,bool,bool,unsigned",
-             LOGLEVEL_CONSTRUCT);
-    tinfo_key=TINFO_LORENTZIDX;
+       debugmsg("lorentzidx constructor from char*,bool,bool,unsigned",
+                        LOGLEVEL_CONSTRUCT);
+       tinfo_key=TINFO_lorentzidx;
 }
 
 }
 
-lorentzidx::lorentzidx(unsigned const v, bool cov) : idx(v,cov),
-    orthogonal_only(false), dim_parallel_space(0)
+/** Construct numeric Lorentz index with specified value.
+ *
+ *  @param v Numeric index value
+ *  @param cov Index is covariant (contravariant otherwise)
+ *  @return newly constructed index */
+lorentzidx::lorentzidx(unsigned v, bool cov)
+  : idx(v,cov), orthogonal_only(false), dim_parallel_space(0)
 {
 {
-    debugmsg("lorentzidx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
-    tinfo_key=TINFO_LORENTZIDX;
+       debugmsg("lorentzidx constructor from unsigned,bool",LOGLEVEL_CONSTRUCT);
+       tinfo_key=TINFO_lorentzidx;
 }
 
 //////////
 }
 
 //////////
-// functions overriding virtual functions from bases classes
+// archiving
 //////////
 
 //////////
 
-// public
-
-basic * lorentzidx::duplicate() const
+/** Construct object from archive_node. */
+lorentzidx::lorentzidx(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
 {
 {
-    debugmsg("lorentzidx duplicate",LOGLEVEL_DUPLICATE);
-    return new lorentzidx(*this);
+       debugmsg("lorentzidx constructor from archive_node", LOGLEVEL_CONSTRUCT);
+       n.find_bool("orthogonal_only", orthogonal_only);
+       n.find_unsigned("pdim", dim_parallel_space);
 }
 
 }
 
-void lorentzidx::printraw(ostream & os) const
+/** Unarchive the object. */
+ex lorentzidx::unarchive(const archive_node &n, const lst &sym_lst)
 {
 {
-    debugmsg("lorentzidx printraw",LOGLEVEL_PRINT);
-
-    os << "lorentzidx(";
-
-    if (symbolic) {
-        os << "symbolic,name=" << name;
-    } else {
-        os << "non symbolic,value=" << value;
-    }
-
-    if (covariant) {
-        os << ",covariant";
-    } else {
-        os << ",contravariant";
-    }
-
-    if (orthogonal_only) {
-        os << ",only orthogonal components at " << dim_parallel_space
-           << " parallel dimensions";
-    } else {
-        os << ",parallel and orthogonal components";
-    }
-
-    os << ",serial=" << serial;
-    os << ",hash=" << hashvalue << ",flags=" << flags;
-    os << ")";
+       ex s = (new lorentzidx(n, sym_lst))->setflag(status_flags::dynallocated);
+
+       if (ex_to_lorentzidx(s).symbolic) {
+               // If lorentzidx is in sym_lst, return the existing lorentzidx
+               for (unsigned i=0; i<sym_lst.nops(); i++) {
+                       if (is_ex_of_type(sym_lst.op(i), lorentzidx) && (ex_to_lorentzidx(sym_lst.op(i)).name == ex_to_lorentzidx(s).name))
+                               return sym_lst.op(i);
+               }
+       }
+       return s;
 }
 
 }
 
-void lorentzidx::printtree(ostream & os, unsigned indent) const
+/** Archive the object. */
+void lorentzidx::archive(archive_node &n) const
 {
 {
-    debugmsg("lorentzidx printtree",LOGLEVEL_PRINT);
-
-    os << string(indent,' ') << "lorentzidx: ";
-
-    if (symbolic) {
-        os << "symbolic,name=" << name;
-    } else {
-        os << "non symbolic,value=" << value;
-    }
+       inherited::archive(n);
+       n.add_bool("orthogonal_only", orthogonal_only);
+       n.add_unsigned("pdim", dim_parallel_space);
+}
 
 
-    if (covariant) {
-        os << ",covariant";
-    } else {
-        os << ",contravariant";
-    }
+//////////
+// functions overriding virtual functions from bases classes
+//////////
 
 
-    if (orthogonal_only) {
-        os << ",only orthogonal components at " << dim_parallel_space
-           << " parallel dimensions";
-    } else {
-        os << ",parallel and orthogonal components";
-    }
+// public
 
 
-    os << ", serial=" << serial
-       << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
-       << ", flags=" << flags << endl;
+void lorentzidx::printraw(std::ostream & os) const
+{
+       debugmsg("lorentzidx printraw",LOGLEVEL_PRINT);
+
+       os << "lorentzidx(";
+
+       if (symbolic) {
+               os << "symbolic,name=" << name;
+       } else {
+               os << "non symbolic,value=" << value;
+       }
+
+       if (covariant) {
+               os << ",covariant";
+       } else {
+               os << ",contravariant";
+       }
+
+       if (orthogonal_only) {
+               os << ",only orthogonal components at " << dim_parallel_space
+                  << " parallel dimensions";
+       } else {
+               os << ",parallel and orthogonal components";
+       }
+
+       os << ",serial=" << serial;
+       os << ",hash=" << hashvalue << ",flags=" << flags;
+       os << ")";
 }
 
 }
 
-void lorentzidx::print(ostream & os, unsigned upper_precedence) const
+void lorentzidx::printtree(std::ostream & os, unsigned indent) const
 {
 {
-    debugmsg("lorentzidx print",LOGLEVEL_PRINT);
+       debugmsg("lorentzidx printtree",LOGLEVEL_PRINT);
+
+       os << std::string(indent,' ') << "lorentzidx: ";
+
+       if (symbolic) {
+               os << "symbolic,name=" << name;
+       } else {
+               os << "non symbolic,value=" << value;
+       }
+
+       if (covariant) {
+               os << ",covariant";
+       } else {
+               os << ",contravariant";
+       }
+
+       if (orthogonal_only) {
+               os << ",only orthogonal components at " << dim_parallel_space
+                  << " parallel dimensions";
+       } else {
+               os << ",parallel and orthogonal components";
+       }
+
+       os << ", serial=" << serial
+          << ", hash=" << hashvalue
+          << " (0x" << std::hex << hashvalue << std::dec << ")"
+          << ", flags=" << flags << std::endl;
+}
 
 
-    if (covariant) {
-        os << "_";
-    } else {
-        os << "~";
-    }
-    if (symbolic) {
-        os << name;
-    } else {
-        os << value;
-    }
+void lorentzidx::print(std::ostream & os, unsigned upper_precedence) const
+{
+       debugmsg("lorentzidx print",LOGLEVEL_PRINT);
+
+       if (covariant) {
+               os << "_";
+       } else {
+               os << "~";
+       }
+       if (symbolic) {
+               os << name;
+       } else {
+               os << value;
+       }
 }
 
 bool lorentzidx::info(unsigned inf) const
 {
 }
 
 bool lorentzidx::info(unsigned inf) const
 {
-    if (inf==info_flags::lorentzidx) return true;
-    return idx::info(inf);
+       if (inf==info_flags::lorentzidx) return true;
+       return inherited::info(inf);
 }
 
 }
 
-//////////
-// new virtual functions which can be overridden by derived classes
-//////////
-
-// none
+int lorentzidx::compare_same_type(const basic & other) const
+{
+       GINAC_ASSERT(is_of_type(other, lorentzidx));
+       const lorentzidx &o = static_cast<const lorentzidx &>(other);
+
+       if (orthogonal_only != o.orthogonal_only)
+               return orthogonal_only ? -1 : 1;
+       if (dim_parallel_space != o.dim_parallel_space)
+               return dim_parallel_space < o.dim_parallel_space ? -1 : 1;
+       return inherited::compare_same_type(other);
+}
 
 //////////
 // non-virtual functions in this class
 
 //////////
 // non-virtual functions in this class
@@ -222,30 +270,28 @@ bool lorentzidx::info(unsigned inf) const
 
 // public
 
 
 // public
 
+/** Create anonymous contravariant copy of a symbolic Lorentz index. */
 lorentzidx lorentzidx::create_anonymous_representative(void) const
 {
 lorentzidx lorentzidx::create_anonymous_representative(void) const
 {
-    ASSERT(is_symbolic());
-    lorentzidx i_copy(*this);
-    i_copy.serial=0;
-    i_copy.name="anonymous_representative";
-    i_copy.covariant=false;
-    i_copy.clearflag(status_flags::dynallocated|
-                     status_flags::hash_calculated);
-    return i_copy;
+       GINAC_ASSERT(is_symbolic());
+       lorentzidx i_copy(*this);
+       i_copy.serial=0;
+       i_copy.name="anonymous_representative";
+       i_copy.covariant=false;
+       i_copy.clearflag(status_flags::dynallocated |
+                        status_flags::hash_calculated);
+       return i_copy;
 }
 
 //////////
 }
 
 //////////
-// static member variables
-//////////
-
-// none
-
-//////////
-// global constants
+// global functions
 //////////
 
 //////////
 
-const lorentzidx some_lorentzidx;
-type_info const & typeid_lorentzidx=typeid(some_lorentzidx);
-
-
+/** Return the global symbol that represents the dimension D of spacetime. */
+ex Dim(void)
+{
+       static symbol *d = new symbol("dim");
+       return *d;
+}
 
 
+} // namespace GiNaC