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 "test5.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 | // Default Construct |
---|
30 | default_construct<MP>::test (); |
---|
31 | |
---|
32 | // Copy and swap |
---|
33 | initialize_matrix (m1); |
---|
34 | initialize_matrix (m2); |
---|
35 | m1 = m2; |
---|
36 | std::cout << "m1 = m2 = " << m1 << std::endl; |
---|
37 | m1.assign_temporary (m2); |
---|
38 | std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl; |
---|
39 | m1.swap (m2); |
---|
40 | std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl; |
---|
41 | |
---|
42 | // Zero assignment |
---|
43 | m1 = ublas::zero_matrix<> (m1.size1 (), m1.size2 ()); |
---|
44 | std::cout << "m1.zero_matrix = " << m1 << std::endl; |
---|
45 | m1 = m2; |
---|
46 | |
---|
47 | // Unary matrix operations resulting in a matrix |
---|
48 | initialize_matrix (m1); |
---|
49 | m2 = - m1; |
---|
50 | std::cout << "- m1 = " << m2 << std::endl; |
---|
51 | m2 = ublas::conj (m1); |
---|
52 | std::cout << "conj (m1) = " << m2 << std::endl; |
---|
53 | |
---|
54 | // Binary matrix operations resulting in a matrix |
---|
55 | initialize_matrix (m1); |
---|
56 | initialize_matrix (m2); |
---|
57 | m3 = m1 + m2; |
---|
58 | std::cout << "m1 + m2 = " << m3 << std::endl; |
---|
59 | m3 = m1 - m2; |
---|
60 | std::cout << "m1 - m2 = " << m3 << std::endl; |
---|
61 | |
---|
62 | // Scaling a matrix |
---|
63 | t = N; |
---|
64 | initialize_matrix (m1); |
---|
65 | m2 = value_type (1.) * m1; |
---|
66 | std::cout << "1. * m1 = " << m2 << std::endl; |
---|
67 | m2 = t * m1; |
---|
68 | std::cout << "N * m1 = " << m2 << std::endl; |
---|
69 | initialize_matrix (m1); |
---|
70 | m2 = m1 * value_type (1.); |
---|
71 | std::cout << "m1 * 1. = " << m2 << std::endl; |
---|
72 | m2 = m1 * t; |
---|
73 | std::cout << "m1 * N = " << m2 << std::endl; |
---|
74 | |
---|
75 | // Some assignments |
---|
76 | initialize_matrix (m1); |
---|
77 | initialize_matrix (m2); |
---|
78 | m2 += m1; |
---|
79 | std::cout << "m2 += m1 = " << m2 << std::endl; |
---|
80 | m2 -= m1; |
---|
81 | std::cout << "m2 -= m1 = " << m2 << std::endl; |
---|
82 | m2 = m2 + m1; |
---|
83 | std::cout << "m2 = m2 + m1 = " << m2 << std::endl; |
---|
84 | m2 = m2 - m1; |
---|
85 | std::cout << "m2 = m2 - m1 = " << m2 << std::endl; |
---|
86 | m1 *= value_type (1.); |
---|
87 | std::cout << "m1 *= 1. = " << m1 << std::endl; |
---|
88 | m1 *= t; |
---|
89 | std::cout << "m1 *= N = " << m1 << std::endl; |
---|
90 | |
---|
91 | // Transpose |
---|
92 | initialize_matrix (m1); |
---|
93 | // Transpose of a triangular isn't triangular of the same kind |
---|
94 | std::cout << "trans (m1) = " << ublas::trans (m1) << std::endl; |
---|
95 | |
---|
96 | // Hermitian |
---|
97 | initialize_matrix (m1); |
---|
98 | // Hermitian of a triangular isn't hermitian of the same kind |
---|
99 | std::cout << "herm (m1) = " << ublas::herm (m1) << std::endl; |
---|
100 | |
---|
101 | // Matrix multiplication |
---|
102 | initialize_matrix (m1); |
---|
103 | initialize_matrix (m2); |
---|
104 | m3 = ublas::prod (m1, m2); |
---|
105 | std::cout << "prod (m1, m2) = " << m3 << std::endl; |
---|
106 | } |
---|
107 | } |
---|
108 | void operator () () const { |
---|
109 | { |
---|
110 | M m1 (N, N), m2 (N, N), m3 (N, N); |
---|
111 | test_with (m1, m2, m3); |
---|
112 | |
---|
113 | #ifdef USE_RANGE |
---|
114 | ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)), |
---|
115 | mr2 (m2, ublas::range (0, N), ublas::range (0, N)), |
---|
116 | mr3 (m3, ublas::range (0, N), ublas::range (0, N)); |
---|
117 | test_with (mr1, mr2, mr3); |
---|
118 | #endif |
---|
119 | |
---|
120 | #ifdef USE_SLICE |
---|
121 | ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
122 | ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
123 | ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
124 | test_with (ms1, ms2, ms3); |
---|
125 | #endif |
---|
126 | } |
---|
127 | |
---|
128 | #ifdef USE_ADAPTOR |
---|
129 | { |
---|
130 | M m1 (N, N), m2 (N, N), m3 (N, N); |
---|
131 | ublas::triangular_adaptor<M> tam1 (m1), tam2 (m2), tam3 (m3); |
---|
132 | test_with (tam1, tam2, tam3); |
---|
133 | |
---|
134 | #ifdef USE_RANGE |
---|
135 | ublas::matrix_range<ublas::triangular_adaptor<M> > mr1 (tam1, ublas::range (0, N), ublas::range (0, N)), |
---|
136 | mr2 (tam2, ublas::range (0, N), ublas::range (0, N)), |
---|
137 | mr3 (tam3, ublas::range (0, N), ublas::range (0, N)); |
---|
138 | test_with (mr1, mr2, mr3); |
---|
139 | #endif |
---|
140 | |
---|
141 | #ifdef USE_SLICE |
---|
142 | ublas::matrix_slice<ublas::triangular_adaptor<M> > ms1 (tam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
143 | ms2 (tam2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)), |
---|
144 | ms3 (tam3, ublas::slice (0, 1, N), ublas::slice (0, 1, N)); |
---|
145 | test_with (ms1, ms2, ms3); |
---|
146 | #endif |
---|
147 | } |
---|
148 | #endif |
---|
149 | } |
---|
150 | }; |
---|
151 | |
---|
152 | // Test matrix |
---|
153 | void test_matrix () { |
---|
154 | std::cout << "test_matrix" << std::endl; |
---|
155 | |
---|
156 | #ifdef USE_BOUNDED_ARRAY |
---|
157 | #ifdef USE_FLOAT |
---|
158 | std::cout << "float, bounded_array" << std::endl; |
---|
159 | test_my_matrix<ublas::triangular_matrix<float, ublas::lower, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3 > () (); |
---|
160 | #endif |
---|
161 | |
---|
162 | #ifdef USE_DOUBLE |
---|
163 | std::cout << "double, bounded_array" << std::endl; |
---|
164 | test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3 > () (); |
---|
165 | #endif |
---|
166 | |
---|
167 | #ifdef USE_STD_COMPLEX |
---|
168 | #ifdef USE_FLOAT |
---|
169 | std::cout << "std::complex<float>, bounded_array" << std::endl; |
---|
170 | test_my_matrix<ublas::triangular_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3 > () (); |
---|
171 | #endif |
---|
172 | |
---|
173 | #ifdef USE_DOUBLE |
---|
174 | std::cout << "std::complex<double>, bounded_array" << std::endl; |
---|
175 | test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3 > () (); |
---|
176 | #endif |
---|
177 | #endif |
---|
178 | #endif |
---|
179 | |
---|
180 | #ifdef USE_UNBOUNDED_ARRAY |
---|
181 | #ifdef USE_FLOAT |
---|
182 | std::cout << "float, unbounded_array" << std::endl; |
---|
183 | test_my_matrix<ublas::triangular_matrix<float, ublas::lower, ublas::row_major, ublas::unbounded_array<float> >, 3 > () (); |
---|
184 | #endif |
---|
185 | |
---|
186 | #ifdef USE_DOUBLE |
---|
187 | std::cout << "double, unbounded_array" << std::endl; |
---|
188 | test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, ublas::unbounded_array<double> >, 3 > () (); |
---|
189 | #endif |
---|
190 | |
---|
191 | #ifdef USE_STD_COMPLEX |
---|
192 | #ifdef USE_FLOAT |
---|
193 | std::cout << "std::complex<float>, unbounded_array" << std::endl; |
---|
194 | test_my_matrix<ublas::triangular_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3 > () (); |
---|
195 | #endif |
---|
196 | |
---|
197 | #ifdef USE_DOUBLE |
---|
198 | std::cout << "std::complex<double>, unbounded_array" << std::endl; |
---|
199 | test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3 > () (); |
---|
200 | #endif |
---|
201 | #endif |
---|
202 | #endif |
---|
203 | |
---|
204 | #ifdef USE_STD_VECTOR |
---|
205 | #ifdef USE_FLOAT |
---|
206 | std::cout << "float, std::vector" << std::endl; |
---|
207 | test_my_matrix<ublas::triangular_matrix<float, ublas::lower, ublas::row_major, std::vector<float> >, 3 > () (); |
---|
208 | #endif |
---|
209 | |
---|
210 | #ifdef USE_DOUBLE |
---|
211 | std::cout << "double, std::vector" << std::endl; |
---|
212 | test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, std::vector<double> >, 3 > () (); |
---|
213 | #endif |
---|
214 | |
---|
215 | #ifdef USE_STD_COMPLEX |
---|
216 | #ifdef USE_FLOAT |
---|
217 | std::cout << "std::complex<float>, std::vector" << std::endl; |
---|
218 | test_my_matrix<ublas::triangular_matrix<std::complex<float>, ublas::lower, ublas::row_major, std::vector<std::complex<float> > >, 3 > () (); |
---|
219 | #endif |
---|
220 | |
---|
221 | #ifdef USE_DOUBLE |
---|
222 | std::cout << "std::complex<double>, std::vector" << std::endl; |
---|
223 | test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, std::vector<std::complex<double> > >, 3 > () (); |
---|
224 | #endif |
---|
225 | #endif |
---|
226 | #endif |
---|
227 | } |
---|