Boost::ublas::vector 之稀疏向量

1 Mapped Vector

1.1 Description

The templated class mapped_vector<T, A> isthe base container adaptor for sparse vectors using element maps. For an-dimensional sparse vector and 0 <= i < nthe non-zero elements vi are mapped toconsecutive elements of the associative container, i.e. forelements k =vi1andk + 1 =vi2of thecontainer holds i1 <i2.

//之所以说是映射向量,是因为它将稀疏向量中的非零元素映射到另一相关联的容器中,其元素在关联容器中连续排列

1.2 Example
#include <boost/numeric/ublas/vector_sparse.hpp> //最后的结果为(0,1,2),其实Mapped vector 是稀疏矩阵的一种表示方法
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    mapped_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}

1.3 Definition

Defined in the header vector_sparse.hpp.

1.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the mapped vector. 
AThe type of the adapted array. [1]map_std<std::size_t, T>
1.5 Model of

Vector .

1.6 Type requirements

None, except for those imposed by the requirements of Vector .

1.7 Public base classes

vector_container<mapped_vector<T, A>>

1.8 Members
MemberDescription
mapped_vector ()Allocates a mapped_vector that holds zeroelements.
mapped_vector (size_type size, size_typenon_zeros = 0)Allocates a mapped_vector that holds at mostsize elements.
mapped_vector (const mapped_vector &v)The copy constructor.
template<class AE>
mapped_vector (size_type non_zeros, constvector_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size, boolpreserve = true)Reallocates a mapped_vector to hold at mostsize elements. The existing elements of themapped_vector are preseved when specified.
size_type size () constReturns the size of the mapped_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
reference operator () (size_type i)Returns a reference of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
reference operator [] (size_type i)Returns a reference of the i-th element.
mapped_vector &operator = (const mapped_vector&v)The assignment operator.
mapped_vector &assign_temporary (mapped_vector&v)Assigns a temporary. May change the mapped vectorv .
template<class AE>
mapped_vector &operator = (const vector_expression<AE>&ae)
The extended assignment operator.
template<class AE>
mapped_vector &assign (const vector_expression<AE>&ae)
Assigns a vector expression to the mapped vector. Left andright hand side of the assignment should be independent.
template<class AE>
mapped_vector &operator += (const vector_expression<AE>&ae)
A computed assignment operator. Adds the vector expression tothe mapped vector.
template<class AE>
mapped_vector &plus_assign (const vector_expression<AE>&ae)
Adds a vector expression to the mapped vector. Left and righthand side of the assignment should be independent.
template<class AE>
mapped_vector &operator -= (const vector_expression<AE>&ae)
A computed assignment operator. Subtracts the vector expressionfrom the mapped vector.
template<class AE>
mapped_vector &minus_assign (const vector_expression<AE>&ae)
Subtracts a vector expression from the mapped vector. Left andright hand side of the assignment should be independent.
template<class AT>
mapped_vector &operator *= (const AT &at)
A computed assignment operator. Multiplies the mapped vectorwith a scalar.
template<class AT>
mapped_vector &operator /= (const AT &at)
A computed assignment operator. Divides the mapped vectorthrough a scalar.
void swap (mapped_vector &v)Swaps the contents of the mapped vectors.
true_reference insert_element (size_type i, const_reference t)Inserts the value t at the i-thelement. Duplicates elements are not allowed.
void erase_element (size_type i)Erases the value at the i-th element.
void clear ()Clears the mapped vector.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the mapped_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe mapped_vector.
iterator begin ()Returns a iterator pointing to the beginning ofthe mapped_vector.
iterator end ()Returns a iterator pointing to the end of themapped_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversed mapped_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversed mapped_vector.
reverse_iterator rbegin ()Returns a reverse_iterator pointing to thebeginning of the reversed mapped_vector.
reverse_iterator rend ()Returns a reverse_iterator pointing to the end ofthe reversed mapped_vector.
1.9 Notes

[1] Supportedparameters for the adapted array aremap_array<std::size_t, T> andmap_std<std::size_t, T>. The latter isequivalent to std::map<std::size_t, T>.

2 Compressed Vector

2.1 Description

