#include "ublas_ser.hpp" #include #include #include #include #include #include #include #include //#include //#include //#include //#include namespace ublas = boost::numeric::ublas; template std::string Saveit (V const& v) { std::ostringstream os; boost::archive::binary_oarchive oa (os); oa << v; return os.str(); } template V Loadit (std::string const& s) { V v; std::istringstream is (s); boost::archive::binary_iarchive ia (is); ia >> v; return v; } // template // struct Scalar { // typedef T type; // }; // template // struct Scalar > { // typedef T type; // }; template struct is_cmplx : public boost::false_type {}; template struct is_cmplx > : public boost::true_type {}; //! both args are integral template //bool is_close_scalar (a_t a, b_t b, const boost::true_type&) { bool is_close_scalar (a_t a, b_t b, const boost::mpl::true_&) { return a == b; } //! at least one arg is not integral template //bool is_close_scalar (a_t a, a_t b, const boost::false_type&) { bool is_close_scalar (a_t a, b_t b, const boost::mpl::false_&) { return std::abs (a - b) < 1e-6; } //! scalar template //inline bool is_close_2 (a_t a, b_t b, const boost::false_type&) { inline bool is_close_2 (a_t a, b_t b, const boost::mpl::false_&) { return is_close_scalar (a, b, typename boost::mpl::and_,boost::is_integral >::type()); } //! complex /*! not using std::abs so this will also work on complex */ template //inline bool is_close_2 (a_t a, b_t b, const boost::true_type&) { inline bool is_close_2 (a_t a, b_t b, const boost::mpl::true_&) { BOOST_STATIC_ASSERT (is_cmplx::value && is_cmplx::value); return is_close_2 (real(a), real(b), boost::false_type()) && is_close_2 (imag(a), imag(b), boost::false_type()); } //! Works for integral, float, or complex template inline bool is_close (a_t a, b_t b) { return is_close_2 (a, b, typename boost::mpl::or_,is_cmplx >::type()); } //! 2 sequences are close if they are same length and elements compare close template inline bool is_close_seq (a_t const& a, b_t const& b) { if (boost::size (a) != boost::size (b)) return false; typename boost::range_const_iterator::type i = boost::begin (a); typename boost::range_const_iterator::type j = boost::begin (b); for (; i != boost::end (a); ++i, ++j) if (!is_close (*i, *j)) return false; return true; } template void check_close_seq (a_t const& a, b_t const&b) { BOOST_CHECK (is_close_seq (a, b)); } // template // inline bool equal (a_t const& a, b_t const& b) { // return std::equal (boost::begin (a), boost::end (a), boost::begin (b)); // } template void run_check() { ublas::vector a (10); for (int i = 0; i < a.size(); ++i) a[i] = i; std::string s = Saveit (a); ublas::vector b = Loadit > (s); check_close_seq (a, b); } int test_main( int, char *[] ) { run_check(); run_check(); run_check >(); exit (0); }