Ticket #5414: dot_product_bug.cpp

File dot_product_bug.cpp, 2.0 KB (added by joe@…, 12 years ago)

Source

Line 
1/*
2 * reftest.cpp
3 *
4 * Created on: Apr 3, 2011
5 * Author: joe
6 */
7
8#include <iostream>
9#include <boost/fusion/container.hpp>
10#include <boost/fusion/view.hpp>
11#include <boost/fusion/include/io.hpp>
12#include <boost/fusion/algorithm.hpp>
13#include <boost/fusion/include/make_vector.hpp>
14#include <boost/fusion/include/next.hpp>
15#include <boost/fusion/include/prior.hpp>
16#include <boost/fusion/include/deref.hpp>
17#include <boost/mpl/assert.hpp>
18#include <boost/spirit/include/phoenix_function.hpp>
19#include <boost/spirit/include/phoenix_core.hpp>
20#include <boost/spirit/include/phoenix_operator.hpp>
21#include <boost/spirit/include/phoenix_statement.hpp>
22#include <boost/spirit/include/phoenix_scope.hpp>
23#include <boost/spirit/include/phoenix_fusion.hpp>
24#include <boost/mpl/int.hpp>
25
26using namespace boost;
27using namespace boost::fusion;
28using boost::fusion::vector;
29
30namespace functors {
31
32// A functor returns the dot product of two sequences. Presumes that the return type is the
33// type of the first element of the first sequence argument.
34struct vector_dot_product_impl {
35 template <typename V1, typename V2>
36 struct result
37 {
38 // Presume that the return type is the type of the first element
39 // of the first sequence argument.
40 typedef typename boost::fusion::result_of::value_at<V1, boost::mpl::int_<0> >::type type;
41 };
42
43 template <typename V1, typename V2>
44 typename result<V1, V2>::type operator()(V1 v1, V2 v2) const
45 {
46 using boost::phoenix::arg_names::_1;
47 using boost::phoenix::arg_names::_2;
48 using boost::fusion::fold;
49 using boost::fusion::transform;
50 return fold(transform(v1, v2, _1 * _2), 0.0, _1 + _2);
51 }
52};
53boost::phoenix::function<vector_dot_product_impl> const vector_dot_product;
54
55} // namespace functors
56
57
58int main() {
59 typedef vector<int, int, int> int3;
60 typedef vector<int, int> int2;
61
62 int3 i3a = make_vector(1,2,3);
63 int3 i3b = make_vector(4,5,6);
64
65 using boost::phoenix::arg_names::_1;
66 using boost::phoenix::arg_names::_2;
67 std::cout << "DOT: " << functors::vector_dot_product(_1, _2)(i3a, i3b) << std::endl;
68}