]> www.ginac.de Git - ginac.git/blobdiff - ginac/matrix.cpp
- class -> typename, because it might be int in template.
[ginac.git] / ginac / matrix.cpp
index edf2a945fef43153b1460dc78b7290a5e24245db..97b473628464e498a233c691e2d0b724f34e7491 100644 (file)
@@ -2,10 +2,37 @@
  *
  *  Implementation of symbolic matrices */
 
+/*
+ *  GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
 #include <algorithm>
 #include <stdexcept>
 
-#include "ginac.h"
+#include "matrix.h"
+#include "archive.h"
+#include "utils.h"
+#include "debugmsg.h"
+
+#ifndef NO_NAMESPACE_GINAC
+namespace GiNaC {
+#endif // ndef NO_NAMESPACE_GINAC
+
+GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic)
 
 //////////
 // default constructor, destructor, copy constructor, assignment operator
 
 /** Default ctor.  Initializes to 1 x 1-dimensional zero-matrix. */
 matrix::matrix()
-    : basic(TINFO_MATRIX), row(1), col(1)
+    : inherited(TINFO_matrix), row(1), col(1)
 {
     debugmsg("matrix default constructor",LOGLEVEL_CONSTRUCT);
-    m.push_back(exZERO());
+    m.push_back(_ex0());
 }
 
 matrix::~matrix()
@@ -27,13 +54,13 @@ matrix::~matrix()
     debugmsg("matrix destructor",LOGLEVEL_DESTRUCT);
 }
 
-matrix::matrix(matrix const & other)
+matrix::matrix(const matrix & other)
 {
     debugmsg("matrix copy constructor",LOGLEVEL_CONSTRUCT);
     copy(other);
 }
 
