|
Ublas : |
From: Georg Baum (Georg.Baum_at_[hidden])
Date: 2006-08-13 04:55:13
Am Sonntag, 13. August 2006 01:54 schrieb Nico Galoppo:
> Hi,
>
> what is wrong with the following code snippet? On my compiler (msvc 8.0)
it
> outputs nothing, because the for loop terminates immediately.
>
> int main()
> {
> typedef ublas::coordinate_vector<double> cv_type;
> cv_type v;
> v.append_element(3, 40);
> v.append_element(5, 41);
> v.append_element(7, 42);
> v.append_element(300, 43);
>
> for (cv_type::iterator it = v.begin(); it != v.end(); ++it)
> std::cout << "(" << it.index() << "," << *it << ")" << std::endl;
> }
Please send a compilable code next time:
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <iostream>
int main()
{
typedef boost::numeric::ublas::coordinate_vector<double> cv_type;
cv_type v;
v.append_element(3, 40);
v.append_element(5, 41);
v.append_element(7, 42);
v.append_element(300, 43);
for (cv_type::iterator it = v.begin(); it != v.end(); ++it)
std::cout << "(" << it.index() << "," << *it << ")" << std::endl;
return 0;
}
> The following snippet gives output, but not all elements are shown. I
have the
> impression that I don't understand the meaning of the different
constructors
> and/or the difference between the 'size' and 'non_zeros' parameters in
the
> coordinate_vector (size_type size, size_type non_zeros) constructor.
That is simple: size is the mathematical size of the vector. non_zeros is
the number of elements that can actually be stored (structural
non-zeroes). All other elements are implicitly zero.
Therefore you can't insert an element at position 300 if the vector is of
mathematical size 10. You need at least
cv_type v(301);
> int main()
> {
> typedef ublas::cooordinate_vector<double> cv_type;
> cv_type v(10);
> v.insert_element(3, 40);
> v.insert_element(5, 41);
> v.insert_element(7, 42);
> v.insert_element(300, 43);
>
> for (cv_type::iterator it = v.begin(); it != v.end(); ++it)
> std::cout << "(" << it.index() << "," << *it << ")" << std::endl;
> }
It works too with append_element if you give the vector an initial size.
IMO the name append_element is suboptimal: It suggests that it enlarges
the mathematical size of the vector, but that is not the case.
I also get a suspicious warning when compiling this code (using boost cvs)
with gcc 4.1:
/usr/include/boost/numeric/ublas/storage.hpp: In copy
constructor 'boost::numeric::ublas::index_pair<V>::index_pair(const
boost::numeric::ublas::index_pair<V>&) [with V =
boost::numeric::ublas::index_pair_array<boost::numeric::ublas::unbounded_array<unsigned
int, std::allocator<unsigned int> >,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> >
>]':
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2642:
instantiated from 'void std::__introsort_loop(_RandomAccessIterator,
_RandomAccessIterator, _Size) [with _RandomAccessIterator =
boost::numeric::ublas::indexed_iterator<boost::numeric::ublas::index_pair_array<boost::numeric::ublas::unbounded_array<unsigned
int, std::allocator<unsigned int> >,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
std::random_access_iterator_tag>, _Size = int]'
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2713:
instantiated from 'void std::sort(_RandomAccessIterator,
_RandomAccessIterator) [with _RandomAccessIterator =
boost::numeric::ublas::indexed_iterator<boost::numeric::ublas::index_pair_array<boost::numeric::ublas::unbounded_array<unsigned
int, std::allocator<unsigned int> >,
boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >,
std::random_access_iterator_tag>]'
/usr/include/boost/numeric/ublas/vector_sparse.hpp:1721: instantiated
from 'void boost::numeric::ublas::coordinate_vector<T, IB, IA, TA>::sort()
const [with T = double, unsigned int IB = 0u, IA =
boost::numeric::ublas::unbounded_array<unsigned int,
std::allocator<unsigned int> >, TA =
boost::numeric::ublas::unbounded_array<double, std::allocator<double> >]'
/usr/include/boost/numeric/ublas/vector_sparse.hpp:1493: instantiated
from 'void boost::numeric::ublas::coordinate_vector<T, IB, IA,
TA>::reserve(typename IA::value_type, bool) [with T = double, unsigned int
IB = 0u, IA = boost::numeric::ublas::unbounded_array<unsigned int,
std::allocator<unsigned int> >, TA =
boost::numeric::ublas::unbounded_array<double, std::allocator<double> >]'
/usr/include/boost/numeric/ublas/vector_sparse.hpp:1566: instantiated
from 'void boost::numeric::ublas::coordinate_vector<T, IB, IA,
TA>::append_element(typename IA::value_type, const T&) [with T = double,
unsigned int IB = 0u, IA = boost::numeric::ublas::unbounded_array<unsigned
int, std::allocator<unsigned int> >, TA =
boost::numeric::ublas::unbounded_array<double, std::allocator<double> >]'
nico.cpp:8: instantiated from here
/usr/include/boost/numeric/ublas/storage.hpp:1550: warning: base
class 'class boost::noncopyable_::noncopyable' should be explicitly
initialized in the copy constructor
If index_pair has a copy constructor then it should not derive from
boost::noncopyable.
Georg