* 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
/** 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());
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(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());
// 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
//////////
}
/** 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];
}
/** 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);
// 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()) {
/** 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"));
* @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"));
// 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)
* @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"));
* @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"));
}
/** 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"));
//////////
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