Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / multiprecision / test / ublas_interop / common / init.hpp
1 /*
2  * Copyright (c) 2004 Michael Stevens
3  * Use, modification and distribution are subject to the
4  * Boost Software License, Version 1.0. (See accompanying file
5  * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  */
7
8 /*
9  * Default construct test when possible
10  */
11
12 template <class E>
13 struct default_construct
14 {
15    static void test() {}
16 };
17 template <class VC>
18 struct default_construct<boost::numeric::ublas::vector_container<VC> >
19 {
20    static void test()
21    {
22       VC default_constuct;
23       initialize_vector(default_constuct);
24       std::cout << "default construct = " << default_constuct << std::endl;
25    }
26 };
27 template <class MC>
28 struct default_construct<boost::numeric::ublas::matrix_container<MC> >
29 {
30    static void test()
31    {
32       MC default_constuct;
33       initialize_vector(default_constuct);
34       std::cout << "default construct = " << default_constuct << std::endl;
35    }
36 };
37
38 /*
39  * Initialise test values in vector/matrix
40  */
41
42 template <class V>
43 void initialize_vector(V& v)
44 {
45    typename V::size_type size = v.size();
46    for (typename V::size_type i = 0; i < size; ++i)
47       v[i] = typename V::value_type(i + 1.f);
48 }
49
50 template <class M>
51 void initialize_matrix_impl(M& m, ublas::packed_proxy_tag)
52 {
53    typename M::size_type size1 = m.size1();
54 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
55    for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++i)
56       for (typename M::iterator2 j = i.begin(); j != i.end(); ++j)
57          *j = typename M::value_type(i.index1() * size1 + j.index2() + 1.f);
58 #else
59    for (typename M::iterator1 i = m.begin1(); i != m.end1(); ++i)
60       for (typename M::iterator2 j = ublas::begin(i, ublas::iterator1_tag()); j != ublas::end(i, ublas::iterator1_tag()); ++j)
61          *j = typename M::value_type(i.index1() * size1 + j.index2() + 1.f);
62 #endif
63 }
64
65 template <class M>
66 void initialize_matrix_impl(M& m, ublas::sparse_proxy_tag)
67 {
68    typename M::size_type size1 = m.size1();
69    typename M::size_type size2 = m.size2();
70    for (typename M::size_type i = 0; i < size1; ++i)
71       for (typename M::size_type j = 0; j < size2; ++j)
72          m(i, j) = typename M::value_type(i * size1 + j + 1.f);
73 }
74
75 template <class M>
76 void initialize_matrix(M& m)
77 {
78    initialize_matrix_impl(m, typename M::storage_category());
79 }
80
81 template <class M>
82 void initialize_matrix(M& m, ublas::lower_tag)
83 {
84    typename M::size_type size1 = m.size1();
85    typename M::size_type size2 = m.size2();
86    for (typename M::size_type i = 0; i < size1; ++i)
87    {
88       typename M::size_type j = 0;
89       for (; j <= i; ++j)
90          m(i, j) = i * size1 + j + 1.f;
91       for (; j < size2; ++j)
92          m(i, j) = 0.f;
93    }
94 }
95 template <class M>
96 void initialize_matrix(M& m, ublas::upper_tag)
97 {
98    typename M::size_type size1 = m.size1();
99    typename M::size_type size2 = m.size2();
100    for (typename M::size_type i = 0; i < size1; ++i)
101    {
102       typename M::size_type j = 0;
103       for (; j < i; ++j)
104          m(i, j) = 0.f;
105       for (; j < size2; ++j)
106          m(i, j) = i * size1 + j + 1.f;
107    }
108 }