#ifndef SPARSEVECTOR_H #define SPARSEVECTOR_H #include "MatrixLibrary.h" // No C++ templated typedefs, so use a define, gets cleaned up at end, // so don't use outside of this class #define STORE typename std::map /** SparseVector class - an implimentation of a vector that contains a ** majority of zero element, and provides the relevant operations ** specified by the base class Vector. **/ template class SparseVector : public Vector { //* Multiplies a Matrix by a SparseVector (M*v) and returns a DenseVector. friend DenseVector operator*(const Matrix &M, const SparseVector &v); //* Multiplies a SparseVector by a Matrix (M'*v) and returns a DenseVector. friend DenseVector operator*(const SparseVector &v, const Matrix &M); //* Computes the dot product between two SparseVectors of equal length. friend T dot(const SparseVector &a, const SparseVector &b); //* computes the product of a SparseMatrix tranpose with a SparseVector (M'*v). friend SparseVector operator*(const SparseMatrix &M, const SparseVector &v); //* computes the product of a SparseMatrix tranpose with a SparseVector (M'*v). friend SparseVector operator*(const SparseVector &v, const SparseMatrix &M); public: //* Constructor - sets length of vector (NOT # of nonzeros). SparseVector(unsigned length=0); //* Copies another SparseVector SparseVector(const SparseVector &c); //* Copies a general Vector (avoid if possible, its going to be slow). SparseVector(const Vector &c); //* Overrides output to string function to list only nonzeros and indices. std::string tostring() const; //* Indexing operators (w/ const overloading). //@{ T operator()(unsigned i, unsigned j=0) const; // NOTE reading a non-const SparseVector will call this and add zero entries. T& operator()(unsigned i, unsigned j=0); T operator[](unsigned i) const; T& operator[](unsigned i); //@} //* assignment operators //@{ SparseVector& operator=(SparseVector &c); SparseVector& operator=(Vector &c); //@} //* Return the number of rows in the Vector. unsigned nRows() const; //* Returns the number of columns - always 1. unsigned nCols() const { return 1; } //* Change # of Vector rows Vector and optionally keeps nonzeros (ignores nCols). void resize(unsigned nRows, unsigned nCols=1, bool copy=0); //* Return the number of nonzeros in the Vector. unsigned size() const; //* Changes size of Vector rows and optionally removes nonzeros. void reset (unsigned nRows, unsigned nCols=1, bool zero=0); //* zeros out all elements while preserving sparcity pattern void zero(); //* TODO impliment copy (or maybe not necessary) void copy(const T* ptr, unsigned nRows, unsigned nCols=1); //* Writes a restart file (TODO impliment this if needed/wanted). void write_restart(FILE *F) const; // output to matlab (is this needed) // using Matrix::matlab; //* Writes a matlab string to a stream that creates this object with a name. void matlab(ostream &o, const string &s="v") const; protected: //* Banned operators //@{ SparseVector(const Matrix &c); SparseVector& operator=(Matrix &c); T* get_ptr() const {return NULL; } //@} STORE data_; //*> sparse data structure unsigned length_; //*> number of rows }; #include "SparseVector-inl.h" #undef STORE #endif