-matrix const & matrix::operator=(matrix const & other)
+const matrix & matrix::operator=(const matrix & other)
 {
     debugmsg("matrix operator=",LOGLEVEL_ASSIGNMENT);
     if (this != &other) {
@@ -45,9 +72,9 @@ matrix const & matrix::operator=(matrix const & other)
 
 // protected
 
-void matrix::copy(matrix const & other)
+void matrix::copy(const matrix & other)
 {
-    basic::copy(other);
+    inherited::copy(other);
     row=other.row;
     col=other.col;
     m=other.m;  // use STL's vector copying
@@ -55,7 +82,7 @@ void matrix::copy(matrix const & other)
 
 void matrix::destroy(bool call_parent)
 {
-    if (call_parent) basic::destroy(call_parent);
+    if (call_parent) inherited::destroy(call_parent);
 }
 
 //////////
@@ -68,20 +95,59 @@ void matrix::destroy(bool call_parent)
  *
  *  @param r number of rows
  *  @param c number of cols */
-matrix::matrix(int r, int c)
-    : basic(TINFO_MATRIX), row(r), col(c)
+matrix::matrix(unsigned r, unsigned c)
+    : inherited(TINFO_matrix), row(r), col(c)
 {
-    debugmsg("matrix constructor from int,int",LOGLEVEL_CONSTRUCT);
-    m.resize(r*c, exZERO());
+    debugmsg("matrix constructor from unsigned,unsigned",LOGLEVEL_CONSTRUCT);
+    m.resize(r*c, _ex0());
 }
 
 // protected
 
 /** Ctor from representation, for internal use only. */
-matrix::matrix(int r, int c, vector<ex> const & m2)
-    : basic(TINFO_MATRIX), row(r), col(c), m(m2)
+matrix::matrix(unsigned r, unsigned c, const exvector & m2)
+    : inherited(TINFO_matrix), row(r), col(c), m(m2)
+{
+    debugmsg("matrix constructor from unsigned,unsigned,exvector",LOGLEVEL_CONSTRUCT);
+}
+
+//////////
+// archiving
+//////////
+
+/** Construct object from archive_node. */
+matrix::matrix(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
+{
+    debugmsg("matrix constructor from archive_node", LOGLEVEL_CONSTRUCT);
+    if (!(n.find_unsigned("row", row)) || !(n.find_unsigned("col", col)))
+        throw (std::runtime_error("unknown matrix dimensions in archive"));
+    m.reserve(row * col);
+    for (unsigned int i=0; true; i++) {
+        ex e;
+        if (n.find_ex("m", e, sym_lst, i))
+            m.push_back(e);
+        else
+            break;
+    }
+}
+
+/** Unarchive the object. */
+ex matrix::unarchive(const archive_node &n, const lst &sym_lst)
+{
+    return (new matrix(n, sym_lst))->setflag(status_flags::dynallocated);
+}
+
+/** Archive the object. */
+void matrix::archive(archive_node &n) const
 {
-    debugmsg("matrix constructor from int,int,vector<ex>",LOGLEVEL_CONSTRUCT);
+    inherited::archive(n);
+    n.add_unsigned("row", row);
+    n.add_unsigned("col", col);
+    exvector::const_iterator i = m.begin(), iend = m.end();
+    while (i != iend) {
+        n.add_ex("m", *i);
+        i++;
+    }
 }
 
 //////////
@@ -96,14 +162,56 @@ basic * matrix::duplicate() const
     return new matrix(*this);
 }
 
+void matrix::print(ostream & os, unsigned upper_precedence) const
+{
+    debugmsg("matrix print",LOGLEVEL_PRINT);
+    os << "[[ ";
+    for (unsigned r=0; r<row-1; ++r) {
+        os << "[[";
+        for (unsigned c=0; c<col-1; ++c) {
+            os << m[r*col+c] << ",";
+        }
+        os << m[col*(r+1)-1] << "]], ";
+    }
+    os << "[[";
+    for (unsigned c=0; c<col-1; ++c) {
+        os << m[(row-1)*col+c] << ",";
+    }
+    os << m[row*col-1] << "]] ]]";
+}
+
+void matrix::printraw(ostream & os) const
+{
+    debugmsg("matrix printraw",LOGLEVEL_PRINT);
+    os << "matrix(" << row << "," << col <<",";
+    for (unsigned r=0; r<row-1; ++r) {
+        os << "(";
+        for (unsigned c=0; c<col-1; ++c) {
+            os << m[r*col+c] << ",";
+        }
+        os << m[col*(r-1)-1] << "),";
+    }
+    os << "(";
+    for (unsigned c=0; c<col-1; ++c) {
+        os << m[(row-1)*col+c] << ",";
+    }
+    os << m[row*col-1] << "))";
+}
+
 /** nops is defined to be rows x columns. */
-int matrix::nops() const
+unsigned matrix::nops() const
 {
     return row*col;
 }
 
 /** returns matrix entry at position (i/col, i%col). */
-ex & matrix::let_op(int const i)
+ex matrix::op(int i) const
+{
+    return m[i];
+}
+
+/** returns matrix entry at position (i/col, i%col). */
+ex & matrix::let_op(int i)
 {
     return m[i];
 }
@@ -111,8 +219,8 @@ ex & matrix::let_op(int const i)
 /** expands the elements of a matrix entry by entry. */
 ex matrix::expand(unsigned options) const
 {
-    vector<ex> tmp(row*col);
-    for (int i=0; i<row*col; ++i) {
+    exvector tmp(row*col);
+    for (unsigned i=0; i<row*col; ++i) {
         tmp[i]=m[i].expand(options);
     }
     return matrix(row, col, tmp);
@@ -120,15 +228,15 @@ ex matrix::expand(unsigned options) const
 
 /** Search ocurrences.  A matrix 'has' an expression if it is the expression
  *  itself or one of the elements 'has' it. */
-bool matrix::has(ex const & other) const
+bool matrix::has(const ex & other) const
 {
-    ASSERT(other.bp!=0);
+    GINAC_ASSERT(other.bp!=0);
     
     // tautology: it is the expression itself
     if (is_equal(*other.bp)) return true;
     
     // search all the elements
-    for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
+    for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
         if ((*r).has(other)) return true;
     }
     return false;
@@ -150,10 +258,10 @@ ex matrix::eval(int level) const
     }
     
     // eval() entry by entry
-    vector<ex> m2(row*col);
+    exvector m2(row*col);
     --level;    
-    for (int r=0; r<row; ++r) {
-        for (int c=0; c<col; ++c) {
+    for (unsigned r=0; r<row; ++r) {
+        for (unsigned c=0; c<col; ++c) {
             m2[r*col+c] = m[r*col+c].eval(level);
         }
     }
@@ -178,10 +286,10 @@ ex matrix::evalf(int level) const
     }
     
     // evalf() entry by entry
-    vector<ex> m2(row*col);
+    exvector m2(row*col);
     --level;
-    for (int r=0; r<row; ++r) {
-        for (int c=0; c<col; ++c) {
+    for (unsigned r=0; r<row; ++r) {
+        for (unsigned c=0; c<col; ++c) {
             m2[r*col+c] = m[r*col+c].evalf(level);
         }
     }
@@ -190,10 +298,10 @@ ex matrix::evalf(int level) const
 
 // protected
 
-int matrix::compare_same_type(basic const & other) const
+int matrix::compare_same_type(const basic & other) const
 {
-    ASSERT(is_exactly_of_type(other, matrix));
-    matrix const & o=static_cast<matrix &>(const_cast<basic &>(other));
+    GINAC_ASSERT(is_exactly_of_type(other, matrix));
+    const matrix & o=static_cast<matrix &>(const_cast<basic &>(other));
     
     // compare number of rows
     if (row != o.rows()) {
@@ -207,8 +315,8 @@ int matrix::compare_same_type(basic const & other) const
     
     // equal number of rows and columns, compare individual elements
     int cmpval;
-    for (int r=0; r<row; ++r) {
-        for (int c=0; c<col; ++c) {
+    for (unsigned r=0; r<row; ++r) {
+        for (unsigned c=0; c<col; ++c) {
             cmpval=((*this)(r,c)).compare(o(r,c));
             if (cmpval!=0) return cmpval;
         }
@@ -226,15 +334,15 @@ int matrix::compare_same_type(basic const & other) const
 /** Sum of matrices.
  *
  *  @exception logic_error (incompatible matrices) */
-matrix matrix::add(matrix const & other) const
+matrix matrix::add(const matrix & other) const
 {
     if (col != other.col || row != other.row) {
         throw (std::logic_error("matrix::add(): incompatible matrices"));
     }
     
-    vector<ex> sum(this->m);
-    vector<ex>::iterator i;
-    vector<ex>::const_iterator ci;
+    exvector sum(this->m);
+    exvector::iterator i;
+    exvector::const_iterator ci;
     for (i=sum.begin(), ci=other.m.begin();
          i!=sum.end();
          ++i, ++ci) {
@@ -246,15 +354,15 @@ matrix matrix::add(matrix const & other) const
 /** Difference of matrices.
  *
  *  @exception logic_error (incompatible matrices) */
-matrix matrix::sub(matrix const & other) const
+matrix matrix::sub(const matrix & other) const
 {
     if (col != other.col || row != other.row) {
         throw (std::logic_error("matrix::sub(): incompatible matrices"));
     }
     
-    vector<ex> dif(this->m);
-    vector<ex>::iterator i;
-    vector<ex>::const_iterator ci;
+    exvector dif(this->m);
+    exvector::iterator i;
+    exvector::const_iterator ci;
     for (i=dif.begin(), ci=other.m.begin();
          i!=dif.end();
          ++i, ++ci) {
@@ -266,16 +374,16 @@ matrix matrix::sub(matrix const & other) const
 /** Product of matrices.
  *
  *  @exception logic_error (incompatible matrices) */
-matrix matrix::mul(matrix const & other) const
+matrix matrix::mul(const matrix & other) const
 {
     if (col != other.row) {
         throw (std::logic_error("matrix::mul(): incompatible matrices"));
     }
     
-    vector<ex> prod(row*other.col);
-    for (int i=0; i<row; ++i) {
-        for (int j=0; j<other.col; ++j) {
-            for (int l=0; l<col; ++l) {
+    exvector prod(row*other.col);
+    for (unsigned i=0; i<row; ++i) {
+        for (unsigned j=0; j<other.col; ++j) {
+            for (unsigned l=0; l<col; ++l) {
                 prod[i*other.col+j] += m[i*col+l] * other.m[l*other.col+j];
             }
         }
@@ -288,7 +396,7 @@ matrix matrix::mul(matrix const & other) const
  *  @param ro row of element
  *  @param co column of element 
  *  @exception range_error (index out of range) */
-ex const & matrix::operator() (int ro, int co) const
+const ex & matrix::operator() (unsigned ro, unsigned co) const
 {
     if (ro<0 || ro>=row || co<0 || co>=col) {
         throw (std::range_error("matrix::operator(): index out of range"));
@@ -300,7 +408,7 @@ ex const & matrix::operator() (int ro, int co) const
 /** Set individual elements manually.
  *
  *  @exception range_error (index out of range) */
-matrix & matrix::set(int ro, int co, ex value)
+matrix & matrix::set(unsigned ro, unsigned co, ex value)
 {
     if (ro<0 || ro>=row || co<0 || co>=col) {
         throw (std::range_error("matrix::set(): index out of range"));
@@ -315,10 +423,10 @@ matrix & matrix::set(int ro, int co, ex value)
  *  represents the transposed. */
 matrix matrix::transpose(void) const
 {
-    vector<ex> trans(col*row);
+    exvector trans(col*row);
     
-    for (int r=0; r<col; ++r) {
-        for (int c=0; c<row; ++c) {
+    for (unsigned r=0; r<col; ++r) {
+        for (unsigned c=0; c<row; ++c) {
             trans[r*row+c] = m[c*col+r];
         }
     }
@@ -329,23 +437,23 @@ matrix matrix::transpose(void) const
  * called internally by matrix::determinant(). */
 ex determinant_numeric(const matrix & M)
 {
-    ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
+    GINAC_ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
     matrix tmp(M);
-    ex det=exONE();
+    ex det=_ex1();
     ex piv;
     
-    for (int r1=0; r1<M.rows(); ++r1) {
+    for (unsigned r1=0; r1<M.rows(); ++r1) {
         int indx = tmp.pivot(r1);
         if (indx == -1) {
-            return exZERO();
+            return _ex0();
         }
         if (indx != 0) {
-            det *= exMINUSONE();
+            det *= _ex_1();
         }
         det = det * tmp.m[r1*M.cols()+r1];
-        for (int r2=r1+1; r2<M.rows(); ++r2) {
+        for (unsigned r2=r1+1; r2<M.rows(); ++r2) {
             piv = tmp.m[r2*M.cols()+r1] / tmp.m[r1*M.cols()+r1];
-            for (int c=r1+1; c<M.cols(); c++) {
+            for (unsigned c=r1+1; c<M.cols(); c++) {
                 tmp.m[r2*M.cols()+c] -= piv * tmp.m[r1*M.cols()+c];
             }
         }
@@ -355,7 +463,7 @@ ex determinant_numeric(const matrix & M)
 
 // Compute the sign of a permutation of a vector of things, used internally
 // by determinant_symbolic_perm() where it is instantiated for int.
-template <class T>
+template <typename T>
 int permutation_sign(vector<T> s)
 {
     if (s.size() < 2)
@@ -378,7 +486,7 @@ int permutation_sign(vector<T> s)
  *  routine is only called internally by matrix::determinant(). */
 ex determinant_symbolic_perm(const matrix & M)
 {
-    ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
+    GINAC_ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
     
     if (M.rows()==1) {  // speed things up
         return M(0,0);
@@ -386,12 +494,12 @@ ex determinant_symbolic_perm(const matrix & M)
     
     ex det;
     ex term;
-    vector<int> sigma(M.cols());
-    for (int i=0; i<M.cols(); ++i) sigma[i]=i;
+    vector<unsigned> sigma(M.cols());
+    for (unsigned i=0; i<M.cols(); ++i) sigma[i]=i;
     
     do {
         term = M(sigma[0],0);
-        for (int i=1; i<M.cols(); ++i) term *= M(sigma[i],i);
+        for (unsigned i=1; i<M.cols(); ++i) term *= M(sigma[i],i);
         det += permutation_sign(sigma)*term;
     } while (next_permutation(sigma.begin(), sigma.end()));
     
@@ -403,7 +511,7 @@ ex determinant_symbolic_perm(const matrix & M)
  *  called internally by matrix::determinant(). */
 ex determinant_symbolic_minor(const matrix & M)
 {
-    ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
+    GINAC_ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
     
     if (M.rows()==1) {  // end of recursion
         return M(0,0);
@@ -420,10 +528,10 @@ ex determinant_symbolic_minor(const matrix & M)
     
     ex det;
     matrix minorM(M.rows()-1,M.cols()-1);
-    for (int r1=0; r1<M.rows(); ++r1) {
+    for (unsigned r1=0; r1<M.rows(); ++r1) {
         // assemble the minor matrix
-        for (int r=0; r<minorM.rows(); ++r) {
-            for (int c=0; c<minorM.cols(); ++c) {
+        for (unsigned r=0; r<minorM.rows(); ++r) {
+            for (unsigned c=0; c<minorM.cols(); ++c) {
                 if (r<r1) {
                     minorM.set(r,c,M(r,c+1));
                 } else {
@@ -447,13 +555,13 @@ ex determinant_symbolic_minor(const matrix & M)
  *  that are very hard to canonicalize. */
 /*ex determinant_symbolic_leverrier(const matrix & M)
  *{
- *    ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
+ *    GINAC_ASSERT(M.rows()==M.cols());  // cannot happen, just in case...
  *    
  *    matrix B(M);
  *    matrix I(M.row, M.col);
  *    ex c=B.trace();
- *    for (int i=1; i<M.row; ++i) {
- *        for (int j=0; j<M.row; ++j)
+ *    for (unsigned i=1; i<M.row; ++i) {
+ *        for (unsigned j=0; j<M.row; ++j)
  *            I.m[j*M.col+j] = c;
  *        B = M.mul(B.sub(I));
  *        c = B.trace()/ex(i+1);
@@ -485,7 +593,7 @@ ex matrix::determinant(bool normalized) const
     }
 
     // check, if there are non-numeric entries in the matrix:
-    for (vector<ex>::const_iterator r=m.begin(); r!=m.end(); ++r) {
+    for (exvector::const_iterator r=m.begin(); r!=m.end(); ++r) {
         if (!(*r).info(info_flags::numeric)) {
             if (normalized) {
                 return determinant_symbolic_minor(*this).normal();
@@ -509,7 +617,7 @@ ex matrix::trace(void) const
     }
     
     ex tr;
-    for (int r=0; r<col; ++r) {
+    for (unsigned r=0; r<col; ++r) {
         tr += m[r*col+r];
     }
     return tr;
@@ -523,14 +631,14 @@ ex matrix::trace(void) const
  *  @return    characteristic polynomial as new expression
  *  @exception logic_error (matrix not square)
  *  @see       matrix::determinant() */
-ex matrix::charpoly(ex const & lambda) const
+ex matrix::charpoly(const ex & lambda) const
 {
     if (row != col) {
         throw (std::logic_error("matrix::charpoly(): matrix not square"));
     }
     
     matrix M(*this);
-    for (int r=0; r<col; ++r) {
+    for (unsigned r=0; r<col; ++r) {
         M.m[r*col+r] -= lambda;
     }
     return (M.determinant());
@@ -549,30 +657,30 @@ matrix matrix::inverse(void) const
     
     matrix tmp(row,col);
     // set tmp to the unit matrix
-    for (int i=0; i<col; ++i) {
-        tmp.m[i*col+i] = exONE();
+    for (unsigned i=0; i<col; ++i) {
+        tmp.m[i*col+i] = _ex1();
     }
     // create a copy of this matrix
     matrix cpy(*this);
-    for (int r1=0; r1<row; ++r1) {
+    for (unsigned r1=0; r1<row; ++r1) {
         int indx = cpy.pivot(r1);
         if (indx == -1) {
             throw (std::runtime_error("matrix::inverse(): singular matrix"));
         }
         if (indx != 0) {  // swap rows r and indx of matrix tmp
-            for (int i=0; i<col; ++i) {
+            for (unsigned i=0; i<col; ++i) {
                 tmp.m[r1*col+i].swap(tmp.m[indx*col+i]);
             }
         }
         ex a1 = cpy.m[r1*col+r1];
-        for (int c=0; c<col; ++c) {
+        for (unsigned c=0; c<col; ++c) {
             cpy.m[r1*col+c] /= a1;
             tmp.m[r1*col+c] /= a1;
         }
-        for (int r2=0; r2<row; ++r2) {
+        for (unsigned r2=0; r2<row; ++r2) {
             if (r2 != r1) {
                 ex a2 = cpy.m[r2*col+r1];
-                for (int c=0; c<col; ++c) {
+                for (unsigned c=0; c<col; ++c) {
                     cpy.m[r2*col+c] -= a2 * cpy.m[r1*col+c];
                     tmp.m[r2*col+c] -= a2 * tmp.m[r1*col+c];
                 }
@@ -582,7 +690,7 @@ matrix matrix::inverse(void) const
     return tmp;
 }
 
-void matrix::ffe_swap(int r1, int c1, int r2 ,int c2)
+void matrix::ffe_swap(unsigned r1, unsigned c1, unsigned r2 ,unsigned c2)
 {
     ensure_if_modifiable();
     
@@ -591,12 +699,12 @@ void matrix::ffe_swap(int r1, int c1, int r2 ,int c2)
     ffe_set(r2,c2,tmp);
 }
 
-void matrix::ffe_set(int r, int c, ex e)
+void matrix::ffe_set(unsigned r, unsigned c, ex e)
 {
     set(r-1,c-1,e);
 }
 
-ex matrix::ffe_get(int r, int c) const
+ex matrix::ffe_get(unsigned r, unsigned c) const
 {
     return operator()(r-1,c-1);
 }
@@ -609,8 +717,8 @@ ex matrix::ffe_get(int r, int c) const
  *  @param rhs m x p matrix
  *  @exception logic_error (incompatible matrices)
  *  @exception runtime_error (singular matrix) */
-matrix matrix::fraction_free_elim(matrix const & vars,
-                                  matrix const & rhs) const
+matrix matrix::fraction_free_elim(const matrix & vars,
+                                  const matrix & rhs) const
 {
     if ((row != rhs.row) || (col != vars.row) || (rhs.col != vars.col)) {
         throw (std::logic_error("matrix::solve(): incompatible matrices"));
@@ -620,30 +728,30 @@ matrix matrix::fraction_free_elim(matrix const & vars,
     matrix b(rhs);     // make a copy of the rhs vector
     
     // given an m x n matrix a, reduce it to upper echelon form
-    int m=a.row;
-    int n=a.col;
+    unsigned m=a.row;
+    unsigned n=a.col;
     int sign=1;
     ex divisor=1;
-    int r=1;
+    unsigned r=1;
     
     // eliminate below row r, with pivot in column k
-    for (int k=1; (k<=n)&&(r<=m); ++k) {
+    for (unsigned k=1; (k<=n)&&(r<=m); ++k) {
         // find a nonzero pivot
-        int p;
-        for (p=r; (p<=m)&&(a.ffe_get(p,k).is_equal(exZERO())); ++p) {}
+        unsigned p;
+        for (p=r; (p<=m)&&(a.ffe_get(p,k).is_equal(_ex0())); ++p) {}
         // pivot is in row p
         if (p<=m) {
             if (p!=r) {
                 // switch rows p and r
-                for (int j=k; j<=n; ++j) {
+                for (unsigned j=k; j<=n; ++j) {
                     a.ffe_swap(p,j,r,j);
                 }
                 b.ffe_swap(p,1,r,1);
                 // keep track of sign changes due to row exchange
                 sign=-sign;
             }
-            for (int i=r+1; i<=m; ++i) {
-                for (int j=k+1; j<=n; ++j) {
+            for (unsigned i=r+1; i<=m; ++i) {
+                for (unsigned j=k+1; j<=n; ++j) {
                     a.ffe_set(i,j,(a.ffe_get(r,k)*a.ffe_get(i,j)
                                   -a.ffe_get(r,j)*a.ffe_get(i,k))/divisor);
                     a.ffe_set(i,j,a.ffe_get(i,j).normal() /*.normal() */ );
@@ -661,36 +769,36 @@ matrix matrix::fraction_free_elim(matrix const & vars,
     // if (r==m+1) { det=sign*divisor; } else { det=0; }
     
     /*
-    for (int r=1; r<=m; ++r) {
-        for (int c=1; c<=n; ++c) {
+    for (unsigned r=1; r<=m; ++r) {
+        for (unsigned c=1; c<=n; ++c) {
             cout << a.ffe_get(r,c) << "\t";
         }
         cout << " | " <<  b.ffe_get(r,1) << endl;
     }
     */
     
-#ifdef DOASSERT
+#ifdef DO_GINAC_ASSERT
     // test if we really have an upper echelon matrix
     int zero_in_last_row=-1;
-    for (int r=1; r<=m; ++r) {
+    for (unsigned r=1; r<=m; ++r) {
         int zero_in_this_row=0;
-        for (int c=1; c<=n; ++c) {
-            if (a.ffe_get(r,c).is_equal(exZERO())) {
+        for (unsigned c=1; c<=n; ++c) {
+            if (a.ffe_get(r,c).is_equal(_ex0())) {
                zero_in_this_row++;
             } else {
                 break;
             }
         }
-        ASSERT((zero_in_this_row>zero_in_last_row)||(zero_in_this_row=n));
+        GINAC_ASSERT((zero_in_this_row>zero_in_last_row)||(zero_in_this_row=n));
         zero_in_last_row=zero_in_this_row;
     }
-#endif // def DOASSERT
+#endif // def DO_GINAC_ASSERT
     
     // assemble solution
     matrix sol(n,1);
-    int last_assigned_sol=n+1;
-    for (int r=m; r>0; --r) {
-        int first_non_zero=1;
+    unsigned last_assigned_sol=n+1;
+    for (unsigned r=m; r>0; --r) {
+        unsigned first_non_zero=1;
         while ((first_non_zero<=n)&&(a.ffe_get(r,first_non_zero).is_zero())) {
             first_non_zero++;
         }
@@ -702,11 +810,11 @@ matrix matrix::fraction_free_elim(matrix const & vars,
         } else {
             // assign solutions for vars between first_non_zero+1 and
             // last_assigned_sol-1: free parameters
-            for (int c=first_non_zero+1; c<=last_assigned_sol-1; ++c) {
+            for (unsigned c=first_non_zero+1; c<=last_assigned_sol-1; ++c) {
                 sol.ffe_set(c,1,vars.ffe_get(c,1));
             }
             ex e=b.ffe_get(r,1);
-            for (int c=first_non_zero+1; c<=n; ++c) {
+            for (unsigned c=first_non_zero+1; c<=n; ++c) {
                 e=e-a.ffe_get(r,c)*sol.ffe_get(c,1);
             }
             sol.ffe_set(first_non_zero,1,
@@ -716,21 +824,21 @@ matrix matrix::fraction_free_elim(matrix const & vars,
     }
     // assign solutions for vars between 1 and
     // last_assigned_sol-1: free parameters
-    for (int c=1; c<=last_assigned_sol-1; ++c) {
+    for (unsigned c=1; c<=last_assigned_sol-1; ++c) {
         sol.ffe_set(c,1,vars.ffe_get(c,1));
     }
 
     /*
-    for (int c=1; c<=n; ++c) {
+    for (unsigned c=1; c<=n; ++c) {
         cout << vars.ffe_get(c,1) << "->" << sol.ffe_get(c,1) << endl;
     }
     */
     
-#ifdef DOASSERT
+#ifdef DO_GINAC_ASSERT
     // test solution with echelon matrix
-    for (int r=1; r<=m; ++r) {
+    for (unsigned r=1; r<=m; ++r) {
         ex e=0;
-        for (int c=1; c<=n; ++c) {
+        for (unsigned c=1; c<=n; ++c) {
             e=e+a.ffe_get(r,c)*sol.ffe_get(c,1);
         }
         if (!(e-b.ffe_get(r,1)).normal().is_zero()) {
@@ -738,13 +846,13 @@ matrix matrix::fraction_free_elim(matrix const & vars,
             cout << "b.ffe_get(" << r<<",1)=" << b.ffe_get(r,1) << endl;
             cout << "diff=" << (e-b.ffe_get(r,1)).normal() << endl;
         }
-        ASSERT((e-b.ffe_get(r,1)).normal().is_zero());
+        GINAC_ASSERT((e-b.ffe_get(r,1)).normal().is_zero());
     }
 
     // test solution with original matrix
-    for (int r=1; r<=m; ++r) {
+    for (unsigned r=1; r<=m; ++r) {
         ex e=0;
-        for (int c=1; c<=n; ++c) {
+        for (unsigned c=1; c<=n; ++c) {
             e=e+ffe_get(r,c)*sol.ffe_get(c,1);
         }
         try {
@@ -761,15 +869,15 @@ matrix matrix::fraction_free_elim(matrix const & vars,
             ex xxx=e-rhs.ffe_get(r,1);
             cerr << "xxx=" << xxx << endl << endl;
         }
-        ASSERT((e-rhs.ffe_get(r,1)).normal().is_zero());
+        GINAC_ASSERT((e-rhs.ffe_get(r,1)).normal().is_zero());
     }
-#endif // def DOASSERT
+#endif // def DO_GINAC_ASSERT
     
     return sol;
 }   
     
 /** Solve simultaneous set of equations. */
-matrix matrix::solve(matrix const & v) const
+matrix matrix::solve(const matrix & v) const
 {
     if (!(row == col && col == v.row)) {
         throw (std::logic_error("matrix::solve(): incompatible matrices"));
@@ -777,24 +885,24 @@ matrix matrix::solve(matrix const & v) const
     
     // build the extended matrix of *this with v attached to the right
     matrix tmp(row,col+v.col);
-    for (int r=0; r<row; ++r) {
-        for (int c=0; c<col; ++c) {
+    for (unsigned r=0; r<row; ++r) {
+        for (unsigned c=0; c<col; ++c) {
             tmp.m[r*tmp.col+c] = m[r*col+c];
         }
-        for (int c=0; c<v.col; ++c) {
+        for (unsigned c=0; c<v.col; ++c) {
             tmp.m[r*tmp.col+c+col] = v.m[r*v.col+c];
         }
     }
-    for (int r1=0; r1<row; ++r1) {
+    for (unsigned r1=0; r1<row; ++r1) {
         int indx = tmp.pivot(r1);
         if (indx == -1) {
             throw (std::runtime_error("matrix::solve(): singular matrix"));
         }
-        for (int c=r1; c<tmp.col; ++c) {
+        for (unsigned c=r1; c<tmp.col; ++c) {
             tmp.m[r1*tmp.col+c] /= tmp.m[r1*tmp.col+r1];
         }
-        for (int r2=r1+1; r2<row; ++r2) {
-            for (int c=r1; c<tmp.col; ++c) {
+        for (unsigned r2=r1+1; r2<row; ++r2) {
+            for (unsigned c=r1; c<tmp.col; ++c) {
                 tmp.m[r2*tmp.col+c]
                     -= tmp.m[r2*tmp.col+r1] * tmp.m[r1*tmp.col+c];
             }
@@ -802,11 +910,11 @@ matrix matrix::solve(matrix const & v) const
     }
     
     // assemble the solution matrix
-    vector<ex> sol(v.row*v.col);
-    for (int c=0; c<v.col; ++c) {
-        for (int r=col-1; r>=0; --r) {
+    exvector sol(v.row*v.col);
+    for (unsigned c=0; c<v.col; ++c) {
+        for (unsigned r=col-1; r>=0; --r) {
             sol[r*v.col+c] = tmp[r*tmp.col+c];
-            for (int i=r+1; i<col; ++i) {
+            for (unsigned i=r+1; i<col; ++i) {
                 sol[r*v.col+c]
                     -= tmp[r*tmp.col+i] * sol[i*v.col+c];
             }
@@ -822,11 +930,11 @@ matrix matrix::solve(matrix const & v) const
  *  value and swaps the current row with the one where the element was found.
  *  Here it does the same with the first non-zero element. (This works fine,
  *  but may be far from optimal for numerics.) */
-int matrix::pivot(int ro)
+int matrix::pivot(unsigned ro)
 {
-    int k=ro;
+    unsigned k=ro;
     
-    for (int r=ro; r<row; ++r) {
+    for (unsigned r=ro; r<row; ++r) {
         if (!m[r*col+ro].is_zero()) {
             k = r;
             break;
@@ -836,7 +944,7 @@ int matrix::pivot(int ro)
         return -1;
     }
     if (k!=ro) {  // swap rows
-        for (int c=0; c<col; ++c) {
+        for (unsigned c=0; c<col; ++c) {
             m[k*col+c].swap(m[ro*col+c]);
         }
         return k;
@@ -849,4 +957,8 @@ int matrix::pivot(int ro)
 //////////
 
 const matrix some_matrix;
-type_info const & typeid_matrix=typeid(some_matrix);
+const type_info & typeid_matrix=typeid(some_matrix);
+
+#ifndef NO_NAMESPACE_GINAC
+} // namespace GiNaC
+#endif // ndef NO_NAMESPACE_GINAC