]> 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 deeba2806e97d81cbb1c5de9803ed7ce90e15bc7..97b473628464e498a233c691e2d0b724f34e7491 100644 (file)
@@ -3,7 +3,7 @@
  *  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 "debugmsg.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
@@ -40,7 +43,7 @@ namespace GiNaC {
 
 /** 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(_ex0());
@@ -51,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) {
@@ -69,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
@@ -79,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);
 }
 
 //////////
@@ -93,7 +96,7 @@ void matrix::destroy(bool call_parent)
  *  @param r number of rows
  *  @param c number of cols */
 matrix::matrix(unsigned r, unsigned c)
-    : basic(TINFO_matrix), row(r), col(c)
+    : inherited(TINFO_matrix), row(r), col(c)
 {
     debugmsg("matrix constructor from unsigned,unsigned",LOGLEVEL_CONSTRUCT);
     m.resize(r*c, _ex0());
@@ -102,12 +105,51 @@ matrix::matrix(unsigned r, unsigned c)
 // protected
 
 /** Ctor from representation, for internal use only. */
-matrix::matrix(unsigned r, unsigned 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)
+{
+    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++;
+    }
+}
+
 //////////
 // functions overriding virtual functions from bases classes
 //////////
@@ -157,13 +199,19 @@ void matrix::printraw(ostream & os) const
 }
 
 /** 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];
 }
@@ -180,7 +228,7 @@ 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
 {
     GINAC_ASSERT(other.bp!=0);
     
@@ -250,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
 {
     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()) {
@@ -286,7 +334,7 @@ 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"));
@@ -306,7 +354,7 @@ 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"));
@@ -326,7 +374,7 @@ 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"));
@@ -348,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() (unsigned ro, unsigned 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"));
@@ -415,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)
@@ -583,7 +631,7 @@ 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"));
@@ -669,8 +717,8 @@ ex matrix::ffe_get(unsigned r, unsigned 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"));
@@ -829,7 +877,7 @@ matrix matrix::fraction_free_elim(matrix const & vars,
 }   
     
 /** 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"));
@@ -909,8 +957,8 @@ int matrix::pivot(unsigned ro)
 //////////
 
 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