]> www.ginac.de Git - ginac.git/commitdiff
- subs() can be used to substitute functions, tensors and indexed objects
authorChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 5 Apr 2001 19:45:05 +0000 (19:45 +0000)
committerChristian Bauer <Christian.Bauer@uni-mainz.de>
Thu, 5 Apr 2001 19:45:05 +0000 (19:45 +0000)
- op(0) of an idx object returns the index value; a nice side-effect of
  this is that idx'es no longer all have the same hash value
- added checks for clifford class

14 files changed:
check/Makefile.am
check/exam_clifford.cpp [new file with mode: 0644]
check/exams.cpp
check/exams.h
check/exams.ref
ginac/basic.cpp
ginac/function.pl
ginac/idx.cpp
ginac/idx.h
ginac/indexed.cpp
ginac/indexed.h
ginac/symbol.cpp
ginac/tensor.cpp
ginac/tensor.h

index 26ef258475273490b3944ecb3c9c6304b3730072..fb943d0b663e9143e524993bb9d048ff53c4d12b 100644 (file)
@@ -10,7 +10,8 @@ checks_LDADD = ../ginac/libginac.la
 exams_SOURCES = exam_paranoia.cpp exam_numeric.cpp exam_powerlaws.cpp \
   exam_inifcns.cpp exam_differentiation.cpp exam_polygcd.cpp \
   exam_normalization.cpp exam_pseries.cpp exam_matrices.cpp exam_lsolve.cpp \
 exams_SOURCES = exam_paranoia.cpp exam_numeric.cpp exam_powerlaws.cpp \
   exam_inifcns.cpp exam_differentiation.cpp exam_polygcd.cpp \
   exam_normalization.cpp exam_pseries.cpp exam_matrices.cpp exam_lsolve.cpp \
-  exam_indexed.cpp exam_color.cpp exam_misc.cpp exams.cpp exams.h
+  exam_indexed.cpp exam_color.cpp exam_clifford.cpp exam_misc.cpp exams.cpp \
+  exams.h
 exams_LDADD = ../ginac/libginac.la
 
 times_SOURCES = time_dennyfliegner.cpp time_gammaseries.cpp \
 exams_LDADD = ../ginac/libginac.la
 
 times_SOURCES = time_dennyfliegner.cpp time_gammaseries.cpp \
