• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  Copyright (c) 2018-2019 Cem Bassoy
2 //
3 //  Distributed under the Boost Software License, Version 1.0. (See
4 //  accompanying file LICENSE_1_0.txt or copy at
5 //  http://www.boost.org/LICENSE_1_0.txt)
6 //
7 //  The authors gratefully acknowledge the support of
8 //  Fraunhofer and Google in producing this work
9 //  which started as a Google Summer of Code project.
10 //
11 
12 
13 
14 
15 #include <boost/numeric/ublas/tensor/expression.hpp>
16 #include <boost/numeric/ublas/tensor/tensor.hpp>
17 #include <boost/test/unit_test.hpp>
18 #include "utility.hpp"
19 
20 #include <functional>
21 #include <complex>
22 
23 
24 
25 using test_types = zip<int,long,float,double,std::complex<float>>::with_t<boost::numeric::ublas::first_order, boost::numeric::ublas::last_order>;
26 
27 
28 struct fixture
29 {
30 	using extents_type = boost::numeric::ublas::shape;
fixturefixture31 	fixture()
32 	  : extents {
33 	      extents_type{},            // 0
34 
35 	      extents_type{1,1},         // 1
36 	      extents_type{1,2},         // 2
37 	      extents_type{2,1},         // 3
38 
39 	      extents_type{2,3},         // 4
40 	      extents_type{2,3,1},       // 5
41 	      extents_type{1,2,3},       // 6
42 	      extents_type{1,1,2,3},     // 7
43 	      extents_type{1,2,3,1,1},   // 8
44 
45 	      extents_type{4,2,3},       // 9
46 	      extents_type{4,2,1,3},     // 10
47 	      extents_type{4,2,1,3,1},   // 11
48 	      extents_type{1,4,2,1,3,1} }  // 12
49 	{
50 	}
51 	std::vector<extents_type> extents;
52 };
53 
54 
BOOST_FIXTURE_TEST_CASE_TEMPLATE(test_tensor_expression_access,value,test_types,fixture)55 BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_expression_access, value,  test_types, fixture)
56 {
57 	using namespace boost::numeric;
58 	using value_type  = typename value::first_type;
59 	using layout_type = typename value::second_type;
60 	using tensor_type = ublas::tensor<value_type, layout_type>;
61 	using tensor_expression_type  = typename tensor_type::super_type;
62 
63 
64 	for(auto const& e : extents) {
65 
66 		auto v = value_type{};
67 		auto t = tensor_type(e);
68 
69 		for(auto& tt: t){ tt = v; v+=value_type{1}; }
70 		const auto& tensor_expression_const = static_cast<tensor_expression_type const&>( t );
71 
72 		for(auto i = 0ul; i < t.size(); ++i)
73 			BOOST_CHECK_EQUAL( tensor_expression_const()(i), t(i)  );
74 
75 	}
76 }
77 
78 
79 
BOOST_FIXTURE_TEST_CASE_TEMPLATE(test_tensor_unary_expression,value,test_types,fixture)80 BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_unary_expression, value,  test_types, fixture)
81 {
82 	using namespace boost::numeric;
83 	using value_type  = typename value::first_type;
84 	using layout_type = typename value::second_type;
85 	using tensor_type = ublas::tensor<value_type, layout_type>;
86 
87 	auto uplus1 = std::bind(  std::plus<value_type>{}, std::placeholders::_1, value_type(1) );
88 
89 	for(auto const& e : extents) {
90 
91 		auto t = tensor_type(e);
92 		auto v = value_type{};
93 		for(auto& tt: t) { tt = v; v+=value_type{1}; }
94 
95 		const auto uexpr = ublas::detail::make_unary_tensor_expression<tensor_type>( t, uplus1 );
96 
97 		for(auto i = 0ul; i < t.size(); ++i)
98 			BOOST_CHECK_EQUAL( uexpr(i), uplus1(t(i))  );
99 
100 		auto uexpr_uexpr = ublas::detail::make_unary_tensor_expression<tensor_type>( uexpr, uplus1 );
101 
102 		for(auto i = 0ul; i < t.size(); ++i)
103 			BOOST_CHECK_EQUAL( uexpr_uexpr(i), uplus1(uplus1(t(i)))  );
104 
105 		const auto & uexpr_e = uexpr.e;
106 
107 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(uexpr_e) >, tensor_type > )   );
108 
109 		const auto & uexpr_uexpr_e_e = uexpr_uexpr.e.e;
110 
111 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(uexpr_uexpr_e_e) >, tensor_type > )   );
112 
113 
114 	}
115 }
116 
117 
BOOST_FIXTURE_TEST_CASE_TEMPLATE(test_tensor_binary_expression,value,test_types,fixture)118 BOOST_FIXTURE_TEST_CASE_TEMPLATE( test_tensor_binary_expression, value,  test_types, fixture)
119 {
120 	using namespace boost::numeric;
121 	using value_type  = typename value::first_type;
122 	using layout_type = typename value::second_type;
123 	using tensor_type = ublas::tensor<value_type, layout_type>;
124 
125 	auto uplus1 = std::bind(  std::plus<value_type>{}, std::placeholders::_1, value_type(1) );
126 	auto uplus2 = std::bind(  std::plus<value_type>{}, std::placeholders::_1, value_type(2) );
127 	auto bplus  = std::plus <value_type>{};
128 	auto bminus = std::minus<value_type>{};
129 
130 	for(auto const& e : extents) {
131 
132 		auto t = tensor_type(e);
133 		auto v = value_type{};
134 		for(auto& tt: t){ tt = v; v+=value_type{1}; }
135 
136 		auto uexpr1 = ublas::detail::make_unary_tensor_expression<tensor_type>( t, uplus1 );
137 		auto uexpr2 = ublas::detail::make_unary_tensor_expression<tensor_type>( t, uplus2 );
138 
139 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(uexpr1.e) >, tensor_type > )   );
140 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(uexpr2.e) >, tensor_type > )   );
141 
142 		for(auto i = 0ul; i < t.size(); ++i)
143 			BOOST_CHECK_EQUAL( uexpr1(i), uplus1(t(i))  );
144 
145 		for(auto i = 0ul; i < t.size(); ++i)
146 			BOOST_CHECK_EQUAL( uexpr2(i), uplus2(t(i))  );
147 
148 		auto bexpr_uexpr = ublas::detail::make_binary_tensor_expression<tensor_type>( uexpr1, uexpr2, bplus );
149 
150 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_uexpr.el.e) >, tensor_type > )   );
151 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_uexpr.er.e) >, tensor_type > )   );
152 
153 
154 		for(auto i = 0ul; i < t.size(); ++i)
155 			BOOST_CHECK_EQUAL( bexpr_uexpr(i), bplus(uexpr1(i),uexpr2(i))  );
156 
157 		auto bexpr_bexpr_uexpr = ublas::detail::make_binary_tensor_expression<tensor_type>( bexpr_uexpr, t, bminus );
158 
159 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_bexpr_uexpr.el.el.e) >, tensor_type > )   );
160 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_bexpr_uexpr.el.er.e) >, tensor_type > )   );
161 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_bexpr_uexpr.er) >, tensor_type > )   );
162 		BOOST_CHECK( ( std::is_same_v< std::decay_t< decltype(bexpr_bexpr_uexpr.er) >, tensor_type > )   );
163 
164 		for(auto i = 0ul; i < t.size(); ++i)
165 			BOOST_CHECK_EQUAL( bexpr_bexpr_uexpr(i), bminus(bexpr_uexpr(i),t(i))  );
166 
167 	}
168 
169 
170 }
171