[GiNaC-devel] [PATCH] clifford_unit: fix possible bugs due to wrong operator[!=]= usage.

Alexei Sheplyakov varg at theor.jinr.ru
Mon Jul 14 15:46:25 CEST 2008


The condition like this one

unsigned n /* = something */;
if (n == ex_to<idx>(mu).get_dim) {
	// do something
}

is *always* true, because == creates an object of type `relational'.
Likewise, this condition

matrix M /* = something */;
if (M(i, j) != M(j, i)) {
	// more code
}

is *always* true too.

Frankly, I've got annoyed by that evil `relational' class. It doesn't do
anything useful and forces everyone to write the code in a very unnatural,
counter-intuitive way. I plan to remove it in the next version of GiNaC (1.5).

---
 ginac/clifford.cpp |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/ginac/clifford.cpp b/ginac/clifford.cpp
index 100954d..4d73d50 100644
--- a/ginac/clifford.cpp
+++ b/ginac/clifford.cpp
@@ -716,6 +716,17 @@ ex dirac_ONE(unsigned char rl)
 	return clifford(ONE, rl);
 }
 
+static unsigned get_dim_uint(const ex& e)
+{
+	if (!is_a<idx>(e))
+		throw std::invalid_argument("get_dim_uint: argument is not an index");
+	ex dim = ex_to<idx>(e).get_dim();
+	if (!dim.info(info_flags::posint))
+		throw std::invalid_argument("get_dim_uint: dimension of index should be a positive integer");
+	unsigned d = ex_to<numeric>(dim).to_int();
+	return d;
+}
+
 ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 {
 	static ex unit = (new cliffordunit)->setflag(status_flags::dynallocated);
@@ -734,10 +745,10 @@ ex clifford_unit(const ex & mu, const ex & metr, unsigned char rl)
 
 		static idx xi((new symbol)->setflag(status_flags::dynallocated), n),
 			chi((new symbol)->setflag(status_flags::dynallocated), n);
-		if ((n ==  M.cols()) && (n == ex_to<idx>(mu).get_dim())) {
+		if ((n ==  M.cols()) && (n == get_dim_uint(mu))) {
 			for (unsigned i = 0; i < n; i++) {
 				for (unsigned j = i+1; j < n; j++) {
-					if (M(i, j) != M(j, i)) {
+					if (!M(i, j).is_equal(M(j, i))) {
 						symmetric = false;
 					}
 				}
@@ -1170,7 +1181,7 @@ ex lst_to_clifford(const ex & v, const ex & e) {
 		ex mu = e.op(1);
 		ex mu_toggle
 			= is_a<varidx>(mu) ? ex_to<varidx>(mu).toggle_variance() : mu;
-		unsigned dim = (ex_to<numeric>(ex_to<idx>(mu).get_dim())).to_int();
+		unsigned dim = get_dim_uint(mu);
 
 		if (is_a<matrix>(v)) {
 			if (ex_to<matrix>(v).cols() > ex_to<matrix>(v).rows()) {
-- 
1.5.6

Best regards,
	Alexei

-- 
All science is either physics or stamp collecting.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-devel/attachments/20080714/3ef0ace3/attachment.sig 


More information about the GiNaC-devel mailing list