* Implementation of symbolic matrices */
/*
- * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
+ * 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
#include <stdexcept>
#include "matrix.h"
+#include "archive.h"
+#include "utils.h"
#include "debugmsg.h"
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
namespace GiNaC {
-#endif // ndef NO_GINAC_NAMESPACE
+#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()
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) {
// 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
void matrix::destroy(bool call_parent)
{
- if (call_parent) basic::destroy(call_parent);
+ if (call_parent) inherited::destroy(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, exvector 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)
{
- debugmsg("matrix constructor from int,int,exvector",LOGLEVEL_CONSTRUCT);
+ return (new matrix(n, sym_lst))->setflag(status_flags::dynallocated);
+}
+
+/** Archive the object. */
+void matrix::archive(archive_node &n) const
+{
+ 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++;
+ }
}
//////////
{
debugmsg("matrix print",LOGLEVEL_PRINT);
os << "[[ ";
- for (int r=0; r<row-1; ++r) {
+ for (unsigned r=0; r<row-1; ++r) {
os << "[[";
- for (int c=0; c<col-1; ++c) {
+ for (unsigned c=0; c<col-1; ++c) {
os << m[r*col+c] << ",";
}
os << m[col*(r+1)-1] << "]], ";
}
os << "[[";
- for (int c=0; c<col-1; ++c) {
+ for (unsigned c=0; c<col-1; ++c) {
os << m[(row-1)*col+c] << ",";
}
os << m[row*col-1] << "]] ]]";
{
debugmsg("matrix printraw",LOGLEVEL_PRINT);
os << "matrix(" << row << "," << col <<",";
- for (int r=0; r<row-1; ++r) {
+ for (unsigned r=0; r<row-1; ++r) {
os << "(";
- for (int c=0; c<col-1; ++c) {
+ for (unsigned c=0; c<col-1; ++c) {
os << m[r*col+c] << ",";
}
os << m[col*(r-1)-1] << "),";
}
os << "(";
- for (int c=0; c<col-1; ++c) {
+ 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];
}
ex matrix::expand(unsigned options) const
{
exvector tmp(row*col);
- for (int i=0; i<row*col; ++i) {
+ for (unsigned i=0; i<row*col; ++i) {
tmp[i]=m[i].expand(options);
}
return matrix(row, col, tmp);
/** 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
{
GINAC_ASSERT(other.bp!=0);
// eval() entry by entry
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);
}
}
// evalf() entry by entry
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);
}
}
// protected
-int matrix::compare_same_type(basic const & other) const
+int matrix::compare_same_type(const basic & other) const
{
GINAC_ASSERT(is_exactly_of_type(other, matrix));
- matrix const & o=static_cast<matrix &>(const_cast<basic &>(other));
+ const matrix & o=static_cast<matrix &>(const_cast<basic &>(other));
// compare number of rows
if (row != o.rows()) {
// 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;
}
/** 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"));
/** 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"));
/** 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"));
}
exvector 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) {
+ 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];
}
}
* @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"));
/** 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"));
{
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];
}
}
{
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];
}
}
// 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)
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()));
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 {
* 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);
}
ex tr;
- for (int r=0; r<col; ++r) {
+ for (unsigned r=0; r<col; ++r) {
tr += m[r*col+r];
}
return tr;
* @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());
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];
}
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();
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);
}
* @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"));
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() */ );
// 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 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;
// 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++;
}
} 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,
}
// 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 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()) {
}
// 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 {
}
/** 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"));
// 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];
}
// assemble the solution matrix
exvector sol(v.row*v.col);
- for (int c=0; c<v.col; ++c) {
- for (int r=col-1; r>=0; --r) {
+ 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];
}
* 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;
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;
//////////
const matrix some_matrix;
-type_info const & typeid_matrix=typeid(some_matrix);
+const type_info & typeid_matrix=typeid(some_matrix);
-#ifndef NO_GINAC_NAMESPACE
+#ifndef NO_NAMESPACE_GINAC
} // namespace GiNaC
-#endif // ndef NO_GINAC_NAMESPACE
+#endif // ndef NO_NAMESPACE_GINAC