The templated class compressed_vector<T, IB, IA,TA> is the base container adaptor for compressed vectors.For a n-dimensional compressed vector and 0 <= i< n the non-zero elements viare mapped to consecutive elements of the index and value container, i.e. for elements k =vi1andk + 1 =vi2of these containers holds i1 <i2.

//压缩向量与之前的映射向量,它们之间的不同是索引的值也参与了映射,索引值如何参加映射就不得而知了
2.2 Example
#include <boost/numeric/ublas/vector_sparse.hpp>//结果和之前的相同
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    compressed_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}

2.3 Definition

Defined in the header vector_sparse.hpp.

2.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the compressed vector. 
IBThe index base of the compressed vector. [1]0
IAThe type of the adapted array for indices. [2]unbounded_array<std::size_t>
TAThe type of the adapted array for values. [2]unbounded_array<T>
2.5 Model of

Vector .

2.6 Type requirements

None, except for those imposed by the requirements of Vector .

2.7 Public base classes

vector_container<compressed_vector<T, IB, IA,TA> >

2.8 Members
MemberDescription
compressed_vector ()Allocates a compressed_vector that holds zeroelements.
compressed_vector (size_type size, size_typenon_zeros)Allocates a compressed_vector that holds at mostsize elements.
compressed_vector (const compressed_vector&v)The copy constructor.
template<class AE>
compressed_vector (size_type non_zeros, constvector_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size, boolpreserve = true)Reallocates a compressed_vector to hold at mostsize elements. The existing elements of thecompress_vector are preseved when specified.
size_type size () constReturns the size of the compressed_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
reference operator () (size_type i)Returns a reference of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
reference operator [] (size_type i)Returns a reference of the i-th element.
compressed_vector &operator = (constcompressed_vector &v)The assignment operator.
compressed_vector &assign_temporary(compressed_vector &v)Assigns a temporary. May change the compressed vectorv.
template<class AE>
compressed_vector &operator = (constvector_expression<AE> &ae)
The extended assignment operator.
template<class AE>
compressed_vector &assign (const vector_expression<AE>&ae)
Assigns a vector expression to the compressed vector. Left andright hand side of the assignment should be independent.
template<class AE>
compressed_vector &operator += (constvector_expression<AE> &ae)
A computed assignment operator. Adds the vector expression tothe compressed vector.
template<class AE>
compressed_vector &plus_assign (constvector_expression<AE> &ae)
Adds a vector expression to the compressed vector. Left andright hand side of the assignment should be independent.
template<class AE>
compressed_vector &operator -= (constvector_expression<AE> &ae)
A computed assignment operator. Subtracts the vector expressionfrom the compressed vector.
template<class AE>
compressed_vector &minus_assign (constvector_expression<AE> &ae)
Subtracts a vector expression from the compressed vector. Leftand right hand side of the assignment should be independent.
template<class AT>
compressed_vector &operator *= (const AT &at)
A computed assignment operator. Multiplies the compressedvector with a scalar.
template<class AT>
compressed_vector &operator /= (const AT &at)
A computed assignment operator. Divides the compressed vectorthrough a scalar.
void swap (compressed_vector &v)Swaps the contents of the compressed vectors.
true_reference insert_element (size_type i, const_reference t)Inserts the value t at the i-thelement. Duplicates elements are not allowed.
void erase_element (size_type i)Erases the value at the i-th element.
void clear ()Clears the compressed vector.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the compressed_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe compressed_vector.
iterator begin ()Returns a iterator pointing to the beginning ofthe compressed_vector.
iterator end ()Returns a iterator pointing to the end of thecompressed_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversed compressed_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversed compressed_vector.
reverse_iterator rbegin ()Returns a reverse_iterator pointing to thebeginning of the reversed compressed_vector.
reverse_iterator rend ()Returns a reverse_iterator pointing to the end ofthe reversed compressed_vector.
2.9 Notes

[1]Supported parameters for the index base are 0 and1 at least.

[2]Supported parameters for the adapted array areunbounded_array<> ,bounded_array<> andstd::vector<> .

3 Coordinate Vector

3.1 Description