diff --git a/check/exam_clifford.cpp b/check/exam_clifford.cpp
new file mode 100644 (file)
index 0000000..d449119
--- /dev/null
@@ -0,0 +1,85 @@
+/** @file exam_clifford.cpp
+ *
+ *  Here we test GiNaC's Clifford algebra objects. */
+
+/*
+ *  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
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "exams.h"
+
+static unsigned check_equal(const ex &e1, const ex &e2)
+{
+       ex e = e1 - e2;
+       if (!e.is_zero()) {
+               clog << e1 << "-" << e2 << " erroneously returned "
+                    << e << " instead of 0" << endl;
+               return 1;
+       }
+       return 0;
+}
+
+static unsigned check_equal_simplify(const ex &e1, const ex &e2)
+{
+       ex e = simplify_indexed(e1) - e2;
+       if (!e.is_zero()) {
+               clog << "simplify_indexed(" << e1 << ")-" << e2 << " erroneously returned "
+                    << e << " instead of 0" << endl;
+               return 1;
+       }
+       return 0;
+}
+
+static unsigned clifford_check1(void)
+{
+       // checks general identities and contractions
+
+       unsigned result = 0;
+
+       symbol dim("D");
+       varidx mu(symbol("mu"), dim), nu(symbol("nu"), dim);
+       ex e;
+
+       e = dirac_gamma(mu) * dirac_gamma(nu) *
+           dirac_gamma(nu.toggle_variance()) * dirac_gamma(mu.toggle_variance());
+       result += check_equal_simplify(e, pow(dim, 2) * dirac_one());
+
+       e = dirac_gamma(mu) * dirac_gamma(nu) *
+           dirac_gamma(mu.toggle_variance()) * dirac_gamma(nu.toggle_variance());
+       result += check_equal_simplify(e, 2*dim*dirac_one()-pow(dim, 2)*dirac_one());
+
+       return result;
+}
+
+unsigned exam_clifford(void)
+{
+       unsigned result = 0;
+       
+       cout << "examining clifford objects" << flush;
+       clog << "----------clifford objects:" << endl;
+
+       result += clifford_check1();  cout << '.' << flush;
+       
+       if (!result) {
+               cout << " passed " << endl;
+               clog << "(no output)" << endl;
+       } else {
+               cout << " failed " << endl;
+       }
+       
+       return result;
+}
index 429bfc13bde4fcef85d8953c176393971f7caa8c..b4ec8cb4be17ffe1ea11b708a108961ea1504150 100644 (file)
@@ -49,6 +49,7 @@ try { \
        EXAM(lsolve)
        EXAM(indexed)
        EXAM(color)
        EXAM(lsolve)
        EXAM(indexed)
        EXAM(color)
+       EXAM(clifford)
        EXAM(misc)
        
        if (result) {
        EXAM(misc)
        
        if (result) {
index 94ae18a1c0f23088ff6114a2ae0082ca4676054b..ae454e40505e2622d862fdd7a5778555fe3b3fc8 100644 (file)
@@ -40,6 +40,7 @@ unsigned exam_matrices();
 unsigned exam_lsolve();
 unsigned exam_indexed();
 unsigned exam_color();
 unsigned exam_lsolve();
 unsigned exam_indexed();
 unsigned exam_color();
+unsigned exam_clifford();
 unsigned exam_misc();
 
 #endif // ndef EXAMS_H
 unsigned exam_misc();
 
 #endif // ndef EXAMS_H
index 47d34012e3a5c696c8bbb8a924552ca65a6fd77a..d44e9104deef214bedbba653c5e45c05fb107e08 100644 (file)
@@ -22,5 +22,7 @@
 (no output)
 ----------color objects:
 (no output)
 (no output)
 ----------color objects:
 (no output)
+----------clifford objects:
+(no output)
 ----------miscellaneous other things:
 (no output)
 ----------miscellaneous other things:
 (no output)
index fe1d4ea121f1c4bedebd5309fc17058a1b4ca2ad..1b3882590b708f7ea0e7fdb4fd0cfd3eaae17a95 100644 (file)
 #include "symbol.h"
 #include "lst.h"
 #include "ncmul.h"
 #include "symbol.h"
 #include "lst.h"
 #include "ncmul.h"
+#include "idx.h"
+#include "indexed.h"
+#include "tensor.h"
+#include "function.h"
 #include "archive.h"
 #include "utils.h"
 #include "debugmsg.h"
 #include "archive.h"
 #include "utils.h"
 #include "debugmsg.h"
@@ -462,11 +466,11 @@ ex basic::expand(unsigned options) const
 
 // public
 
 
 // public
 
-/** Substitute symbols in expression and return the result as a new expression.
- *  There are two valid types of replacement arguments: 1) a relational like
- *  symbol==ex and 2) a list of relationals lst(symbol1==ex1,symbol2==ex2,...),
- *  which is converted to subs(lst(symbol1,symbol2,...),lst(ex1,ex2,...)).
- *  In addition, an object of class idx can be used instead of a symbol. */
+/** Substitute objects (symbols, indices, tensors, functions, indexed) in
+ *  expression and return the result as a new expression.  There are two
+ *  valid types of replacement arguments: 1) a relational like object==ex
+ *  and 2) a list of relationals lst(object1==ex1,object2==ex2,...), which
+ *  is converted to subs(lst(object1,object2,...),lst(ex1,ex2,...)). */
 ex basic::subs(const ex & e) const
 {
        if (e.info(info_flags::relation_equal)) {
 ex basic::subs(const ex & e) const
 {
        if (e.info(info_flags::relation_equal)) {
@@ -481,13 +485,15 @@ ex basic::subs(const ex & e) const
                if (!e.op(i).info(info_flags::relation_equal)) {
                        throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
                }
                if (!e.op(i).info(info_flags::relation_equal)) {
                        throw(std::invalid_argument("basic::subs(ex): argument must be a list or equations"));
                }
-               if (!e.op(i).op(0).info(info_flags::symbol)) {
-                       if (!e.op(i).op(0).info(info_flags::idx)) {
-                               throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol or an idx"));
-                       }
+               ex s = e.op(i).op(0);
+               ex r = e.op(i).op(1);
+               if (!is_ex_of_type(s, symbol) && !is_ex_of_type(s, idx) &&
+                   !is_ex_of_type(s, tensor) && !is_ex_of_type(s, function) &&
+                       !is_ex_of_type(s, indexed)) {
+                       throw(std::invalid_argument("basic::subs(ex): lhs must be a symbol, idx, tensor, function or indexed"));
                }
                }
-               ls.append(e.op(i).op(0));
-               lr.append(e.op(i).op(1));
+               ls.append(s);
+               lr.append(r);
        }
        return subs(ls,lr);
 }
        }
        return subs(ls,lr);
 }
index b36df872337b225da8534ed867741009e6d92347..f6e7b33276ea306d6bd6c7df7d2a46d26962c539 100755 (executable)
@@ -345,6 +345,7 @@ public:
        ex evalf(int level=0) const;
        unsigned calchash(void) const;
        ex series(const relational & r, int order, unsigned options = 0) const;
        ex evalf(int level=0) const;
        unsigned calchash(void) const;
        ex series(const relational & r, int order, unsigned options = 0) const;
