Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / ublas_interop / test13.cpp
1 //
2 //  Copyright (c) 2000-2002
3 //  Joerg Walter, Mathias Koch
4 //
5 //  Distributed under the Boost Software License, Version 1.0. (See
6 //  accompanying file LICENSE_1_0.txt or copy at
7 //  http://www.boost.org/LICENSE_1_0.txt)
8 //
9 //  The authors gratefully acknowledge the support of
10 //  GeNeSys mbH & Co. KG in producing this work.
11 //
12
13 #if defined(__GNUC__) && (__GNUC__ >= 9)
14 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
15 #endif
16
17 #include "test1.hpp"
18
19 // Test matrix expression templates
20 template <class M, int N>
21 struct test_my_matrix
22 {
23    typedef typename M::value_type value_type;
24
25    template <class VP>
26    void test_container_with(VP& v1) const
27    {
28       // Container type tests in addition to expression types
29       // Insert and erase
30       v1.insert_element(0, 0, 55);
31       v1.erase_element(1, 1);
32       v1.clear();
33    }
34
35    template <class MP>
36    void test_expression_with(MP& m1, MP& m2, MP& m3) const
37    {
38       value_type t;
39
40       // Default Construct
41       default_construct<MP>::test();
42
43       // Copy and swap
44       initialize_matrix(m1);
45       initialize_matrix(m2);
46       m1 = m2;
47       std::cout << "m1 = m2 = " << m1 << std::endl;
48       m1.assign_temporary(m2);
49       std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl;
50       m1.swap(m2);
51       std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl;
52
53       // Zero assignment
54       m1 = ublas::zero_matrix<>(m1.size1(), m1.size2());
55       std::cout << "m1.zero_matrix = " << m1 << std::endl;
56       m1 = m2;
57
58 #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
59       // Project range and slice
60       initialize_matrix(m1);
61       initialize_matrix(m2);
62       project(m1, ublas::range(0, 1), ublas::range(0, 1))         = project(m2, ublas::range(0, 1), ublas::range(0, 1));
63       project(m1, ublas::range(0, 1), ublas::range(0, 1))         = project(m2, ublas::slice(0, 1, 1), ublas::slice(0, 1, 1));
64       project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::slice(0, 1, 2), ublas::slice(0, 1, 2));
65       project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::range(0, 2), ublas::range(0, 2));
66       std::cout << "m1 = range/slice " << m1 << std::endl;
67 #endif
68
69       // Unary matrix operations resulting in a matrix
70       initialize_matrix(m1);
71       m2 = -m1;
72       std::cout << "- m1 = " << m2 << std::endl;
73       m2 = ublas::conj(m1);
74       std::cout << "conj (m1) = " << m2 << std::endl;
75
76       // Binary matrix operations resulting in a matrix
77       initialize_matrix(m1);
78       initialize_matrix(m2);
79       m3 = m1 + m2;
80       std::cout << "m1 + m2 = " << m3 << std::endl;
81       m3 = m1 - m2;
82       std::cout << "m1 - m2 = " << m3 << std::endl;
83       m3 = ublas::element_prod(m1, m2);
84       std::cout << "element_prod (m1, m2) = " << m3 << std::endl;
85
86       // Scaling a matrix
87       t = N;
88       initialize_matrix(m1);
89       m2 = value_type(1.) * m1;
90       std::cout << "1. * m1 = " << m2 << std::endl;
91       m2 = t * m1;
92       std::cout << "N * m1 = " << m2 << std::endl;
93       initialize_matrix(m1);
94       m2 = m1 * value_type(1.);
95       std::cout << "m1 * 1. = " << m2 << std::endl;
96       m2 = m1 * t;
97       std::cout << "m1 * N = " << m2 << std::endl;
98
99       // Some assignments
100       initialize_matrix(m1);
101       initialize_matrix(m2);
102       m2 += m1;
103       std::cout << "m2 += m1 = " << m2 << std::endl;
104       m2 -= m1;
105       std::cout << "m2 -= m1 = " << m2 << std::endl;
106       m2 = m2 + m1;
107       std::cout << "m2 = m2 + m1 = " << m2 << std::endl;
108       m2 = m2 - m1;
109       std::cout << "m2 = m2 - m1 = " << m2 << std::endl;
110       m1 *= value_type(1.);
111       std::cout << "m1 *= 1. = " << m1 << std::endl;
112       m1 *= t;
113       std::cout << "m1 *= N = " << m1 << std::endl;
114
115       // Transpose
116       initialize_matrix(m1);
117       m2 = ublas::trans(m1);
118       std::cout << "trans (m1) = " << m2 << std::endl;
119
120       // Hermitean
121       initialize_matrix(m1);
122       m2 = ublas::herm(m1);
123       std::cout << "herm (m1) = " << m2 << std::endl;
124
125       // Matrix multiplication
126       initialize_matrix(m1);
127       initialize_matrix(m2);
128       m3 = ublas::prod(m1, m2);
129       std::cout << "prod (m1, m2) = " << m3 << std::endl;
130    }
131
132    void operator()() const
133    {
134       M m1(N, N), m2(N, N), m3(N, N);
135       test_expression_with(m1, m2, m3);
136       test_container_with(m1);
137
138 #ifdef USE_RANGE
139       ublas::matrix_range<M> mr1(m1, ublas::range(0, N), ublas::range(0, N)),
140           mr2(m2, ublas::range(0, N), ublas::range(0, N)),
141           mr3(m3, ublas::range(0, N), ublas::range(0, N));
142       test_expression_with(mr1, mr2, mr3);
143 #endif
144
145 #ifdef USE_SLICE
146       ublas::matrix_slice<M> ms1(m1, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
147           ms2(m2, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
148           ms3(m3, ublas::slice(0, 1, N), ublas::slice(0, 1, N));
149       test_expression_with(ms1, ms2, ms3);
150 #endif
151    }
152 };
153
154 // Test matrix
155 void test_matrix()
156 {
157    std::cout << "test_matrix" << std::endl;
158
159 #ifdef USE_MATRIX
160 #ifdef USE_BOUNDED_ARRAY
161 #ifdef USE_FLOAT
162    std::cout << "mp_test_type, bounded_array" << std::endl;
163    test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, ublas::bounded_array<mp_test_type, 3 * 3> >, 3>()();
164 #endif
165
166 #ifdef USE_DOUBLE
167    std::cout << "double, bounded_array" << std::endl;
168    test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3>()();
169 #endif
170
171 #ifdef USE_STD_COMPLEX
172 #ifdef USE_FLOAT
173    std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
174    test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, ublas::bounded_array<std::complex<mp_test_type>, 3 * 3> >, 3>()();
175 #endif
176
177 #ifdef USE_DOUBLE
178    std::cout << "std::complex<double>, bounded_array" << std::endl;
179    test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3>()();
180 #endif
181 #endif
182 #endif
183
184 #ifdef USE_UNBOUNDED_ARRAY
185 #ifdef USE_FLOAT
186    std::cout << "mp_test_type, unbounded_array" << std::endl;
187    test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, ublas::unbounded_array<mp_test_type> >, 3>()();
188 #endif
189
190 #ifdef USE_DOUBLE
191    std::cout << "double, unbounded_array" << std::endl;
192    test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3>()();
193 #endif
194
195 #ifdef USE_STD_COMPLEX
196 #ifdef USE_FLOAT
197    std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
198    test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, ublas::unbounded_array<std::complex<mp_test_type> > >, 3>()();
199 #endif
200
201 #ifdef USE_DOUBLE
202    std::cout << "std::complex<double>, unbounded_array" << std::endl;
203    test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3>()();
204 #endif
205 #endif
206 #endif
207
208 #ifdef USE_STD_VECTOR
209 #ifdef USE_FLOAT
210    std::cout << "mp_test_type, std::vector" << std::endl;
211    test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, std::vector<mp_test_type> >, 3>()();
212 #endif
213
214 #ifdef USE_DOUBLE
215    std::cout << "double, std::vector" << std::endl;
216    test_my_matrix<ublas::matrix<double, ublas::row_major, std::vector<double> >, 3>()();
217 #endif
218
219 #ifdef USE_STD_COMPLEX
220 #ifdef USE_FLOAT
221    std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
222    test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, std::vector<std::complex<mp_test_type> > >, 3>()();
223 #endif
224
225 #ifdef USE_DOUBLE
226    std::cout << "std::complex<double>, std::vector" << std::endl;
227    test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3>()();
228 #endif
229 #endif
230 #endif
231 #endif
232
233 #ifdef USE_BOUNDED_MATRIX
234 #ifdef USE_FLOAT
235    std::cout << "mp_test_type, bounded" << std::endl;
236    test_my_matrix<ublas::bounded_matrix<mp_test_type, 3, 3>, 3>()();
237 #endif
238
239 #ifdef USE_DOUBLE
240    std::cout << "double, bounded" << std::endl;
241    test_my_matrix<ublas::bounded_matrix<double, 3, 3>, 3>()();
242 #endif
243
244 #ifdef USE_STD_COMPLEX
245 #ifdef USE_FLOAT
246    std::cout << "std::complex<mp_test_type>, bounded" << std::endl;
247    test_my_matrix<ublas::bounded_matrix<std::complex<mp_test_type>, 3, 3>, 3>()();
248 #endif
249
250 #ifdef USE_DOUBLE
251    std::cout << "std::complex<double>, bounded" << std::endl;
252    test_my_matrix<ublas::bounded_matrix<std::complex<double>, 3, 3>, 3>()();
253 #endif
254 #endif
255 #endif
256
257 #ifdef USE_VECTOR_OF_VECTOR
258 #ifdef USE_BOUNDED_ARRAY
259 #ifdef USE_FLOAT
260    std::cout << "mp_test_type, bounded_array" << std::endl;
261    test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, ublas::bounded_array<ublas::bounded_array<mp_test_type, 3>, 3 + 1> >, 3>()();
262 #endif
263
264 #ifdef USE_DOUBLE
265    std::cout << "double, bounded_array" << std::endl;
266    test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::bounded_array<ublas::bounded_array<double, 3>, 3 + 1> >, 3>()();
267 #endif
268
269 #ifdef USE_STD_COMPLEX
270 #ifdef USE_FLOAT
271    std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
272    test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<mp_test_type>, 3>, 3 + 1> >, 3>()();
273 #endif
274
275 #ifdef USE_DOUBLE
276    std::cout << "std::complex<double>, bounded_array" << std::endl;
277    test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<double>, 3>, 3 + 1> >, 3>()();
278 #endif
279 #endif
280 #endif
281
282 #ifdef USE_UNBOUNDED_ARRAY
283 #ifdef USE_FLOAT
284    std::cout << "mp_test_type, unbounded_array" << std::endl;
285    test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<mp_test_type> > >, 3>()();
286 #endif
287
288 #ifdef USE_DOUBLE
289    std::cout << "double, unbounded_array" << std::endl;
290    test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<double> > >, 3>()();
291 #endif
292
293 #ifdef USE_STD_COMPLEX
294 #ifdef USE_FLOAT
295    std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
296    test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<mp_test_type> > > >, 3>()();
297 #endif
298
299 #ifdef USE_DOUBLE
300    std::cout << "std::complex<double>, unbounded_array" << std::endl;
301    test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<double> > > >, 3>()();
302 #endif
303 #endif
304 #endif
305
306 #ifdef USE_STD_VECTOR
307 #ifdef USE_FLOAT
308    std::cout << "mp_test_type, std::vector" << std::endl;
309    test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, std::vector<std::vector<mp_test_type> > >, 3>()();
310 #endif
311
312 #ifdef USE_DOUBLE
313    std::cout << "double, std::vector" << std::endl;
314    test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, std::vector<std::vector<double> > >, 3>()();
315 #endif
316
317 #ifdef USE_STD_COMPLEX
318 #ifdef USE_FLOAT
319    std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
320    test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, std::vector<std::vector<std::complex<mp_test_type> > > >, 3>()();
321 #endif
322
323 #ifdef USE_DOUBLE
324    std::cout << "std::complex<double>, std::vector" << std::endl;
325    test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, std::vector<std::vector<std::complex<double> > > >, 3>()();
326 #endif
327 #endif
328 #endif
329 #endif
330 }