[GiNaC-devel] is_zero_matrix()

Vladimir Kisil kisilv at maths.leeds.ac.uk
Fri Apr 28 16:17:07 CEST 2006


		Dear All,

		I propose to add is_zero_matrix() method to matrix and ex
  classes. From the tutorial:

		   The method `matrix::is_zero_matrix()' returns `true' only if all
		   entries of the matrix are zeros. There is also method
		   `ex::is_zero_matrix()' which returns `true' only if the expression is
		   zero or a zero matrix.

  The (cumulative) patch is attached.

  Best wishes,
  Vladimir
-- 
Vladimir V. Kisil     email: kisilv at maths.leeds.ac.uk
--                      www: http://maths.leeds.ac.uk/~kisilv/

-------------- next part --------------
/home/amsta/kisilv/GiNaC
Index: debian/changelog
===================================================================
RCS file: /home/cvs/GiNaC/debian/changelog,v
retrieving revision 1.36
diff -u -r1.36 changelog
--- debian/changelog	20 Apr 2006 22:59:39 -0000	1.36
+++ debian/changelog	28 Apr 2006 14:13:50 -0000
@@ -3,7 +3,7 @@
   * New upstream release; binary incompatible, so it's libginac1.4 now.
   * debian/*: Streamlining by Peter Eisentraut <petere at debian.org>.
 
- -- Richard Kreckel <kreckel at ginac.de>  <DATE>
+ -- Richard Kreckel <kreckel at ginac.de>  Mon, 17 Apr 2006 22:25:48 +0200
 
 ginac (1.3.4-1) unstable; urgency=low
 
Index: doc/tutorial/ginac.texi
===================================================================
RCS file: /home/cvs/GiNaC/doc/tutorial/ginac.texi,v
retrieving revision 1.185
diff -u -r1.185 ginac.texi
--- doc/tutorial/ginac.texi	22 Apr 2006 15:42:57 -0000	1.185
+++ doc/tutorial/ginac.texi	28 Apr 2006 14:13:51 -0000
@@ -2053,6 +2053,12 @@
 @}
 @end example
 
+ at cindex @code{is_zero_matrix()} 
+The method @code{matrix::is_zero_matrix()} returns @code{true} only if
+all entries of the matrix are zeros. There is also method
+ at code{ex::is_zero_matrix()} which returns @code{true} only if the
+expression is zero or a zero matrix.
+
 @cindex @code{transpose()}
 There are three ways to do arithmetic with matrices. The first (and most
 direct one) is to use the methods provided by the @code{matrix} class:
@@ -3545,9 +3551,10 @@
 the matrix @samp{M = [[a, b], [c, d]]}. The parameter @code{G} defines
 the metric of the surrounding (pseudo-)Euclidean space. This can be an
 indexed object, tensormetric, matrix or a Clifford unit, in the later
-case the optional parameters @code{rl} and @code{anticommuting} are ignored
-even if supplied.  The returned value of this function is a list of
-components of the resulting vector.
+case the optional parameters @code{rl} and @code{anticommuting} are
+ignored even if supplied.  Depending from the type of @code{v} the
+returned value of this function is either a vector or a list holding vector's
+components.
 
 @cindex @code{clifford_max_label()}
 Finally the function
@@ -4097,7 +4104,8 @@
 @end example
 
 for checking whether one expression is equal to another, or equal to zero,
-respectively.
+respectively. See also the method @code{ex::is_zero_matrix()}, 
+ at pxref{Matrices}. 
 
 
 @subsection Ordering expressions
Index: ginac/clifford.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/clifford.cpp,v
retrieving revision 1.95
diff -u -r1.95 clifford.cpp
--- ginac/clifford.cpp	22 Feb 2006 14:52:07 -0000	1.95
+++ ginac/clifford.cpp	28 Apr 2006 14:13:51 -0000
@@ -1227,7 +1227,7 @@
 				else
 					throw(std::invalid_argument("lst_to_clifford(): dimensions of vector and clifford unit mismatch"));
 			} else
-				throw(std::invalid_argument("lst_to_clifford(): first argument should be a vector vector"));
+				throw(std::invalid_argument("lst_to_clifford(): first argument should be a vector (nx1 or 1xn matrix)"));
 		} else if (is_a<lst>(v)) {
 			if (dim == ex_to<lst>(v).nops())
 				return indexed(matrix(dim, 1, ex_to<lst>(v)), ex_to<varidx>(mu).toggle_variance()) * e;
@@ -1352,8 +1352,8 @@
 	}
 	
 	x = lst_to_clifford(v, cu); 
-	ex e = simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d)));
-	return clifford_to_lst(e, cu, false);
+	ex e = clifford_to_lst(simplify_indexed(canonicalize_clifford((a * x + b) * clifford_inverse(c * x + d))), cu, false);
+	return (is_a<matrix>(v) ? matrix(ex_to<matrix>(v).rows(), ex_to<matrix>(v).cols(), ex_to<lst>(e)) : e);
 }
 
 ex clifford_moebius_map(const ex & M, const ex & v, const ex & G, unsigned char rl, bool anticommuting)
Index: ginac/ex.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/ex.h,v
retrieving revision 1.87
diff -u -r1.87 ex.h
--- ginac/ex.h	21 Apr 2006 12:16:01 -0000	1.87
+++ ginac/ex.h	28 Apr 2006 14:13:51 -0000
@@ -205,6 +205,7 @@
 	int compare(const ex & other) const;
 	bool is_equal(const ex & other) const;
 	bool is_zero() const { extern const ex _ex0; return is_equal(_ex0); }
+	bool is_zero_matrix() const;
 	
 	// symmetry
 	ex symmetrize() const;
Index: ginac/ex.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/ex.cpp,v
retrieving revision 1.60
diff -u -r1.60 ex.cpp
--- ginac/ex.cpp	27 Mar 2006 15:13:22 -0000	1.60
+++ ginac/ex.cpp	28 Apr 2006 14:13:51 -0000
@@ -28,6 +28,7 @@
 #include "mul.h"
 #include "ncmul.h"
 #include "numeric.h"
+#include "matrix.h"
 #include "power.h"
 #include "lst.h"
 #include "relational.h"
@@ -254,6 +255,17 @@
 		return bp->is_polynomial(vars);
 }
 
+/** Check whether expression is zero or zero matrix. */
+bool ex::is_zero_matrix() const
+{
+	if (is_zero())
+		return  true;
+	else {
+		ex e = evalm();
+		return is_a<matrix>(e) && ex_to<matrix>(e).is_zero_matrix();
+	}
+}
+
 // private
 
 /** Make this ex writable (if more than one ex handle the same basic) by 
Index: ginac/matrix.h
===================================================================
RCS file: /home/cvs/GiNaC/ginac/matrix.h,v
retrieving revision 1.71
diff -u -r1.71 matrix.h
--- ginac/matrix.h	21 Apr 2006 12:16:01 -0000	1.71
+++ ginac/matrix.h	28 Apr 2006 14:13:51 -0000
@@ -151,6 +151,7 @@
 	matrix solve(const matrix & vars, const matrix & rhs,
 	             unsigned algo = solve_algo::automatic) const;
 	unsigned rank() const;
+	bool is_zero_matrix() const;
 protected:
 	ex determinant_minor() const;
 	int gauss_elimination(const bool det = false);
Index: ginac/matrix.cpp
===================================================================
RCS file: /home/cvs/GiNaC/ginac/matrix.cpp,v
retrieving revision 1.108
diff -u -r1.108 matrix.cpp
--- ginac/matrix.cpp	21 Apr 2006 12:16:01 -0000	1.108
+++ ginac/matrix.cpp	28 Apr 2006 14:13:51 -0000
@@ -1527,6 +1527,16 @@
 	return k;
 }
 
+/** Function to check that all elements of the matrix are zero.
+ */
+bool matrix::is_zero_matrix() const
+{
+	bool result = true;
+	for (exvector::const_iterator i=m.begin(); i!=m.end(); ++i) 
+		result = result && (*i).is_zero();
+	return result;
+}
+
 ex lst_to_matrix(const lst & l)
 {
 	lst::const_iterator itr, itc;


More information about the GiNaC-devel mailing list