// SparseVector-inl.h: provides templated functions for SparseVector in // separate header template SparseVector sparse_rand(unsigned n, unsigned fill, int seed=1234) { srand(seed); const double rmax_inv = 1.0/double(RAND_MAX); SparseVector r(n); while (r.size() DenseVector operator*(const Matrix &M, const SparseVector &v) { DenseVector y(M.nRows()); STORE::const_iterator it=v.data_.begin(); for (; it!=v.data_.end(); it++) { const unsigned j = it->first; const T& vj = it->second; for (unsigned i=0; i DenseVector operator*(const SparseVector &v, const Matrix &M) { DenseVector y(M.nCols()); STORE::const_iterator it=v.data_.begin(); for (; it!=v.data_.end(); it++) { const unsigned i = it->first; const T& vi = it->second; for (unsigned j=0; j T dot(const SparseVector &a, const SparseVector &b) { double v = 0.0; for (STORE::const_iterator ai=a.data_.begin(); ai!=a.data_.end(); ai++) { STORE::const_iterator bi=b.data_.find(ai->first); if (bi == b.data_.end()) continue; v += ai->second * bi->second; } return v; } // Computes the product of a SparseMatrix tranpose with a SparseVector (M'*v). template SparseVector operator*(const SparseMatrix &M, const SparseVector &v) { SparseVector y(M.nRows()); for (unsigned i=0; isecond; } if (yi!=0.0) y(i)+=yi; } return y; } // computes the product of a SparseMatrix tranpose with a SparseVector (M'*v). template SparseVector operator*(const SparseVector &v, const SparseMatrix &M) { SparseVector y(M.nCols()); for (unsigned i=0; isecond * M._v[ij]; } return y; } // General constructor - sets the vector length. template SparseVector::SparseVector(unsigned n) : length_(n){} // Outputs the vector to string template std::string SparseVector::tostring() const { if (size() > nRows()/2) return Vector::tostring(); STORE::const_iterator it=data_.begin(); std::string str; using ATC_STRING::tostring; for (; it!=data_.end(); it++) str += tostring(it->first)+": "+tostring(it->second)+'\n'; return str; } // Indexes the ith component of the vector or returns zero if not found. template T SparseVector::operator()(unsigned i, unsigned j) const { STORE::const_iterator it = data_.find(i); if (it == data_.end()) return 0.0; return it->second; } // Indexes the ith component of the vector or returns zero if not found. template T& SparseVector::operator()(unsigned i, unsigned j) { return data_[i]; } // Indexes the ith component of the vector or returns zero if not found. template T SparseVector::operator[](unsigned i) const { return (*this)(i); } // Indexes the ith component of the vector or returns zero if not found. template T& SparseVector::operator[](unsigned i) { return (*this)[i]; } // Returns the number of nonzeros in the sparse vector. template inline unsigned SparseVector::size() const { return data_.size(); } // Returns the number of nonzeros in the sparse vector. template inline unsigned SparseVector::nRows() const { return length_; } // Changes the size of the SparseVector template void SparseVector::resize(unsigned nRows, unsigned nCols, bool copy) { length_ = nRows; STORE::iterator it; for (it=data_.begin(); it!=data_.end(); it++) { if (it->second >= length_) data_.erase(it); else if (!copy) it->second=T(0); } } // same as resize, but zero rather than copy template void SparseVector::reset(unsigned nRows, unsigned nCols, bool zero) { resize(nRows, nCols, !zero); } // sets all elements to zero but preserves sparcity template void SparseVector::zero() { STORE::iterator it; for (it=data_.begin(); it!=data_.end(); it++) it->second=T(0); } // TODO template void SparseVector::copy(const T* ptr, unsigned nRows, unsigned nCols) { } // TODO template void SparseVector::write_restart(FILE *F) const { } // writes a stream to a matlab script to recreate this variable template void SparseVector::matlab(ostream &o, const string &s) const { o << s << "=sparse(" << nRows() << ",1);\n"; o << showbase << scientific; STORE::const_iterator it; for (it=data_.begin(); it!=data_.end(); it++) o << s << "(" << it->first+1 << ") = " << it->second << ";\n"; }