+       ex subs(const lst & ls, const lst & lr) const;
        ex thisexprseq(const exvector & v) const;
        ex thisexprseq(exvector * vp) const;
 protected:
        ex thisexprseq(const exvector & v) const;
        ex thisexprseq(exvector * vp) const;
 protected:
@@ -821,6 +822,19 @@ ${series_switch_statement}
        throw(std::logic_error("function::series(): invalid nparams"));
 }
 
        throw(std::logic_error("function::series(): invalid nparams"));
 }
 
+ex function::subs(const lst & ls, const lst & lr) const
+{
+       GINAC_ASSERT(ls.nops() == lr.nops());
+
+       for (unsigned i=0; i<ls.nops(); i++) {
+               if (is_ex_of_type(ls.op(i), function) &&
+                   compare_same_type(ex_to_function(ls.op(i)))==0)
+                       return lr.op(i);
+       }
+
+       return inherited::subs(ls, lr);
+}
+
 // protected
 
 
 // protected
 
 
index d8e8e70c6368b4a3f6116f1cfbe0d58be1f6a384..0a8686de4593a06e61b2b252226e0809026c5edb 100644 (file)
@@ -29,6 +29,8 @@
 #include "utils.h"
 #include "debugmsg.h"
 
 #include "utils.h"
 #include "debugmsg.h"
 
+#include "exprseq.h" // !!
+
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
 namespace GiNaC {
 
 GINAC_IMPLEMENT_REGISTERED_CLASS(idx, basic)
@@ -182,6 +184,18 @@ bool idx::info(unsigned inf) const
        return inherited::info(inf);
 }
 
        return inherited::info(inf);
 }
 
+unsigned idx::nops() const
+{
+       // don't count the dimension as that is not really a sub-expression
+       return 1;
+}
+
+ex & idx::let_op(int i)
+{
+       GINAC_ASSERT(i == 0);
+       return value;
+}
+
 /** Returns order relation between two indices of the same type. The order
  *  must be such that dummy indices lie next to each other. */
 int idx::compare_same_type(const basic & other) const
 /** Returns order relation between two indices of the same type. The order
  *  must be such that dummy indices lie next to each other. */
 int idx::compare_same_type(const basic & other) const
@@ -225,6 +239,7 @@ ex idx::subs(const lst & ls, const lst & lr) const
                        // Otherwise substitute value
                        idx *i_copy = static_cast<idx *>(duplicate());
                        i_copy->value = lr.op(i);
                        // Otherwise substitute value
                        idx *i_copy = static_cast<idx *>(duplicate());
                        i_copy->value = lr.op(i);
+                       i_copy->clearflag(status_flags::hash_calculated);
                        return i_copy->setflag(status_flags::dynallocated);
                }
        }
                        return i_copy->setflag(status_flags::dynallocated);
                }
        }
@@ -236,6 +251,7 @@ ex idx::subs(const lst & ls, const lst & lr) const
 
        idx *i_copy = static_cast<idx *>(duplicate());
        i_copy->value = subsed_value;
 
        idx *i_copy = static_cast<idx *>(duplicate());
        i_copy->value = subsed_value;
+       i_copy->clearflag(status_flags::hash_calculated);
        return i_copy->setflag(status_flags::dynallocated);
 }
 
        return i_copy->setflag(status_flags::dynallocated);
 }
 
index aa7f94e1fdf3fa89ae8846e96de0b04db5855854..a54ad7c339c59347dfcda870cd5bad47ecceef18 100644 (file)
@@ -51,6 +51,8 @@ public:
        void printtree(std::ostream & os, unsigned indent) const;
        void print(std::ostream & os, unsigned upper_precedence=0) const;
        bool info(unsigned inf) const;
        void printtree(std::ostream & os, unsigned indent) const;
        void print(std::ostream & os, unsigned upper_precedence=0) const;
        bool info(unsigned inf) const;
+       unsigned nops() const;
+       ex & let_op(int i);
 protected:
        ex subs(const lst & ls, const lst & lr) const;
 
 protected:
        ex subs(const lst & ls, const lst & lr) const;
 
index 833d46c94111db070e6acf0ee34c6e8120e5f9e1..c530c1f4479e4d1e012208c77c40cf6b143a29c0 100644 (file)
@@ -28,6 +28,7 @@
 #include "mul.h"
 #include "ncmul.h"
 #include "power.h"
 #include "mul.h"
 #include "ncmul.h"
 #include "power.h"
