*** empty log message ***
This commit is contained in:
parent
87be923400
commit
f44662bdab
@ -28,6 +28,7 @@
|
||||
template <typename T>
|
||||
void SparseSMat<T>::gemm(const T beta, const SparseSMat &a, const char transa, const SparseSMat &b, const char transb, const T alpha)
|
||||
{
|
||||
std::cerr << "enter gemm\n";
|
||||
(*this) *= beta;
|
||||
if(alpha==(T)0) return;
|
||||
if(a.nn!=b.nn || a.nn!=nn) laerror("incompatible sizes in SparseSMat::gemm");
|
||||
@ -63,7 +64,11 @@ for(SPMatindex k=0; k<nn; ++k) //summation loop
|
||||
add(ai[i],bi[j],prod(i,j),false);
|
||||
|
||||
}
|
||||
simplify(); //erase elements below threshold
|
||||
std::cerr << "before simplify in gemm\n";
|
||||
std::cerr << " nterms = "<<
|
||||
simplify()
|
||||
<<std::endl; //erase elements below threshold
|
||||
std::cerr << "regular exit gemm\n";
|
||||
}
|
||||
|
||||
|
||||
@ -97,6 +102,7 @@ for(SPMatindex i=0; i<nn; ++i) if(x.v[i])
|
||||
typename std::map<SPMatindex,T>::iterator p;
|
||||
for(p=x.v[i]->begin(); p!=x.v[i]->end(); ++p) (*v[i])[p->first] = p->second * alpha;
|
||||
}
|
||||
simplify();
|
||||
}
|
||||
|
||||
|
||||
@ -115,11 +121,83 @@ for(SPMatindex i=0; i<nn; ++i) if(v[i])
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
SparseSMat<T> & SparseSMat<T>::operator=(const T &a)
|
||||
{
|
||||
clear();
|
||||
for(SPMatindex i=0; i<nn; ++i)
|
||||
{
|
||||
if(!v[i]) v[i] = new std::map<SPMatindex,T>;
|
||||
(*v[i])[i] = a;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
SparseSMat<T> & SparseSMat<T>::operator+=(const T &a)
|
||||
{
|
||||
copyonwrite();
|
||||
for(SPMatindex i=0; i<nn; ++i)
|
||||
{
|
||||
if(v[i])
|
||||
{
|
||||
typename std::map<SPMatindex,T>::iterator p;
|
||||
p= v[i]->find(i);
|
||||
if(p!=v[i]->end()) p->second+=a; else (*v[i])[i] = a;
|
||||
}
|
||||
else {v[i] = new std::map<SPMatindex,T>; (*v[i])[i] = a;}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
SparseSMat<T> & SparseSMat<T>::operator-=(const T &a)
|
||||
{
|
||||
copyonwrite();
|
||||
for(SPMatindex i=0; i<nn; ++i)
|
||||
{
|
||||
if(v[i])
|
||||
{
|
||||
typename std::map<SPMatindex,T>::iterator p;
|
||||
p= v[i]->find(i);
|
||||
if(p!=v[i]->end()) p->second-=a; else (*v[i])[i] = -a;
|
||||
}
|
||||
else {v[i] = new std::map<SPMatindex,T>; (*v[i])[i] = -a;}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
typename LA_traits<T>::normtype SparseSMat<T>::norm(const T scalar) const
|
||||
{
|
||||
typename LA_traits<T>::normtype sum=0;
|
||||
|
||||
for(SPMatindex i=0; i<nn; ++i)
|
||||
if(v[i])
|
||||
{
|
||||
typename std::map<SPMatindex,T>::iterator p;
|
||||
p= v[i]->find(i);
|
||||
if(p!=v[i]->end()) sum += LA_traits<T>::sqrabs(p->second - scalar);
|
||||
else sum += LA_traits<T>::sqrabs(scalar);
|
||||
}
|
||||
else sum += LA_traits<T>::sqrabs(scalar); //missing diagonal element
|
||||
|
||||
return sqrt(sum);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#define INSTANTIZE(T) \
|
||||
template void SparseSMat<T>::gemm(const T beta, const SparseSMat &a, const char transa, const SparseSMat &b, const char transb, const T alpha); \
|
||||
template SparseSMat<T> & SparseSMat<T>::operator*=(const T &a); \
|
||||
template void SparseSMat<T>::gemv(const T beta, NRVec<T> &r, const char trans, const T alpha, const NRVec<T> &x) const; \
|
||||
template void SparseSMat<T>::axpy(const T alpha, const SparseSMat &x, const bool transp); \
|
||||
template SparseSMat<T> & SparseSMat<T>::operator=(const T &a); \
|
||||
template SparseSMat<T> & SparseSMat<T>::operator+=(const T &a); \
|
||||
template SparseSMat<T> & SparseSMat<T>::operator-=(const T &a); \
|
||||
template LA_traits<T>::normtype SparseSMat<T>::norm(const T scalar) const; \
|
||||
|
||||
|
||||
INSTANTIZE(double)
|
||||
|
34
sparsesmat.h
34
sparsesmat.h
@ -59,12 +59,11 @@ public:
|
||||
void copyonwrite();
|
||||
void resize(const SPMatindex n);
|
||||
void clear() {resize(nn);}
|
||||
void simplify();
|
||||
unsigned long long simplify();
|
||||
~SparseSMat();
|
||||
inline int getcount() const {return count?*count:0;}
|
||||
SparseSMat & operator*=(const T &a); //multiply by a scalar
|
||||
inline const SparseSMat operator*(const T &rhs) const {return SparseSMat(*this) *= rhs;}
|
||||
/*@@@to be done
|
||||
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;}
|
||||
@ -72,16 +71,15 @@ public:
|
||||
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 SparseSMat &rhs);
|
||||
SparseSMat & operator-=(const SparseSMat &rhs);
|
||||
void gemv(const T beta, NRVec<T> &r, const char trans, const T alpha, const NRVec<T> &x) const;
|
||||
void axpy(const T alpha, const SparseSMat &x, const bool transp=0); // this+= a*x
|
||||
const typename LA_traits<T>::normtype norm(const T scalar=(T)0) const;
|
||||
*/
|
||||
inline SparseSMat & operator+=(const SparseSMat &rhs) {axpy((T)1,rhs); return *this;};
|
||||
inline SparseSMat & operator-=(const SparseSMat &rhs) {axpy((T)-1,rhs); return *this;};
|
||||
void gemv(const T beta, NRVec<T> &r, const char trans, const T alpha, const NRVec<T> &x) const;
|
||||
typename LA_traits<T>::normtype norm(const T scalar=(T)0) const;
|
||||
inline const SparseSMat operator*(const SparseSMat &rhs) const {SparseSMat<T> r(nn); r.gemm(0,*this,'n',rhs,'n',1); return r;}; //!!!NOT A GENERAL ROUTINE, JUST FOR THE CASES WHEN THE RESULT STAYS SYMMETRIC
|
||||
void gemm(const T beta, const SparseSMat &a, const char transa, const SparseSMat &b, const char transb, const T alpha); //this := alpha*op( A )*op( B ) + beta*this !!!NOT A GENERAL ROUTINE, JUST FOR THE CASES WHEN THE RESULT STAYS SYMMETRIC
|
||||
inline void add(const SPMatindex n, const SPMatindex m, const T elem, const bool both=true);
|
||||
unsigned int length() const;
|
||||
inline unsigned long long length() {return simplify();};
|
||||
void transposeme() const {};
|
||||
int nrows() const {return nn;}
|
||||
int ncols() const {return nn;}
|
||||
@ -234,7 +232,7 @@ if(*count > 1) //it was shared
|
||||
else //it was not shared
|
||||
{
|
||||
//delete all trees
|
||||
for(SPMatindex i=0; i<nn; ++i) if(v[i]) {delete[] v[i]; v[i]=NULL;}
|
||||
for(SPMatindex i=0; i<nn; ++i) if(v[i]) {delete v[i]; v[i]=NULL;}
|
||||
if(n!=nn)
|
||||
{
|
||||
nn=n;
|
||||
@ -254,7 +252,7 @@ SparseSMat<T> & SparseSMat<T>::operator=(const SparseSMat &rhs)
|
||||
{
|
||||
if(v)
|
||||
{
|
||||
for(SPMatindex i=0; i<nn; ++i) if(v[i]) delete[] v[i];
|
||||
for(SPMatindex i=0; i<nn; ++i) if(v[i]) delete v[i];
|
||||
delete[] (v);
|
||||
}
|
||||
delete count;
|
||||
@ -310,20 +308,22 @@ if(n!=m && both) //add also transposed
|
||||
|
||||
|
||||
template <typename T>
|
||||
void SparseSMat<T>::simplify()
|
||||
unsigned long long SparseSMat<T>::simplify()
|
||||
{
|
||||
unsigned long long count=0;
|
||||
for(SPMatindex i=0; i<nn; ++i) if(v[i])
|
||||
{
|
||||
//check for zero elements and erase them from the list
|
||||
//build a list since we are not sure whether erase from within the traversal loop is safe
|
||||
std::list<SPMatindex> l;
|
||||
typename std::map<SPMatindex,T>::iterator p;
|
||||
for(p=v[i]->begin(); p!=v[i]->end(); ++p)
|
||||
if(std::abs(p->second) < SPARSEEPSILON) l.push_front(p->first);
|
||||
typename std::list<SPMatindex>::iterator q;
|
||||
for(q=l.begin(); q!=l.end(); ++q) v[i]->erase(*q);
|
||||
if(v[i]->size() == 0) delete v[i];
|
||||
}
|
||||
for(p=v[i]->begin(); p!=v[i]->end(); ++p)
|
||||
if(std::abs(p->second) < SPARSEEPSILON) l.push_front(p->first); else ++count;
|
||||
typename std::list<SPMatindex>::iterator q;
|
||||
for(q=l.begin(); q!=l.end(); ++q) v[i]->erase(*q);
|
||||
if(v[i]->size() == 0) {delete v[i]; v[i]=NULL;}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
Loading…
Reference in New Issue
Block a user