如何在boost / c ++中使用zero_vector进行初始化?

时间:2012-07-19 01:30:09

标签: c++ boost vector ublas boost-ublas

我正在寻找其中一个答案: filling a boost vector or matrix 但是我认为我是新手(和xcode一样),并试图将我的头围绕在提升零点上。

我尝试了一个简单的程序,我认为它与其中一个答案相同:

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main (int argc, char * const argv[]) {
    // insert code here...
    using namespace boost::numeric::ublas;

    int gameSize = 9;

    typedef vector<int> possiblesVector;
    possiblesVector foo;
    foo.resize(gameSize);
    foo = zero_vector<int>(gameSize);
    std::cout << foo << std::endl;

    return 0;
}

编译,但是当它运行时,我得到一个运行时错误(用“/ PATH / TO”代替真实路径)。

Check failed in file /PATH/TO/boost_1_48_0/boost/numeric/ublas/detail/vector_assign.hpp at line 370:
detail::expression_type_check (v, cv)
terminate called after throwing an instance of 'boost::numeric::ublas::external_logic'
  what():  external logic or bad condition of inputs
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

在这里,我只是使用一个main.cpp作为测试区域。在我的真实程序中,我将声明拆分为.h文件,并在我的对象的.cpp文件中初始化。但上面的代码与我的真实程序失败的方式相同。 (也就是为什么我将声明和初始化分成两步)

另外,我知道调整大小已经初始化为零。也许我会做一个scalar_vector,或者我可能需要稍后重置数组或其他东西。我只是试图隔离破坏的代码。

1 个答案:

答案 0 :(得分:0)

    template<template <class T1, class T2> class F, class V, class E>
    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    void vector_assign (V &v, const vector_expression<E> &e, sparse_tag) {
        BOOST_UBLAS_CHECK (v.size () == e ().size (), bad_size ());
        typedef F<typename V::iterator::reference, typename E::value_type> functor_type;
        BOOST_STATIC_ASSERT ((!functor_type::computed));
        typedef typename V::value_type value_type;
#if BOOST_UBLAS_TYPE_CHECK
        vector<value_type> cv (v.size ());
        indexing_vector_assign<scalar_assign> (cv, v);
        indexing_vector_assign<F> (cv, e);
#endif
        v.clear ();
        typename E::const_iterator ite (e ().begin ());
        typename E::const_iterator ite_end (e ().end ());
        while (ite != ite_end) {
            value_type t (*ite);
            if (t != value_type/*zero*/())
                v.insert_element (ite.index (), t);
            ++ ite;
        }
#if BOOST_UBLAS_TYPE_CHECK
        if (! disable_type_check<bool>::value) 
            BOOST_UBLAS_CHECK (detail::expression_type_check (v, cv), 
                               external_logic ("external logic or bad condition of inputs"));
#endif
    }

其中v是您的vector<int> possiblesVector,e是临时zero_vector<int>

// Weak equality check - useful to compare equality two arbitary vector expression results.
// Since the actual expressions are unknown, we check for and arbitary error bound
// on the relative error.
// For a linear expression the infinity norm makes sense as we do not know how the elements will be
// combined in the expression. False positive results are inevitable for arbirary expressions!
template<class E1, class E2, class S>
BOOST_UBLAS_INLINE
bool equals (const vector_expression<E1> &e1, const vector_expression<E2> &e2, S epsilon, S min_norm) {
    return norm_inf (e1 - e2) < epsilon *
           std::max<S> (std::max<S> (norm_inf (e1), norm_inf (e2)), min_norm);
}

template<class E1, class E2>
BOOST_UBLAS_INLINE
bool expression_type_check (const vector_expression<E1> &e1, const vector_expression<E2> &e2) {
    typedef typename type_traits<typename promote_traits<typename E1::value_type,
                                 typename E2::value_type>::promote_type>::real_type real_type;
    return equals (e1, e2, BOOST_UBLAS_TYPE_CHECK_EPSILON, BOOST_UBLAS_TYPE_CHECK_MIN);
}

检查功能。

zero_vector的所有元素都是0,所以在

之后
    v.clear ();
    typename E::const_iterator ite (e ().begin ());
    typename E::const_iterator ite_end (e ().end ());
    while (ite != ite_end) {
        value_type t (*ite);
        if (t != value_type/*zero*/())
            v.insert_element (ite.index (), t);
        ++ ite;
    }

vector v rest为空并检查失败。 尝试使用doublefloat代替int