+#include "lst.h"
 #include "archive.h"
 #include "utils.h"
 #include "debugmsg.h"
 #include "archive.h"
 #include "utils.h"
 #include "debugmsg.h"
@@ -368,6 +369,19 @@ ex indexed::coeff(const ex & s, int n) const
                return n==0 ? ex(*this) : _ex0();
 }
 
                return n==0 ? ex(*this) : _ex0();
 }
 
+ex indexed::subs(const lst & ls, const lst & lr) const
+{
+       GINAC_ASSERT(ls.nops() == lr.nops());
+
+       for (unsigned i=0; i<ls.nops(); i++) {
+               if (is_ex_of_type(ls.op(i), indexed) &&
+                   compare_same_type(ex_to_indexed(ls.op(i)))==0)
+                       return lr.op(i);
+       }
+
+       return inherited::subs(ls, lr);
+}
+
 ex indexed::thisexprseq(const exvector & v) const
 {
        return indexed(symmetry, v);
 ex indexed::thisexprseq(const exvector & v) const
 {
        return indexed(symmetry, v);
index e98375d38165d56610fe1e72a111e8412f705711..5b3497820eec60b1e37b44f3ee18e4c19b04b2a6 100644 (file)
@@ -159,6 +159,7 @@ public:
        int degree(const ex & s) const;
        int ldegree(const ex & s) const;
        ex coeff(const ex & s, int n = 1) const;
        int degree(const ex & s) const;
        int ldegree(const ex & s) const;
        ex coeff(const ex & s, int n = 1) const;
+       ex subs(const lst & ls, const lst & lr) const;
        exvector get_free_indices(void) const;
 
 protected:
        exvector get_free_indices(void) const;
 
 protected:
index b905a9a9346c5f75ee871a95b49059c19717ebfa..8547ffefede2f3059f9f38ec6eabd7496c42ac83 100644 (file)
@@ -223,7 +223,7 @@ ex symbol::eval(int level) const
 
 ex symbol::subs(const lst & ls, const lst & lr) const
 {
 
 ex symbol::subs(const lst & ls, const lst & lr) const
 {
-       GINAC_ASSERT(ls.nops()==lr.nops());
+       GINAC_ASSERT(ls.nops() == lr.nops());
 
        for (unsigned i=0; i<ls.nops(); i++) {
                if (is_ex_exactly_of_type(ls.op(i),symbol)) {
 
        for (unsigned i=0; i<ls.nops(); i++) {
                if (is_ex_exactly_of_type(ls.op(i),symbol)) {
index 414225d23d27294b17d86120ec2e12a9a6fb3f81..b53d6f7d0edb519a8ddee3ed6f5bd02855547971 100644 (file)
@@ -27,6 +27,7 @@
 #include "idx.h"
 #include "indexed.h"
 #include "relational.h"
 #include "idx.h"
 #include "indexed.h"
 #include "relational.h"
+#include "lst.h"
 #include "numeric.h"
 #include "archive.h"
 #include "utils.h"
 #include "numeric.h"
 #include "archive.h"
 #include "utils.h"
@@ -165,6 +166,18 @@ DEFAULT_PRINT(tensmetric, "g")
 DEFAULT_PRINT(minkmetric, "eta")
 DEFAULT_PRINT(tensepsilon, "eps")
 
 DEFAULT_PRINT(minkmetric, "eta")
 DEFAULT_PRINT(tensepsilon, "eps")
 
+ex tensor::subs(const lst & ls, const lst & lr) const
+{
+       GINAC_ASSERT(ls.nops() == lr.nops());
+
+       for (unsigned i=0; i<ls.nops(); i++) {
+               if (is_ex_of_type(ls.op(i), tensor) &&
+                   compare_same_type(ex_to_tensor(ls.op(i)))==0)
+                       return lr.op(i);
+       }
+       return *this;
+}
+
 /** Automatic symbolic evaluation of an indexed delta tensor. */
 ex tensdelta::eval_indexed(const basic & i) const
 {
 /** Automatic symbolic evaluation of an indexed delta tensor. */
 ex tensdelta::eval_indexed(const basic & i) const
 {
index 65c86c45bd5c703407980f390683f2a017f57a92..ac991d5f666f5e47d621e1829bdf5f3bd4b2c048 100644 (file)
@@ -40,6 +40,8 @@ protected:
        tensor(unsigned ti);
 
        // functions overriding virtual functions from bases classes
        tensor(unsigned ti);
 
        // functions overriding virtual functions from bases classes
+public:
+       ex subs(const lst & ls, const lst & lr) const;
 protected:
        unsigned return_type(void) const { return return_types::noncommutative_composite; }
 };
 protected:
        unsigned return_type(void) const { return return_types::noncommutative_composite; }
 };