template template par used for a container statistic

Discussion in 'C++' started by er, Sep 5, 2007.

  1. er

    er Guest

    hi,

    a) what's wrong with this code? it looks like allocator is the
    problem.

    the commented version works fine, but if possible i'd rather not limit
    myself to ublas::vector

    b) the version that works is slow compared to mathematica. is there
    anything i can do about it (datatype or algorithm)?

    //code starts here
    #include <numeric>
    #include <stdexcept>
    #include <boost/numeric/ublas/vector.hpp>

    template<class T,template<typename E,typename A = std::allocator<E>
    class C >
    T mean(const C<T>& elems){
    //template<typename T>//works
    //T mean(const boost::numeric::ublas::vector<T>& elems){//works
    long int size = elems.size();
    if(size>0){
    return accumulate(elems.begin(),elems.end(),0.0)/
    size;
    }else{
    throw std::runtime_error("Sample_stats::mean");
    };
    };
    //main:
    boost_auxiliary::vector_double vec(10000000);
    iota(vec.begin(),vec.end(),0.0);
    std::cout<<"compute->"<<std::endl;
    double m =
    Sample_stats::mean<double,boost::numeric::ublas::vector>(vec);

    //code ends here

    make -k all
    Building file: ../main.cpp
    Invoking: GCC C++ Compiler
    g++ -I/usr/local/boost_1_34_1 -O0 -g3 -Wall -c -fmessage-length=0 -
    MMD
    -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.cpp"
    ./main.cpp:21:3: warning: no newline at end of file
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In
    instantiation of 'boost::numeric::ublas::vector<double,
    std::allocator<double> >':
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:253: error: no
    type named 'const_iterator' in 'class std::allocator<double>'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:254: error: no
    type named 'iterator' in 'class std::allocator<double>'
    ./main.cpp: In function 'int main()':
    ./main.cpp:15: warning: unused variable 'm'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In
    constructor
    'boost::numeric::ublas::vector<T, A>::vector(const
    boost::numeric::ublas::vector_expression<E>&) [with AE =
    boost::numeric::ublas::vector<double,
    boost::numeric::ublas::unbounded_array<double, std::allocator<double>
    > >, T = double, A = std::allocator<double>]':

    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:73: error: no
    matching function for call to
    'std::allocator<double>::allocator(size_t)'
    /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
    bits/
    allocator.h:99: note: candidates are:
    std::allocator<_Alloc>::allocator(const std::allocator<_Alloc>&)
    [with
    _Tp = double]
    /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
    bits/
    allocator.h:97: note:
    std::allocator<_Alloc>::allocator() [with _Tp = double]
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: At global
    scope:
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In
    instantiation of 'boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator':
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:299: error: no
    type named 'const_iterator' in 'class std::allocator<double>'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:371: error: no
    type named 'const_iterator' in 'class std::allocator<double>'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In
    instantiation of 'boost::numeric::ublas::vector<double,
    std::allocator<double> >::iterator':
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:403: error: no
    type named 'iterator' in 'class std::allocator<double>'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:472: error: no
    type named 'iterator' in 'class std::allocator<double>'
    ./Sample_stats_mean_var.hpp: In function 'T Sample_stats::mean(const
    C<T, std::allocator<_CharT> >&) [with T = double, C =
    boost::numeric::ublas::vector]':
    ./main.cpp:15: instantiated from here
    ./Sample_stats_mean_var.hpp:25: error: initializing argument 1 of
    '_Tp std::accumulate(_InputIterator, _InputIterator, _Tp) [with
    _InputIterator = boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator, _Tp = double]'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'typename A::size_type boost::numeric::ublas::vector<T,
    A>::size() const [with T = double, A = std::allocator<double>]':
    ./Sample_stats_mean_var.hpp:23: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:90: error:
    'const class std::allocator<double>' has no member named 'size'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'boost::numeric::ublas::vector<T, A>::const_iterator
    boost::numeric::ublas::vector<T, A>::end() const [with T = double, A
    =
    std::allocator<double>]':
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:383: error:
    'const class std::allocator<double>' has no member named 'size'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'boost::numeric::ublas::vector<T, A>::const_iterator
    boost::numeric::ublas::vector<T, A>::find(typename A::size_type)
    const
    [with T = double, A = std::allocator<double>]':
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:379:
    instantiated from 'boost::numeric::ublas::vector<T,
    A>::const_iterator
    boost::numeric::ublas::vector<T, A>::begin() const [with T = double,
    A
    = std::allocator<double>]'
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:269: error:
    'const class std::allocator<double>' has no member named 'begin'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'boost::numeric::ublas::vector<T, A>::const_iterator&
    boost::numeric::ublas::vector<T, A>::const_iterator::eek:perator++()
    [with T = double, A = std::allocator<double>]':
    /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
    bits/
    stl_numeric.h:88: instantiated from '_Tp
    std::accumulate(_InputIterator, _InputIterator, _Tp) [with
    _InputIterator = boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator, _Tp = double]'
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:308: error:
    using invalid field 'boost::numeric::ublas::vector<T,
    A>::const_iterator::it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'typename
    boost::numeric::ublas::type_traits<T>::const_reference
    boost::numeric::ublas::vector<T, A>::const_iterator::eek:perator*()
    const
    [with T = double, A = std::allocator<double>]':
    /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
    bits/
    stl_numeric.h:89: instantiated from '_Tp
    std::accumulate(_InputIterator, _InputIterator, _Tp) [with
    _InputIterator = boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator, _Tp = double]'
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:335: error:
    'class boost::numeric::ublas::vector<double, std::allocator<double>
    >::const_iterator' has no member named 'it_'

    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:335: error:
    using invalid field 'boost::numeric::ublas::vector<T,
    A>::const_iterator::it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:335: error:
    'class boost::numeric::ublas::vector<double, std::allocator<double>
    >::const_iterator' has no member named 'it_'

    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:335: error:
    using invalid field 'boost::numeric::ublas::vector<T,
    A>::const_iterator::it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:336: error:
    using invalid field 'boost::numeric::ublas::vector<T,
    A>::const_iterator::it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'bool boost::numeric::ublas::vector<T,
    A>::const_iterator::eek:perator==(const boost::numeric::ublas::vector<T,
    A>::const_iterator&) const [with T = double, A =
    std::allocator<double>]':
    /usr/local/boost_1_34_1/boost/numeric/ublas/detail/iterator.hpp:279:
    instantiated from 'bool
    boost::numeric::ublas::random_access_iterator_base<IC, I, T,
    D>::eek:perator!=(const I&) const [with IC =
    boost::numeric::ublas::dense_random_access_iterator_tag, I =
    boost::numeric::ublas::vector<double, std::allocator<double>
    >::const_iterator, T = double, D = long int]'

    /usr/lib/gcc/x86_64-linux-gnu/4.1.2/../../../../include/c++/4.1.2/
    bits/
    stl_numeric.h:88: instantiated from '_Tp
    std::accumulate(_InputIterator, _InputIterator, _Tp) [with
    _InputIterator = boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator, _Tp = double]'
    ./Sample_stats_mean_var.hpp:25: instantiated from 'T
    Sample_stats::mean(const C<T, std::allocator<_CharT> >&) [with T =
    double, C = boost::numeric::ublas::vector]'
    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:362: error:
    'const class boost::numeric::ublas::vector<double,
    std::allocator<double> >::const_iterator' has no member named 'it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:362: error:
    using invalid field 'boost::numeric::ublas::vector<T,
    A>::const_iterator::it_'
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp: In member
    function 'T& boost::numeric::ublas::vector<T, A>::eek:perator()(typename
    A::size_type) [with T = double, A = std::allocator<double>]':
    /usr/local/boost_1_34_1/boost/numeric/ublas/detail/vector_assign.hpp:
    268: instantiated from 'void
    boost::numeric::ublas::indexing_vector_assign(V&, const
    boost::numeric::ublas::vector_expression<E>&) [with F =
    boost::numeric::ublas::scalar_assign, V =
    boost::numeric::ublas::vector<double, std::allocator<double> >, E =
    boost::numeric::ublas::vector<double,
    boost::numeric::ublas::unbounded_array<double, std::allocator<double>
    > >]'

    /usr/local/boost_1_34_1/boost/numeric/ublas/detail/vector_assign.hpp:
    280: instantiated from 'void
    boost::numeric::ublas::vector_assign(V&, const
    boost::numeric::ublas::vector_expression<E>&,
    boost::numeric::ublas::dense_proxy_tag) [with F =
    boost::numeric::ublas::scalar_assign, V =
    boost::numeric::ublas::vector<double, std::allocator<double> >, E =
    boost::numeric::ublas::vector<double,
    boost::numeric::ublas::unbounded_array<double, std::allocator<double>
    > >]'

    /usr/local/boost_1_34_1/boost/numeric/ublas/detail/vector_assign.hpp:
    448: instantiated from 'void
    boost::numeric::ublas::vector_assign(V&, const
    boost::numeric::ublas::vector_expression<E>&) [with F =
    boost::numeric::ublas::scalar_assign, V =
    boost::numeric::ublas::vector<double, std::allocator<double> >, E =
    boost::numeric::ublas::vector<double,
    boost::numeric::ublas::unbounded_array<double, std::allocator<double>
    > >]'

    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:74:
    instantiated from 'boost::numeric::ublas::vector<T, A>::vector(const
    boost::numeric::ublas::vector_expression<E>&) [with AE =
    boost::numeric::ublas::vector<double,
    boost::numeric::ublas::unbounded_array<double, std::allocator<double>
    > >, T = double, A = std::allocator<double>]'

    ./main.cpp:15: instantiated from here
    /usr/local/boost_1_34_1/boost/numeric/ublas/vector.hpp:129: error: no
    match for 'operator[]' in 'boost::numeric::ublas::vector<T, A>::data
    [with T = double, A = std::allocator<double>]()'
    make: *** [main.o] Error 1
    make: Target `all' not remade because of errors.
    Build complete for project testing_sample_stat
     
    er, Sep 5, 2007
    #1
    1. Advertising

  2. er

    Barry Guest

    er wrote:
    > hi,
    >
    > a) what's wrong with this code? it looks like allocator is the
    > problem.
    >
    > the commented version works fine, but if possible i'd rather not limit
    > myself to ublas::vector
    >
    > b) the version that works is slow compared to mathematica. is there
    > anything i can do about it (datatype or algorithm)?
    >
    > //code starts here
    > #include <numeric>
    > #include <stdexcept>
    > #include <boost/numeric/ublas/vector.hpp>
    >
    > template<class T,template<typename E,typename A = std::allocator<E>
    > class C >


    It seems that you angle parenthesis don't match

    template
    <
    class T,
    template <typname E, typename A = std::allocator<E> > class C
    >


    one more problem
    can we reference E here for std::allocator<E>?
    I think it's invalid.

    As the code using template temlpate params I read,
    they often ignore the name of the param like
    template
    <
    class T,
    template <class, class> class U
    >


    As the first time I tried to use this tech,
    I wrote

    template
    <
    class T,
    template <class K> class U
    >

    class C
    {
    typedef K the_type;
    };

    using K here is invalid.

    > T mean(const C<T>& elems){
    > //template<typename T>//works
    > //T mean(const boost::numeric::ublas::vector<T>& elems){//works
    > long int size = elems.size();
    > if(size>0){
    > return accumulate(elems.begin(),elems.end(),0.0)/
    > size;
    > }else{
    > throw std::runtime_error("Sample_stats::mean");
    > };
    > };
    > //main:
    > boost_auxiliary::vector_double vec(10000000);
    > iota(vec.begin(),vec.end(),0.0);
    > std::cout<<"compute->"<<std::endl;
    > double m =
    > Sample_stats::mean<double,boost::numeric::ublas::vector>(vec);
    >
    > //code ends here




    --
    Thanks
    Barry
     
    Barry, Sep 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tony lincoln
    Replies:
    0
    Views:
    320
    tony lincoln
    Oct 13, 2003
  2. Replies:
    0
    Views:
    393
  3. PAR, PAR::Packer

    , Dec 1, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    228
    John Bokma
    Dec 1, 2006
  4. Replies:
    0
    Views:
    238
  5. Oliver Soeder
    Replies:
    1
    Views:
    259
Loading...

Share This Page