Boost::ublas::vector 解析

本文详细介绍了Boost库中的ublas::vector类,包括其基本概念、模板参数、模型、类型需求以及公共基类。此外,还分别讨论了Unit Vector、Zero Vector和Scalar Vector的特性和用法,提供了示例代码帮助理解。

1 Vector

1.1 Description

The templated class vector<T, A> is the basecontainer adaptor for dense vectors. For an-dimensionalvector and 0 <= i < n every elementvi is mapped to thei-thelement of the container.

1.2 Example
#include <boost/numeric/ublas/vector.hpp>  //生成一般的向量,其内置类型的值为0,在默认的情况下
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (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.hpp.

1.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the vector. 
AThe type of the Storage array. [1]unbounded_array<T>
1.5 Model of

Vector,RandomAccessContainer

1.6 Type requirements

None, except for those imposed by the requirements of Vectorand RandomAccessContainer.

1.7 Public base classes

vector_container<vector<T, A> >

1.8 Members
MemberWhere definedDescription
value_typeVectorExpression 
referenceVectorExpression 
const_referenceVectorExpression 
size_typeVectorExpression 
difference_typeVectorExpression 
const_iteratorVectorExpression 
iteratorVectorExpression 
const_reverse_iteratorVectorExpression 
reverse_iteratorVectorExpression 
array_typeVector 
vector ()VectorExpressionAllocates an uninitialized vector that holds zeroelements.
vector (size_type size)VectorAllocates an uninitialized vector that holdssize elements.
vector (const vector &v) The copy constructor.
template<class AE>
vector (const vector_expression<AE> &ae)
 The extended copy constructor.
void resize (size_type size, bool preserve =true)VectorReallocates a vector to hold sizeelements. The existing elements of thevector arepreseved when specified.
size_type size () constVectorExpressionReturns the size of the vector.
size_type max_size () constRandomAccessContainerReturns the upper bound on the size of the vector.
bool empty () constRandomAccessContainerEquivilent to size () == 0.
const array_type& data () constVector 
array_type& data ()Vector 
const_reference operator () (size_type i)constVectorExpressionReturns a const reference of the i-th element.
reference operator () (size_type i)VectorExpressionReturns a reference of the i-th element.
const_reference operator [] (size_type i) constVectorReturns a const reference of the i-th element.
reference operator [] (size_type i)VectorReturns a reference of the i-th element.
vector &operator = (const vector &v)VectorExpressionThe assignment operator.
vector &assign_temporary (vector &v)VectorExpressionAssigns a temporary. May change the vector v.
template<class AE>
vector &operator = (const vector_expression<AE>&ae)
VectorExpressionThe extended assignment operator.
template<class AE>
vector &assign (const vector_expression<AE>&ae)
VectorExpressionAssigns a vector expression to the vector. Left and right handside of the assignment should be independent.
template<class AE>
vector &operator += (const vector_expression<AE>&ae)
VectorExpressionA computed assignment operator. Adds the vector expression tothe vector.
template<class AE>
vector &plus_assign (const vector_expression<AE>&ae)
VectorExpressionAdds a vector expression to the vector. Left and right handside of the assignment should be independent.
template<class AE>
vector &operator -= (const vector_expression<AE>&ae)
VectorExpressionA computed assignment operator. Subtracts the vector expressionfrom the vector.
template<class AE>
vector &minus_assign (const vector_expression<AE>&ae)
VectorExpressionSubtracts a vector expression from the vector. Left and righthand side of the assignment should be independent.
template<class AT>
vector &operator *= (const AT &at)
VectorExpressionA computed assignment operator. Multiplies the vector with ascalar.
template<class AT>
vector &operator /= (const AT &at)
VectorExpressionA computed assignment operator. Divides the vector through ascalar.
void swap (vector &v)VectorExpressionSwaps the contents of the vectors.
void insert_element (size_type i, const_reference t)VectorInserts the value t at the i-thelement.
void erase_element (size_type i)VectorErases the value at the i-th element.
void clear ()VectorClears the vector.
const_iterator begin () constVectorExpressionReturns a const_iterator pointing to the beginningof the vector.
const_iterator end () constVectorExpressionReturns a const_iterator pointing to the end ofthe vector.
iterator begin ()VectorExpressionReturns a iterator pointing to the beginning ofthe vector.
iterator end ()VectorExpressionReturns a iterator pointing to the end of thevector.
const_reverse_iterator rbegin () constVectorExpressionReturns a const_reverse_iterator pointing to thebeginning of the reversedvector.
const_reverse_iterator rend () constVectorExpressionReturns a const_reverse_iterator pointing to theend of the reversedvector.
reverse_iterator rbegin ()VectorExpressionReturns a reverse_iterator pointing to thebeginning of the reversedvector.
reverse_iterator rend ()VectorExpressionReturns a reverse_iterator pointing to the end ofthe reversed vector.
1.9 Notes

[1] Common parametersfor the Storage array are unbounded_array<T> ,bounded_array<T> andstd::vector<T> .

2 Unit Vector

2.1 Description

The templated class unit_vector<T, ALLOC> representscanonical unit vectors. For thek-thn-dimensional canonical unit vector and 0 <= i <n holdsuki= 0, if i <> k, anduki=1.

2.2 Example

#include <boost/numeric/ublas/vector.hpp> //单位向量,其k个不同的维度,只能有一个维度的值为1,其余为零。是构成单位矩阵的元素
#include <boost/numeric/ublas/io.hpp>

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


2.3 Definition

Defined in the header vector.hpp.

2.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the vector.int
ALLOCAn STL Allocator for size_type and difference_type.std::allocator
2.5 Model of

Vector .

2.6 Type requirements

None, except for those imposed by the requirements ofVector .

2.7 Public base classes

vector_container<unit_vector<T> >

2.8 Members
MemberDescription
unit_vector ()Constructs an unit_vector that holds zeroelements.
unit_vector (size_type size, size_type index)Constructs the index-th unit_vectorthat holds size elements.
unit_vector (const unit_vector &v)The copy constructor.
void resize (size_type size, bool preserve =true)Resizes a unit_vector to hold sizeelements. Therefore the existing elements of theunit_vector are always preseved.
size_type size () constReturns the size of the unit_vector.
size_type index () constReturns the index of the unit_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
unit_vector &operator = (const unit_vector&v)The assignment operator.
unit_vector &assign_temporary (unit_vector&v)Assigns a temporary. May change the unit vector v.
void swap (unit_vector &v)Swaps the contents of the unit vectors.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the unit_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe unit_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversedunit_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversedunit_vector.

3 Zero Vector

3.1 Description

The templated class zero_vector<T, ALLOC> representszero vectors. For an-dimensional zero vector and 0<= i < n holds zi=0.

3.2 Example

#include <boost/numeric/ublas/vector.hpp> //零向量,不用解释
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    zero_vector<double> v (3);
    std::cout << v << std::endl;
}


