Dear all, I've tried making ex_to<T> functions [more] type-safe, like this: diff --git a/ginac/ex.h b/ginac/ex.h index 3070b7a..79f872b 100644 --- a/ginac/ex.h +++ b/ginac/ex.h @@ -949,7 +949,8 @@ inline bool is_exactly_a(const ex &obj) template <class T> inline const T &ex_to(const ex &e) { - GINAC_ASSERT(is_a<T>(e)); + if (__builtin_expect(! is_a<T>(e), 0)) + throw std::bad_cast(); return static_cast<const T &>(*e.bp); } and got failure in exam_clifford (to be more specific, last expression in clifford_check4 triggered bad_cast). I've found a simple[r] example which fails in the same way: #include <iostream> #include <stdexcept> #include "ginac.h" using namespace GiNaC; int main(int argc, char** argv) { symbol d("D"); varidx mu(symbol("mu"), d), nu(symbol("nu"), d); ex e = lorentz_g(mu, nu)*dirac_gamma(nu.toggle_variance()); e = e.simplify_indexed(); return 0; } The reason of this error seems to be inconsistency between clifford::op and tensor::replace_contr_index. tensor::replace_contr_index expects any subexpression of `indexed' referenced via op() to be `idx' (see tensor.cpp:398). On the other hand, `clifford' objects make their representation label accessible via op(), so this assumption is incorrect. So, the questions are: 1) Any ideas how to fix this? 2) In general, is it OK for class derived from indexed return something which is *not* idx (or derived from it) via op()? Best regards, Alexei -- All science is either physics or stamp collecting.
Hi, Alexei!
"ASh" == Sheplyakov Alexei <varg@theor.jinr.ru> writes: ASh> expression in clifford_check4 triggered bad_cast). I've found a ASh> simple[r] example which fails in the same way:
Your exam works smoothly on my computer with CVS GiNaC, expression e is evaluated to gamma~mu, is it correct? ASh> 2) In general, is it OK for class derived from indexed return ASh> something which is *not* idx (or derived from it) via op()? One of the reasons why clifford class returns its metric among its ops, I think, because this is required by the subs() method to make symbolic substitution in the metric as well. Best wishes, Vladimir -- Vladimir V. Kisil email: kisilv@maths.leeds.ac.uk -- www: http://maths.leeds.ac.uk/~kisilv/
Hello, Vladimir! On Wed, Oct 18, 2006 at 03:55:35PM +0100, Vladimir Kisil wrote:
Your exam works smoothly on my computer with CVS GiNaC, expression e is evaluated to gamma~mu, is it correct? Have you applied my patch to ex.h?
ASh> 2) In general, is it OK for class derived from indexed return ASh> something which is *not* idx (or derived from it) via op()?
One of the reasons why clifford class returns its metric among its ops, I think, because this is required by the subs() method to make symbolic substitution in the metric as well.
Actually, I think this is perfectly sensible, not only for subs()' sake (one could provide custom implementation of subs). The point of a derived class is to store some additional data. One might want to make that data availiable via op(). However, some code in GiNaC seems to make different assumption (BTW, I hate such *implicit* assumptions). What about the following patch: tensor::replace_contr_index: ignore all non-idx subexpressions. Classes derived from indexed could make their additional data members avialable via op(). Ignore such non-idx subexperssions when performing contractions. --- ginac/tensor.cpp | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-) diff --git a/ginac/tensor.cpp b/ginac/tensor.cpp index c215237..25935f4 100644 --- a/ginac/tensor.cpp +++ b/ginac/tensor.cpp @@ -395,6 +395,8 @@ bool tensor::replace_contr_index(exvecto again: if (self_idx->is_symbolic()) { for (size_t i=1; i<other->nops(); i++) { + if (! is_a<idx>(other->op(i))) + continue; const idx &other_idx = ex_to<idx>(other->op(i)); if (is_dummy_pair(*self_idx, other_idx)) { -- 1.4.2.3 Best regards, Alexei. -- All science is either physics or stamp collecting.
"ASh" == Sheplyakov Alexei <varg@theor.jinr.ru> writes: ASh> Have you applied my patch to ex.h?
No, I did not realise that they should be considered in a conjunction. ASh> What about the following patch: It seems to be most logical resolution of the issue. Best wishes, Vladimir -- Vladimir V. Kisil email: kisilv@maths.leeds.ac.uk -- www: http://maths.leeds.ac.uk/~kisilv/
participants (2)
-
varg@theor.jinr.ru
-
Vladimir Kisil