From eda9171eeea420248c81b57d481f12ca26dc4880 Mon Sep 17 00:00:00 2001 From: jiri Date: Wed, 11 Nov 2009 20:46:21 +0000 Subject: [PATCH] *** empty log message *** --- sparsesmat.h | 67 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/sparsesmat.h b/sparsesmat.h index 8a8040f..3282389 100644 --- a/sparsesmat.h +++ b/sparsesmat.h @@ -27,6 +27,7 @@ #include #include #include +#include "la_traits.h" #include "sparsemat.h" #include "vec.h" #include "mat.h" @@ -50,28 +51,37 @@ protected: int *count; public: SparseSMat() : nn(0), v(NULL), count(NULL) {}; - SparseSMat(const SPMatindex n); + explicit SparseSMat(const SPMatindex n); //prevent double -> int -> SparseSMat SparseSMat(const SparseSMat &rhs); - SparseSMat(const SparseMat &rhs); - SparseSMat(const NRSMat &rhs); + explicit SparseSMat(const SparseMat &rhs); + explicit SparseSMat(const NRSMat &rhs); SparseSMat & operator=(const SparseSMat &rhs); void copyonwrite(); void resize(const SPMatindex n); void clear() {resize(nn);} void simplify(); ~SparseSMat(); + inline int getcount() const {return count?*count:0;} // - SparseSMat & operator=(const T a); //assign a to diagonal - SparseSMat & operator+=(const T a); //assign a to diagonal - SparseSMat & operator-=(const T a); //assign a to diagonal - SparseSMat & operator*=(const T a); //multiply by a scalar + inline const SparseSMat operator+(const T &rhs) const {return SparseSMat(*this) += rhs;} + inline const SparseSMat operator-(const T &rhs) const {return SparseSMat(*this) -= rhs;} + inline const SparseSMat operator*(const T &rhs) const {return SparseSMat(*this) *= rhs;} + inline const SparseSMat operator+(const SparseSMat &rhs) const {return SparseSMat(*this) += rhs;} + inline const SparseSMat operator-(const SparseSMat &rhs) const {return SparseSMat(*this) -= rhs;} + inline const SparseSMat operator*(const SparseSMat &rhs) const; //!!!NOT A GENERAL ROUTINE, JUST FOR THE CASES WHEN THE RESULT STAYS SYMMETRIC + + SparseSMat & operator=(const T &a); //assign a to diagonal + SparseSMat & operator+=(const T &a); //assign a to diagonal + SparseSMat & operator-=(const T &a); //assign a to diagonal + SparseSMat & operator*=(const T &a); //multiply by a scalar SparseSMat & operator+=(const SparseSMat &rhs); SparseSMat & operator-=(const SparseSMat &rhs); void gemv(const T beta, NRVec &r, const char trans, const T alpha, const NRVec &x) const; - const SparseSMat operator*(const SparseSMat &rhs) const; //!!!NOT A GENERAL ROUTINE, JUST FOR THE CASES WHEN THE RESULT STAYS SYMMETRIC + void axpy(const T alpha, const SparseSMat &x, const bool transp=0); // this+= a*x const typename LA_traits::normtype norm(const T scalar=(T)0) const; void add(const SPMatindex n, const SPMatindex m, const T elem, const bool both=true); unsigned int length() const; + void transposeme() const {}; int nrows() const {return nn;} int ncols() const {return nn;} @@ -145,6 +155,17 @@ v= new std::map * [n]; memset(v,0,nn*sizeof(std::map *)); } +template +SparseSMat::SparseSMat(const NRSMat &rhs) +:nn(rhs.nrows()), +count(new int(1)) +{ +v= new std::map * [nn]; +memset(v,0,nn*sizeof(std::map *)); +int i,j; +for(i=0; iSPARSEEPSILON) (*this).add(i,j,rhs(i,j),true); +} + template SparseSMat::SparseSMat(const SparseSMat &rhs) { @@ -154,6 +175,20 @@ count = rhs.count; if(count) (*count)++; } +//NRSMat from SparseSMat +#define nn2 (nn*(nn+1)/2) +template +NRSMat::NRSMat(const SparseSMat &rhs) +: nn(rhs.nrows()) +{ +count = new int(1); +v=new T[nn2]; +memset(v,0,nn2*sizeof(T)); +typename SparseSMat::iterator p(rhs); +for(; p.notend(); ++p) if(p->row <= p->col) (*this)(p->row,p->col)=p->elem; +} +#undef nn2 + template SparseSMat::~SparseSMat() @@ -231,6 +266,7 @@ SparseSMat & SparseSMat::operator=(const SparseSMat &rhs) return *this; } + template void SparseSMat::copyonwrite() { @@ -240,11 +276,11 @@ void SparseSMat::copyonwrite() (*count)--; count = new int; *count = 1; - std::map *newv= new std::map * [nn]; + typename std::map **newv= new std::map * [nn]; for(SPMatindex i=0; i(*(v[i])); //deep copy of each map + newv[i]= new typename std::map(*(v[i])); //deep copy of each map else - newv[i]=NULL; + newv[i]= NULL; v = newv; } } @@ -298,11 +334,7 @@ std::ostream & operator<<(std::ostream &s, const SparseSMat &x) s << n << " "<< n<< std::endl; typename SparseSMat::iterator p(x); -for(; -p.notend(); -++p) -s << (int)p->row << ' ' << (int)p->col << ' ' << (typename LA_traits_io::IOtype) p->elem << '\n'; - +for(; p.notend(); ++p) s << (int)p->row << ' ' << (int)p->col << ' ' << (typename LA_traits_io::IOtype) p->elem << '\n'; s << "-1 -1\n"; return s; } @@ -328,7 +360,4 @@ std::istream& operator>>(std::istream &s, SparseSMat &x) - - - #endif //_SPARSESMAT_H_