Boost uBlas Vector

Vector overview
Header : #include <boost/numeric/ublas/vector.hpp> Header for ublas IO : #include <boost/numeric/ublas/io.hpp> unit_vector v (nelem, upos); upos element has 1, nelem number of element in vector zero_vector v (nelem); zero vector with nelem size scalar_vector v (nelem); scalar vector with nelem element all have value 1. boost::numeric::ublas::vector v (3); elements can be modified. sparse vectors : still don't understood compeletly, for time being added for future referance. Header : #include mapped_vector v (nelem); compressed_vector v (nelem, 3); coordinate_vector v (3, 3); got some compilation error
void testvector() // this can work for both vector and sparse vectors. { using namespace boost::numeric::ublas; mapped_vector v (3, 3); std::cout << v << std::endl; for (int i = 0; i < v.size(); ++i) { v(i)=i; } std::cout << v << std::endl; }
Vector Proxies
Header : <boost/numeric/ublas/vector_proxy.hpp>
void testVectorProxy() { using namespace boost::numeric::ublas; boost::numeric::ublas::vector v (5);
// The templated class vector_range allows addressing a sub-range of a vector's element. //The free subrange functions support the construction of vector ranges. //The free project functions support the construction of vector ranges.
std::cout << v << std::endl; //vector_range > vr(v,range(2,4)); //vector_range > vr =subrange(v, 2, 4); vector_range > vr =project(v, range(2, 4)); for (unsigned i = 0; i < vr.size (); ++ i) vr (i) = i*5+5; std::cout << vr << "\t" << vr.start() << std::endl; std::cout << v << std::endl; std::cout <<"============================================="<< std::endl;
// The templated class vector_slice allows addressing a slice of a vector. //The free subslice functions support the construction of vector slices. //The free project functions support the construction of vector slices
std::cout << v << std::endl; //vector_slice > vs (v, slice (0, 1, 3)); //vector_slice > vs = subslice(v,0,1,2); vector_slice > vs = project(v,slice(0,1,2)); for (unsigned i = 0; i < vs.size (); ++ i) vs (i) = i; std::cout << vs << std::endl; std::cout << v << std::endl; }
Vector Expressions
void testVectorExpressions() { using namespace boost::numeric::ublas; boost::numeric::ublas::vector > v (3); for (unsigned i = 0; i < v.size (); ++ i) v (i) = std::complex (i*7, i*2);
//The templated class vector_expression is required to be a public base of all classes which model the Vector Expression concept. //operator - computes the additive inverse of a vector expression. conj computes the complex conjugate of a vector expression. real and imag compute the real and imaginary parts of a vector expression. trans computes the transpose of a vector expression. herm computes the hermitian, i.e. the complex conjugate of the transpose of a vector expression.
std::cout << - v << std::endl; std::cout << conj (v) << std::endl; std::cout << real (v) << std::endl; std::cout << imag (v) << std::endl; std::cout << trans (v) << std::endl; std::cout << herm (v) << std::endl; std::cout <<"============================================="<< std::endl;
//operator + computes the sum of two vector expressions. operator - computes the difference of two vector expressions. v1.size==v2.size otherwise throw exception
boost::numeric::ublas::vector v1 (3), v2 (3); for (unsigned i = 0; i < std::min (v1.size (), v2.size ()); ++ i) v1 (i) = v2 (i) = i; std::cout << v1 << std::endl; std::cout << v2 << std::endl; std::cout << v1 + v2 << std::endl; std::cout << v1 - v2 << std::endl; std::cout <<"============================================="<< std::endl;
//outer_prod computes the outer product of two vector expressions.
std::cout << outer_prod (v1, v2) << std::endl;
//inner_prod computes the inner product of the vector expressions. prec_inner_prod computes the double precision inner product of the vector expressions.inner_prod (v1, v2) = sum (v1 [i] * v2 [i])
std::cout << inner_prod (v1, v2) << std::endl; std::cout << prec_inner_prod (v1, v2) << std::endl; std::cout <<"============================================="<< std::endl;
//operator * computes the product of a scalar and a vector expression. operator / multiplies the vector with the reciprocal of the scalar. 2.0/v1 not allowed.
std::cout << v1 / 2.0 << std::endl; std::cout << 2.0 * v1 << std::endl; std::cout <<"============================================="<< std::endl; boost::numeric::ublas::vector v3 (3); for (unsigned i = 0; i < v3.size (); ++ i) v3 (i) = i*2.2; std::cout << v3 << std::endl; std::cout << sum (v3) << std::endl;
// sum v = sum (v [i])
std::cout << norm_1 (v3) << std::endl;
// norm_1 v = sum (abs (v [i]))
std::cout << norm_2 (v3) << std::endl;
// norm_2 v = sqrt (sum (v [i] * v [i]))
std::cout << norm_inf (v3) << std::endl;
// norm_inf v = max (abs (v [i])
std::cout << index_norm_inf (v3) << std::endl;
// index_norm_inf v = min (i: abs (v [i]) == max (abs (v [i])))
}

No comments:

Post a Comment

would you like it. :)