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