get_free_indices() returns wrong result
Hello! This simple program: #include <iostream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; int main(int argc, char** argv) { symbol A("A"); symbol i_sym("i"); idx i(i_sym, 3); ex test = indexed(A, i)*indexed(A,i); exprseq free_ind = test.get_free_indices(); cout << "Free indices of expression " << test << ": "; cout << free_ind << endl; return 0; } gives: Free indices of expression (A.i)^2: (.i) but not just an empty sequence, as I expected. This patch seems to fix the inconsistency: diff -u -r GiNaC-orig/ginac/indexed.cpp GiNaC/ginac/indexed.cpp --- GiNaC-orig/ginac/indexed.cpp 2004-06-10 02:19:36.000000000 +0400 +++ GiNaC/ginac/indexed.cpp 2004-07-26 22:31:52.000000000 +0400 @@ -499,10 +499,31 @@ return free_indices; } +// checks if index is symbolic and is_exactly_a<idx> +struct is_a_symb_idx : public std::unary_function<ex, bool> { + bool operator()(const ex & e) + { + return is_exactly_a<idx>(e) && ex_to<idx>(e).is_symbolic(); + } +}; + exvector power::get_free_indices() const { - // Return free indices of basis - return basis.get_free_indices(); + exvector tmp_inds = basis.get_free_indices(); + + if (exponent.info(info_flags::even)) { + // if the exponent is an even number, then any "free" index + // of base is actually a dummy index, if it + // 1) has no variance, + // 2) is not a numeric index + exvector really_free; + remove_copy_if(tmp_inds.begin(), tmp_inds.end(), + back_inserter(really_free), is_a_symb_idx()); + return really_free; + } else { + return tmp_inds; + } + } /** Rename dummy indices in an expression.
Hi! On Mon, Jul 26, 2004 at 10:44:25PM +0400, Sheplyakov Alexei wrote:
Free indices of expression (A.i)^2: (.i) but not just an empty sequence, as I expected.
This patch seems to fix the inconsistency: [...]
Well spotted. Thanks for the patch! :) Bye, Christian -- / Physics is an algorithm \/ http://www.uni-mainz.de/~bauec002/
participants (2)
-
Christian Bauer
-
varg@thsun1.jinr.ru