X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fmatrix.cpp;h=8daa11db0fab78332effeec157ed16fb17b4294f;hp=e9c68175897daa5222ee8b0750fcc85333cb05cd;hb=7e7beee2c946694130a484c923f6af8391867495;hpb=2862087ce55d944c1ac5d37283944b2b37507fd2 diff --git a/ginac/matrix.cpp b/ginac/matrix.cpp index e9c68175..8daa11db 100644 --- a/ginac/matrix.cpp +++ b/ginac/matrix.cpp @@ -3,7 +3,7 @@ * Implementation of symbolic matrices */ /* - * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2002 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 @@ -20,6 +20,7 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#include #include #include #include @@ -35,7 +36,6 @@ #include "print.h" #include "archive.h" #include "utils.h" -#include "debugmsg.h" namespace GiNaC { @@ -48,8 +48,7 @@ GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic) /** Default ctor. Initializes to 1 x 1-dimensional zero-matrix. */ matrix::matrix() : inherited(TINFO_matrix), row(1), col(1) { - debugmsg("matrix default ctor",LOGLEVEL_CONSTRUCT); - m.push_back(_ex0()); + m.push_back(_ex0); } void matrix::copy(const matrix & other) @@ -75,18 +74,14 @@ DEFAULT_DESTROY(matrix) matrix::matrix(unsigned r, unsigned c) : inherited(TINFO_matrix), row(r), col(c) { - debugmsg("matrix ctor from unsigned,unsigned",LOGLEVEL_CONSTRUCT); - m.resize(r*c, _ex0()); + m.resize(r*c, _ex0); } // protected /** Ctor from representation, for internal use only. */ matrix::matrix(unsigned r, unsigned c, const exvector & m2) - : inherited(TINFO_matrix), row(r), col(c), m(m2) -{ - debugmsg("matrix ctor from unsigned,unsigned,exvector",LOGLEVEL_CONSTRUCT); -} + : inherited(TINFO_matrix), row(r), col(c), m(m2) {} /** Construct matrix from (flat) list of elements. If the list has fewer * elements than the matrix, the remaining matrix elements are set to zero. @@ -95,8 +90,7 @@ matrix::matrix(unsigned r, unsigned c, const exvector & m2) matrix::matrix(unsigned r, unsigned c, const lst & l) : inherited(TINFO_matrix), row(r), col(c) { - debugmsg("matrix ctor from unsigned,unsigned,lst",LOGLEVEL_CONSTRUCT); - m.resize(r*c, _ex0()); + m.resize(r*c, _ex0); for (unsigned i=0; i(c)) { inherited::print(c, level); } else { - c.s << "["; - for (unsigned y=0; y(c)) + c.s << class_name() << '('; + + if (is_a(c)) + c.s << "\\left(\\begin{array}{" << std::string(col,'c') << "}"; + else c.s << "["; - for (unsigned x=0; x(c)) + c.s << "["; + for (unsigned co=0; co(c)) + c.s << "&"; + else + c.s << ","; + } else { + if (!is_a(c)) + c.s << "]"; + } + } + if (ro(c)) + c.s << "\\\\"; + else + c.s << ","; } - m[col*(y+1)-1].print(c); - c.s << "],"; - } - c.s << "["; - for (unsigned x=0; x(c)) + c.s << "\\end{array}\\right)"; + else + c.s << "]"; + + if (is_a(c)) + c.s << ')'; } } @@ -201,8 +213,6 @@ ex & matrix::let_op(int i) /** Evaluate matrix entry by entry. */ ex matrix::eval(int level) const { - debugmsg("matrix eval",LOGLEVEL_MEMBER_FUNCTION); - // check if we have to do anything at all if ((level==1)&&(flags & status_flags::evaluated)) return *this; @@ -236,8 +246,8 @@ ex matrix::subs(const lst & ls, const lst & lr, bool no_pattern) const int matrix::compare_same_type(const basic & other) const { - GINAC_ASSERT(is_exactly_of_type(other, matrix)); - const matrix & o = static_cast(other); + GINAC_ASSERT(is_exactly_a(other)); + const matrix &o = static_cast(other); // compare number of rows if (row != o.rows()) @@ -261,7 +271,7 @@ int matrix::compare_same_type(const basic & other) const bool matrix::match_same_type(const basic & other) const { - GINAC_ASSERT(is_exactly_of_type(other, matrix)); + GINAC_ASSERT(is_exactly_a(other)); const matrix & o = static_cast(other); // The number of rows and columns must be the same. This is necessary to @@ -272,8 +282,8 @@ bool matrix::match_same_type(const basic & other) const /** Automatic symbolic evaluation of an indexed matrix. */ ex matrix::eval_indexed(const basic & i) const { - GINAC_ASSERT(is_of_type(i, indexed)); - GINAC_ASSERT(is_ex_of_type(i.op(0), matrix)); + GINAC_ASSERT(is_a(i)); + GINAC_ASSERT(is_a(i.op(0))); bool all_indices_unsigned = static_cast(i).all_index_values_are(info_flags::nonnegint); @@ -349,9 +359,9 @@ ex matrix::eval_indexed(const basic & i) const /** Sum of two indexed matrices. */ ex matrix::add_indexed(const ex & self, const ex & other) const { - GINAC_ASSERT(is_ex_of_type(self, indexed)); - GINAC_ASSERT(is_ex_of_type(self.op(0), matrix)); - GINAC_ASSERT(is_ex_of_type(other, indexed)); + GINAC_ASSERT(is_a(self)); + GINAC_ASSERT(is_a(self.op(0))); + GINAC_ASSERT(is_a(other)); GINAC_ASSERT(self.nops() == 2 || self.nops() == 3); // Only add two matrices @@ -385,8 +395,8 @@ ex matrix::add_indexed(const ex & self, const ex & other) const /** Product of an indexed matrix with a number. */ ex matrix::scalar_mul_indexed(const ex & self, const numeric & other) const { - GINAC_ASSERT(is_ex_of_type(self, indexed)); - GINAC_ASSERT(is_ex_of_type(self.op(0), matrix)); + GINAC_ASSERT(is_a(self)); + GINAC_ASSERT(is_a(self.op(0))); GINAC_ASSERT(self.nops() == 2 || self.nops() == 3); const matrix &self_matrix = ex_to(self.op(0)); @@ -400,10 +410,10 @@ ex matrix::scalar_mul_indexed(const ex & self, const numeric & other) const /** Contraction of an indexed matrix with something else. */ bool matrix::contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const { - GINAC_ASSERT(is_ex_of_type(*self, indexed)); - GINAC_ASSERT(is_ex_of_type(*other, indexed)); + GINAC_ASSERT(is_a(*self)); + GINAC_ASSERT(is_a(*other)); GINAC_ASSERT(self->nops() == 2 || self->nops() == 3); - GINAC_ASSERT(is_ex_of_type(self->op(0), matrix)); + GINAC_ASSERT(is_a(self->op(0))); // Only contract with other matrices if (!is_ex_of_type(other->op(0), matrix)) @@ -435,7 +445,7 @@ bool matrix::contract_with(exvector::iterator self, exvector::iterator other, ex *self = self_matrix.mul(other_matrix.transpose())(0, 0); } } - *other = _ex1(); + *other = _ex1; return true; } else { // vector * matrix @@ -446,7 +456,7 @@ bool matrix::contract_with(exvector::iterator self, exvector::iterator other, ex *self = indexed(self_matrix.mul(other_matrix), other->op(2)); else *self = indexed(self_matrix.transpose().mul(other_matrix), other->op(2)); - *other = _ex1(); + *other = _ex1; return true; } @@ -456,7 +466,7 @@ bool matrix::contract_with(exvector::iterator self, exvector::iterator other, ex *self = indexed(other_matrix.mul(self_matrix), other->op(1)); else *self = indexed(other_matrix.mul(self_matrix.transpose()), other->op(1)); - *other = _ex1(); + *other = _ex1; return true; } } @@ -466,28 +476,28 @@ bool matrix::contract_with(exvector::iterator self, exvector::iterator other, ex // A_ij * B_jk = (A*B)_ik if (is_dummy_pair(self->op(2), other->op(1))) { *self = indexed(self_matrix.mul(other_matrix), self->op(1), other->op(2)); - *other = _ex1(); + *other = _ex1; return true; } // A_ij * B_kj = (A*Btrans)_ik if (is_dummy_pair(self->op(2), other->op(2))) { *self = indexed(self_matrix.mul(other_matrix.transpose()), self->op(1), other->op(1)); - *other = _ex1(); + *other = _ex1; return true; } // A_ji * B_jk = (Atrans*B)_ik if (is_dummy_pair(self->op(1), other->op(1))) { *self = indexed(self_matrix.transpose().mul(other_matrix), self->op(2), other->op(2)); - *other = _ex1(); + *other = _ex1; return true; } // A_ji * B_kj = (B*A)_ki if (is_dummy_pair(self->op(1), other->op(2))) { *self = indexed(other_matrix.mul(self_matrix), other->op(1), self->op(2)); - *other = _ex1(); + *other = _ex1; return true; } } @@ -609,18 +619,20 @@ matrix matrix::pow(const ex & expn) const } matrix C(row,col); for (unsigned r=0; r uintpair; std::vector c_zeros; // number of zeros in column for (unsigned c=0; c pre_sort; for (std::vector::const_iterator i=c_zeros.begin(); i!=c_zeros.end(); ++i) pre_sort.push_back(i->second); @@ -900,7 +915,7 @@ matrix matrix::inverse(void) const // First populate the identity matrix supposed to become the right hand side. matrix identity(row,col); for (unsigned i=0; im[r2*n+c] = _ex0(); + this->m[r2*n+c] = _ex0; } if (det) { // save space by deleting no longer needed elements for (unsigned c=r0+1; cm[r0*n+c] = _ex0(); + this->m[r0*n+c] = _ex0; } ++r0; } @@ -1238,12 +1253,12 @@ int matrix::division_free_elimination(const bool det) this->m[r2*n+c] = (this->m[r0*n+r1]*this->m[r2*n+c] - this->m[r2*n+r1]*this->m[r0*n+c]).expand(); // fill up left hand side with zeros for (unsigned c=0; c<=r1; ++c) - this->m[r2*n+c] = _ex0(); + this->m[r2*n+c] = _ex0; } if (det) { // save space by deleting no longer needed elements for (unsigned c=r0+1; cm[r0*n+c] = _ex0(); + this->m[r0*n+c] = _ex0; } ++r0; } @@ -1351,7 +1366,7 @@ int matrix::fraction_free_elimination(const bool det) } // fill up left hand side with zeros for (unsigned c=0; c<=r1; ++c) - tmp_n.m[r2*n+c] = _ex0(); + tmp_n.m[r2*n+c] = _ex0; } if ((r1m[k*col+co],numeric)); + GINAC_ASSERT(is_a(this->m[k*col+co])); unsigned kmax = k+1; numeric mmax = abs(ex_to(m[kmax*col+co])); while (kmaxm[kmax*col+co],numeric)); + GINAC_ASSERT(is_a(this->m[kmax*col+co])); numeric tmp = ex_to(this->m[kmax*col+co]); if (abs(tmp) > mmax) { mmax = tmp; @@ -1446,7 +1461,7 @@ ex lst_to_matrix(const lst & l) if (l.op(i).nops() > j) m(i, j) = l.op(i).op(j); else - m(i, j) = _ex0(); + m(i, j) = _ex0; return m; } @@ -1462,4 +1477,12 @@ ex diag_matrix(const lst & l) return m; } +ex unit_matrix(unsigned r, unsigned c) +{ + matrix Id(r,c); + for (unsigned i=0; i