[GiNaC-list] get_free_indices() returns wrong result

Sheplyakov Alexei varg at thsun1.jinr.ru
Mon Jul 26 20:45:04 CEST 2004


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.



More information about the GiNaC-list mailing list