X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fmatrix.cpp;h=25de42bf57d2fc4262fd4c8f189aa385d9e10857;hp=1a5d6953c3d994365d9d6b05a60ebf351119e7bd;hb=e455b8fcbb2788d437a7d212811bb5ba171e0cd4;hpb=539de73dc25de63f2888f8696d73ac3fc83db45f diff --git a/ginac/matrix.cpp b/ginac/matrix.cpp index 1a5d6953..25de42bf 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; r 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) @@ -1380,4 +1446,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