// SparseVector-inl.h: provides templated functions for SparseVector in // separate header namespace ATC_matrix { template SparseVector sparse_rand(INDEX n, INDEX 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 INDEX j = it->first; const T& vj = it->second; for (INDEX 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 INDEX i = it->first; const T& vi = it->second; for (INDEX j=0; j T dot(const SparseVector &a, const SparseVector &b) { T 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 (INDEX 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 (INDEX i=0; isecond * M._v[ij]; } return y; } // General constructor - sets the vector length. template SparseVector::SparseVector(INDEX n) : length_(n){} // Outputs the vector to string template std::string SparseVector::to_string() const { if (size() > nRows()/2) return Vector::to_string(); STORE::const_iterator it=data_.begin(); std::string str; using ATC_Utility::to_string; for (; it!=data_.end(); it++) str += to_string(it->first)+": "+to_string(it->second)+'\n'; return str; } // Indexes the ith component of the vector or returns zero if not found. template T SparseVector::operator()(INDEX i, INDEX 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()(INDEX i, INDEX j) { return data_[i]; } // Indexes the ith component of the vector or returns zero if not found. template T SparseVector::operator[](INDEX i) const { return (*this)(i); } // Indexes the ith component of the vector or returns zero if not found. template T& SparseVector::operator[](INDEX i) { return (*this)[i]; } // Returns a pair (index, value) for a nonzero in the vector. template std::pair SparseVector::pair(INDEX i) const { STORE::const_iterator it=data_.begin() + i; return std::pair(it->first, it->second); } //* Adds SparseVector x, scaled by s to this one. Can be different sparcity. template void SparseVector::add_scaled(SparseVector& x, const T& s) { STORE::const_iterator it; for (it=x.data_.begin(); it!=x.data_.end(); it++) { data_[it->first] += it->second * s; } } // Returns the number of nonzeros in the sparse vector. template inline INDEX SparseVector::size() const { return data_.size(); } // Returns the number of nonzeros in the sparse vector. template inline INDEX SparseVector::nRows() const { return length_; } // Copy constructor for sparse vector. template SparseVector::SparseVector(const SparseVector &c) { length_ = c.length_; data_ = c.data_; } // operator equal for sparse vector. template SparseVector& SparseVector::operator=(const SparseVector &c) { length_ = c.length_; data_ = c.data_; return *this; } // Changes the size of the SparseVector template void SparseVector::resize(INDEX nRows, INDEX 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(INDEX nRows, INDEX 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); } template void SparseVector::copy(const T* ptr, INDEX nRows, INDEX nCols) { } template void SparseVector::write_restart(FILE *F) const { } // writes a stream to a matlab script to recreate this variable template void SparseVector::matlab(std::ostream &o, const std::string &s) const { o << s << "=sparse(" << nRows() << ",1);\n"; o << std::showbase << std::scientific; STORE::const_iterator it; for (it=data_.begin(); it!=data_.end(); it++) o << s << "(" << it->first+1 << ") = " << it->second << ";\n"; } } // end namespace