X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fmatrix.cpp;h=65b1254936e0a378eb5ba1760ee9709a3a52c409;hp=a63275f54338e7688065a79176091e36902503a1;hb=df5f8db62815995d87ebd4f97a5dbc0d1a327b94;hpb=094911eb78cacb6f2877a70c9ac74766df58ccea diff --git a/ginac/matrix.cpp b/ginac/matrix.cpp index a63275f5..65b12549 100644 --- a/ginac/matrix.cpp +++ b/ginac/matrix.cpp @@ -25,16 +25,17 @@ #include #include "matrix.h" -#include "archive.h" #include "numeric.h" #include "lst.h" #include "idx.h" #include "indexed.h" -#include "utils.h" -#include "debugmsg.h" #include "power.h" #include "symbol.h" #include "normal.h" +#include "print.h" +#include "archive.h" +#include "utils.h" +#include "debugmsg.h" namespace GiNaC { @@ -44,8 +45,6 @@ GINAC_IMPLEMENT_REGISTERED_CLASS(matrix, basic) // default ctor, dtor, copy ctor, assignment operator and helpers: ////////// -// public - /** Default ctor. Initializes to 1 x 1-dimensional zero-matrix. */ matrix::matrix() : inherited(TINFO_matrix), row(1), col(1) { @@ -53,9 +52,6 @@ matrix::matrix() : inherited(TINFO_matrix), row(1), col(1) m.push_back(_ex0()); } -// protected - -/** For use by copy ctor and assignment operator. */ void matrix::copy(const matrix & other) { inherited::copy(other); @@ -64,10 +60,7 @@ void matrix::copy(const matrix & other) m = other.m; // STL's vector copying invoked here } -void matrix::destroy(bool call_parent) -{ - if (call_parent) inherited::destroy(call_parent); -} +DEFAULT_DESTROY(matrix) ////////// // other ctors @@ -95,11 +88,29 @@ matrix::matrix(unsigned r, unsigned c, const exvector & m2) debugmsg("matrix ctor from unsigned,unsigned,exvector",LOGLEVEL_CONSTRUCT); } +/** Construct matrix from (flat) list of elements. If the list has fewer + * elements than the matrix, the remaining matrix elements are set to zero. + * If the list has more elements than the matrix, the excessive elements are + * thrown away. */ +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()); + + for (unsigned i=0; i= r) + break; // matrix smaller than list: throw away excessive elements + m[y*c+x] = l.op(i); + } +} + ////////// // archiving ////////// -/** Construct object from archive_node. */ matrix::matrix(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) { debugmsg("matrix ctor from archive_node", LOGLEVEL_CONSTRUCT); @@ -115,13 +126,6 @@ matrix::matrix(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst } } -/** 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 { inherited::archive(n); @@ -134,42 +138,43 @@ void matrix::archive(archive_node &n) const } } +DEFAULT_UNARCHIVE(matrix) + ////////// // functions overriding virtual functions from bases classes ////////// // public -void matrix::print(std::ostream & os, unsigned upper_precedence) const +void matrix::print(const print_context & c, unsigned level) const { - debugmsg("matrix print",LOGLEVEL_PRINT); - os << "[[ "; - for (unsigned r=0; rbasic::subs(ls, lr, no_pattern); } // protected @@ -381,104 +370,150 @@ ex matrix::eval_indexed(const basic & i) const return i.hold(); } -/** Contraction of an indexed matrix with something else. */ -bool matrix::contract_with(ex & self, ex & other) 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(self.nops() == 2 || self.nops() == 3); + + // Only add two matrices + if (is_ex_of_type(other.op(0), matrix)) { + GINAC_ASSERT(other.nops() == 2 || other.nops() == 3); + + const matrix &self_matrix = ex_to_matrix(self.op(0)); + const matrix &other_matrix = ex_to_matrix(other.op(0)); + + if (self.nops() == 2 && other.nops() == 2) { // vector + vector + + if (self_matrix.row == other_matrix.row) + return indexed(self_matrix.add(other_matrix), self.op(1)); + else if (self_matrix.row == other_matrix.col) + return indexed(self_matrix.add(other_matrix.transpose()), self.op(1)); + + } else if (self.nops() == 3 && other.nops() == 3) { // matrix + matrix + + if (self.op(1).is_equal(other.op(1)) && self.op(2).is_equal(other.op(2))) + return indexed(self_matrix.add(other_matrix), self.op(1), self.op(2)); + else if (self.op(1).is_equal(other.op(2)) && self.op(2).is_equal(other.op(1))) + return indexed(self_matrix.add(other_matrix.transpose()), self.op(1), self.op(2)); + + } + } + + // Don't know what to do, return unevaluated sum + return self + other; +} + +/** 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(self.nops() == 2 || self.nops() == 3); + + const matrix &self_matrix = ex_to_matrix(self.op(0)); + + if (self.nops() == 2) + return indexed(self_matrix.mul(other), self.op(1)); + else // self.nops() == 3 + return indexed(self_matrix.mul(other), self.op(1), self.op(2)); +} + +/** 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(self->nops() == 2 || self->nops() == 3); + GINAC_ASSERT(is_ex_of_type(self->op(0), matrix)); // Only contract with other matrices - if (!is_ex_of_type(other.op(0), matrix)) + if (!is_ex_of_type(other->op(0), matrix)) return false; - GINAC_ASSERT(other.nops() == 2 || other.nops() == 3); + GINAC_ASSERT(other->nops() == 2 || other->nops() == 3); - const matrix &self_matrix = ex_to_matrix(self.op(0)); - const matrix &other_matrix = ex_to_matrix(other.op(0)); + const matrix &self_matrix = ex_to_matrix(self->op(0)); + const matrix &other_matrix = ex_to_matrix(other->op(0)); - if (self.nops() == 2) { + if (self->nops() == 2) { unsigned self_dim = (self_matrix.col == 1) ? self_matrix.row : self_matrix.col; - if (other.nops() == 2) { // vector * vector (scalar product) + if (other->nops() == 2) { // vector * vector (scalar product) unsigned other_dim = (other_matrix.col == 1) ? other_matrix.row : other_matrix.col; if (self_matrix.col == 1) { if (other_matrix.col == 1) { // Column vector * column vector, transpose first vector - self = self_matrix.transpose().mul(other_matrix)(0, 0); + *self = self_matrix.transpose().mul(other_matrix)(0, 0); } else { // Column vector * row vector, swap factors - self = other_matrix.mul(self_matrix)(0, 0); + *self = other_matrix.mul(self_matrix)(0, 0); } } else { if (other_matrix.col == 1) { // Row vector * column vector, perfect - self = self_matrix.mul(other_matrix)(0, 0); + *self = self_matrix.mul(other_matrix)(0, 0); } else { // Row vector * row vector, transpose second vector - self = self_matrix.mul(other_matrix.transpose())(0, 0); + *self = self_matrix.mul(other_matrix.transpose())(0, 0); } } - other = _ex1(); + *other = _ex1(); return true; } else { // vector * matrix - GINAC_ASSERT(other.nops() == 3); - // B_i * A_ij = (B*A)_j (B is row vector) - if (is_dummy_pair(self.op(1), other.op(1))) { + if (is_dummy_pair(self->op(1), other->op(1))) { if (self_matrix.row == 1) - self = indexed(self_matrix.mul(other_matrix), other.op(2)); + *self = indexed(self_matrix.mul(other_matrix), other->op(2)); else - self = indexed(self_matrix.transpose().mul(other_matrix), other.op(2)); - other = _ex1(); + *self = indexed(self_matrix.transpose().mul(other_matrix), other->op(2)); + *other = _ex1(); return true; } // B_j * A_ij = (A*B)_i (B is column vector) - if (is_dummy_pair(self.op(1), other.op(2))) { + if (is_dummy_pair(self->op(1), other->op(2))) { if (self_matrix.col == 1) - self = indexed(other_matrix.mul(self_matrix), other.op(1)); + *self = indexed(other_matrix.mul(self_matrix), other->op(1)); else - self = indexed(other_matrix.mul(self_matrix.transpose()), other.op(1)); - other = _ex1(); + *self = indexed(other_matrix.mul(self_matrix.transpose()), other->op(1)); + *other = _ex1(); return true; } } - } else if (other.nops() == 3) { // matrix * matrix - - GINAC_ASSERT(self.nops() == 3); - GINAC_ASSERT(other.nops() == 3); + } else if (other->nops() == 3) { // matrix * matrix // 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(); + 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(); 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(); + 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(); 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(); + 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(); 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(); + 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(); return true; } } @@ -551,6 +586,19 @@ matrix matrix::mul(const matrix & other) const } +/** Product of matrix and scalar. */ +matrix matrix::mul(const numeric & other) const +{ + exvector prod(row * col); + + for (unsigned r=0; rsolve(vars,identity); + } catch (const std::runtime_error & e) { + if (e.what()==std::string("matrix::solve(): inconsistent linear system")) throw (std::runtime_error("matrix::inverse(): singular matrix")); - } - if (indx != 0) { // swap rows r and indx of matrix tmp - for (unsigned i=0; i cols) cols = l.op(i).nops(); - + // Allocate and fill matrix matrix &m = *new matrix(rows, cols); + m.setflag(status_flags::dynallocated); for (i=0; i j) @@ -1385,4 +1414,16 @@ ex lst_to_matrix(const ex &l) return m; } +ex diag_matrix(const lst & l) +{ + unsigned dim = l.nops(); + + matrix &m = *new matrix(dim, dim); + m.setflag(status_flags::dynallocated); + for (unsigned i=0; i