Template Class SparseVector¶
Defined in File sparse_vector.hpp
Class Documentation¶
-
template<typename TV, typename TI = size_t>
class SparseVector¶ -
Public Functions
-
inline void sort()¶
-
inline void reverse_inds(size_t n)¶
map indices i -> n-i
-
inline SparseVector()¶
-
inline SparseVector(const std::vector<key_type> &indval)¶
-
template<typename TV2>
inline SparseVector<TV2, TI> cast_values(TV2) const¶
-
template<typename TI2>
inline SparseVector(const SparseVector<int, TI2> &other)¶
-
inline SparseVector(const std::vector<std::tuple<size_t, int>> &ival)¶
-
inline SparseVector(std::initializer_list<TI> ind, std::initializer_list<TV> val)¶
constructor for initializer lists
- Parameters
ind – nonzero indices
val – nonzero values
-
inline SparseVector(std::string &line)¶
-
inline SparseVector shift_inds(TI shift) const¶
-
template<typename T>
inline SparseVector operator[](const T &indset) const¶
-
inline auto nzbegin() const¶
-
inline auto nzend() const¶
-
inline auto nzbegin()¶
-
inline auto nzend()¶
-
inline void clear()¶
-
inline void clear_dealloc()¶
-
inline void clear_zeros()¶
-
inline void clear_inds(const std::vector<bool> &c)¶
clear indices marked true in c i.e. remove entry i if c[i] is true
-
inline void clear_inds(const std::vector<bool> &c, std::vector<key_type> &tmp)¶
-
inline size_t nnz() const¶
-
inline std::vector<size_t> nzinds() const¶
-
inline auto emplace_back(const key_type &k)¶
-
inline const key_type &lastnz() const¶
-
inline const key_type &firstnz() const¶
-
inline void permute(const std::vector<size_t> &perm)¶
-
inline void ipermute(const std::vector<size_t> &perm)¶
-
inline SparseVector subvector(const std::vector<size_t> &pind) const¶
-
inline SparseVector block(const size_t i0, const size_t i1) const¶
-
inline void set_block(const size_t i0, const size_t i1, const SparseVector &b)¶
-
inline void J(const size_t m)¶
-
inline void swap_rows(const size_t i, const size_t j)¶
-
inline void erase_all()¶
-
inline void erase_initial_rows(const TI ndelete)¶
erase the first ndelete rows of the matrix
decreents all subsequent indices by ndelete
-
inline void mix_rows(const size_t i, const size_t j, const TV &a, const TV &b, const TV &c, const TV &d)¶
-
inline void insert_rows(const std::vector<size_t> &r_inds, const std::vector<TV> &r_vals)¶
(for matrix nonzero row addition usage) Insert row indices at specified locations(assumes indices of rows is in ascending order.) Input: r_inds: the indices of new non-zero elements r_vals: the values of new non-zero elements The key idea is to a) first modify all existing non-zero indices b) second add new elements c) sort all indices
For example, if indval has inds [3,5] and r_inds = [1,3,4] then for each element in [3,5], say 3, a) find how many indices in r_inds that are smaller than 3, there are 1 b) add up: 3+1 = 4 c) loop over r_inds, we find 3 < 4, add up 1 => 4+1 = 5; keep going find 4 < 5, add up 1 => 5+1 = 6 So, the row with index 3 finally locates at 6.
-
inline void insert_rows(const std::vector<size_t> &r_inds, size_t ninserted = 0)¶
(for matrix zero row addition usage) insert zero row indices at specified locations assumes that list of rows is in sorted order
ninserted is lower bound on number inserted before first element
-
template<class SVT>
inline size_t nnz_intersection(const SVT &x) const¶ calculate the number of non-zeros common to two vectors
- Parameters
x – sparse vector for comparison
- Returns
ct number of non-zeros common to both this vector and x
-
template<class SVT>
inline void coeff_intersection(const SVT &x, std::map<TV, size_t> &ct) const¶ calculate the number of non-zeros common with c * x where c ranges over any possible value
- Parameters
x – sparse vector for comparison
ct – map from coefficient c to number of identical nonzeros with c*x ct is not cleared in the function.
-
inline SparseVector operator+(const SparseVector &other) const¶
-
inline SparseVector operator*(const TV a) const¶
scalar multiplication
-
inline TV operator*(const SparseVector &x) const¶
dot product
-
template<class SVT>
inline void axpy(const TV &a, const SVT &x, const TI &firstind, const TI &lastind, std::vector<key_type> &tmp)¶
-
template<class SVT>
inline void axpy(const SVT &x, const std::vector<TV> &coeff, const std::vector<TI> &inds, std::vector<key_type> &tmp)¶
-
inline SparseVector scale(const TV c) const¶
-
inline void print() const¶
-
inline void print_row() const¶
-
inline std::string str()¶
-
inline SparseVector kron(const SparseVector &other, size_t m) const¶
-
inline SparseVector tensor(const SparseVector &other, size_t m) const¶
Public Static Functions
-
static inline SparseVector dense(const TV a, const TI m)¶
static constructor that fills vector with m copies of a
-
static inline SparseVector random(size_t n, double p, int maxval, std::default_random_engine &generator)¶
-
static inline SparseVector random(size_t n, double p, int maxval)¶
-
inline void sort()¶