3.3 Definition

Defined in the header vector.hpp.

3.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the vector.int
ALLOCAn STL Allocator for size_type and difference_type.std::allocator
3.5 Model of

Vector .

3.6 Type requirements

None, except for those imposed by the requirements ofVector .

3.7 Public base classes

vector_container<zero_vector<T> >

3.8 Members
MemberDescription
zero_vector ()Constructs a zero_vector that holds zeroelements.
zero_vector (size_type size)Constructs a zero_vector that holdssize elements.
zero_vector (const zero_vector &v)The copy constructor.
void resize (size_type size, bool preserve =true)Resizes a zero_vector to hold sizeelements. Therefore the existing elements of thezero_vector are always preseved.
size_type size () constReturns the size of the zero_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
zero_vector &operator = (const zero_vector&v)The assignment operator.
zero_vector &assign_temporary (zero_vector&v)Assigns a temporary. May change the zero vector v.
void swap (zero_vector &v)Swaps the contents of the zero vectors.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the zero_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe zero_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversedzero_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversedzero_vector.

4 Scalar Vector

4.1 Description

The templated class scalar_vector<T, ALLOC>represents scalar vectors. For an-dimensional scalarvector and 0 <= i < n holdszi= s.

4.2 Example
#include <boost/numeric/ublas/vector.hpp>  //标量向量,其元素的各个值全部为1
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    scalar_vector<double> v (3);
    std::cout << v << std::endl;
}

4.3 Definition

Defined in the header vector.hpp.

4.4 Template parameters
ParameterDescriptionDefault
TThe type of object stored in the vector.int
ALLOCAn STL Allocator for size_type and difference_type.std::allocator
4.5 Model of

Vector .

4.6 Type requirements

None, except for those imposed by the requirements ofVector .

4.7 Public base classes

vector_container<scalar_vector<T>>

4.8 Members
MemberDescription
scalar_vector ()Constructs a scalar_vector that holds zeroelements.
scalar_vector (size_type size, const value_type&value)Constructs a scalar_vector that holdssize elements each of the specified value.
scalar_vector (const scalar_vector &v)The copy constructor.
void resize (size_type size, bool preserve =true)Resizes a scalar_vector to hold sizeelements. Therefore the existing elements of thescalar_vector are always preseved.
size_type size () constReturns the size of the scalar_vector.
const_reference operator () (size_type i)constReturns the value of the i-th element.
const_reference operator [] (size_type i)constReturns the value of the i-th element.
scalar_vector &operator = (const scalar_vector&v)The assignment operator.
scalar_vector &assign_temporary (scalar_vector&v)Assigns a temporary. May change the scalar vectorv .
void swap (scalar_vector &v)Swaps the contents of the scalar vectors.
const_iterator begin () constReturns a const_iterator pointing to the beginningof the scalar_vector.
const_iterator end () constReturns a const_iterator pointing to the end ofthe scalar_vector.
const_reverse_iterator rbegin () constReturns a const_reverse_iterator pointing to thebeginning of the reversedscalar_vector.
const_reverse_iterator rend () constReturns a const_reverse_iterator pointing to theend of the reversedscalar_vector.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值