The templated class coordinate_vector<T, IB, IA,TA> is the base container adaptor for compressed vectors.For a n-dimensional sorted coordinate vector and 0<= i < n the non-zero elementsvi are mapped to consecutive elementsof the index and value container, i.e. for elements k =vi1andk + 1 =vi2of thesecontainers holds i1 <i2.

3.2 Example
#include <boost/numeric/ublas/vector_sparse.hpp>// 结果和之前相同
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    coordinate_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}

3.3 Definition

Defined in the header vector_sparse.hpp.

3.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the coordinate vector. 
IBThe index base of the coordinate vector. [1]0
IAThe type of the adapted array for indices. [2]unbounded_array<std::size_t>
TAThe type of the adapted array for values. [2]unbounded_array<T>
3.5 Model of

Vector .

3.6 Type requirements

None, except for those imposed by the requirements of Vector .

3.7 Public base classes

vector_container<coordinate_vector<T, IB, IA,TA> >

3.8 Members
MemberDescription
coordinate_vector ()Allocates a coordinate_vector that holds zeroelements.
coordinate_vector (size_type size, size_typenon_zeros)Allocates a coordinate_vector that holds at mostsize elements.
coordinate_vector (const coordinate_vector&v)The copy constructor.
template<class AE>
coordinate_vector (size_type non_zeros, constvector_expression<AE> &ae)
The extended copy constructor.
void resize (size_type size, boolpreserve = true)Reallocates a coordinate_vector to hold at mostsize elements. The existing elements of thecoordinate_vector are preseved when specified.
size_type size () constReturns the size of the coordinate_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
reference operator () (size_type i)Returns a reference of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
reference operator [] (size_type i)Returns a reference of the i-th element.
coordinate_vector &operator = (constcoordinate_vector &v)The assignment operator.
coordinate_vector &assign_temporary(coordinate_vector &v)Assigns a temporary. May change the coordinate vectorv.
template<class AE>
coordinate_vector &operator = (constvector_expression<AE> &ae)
The extended assignment operator.
template<class AE>
coordinate_vector &assign (const vector_expression<AE>&ae)
Assigns a vector expression to the coordinate vector. Left andright hand side of the assignment should be independent.
template<class AE>
coordinate_vector &operator += (constvector_expression<AE> &ae)
A computed assignment operator. Adds the vector expression tothe coordinate vector.
template<class AE>
coordinate_vector &plus_assign (constvector_expression<AE> &ae)
Adds a vector expression to the coordinate vector. Left andright hand side of the assignment should be independent.
template<class AE>
coordinate_vector &operator -= (constvector_expression<AE> &ae)
A computed assignment operator. Subtracts the vector expressionfrom the coordinate vector.
template<class AE>
coordinate_vector &minus_assign (constvector_expression<AE> &ae)
Subtracts a vector expression from the coordinate vector. Leftand right hand side of the assignment should be independent.
template<class AT>
coordinate_vector &operator *= (const AT &at)
A computed assignment operator. Multiplies the coordinatevector with a scalar.
template<class AT>
coordinate_vector &operator /= (const AT &at)
A computed assignment operator. Divides the coordinate vectorthrough a scalar.
void swap (coordinate_vector &v)Swaps the contents of the coordinate vectors.
true_reference insert_element (size_type i, const_reference t)Inserts the value t at the i-thelement. Duplicates elements are not allowed.
void append_element (size_type i, size_type j, const_reference t)Appends the value t at the i-th element.Duplicate elements can be appended to a coordinate_vector. They are merged into a singlearithmetically summed element by the sort function.
void erase_element (size_type i)Erases the value at the i-th element.
void clear ()Clears the coordinate vector.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the coordinate_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe coordinate_vector.
iterator begin ()Returns a iterator pointing to the beginning ofthe coordinate_vector.
iterator end ()Returns a iterator pointing to the end of thecoordinate_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversed coordinate_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversed coordinate_vector.
reverse_iterator rbegin ()Returns a reverse_iterator pointing to thebeginning of the reversed coordinate_vector.
reverse_iterator rend ()Returns a reverse_iterator pointing to the end ofthe reversed coordinate_vector.
3.9 Notes

[1]Supported parameters for the index base are 0 and1 at least.

[2]Supported parameters for the adapted array areunbounded_array<> ,bounded_array<> andstd::vector<> .



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值