| 1 | // | 
|---|
| 2 | //  Copyright (c) 2000-2002 | 
|---|
| 3 | //  Joerg Walter, Mathias Koch | 
|---|
| 4 | // | 
|---|
| 5 | //  Permission to use, copy, modify, distribute and sell this software | 
|---|
| 6 | //  and its documentation for any purpose is hereby granted without fee, | 
|---|
| 7 | //  provided that the above copyright notice appear in all copies and | 
|---|
| 8 | //  that both that copyright notice and this permission notice appear | 
|---|
| 9 | //  in supporting documentation.  The authors make no representations | 
|---|
| 10 | //  about the suitability of this software for any purpose. | 
|---|
| 11 | //  It is provided "as is" without express or implied warranty. | 
|---|
| 12 | // | 
|---|
| 13 | //  The authors gratefully acknowledge the support of | 
|---|
| 14 | //  GeNeSys mbH & Co. KG in producing this work. | 
|---|
| 15 | // | 
|---|
| 16 |  | 
|---|
| 17 | #include "test7.hpp" | 
|---|
| 18 |  | 
|---|
| 19 | // Test matrix expression templates | 
|---|
| 20 | template<class M, int N> | 
|---|
| 21 | struct test_my_matrix { | 
|---|
| 22 |     typedef typename M::value_type value_type; | 
|---|
| 23 |  | 
|---|
| 24 |     template<class MP> | 
|---|
| 25 |     void test_with (MP &m1, MP &m2, MP &m3) const { | 
|---|
| 26 |         { | 
|---|
| 27 |             value_type t; | 
|---|
| 28 |  | 
|---|
| 29 |             // Copy and swap | 
|---|
| 30 |             initialize_matrix (m1); | 
|---|
| 31 |             initialize_matrix (m2); | 
|---|
| 32 |             m1 = m2; | 
|---|
| 33 |             std::cout << "m1 = m2 = " << m1 << std::endl; | 
|---|
| 34 |             m1.assign_temporary (m2); | 
|---|
| 35 |             std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl; | 
|---|
| 36 |             m1.swap (m2); | 
|---|
| 37 |             std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl; | 
|---|
| 38 |  | 
|---|
| 39 |             // Zero assignment | 
|---|
| 40 |             m1 = ublas::zero_matrix<> (m1.size (), m1.size2 ()); | 
|---|
| 41 |             std::cout << "m1.zero_matrix = " << m1 << std::endl; | 
|---|
| 42 |             m1 = m2; | 
|---|
| 43 |  | 
|---|
| 44 |             // Unary matrix operations resulting in a matrix | 
|---|
| 45 |             initialize_matrix (m1); | 
|---|
| 46 |             m2 = - m1; | 
|---|
| 47 |             std::cout << "- m1 = " << m2 << std::endl; | 
|---|
| 48 |             m2 = ublas::conj (m1); | 
|---|
| 49 |             std::cout << "conj (m1) = " << m2 << std::endl; | 
|---|
| 50 |  | 
|---|
| 51 |             // Binary matrix operations resulting in a matrix | 
|---|
| 52 |             initialize_matrix (m1); | 
|---|
| 53 |             initialize_matrix (m2); | 
|---|
| 54 |             m3 = m1 + m2; | 
|---|
| 55 |             std::cout << "m1 + m2 = " << m3 << std::endl; | 
|---|
| 56 |             m3 = m1 - m2; | 
|---|
| 57 |             std::cout << "m1 - m2 = " << m3 << std::endl; | 
|---|
| 58 |  | 
|---|
| 59 |             // Scaling a matrix | 
|---|
| 60 |             t = N; | 
|---|
| 61 |             initialize_matrix (m1); | 
|---|
| 62 |             m2 = value_type (1.) * m1; | 
|---|
| 63 |             std::cout << "1. * m1 = " << m2 << std::endl; | 
|---|
| 64 |             m2 = t * m1; | 
|---|
| 65 |             std::cout << "N * m1 = " << m2 << std::endl; | 
|---|
| 66 |             initialize_matrix (m1); | 
|---|
| 67 |             m2 = m1 * value_type (1.); | 
|---|
| 68 |             std::cout << "m1 * 1. = " << m2 << std::endl; | 
|---|
| 69 |             m2 = m1 * t; | 
|---|
| 70 |             std::cout << "m1 * N = " << m2 << std::endl; | 
|---|
| 71 |  | 
|---|
| 72 |             // Some assignments | 
|---|
| 73 |             initialize_matrix (m1); | 
|---|
| 74 |             initialize_matrix (m2); | 
|---|
| 75 |             m2 += m1; | 
|---|
| 76 |             std::cout << "m2 += m1 = " << m2 << std::endl; | 
|---|
| 77 |             m2 -= m1; | 
|---|
| 78 |             std::cout << "m2 -= m1 = " << m2 << std::endl; | 
|---|
| 79 |             m2 = m2 + m1; | 
|---|
| 80 |             std::cout << "m2 = m2 + m1 = " << m2 << std::endl; | 
|---|
| 81 |             m2 = m2 - m1; | 
|---|
| 82 |             std::cout << "m2 = m1 - m1 = " << m2 << std::endl; | 
|---|
| 83 |             m1 *= value_type (1.); | 
|---|
| 84 |             std::cout << "m1 *= 1. = " << m1 << std::endl; | 
|---|
| 85 |             m1 *= t; | 
|---|
| 86 |             std::cout << "m1 *= N = " << m1 << std::endl; | 
|---|
| 87 |  | 
|---|
| 88 |             // Transpose | 
|---|
| 89 |             initialize_matrix (m1); | 
|---|
| 90 |             m2 = ublas::trans (m1); | 
|---|
| 91 |             std::cout << "trans (m1) = " << m2 << std::endl; | 
|---|
| 92 |  | 
|---|
| 93 |             // Hermitean | 
|---|
| 94 |             initialize_matrix (m1); | 
|---|
| 95 |             m2 = ublas::herm (m1); | 
|---|
| 96 |             std::cout << "herm (m1) = " << m2 << std::endl; | 
|---|
| 97 |  | 
|---|
| 98 |             // Matrix multiplication | 
|---|
| 99 |             initialize_matrix (m1); | 
|---|
| 100 |             initialize_matrix (m2); | 
|---|
| 101 |             m3 = ublas::prod (m1, m2); | 
|---|
| 102 |             std::cout << "prod (m1, m2) = " << m3 << std::endl; | 
|---|
| 103 |         } | 
|---|
| 104 |     } | 
|---|
| 105 |     void operator () () const { | 
|---|
| 106 |         { | 
|---|
| 107 |             M m1 (N, N), m2 (N, N), m3 (N, N); | 
|---|
| 108 |             test_with (m1, m2, m3); | 
|---|
| 109 |  | 
|---|
| 110 | #ifdef USE_RANGE | 
|---|
| 111 |             ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)), | 
|---|
| 112 |                                    mr2 (m2, ublas::range (0, N), ublas::range (0, N)), | 
|---|
| 113 |                                    mr3 (m3, ublas::range (0, N), ublas::range (0, N)); | 
|---|
| 114 |             test_with (mr1, mr2, mr3); | 
|---|
| 115 | #endif | 
|---|
| 116 |  | 
|---|
| 117 | #ifdef USE_SLICE | 
|---|
| 118 |             ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), | 
|---|
| 119 |                                    ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), | 
|---|
| 120 |                                    ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); | 
|---|
| 121 |             test_with (ms1, ms2, ms3); | 
|---|
| 122 | #endif | 
|---|
| 123 |         } | 
|---|
| 124 |     } | 
|---|
| 125 | }; | 
|---|
| 126 |  | 
|---|
| 127 | // Test matrix | 
|---|
| 128 | void test_matrix () { | 
|---|
| 129 |     std::cout << "test_matrix" << std::endl; | 
|---|
| 130 |  | 
|---|
| 131 | #ifdef USE_MATRIX | 
|---|
| 132 | #ifdef USE_BOUNDED_ARRAY | 
|---|
| 133 | #ifdef USE_FLOAT | 
|---|
| 134 |     std::cout << "boost::numeric::interval<float>, bounded_array" << std::endl; | 
|---|
| 135 |     test_my_matrix<ublas::matrix<boost::numeric::interval<float>, ublas::row_major, ublas::bounded_array<boost::numeric::interval<float>, 3 * 3> >, 3 > () (); | 
|---|
| 136 | #endif | 
|---|
| 137 |  | 
|---|
| 138 | #ifdef USE_DOUBLE | 
|---|
| 139 |     std::cout << "boost::numeric::interval<double>, bounded_array" << std::endl; | 
|---|
| 140 |     test_my_matrix<ublas::matrix<boost::numeric::interval<double>, ublas::row_major, ublas::bounded_array<boost::numeric::interval<double>, 3 * 3> >, 3 > () (); | 
|---|
| 141 | #endif | 
|---|
| 142 | #endif | 
|---|
| 143 |  | 
|---|
| 144 | #ifdef USE_UNBOUNDED_ARRAY | 
|---|
| 145 | #ifdef USE_FLOAT | 
|---|
| 146 |     std::cout << "boost::numeric::interval<float>, unbounded_array" << std::endl; | 
|---|
| 147 |     test_my_matrix<ublas::matrix<boost::numeric::interval<float>, ublas::row_major, ublas::unbounded_array<boost::numeric::interval<float> > >, 3 > () (); | 
|---|
| 148 | #endif | 
|---|
| 149 |  | 
|---|
| 150 | #ifdef USE_DOUBLE | 
|---|
| 151 |     std::cout << "boost::numeric::interval<double>, unbounded_array" << std::endl; | 
|---|
| 152 |     test_my_matrix<ublas::matrix<boost::numeric::interval<double>, ublas::row_major, ublas::unbounded_array<boost::numeric::interval<double> > >, 3 > () (); | 
|---|
| 153 | #endif | 
|---|
| 154 | #endif | 
|---|
| 155 |  | 
|---|
| 156 | #ifdef USE_STD_VECTOR | 
|---|
| 157 | #ifdef USE_FLOAT | 
|---|
| 158 |     std::cout << "boost::numeric::interval<float>, std::vector" << std::endl; | 
|---|
| 159 |     test_my_matrix<ublas::matrix<boost::numeric::interval<float>, ublas::row_major, std::vector<boost::numeric::interval<float> > >, 3 > () (); | 
|---|
| 160 | #endif | 
|---|
| 161 |  | 
|---|
| 162 | #ifdef USE_DOUBLE | 
|---|
| 163 |     std::cout << "boost::numeric::interval<double>, std::vector" << std::endl; | 
|---|
| 164 |     test_my_matrix<ublas::matrix<boost::numeric::interval<double>, ublas::row_major, std::vector<boost::numeric::interval<double> > >, 3 > () (); | 
|---|
| 165 | #endif | 
|---|
| 166 | #endif | 
|---|
| 167 | #endif | 
|---|
| 168 |  | 
|---|
| 169 | #ifdef USE_VECTOR_OF_VECTOR | 
|---|
| 170 | #ifdef USE_BOUNDED_ARRAY | 
|---|
| 171 | #ifdef USE_FLOAT | 
|---|
| 172 |     std::cout << "boost::numeric::interval<float>, bounded_array" << std::endl; | 
|---|
| 173 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<float>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<boost::numeric::interval<float>, 3>, 3 + 1> >, 3 > () (); | 
|---|
| 174 | #endif | 
|---|
| 175 |  | 
|---|
| 176 | #ifdef USE_DOUBLE | 
|---|
| 177 |     std::cout << "boost::numeric::interval<double>, bounded_array" << std::endl; | 
|---|
| 178 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<double>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<boost::numeric::interval<double>, 3>, 3 + 1> >, 3 > () (); | 
|---|
| 179 | #endif | 
|---|
| 180 | #endif | 
|---|
| 181 |  | 
|---|
| 182 | #ifdef USE_UNBOUNDED_ARRAY | 
|---|
| 183 | #ifdef USE_FLOAT | 
|---|
| 184 |     std::cout << "boost::numeric::interval<float>, unbounded_array" << std::endl; | 
|---|
| 185 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<float>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<boost::numeric::interval<float> > > >, 3 > () (); | 
|---|
| 186 | #endif | 
|---|
| 187 |  | 
|---|
| 188 | #ifdef USE_DOUBLE | 
|---|
| 189 |     std::cout << "boost::numeric::interval<double>, unbounded_array" << std::endl; | 
|---|
| 190 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<double>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<boost::numeric::interval<double> > > >, 3 > () (); | 
|---|
| 191 | #endif | 
|---|
| 192 | #endif | 
|---|
| 193 |  | 
|---|
| 194 | #ifdef USE_STD_VECTOR | 
|---|
| 195 | #ifdef USE_FLOAT | 
|---|
| 196 |     std::cout << "boost::numeric::interval<float>, std::vector" << std::endl; | 
|---|
| 197 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<float>, ublas::row_major, std::vector<std::vector<boost::numeric::interval<float> > > >, 3 > () (); | 
|---|
| 198 | #endif | 
|---|
| 199 |  | 
|---|
| 200 | #ifdef USE_DOUBLE | 
|---|
| 201 |     std::cout << "boost::numeric::interval<double>, std::vector" << std::endl; | 
|---|
| 202 |     test_my_matrix<ublas::vector_of_vector<boost::numeric::interval<double>, ublas::row_major, std::vector<std::vector<boost::numeric::interval<double> > > >, 3 > () (); | 
|---|
| 203 | #endif | 
|---|
| 204 | #endif | 
|---|
| 205 | #endif | 
|---|
| 206 | } | 
|---|