Teh first one
[mldemos:kalians-mldemos.git] / _AlgorithmsPlugins / KernelMethods / dlib / test / blas_bindings / blas_bindings_ger.cpp
1 // Copyright (C) 2009  Davis E. King (davis@dlib.net)\r
2 // License: Boost Software License   See LICENSE.txt for the full license.\r
3 \r
4 #include "../tester.h"\r
5 #include <dlib/matrix.h>\r
6 \r
7 #ifndef DLIB_USE_BLAS\r
8 #error "BLAS bindings must be used for this test to make any sense"\r
9 #endif\r
10 \r
11 namespace dlib\r
12 {\r
13     namespace blas_bindings\r
14     {\r
15         // This is a little screwy.  This function is used inside the BLAS\r
16         // bindings to count how many times each of the BLAS functions get called.\r
17 #ifdef DLIB_TEST_BLAS_BINDINGS\r
18         int& counter_ger() { static int counter = 0; return counter; }\r
19 #endif\r
20 \r
21     }\r
22 }\r
23 \r
24 namespace  \r
25 {\r
26     using namespace test;\r
27     using namespace std;\r
28     // Declare the logger we will use in this test.  The name of the logger \r
29     // should start with "test."\r
30     dlib::logger dlog("test.ger");\r
31 \r
32 \r
33     class blas_bindings_ger_tester : public tester\r
34     {\r
35     public:\r
36         blas_bindings_ger_tester (\r
37         ) :\r
38             tester (\r
39                 "test_ger", // the command line argument name for this test\r
40                 "Run tests for GER routines.", // the command line argument description\r
41                 0                     // the number of command line arguments for this test\r
42             )\r
43         {}\r
44 \r
45         template <typename matrix_type, typename cv_type, typename rv_type>\r
46         void test_ger_stuff(\r
47             matrix_type& m,\r
48             rv_type& rv,\r
49             cv_type& cv\r
50         ) const\r
51         {\r
52             using namespace dlib;\r
53             using namespace dlib::blas_bindings;\r
54 \r
55             rv_type rv2;\r
56             cv_type cv2;\r
57             matrix_type m2;\r
58 \r
59             counter_ger() = 0;\r
60             m2 = m + cv*rv;\r
61             DLIB_TEST_MSG(counter_ger() == 1, counter_ger());\r
62 \r
63             counter_ger() = 0;\r
64             m += trans(rv)*rv;\r
65             DLIB_TEST(counter_ger() == 1);\r
66 \r
67             counter_ger() = 0;\r
68             m += trans(rv)*trans(cv);\r
69             DLIB_TEST(counter_ger() == 1);\r
70 \r
71             counter_ger() = 0;\r
72             m += cv*trans(cv);\r
73             DLIB_TEST(counter_ger() == 1);\r
74 \r
75             counter_ger() = 0;\r
76             m += trans(rv)*rv + trans(cv*3*rv);\r
77             DLIB_TEST(counter_ger() == 2);\r
78         }\r
79 \r
80 \r
81         template <typename matrix_type, typename cv_type, typename rv_type>\r
82         void test_ger_stuff_conj(\r
83             matrix_type& m,\r
84             rv_type& rv,\r
85             cv_type& cv\r
86         ) const\r
87         {\r
88             using namespace dlib;\r
89             using namespace dlib::blas_bindings;\r
90 \r
91             rv_type rv2;\r
92             cv_type cv2;\r
93             matrix_type m2;\r
94 \r
95             counter_ger() = 0;\r
96             m += cv*conj(rv);\r
97             DLIB_TEST_MSG(counter_ger() == 1, counter_ger());\r
98 \r
99             counter_ger() = 0;\r
100             m += trans(rv)*conj(rv);\r
101             DLIB_TEST(counter_ger() == 1);\r
102 \r
103             counter_ger() = 0;\r
104             m += trans(rv)*conj(trans(cv));\r
105             DLIB_TEST(counter_ger() == 1);\r
106 \r
107             counter_ger() = 0;\r
108             m += cv*trans(conj(cv));\r
109             DLIB_TEST(counter_ger() == 1);\r
110 \r
111             counter_ger() = 0;\r
112             m += trans(rv)*rv + trans(cv*3*conj(rv));\r
113             DLIB_TEST(counter_ger() == 2);\r
114         }\r
115 \r
116         void perform_test (\r
117         )\r
118         {\r
119             using namespace dlib;\r
120             typedef dlib::memory_manager<char>::kernel_1a mm;\r
121 \r
122             dlog << dlib::LINFO << "test double";\r
123             {\r
124                 matrix<double> m = randm(4,4);\r
125                 matrix<double,1,0> rv = randm(1,4);\r
126                 matrix<double,0,1> cv = randm(4,1);\r
127                 test_ger_stuff(m,rv,cv);\r
128             }\r
129 \r
130             dlog << dlib::LINFO << "test float";\r
131             {\r
132                 matrix<float> m = matrix_cast<float>(randm(4,4));\r
133                 matrix<float,1,0> rv = matrix_cast<float>(randm(1,4));\r
134                 matrix<float,0,1> cv = matrix_cast<float>(randm(4,1));\r
135                 test_ger_stuff(m,rv,cv);\r
136             }\r
137 \r
138             dlog << dlib::LINFO << "test complex<double>";\r
139             {\r
140                 matrix<complex<double> > m = complex_matrix(randm(4,4), randm(4,4));\r
141                 matrix<complex<double>,1,0> rv = complex_matrix(randm(1,4), randm(1,4));\r
142                 matrix<complex<double>,0,1> cv = complex_matrix(randm(4,1), randm(4,1));\r
143                 test_ger_stuff(m,rv,cv);\r
144                 test_ger_stuff_conj(m,rv,cv);\r
145             }\r
146 \r
147             dlog << dlib::LINFO << "test complex<float>";\r
148             {\r
149                 matrix<complex<float> > m = matrix_cast<complex<float> >(complex_matrix(randm(4,4), randm(4,4)));\r
150                 matrix<complex<float>,1,0> rv = matrix_cast<complex<float> >(complex_matrix(randm(1,4), randm(1,4)));\r
151                 matrix<complex<float>,0,1> cv = matrix_cast<complex<float> >(complex_matrix(randm(4,1), randm(4,1)));\r
152                 test_ger_stuff(m,rv,cv);\r
153                 test_ger_stuff_conj(m,rv,cv);\r
154             }\r
155 \r
156 \r
157             dlog << dlib::LINFO << "test double";\r
158             {\r
159                 matrix<double,0,0,mm,column_major_layout> m = randm(4,4);\r
160                 matrix<double,1,0,mm,column_major_layout> rv = randm(1,4);\r
161                 matrix<double,0,1,mm,column_major_layout> cv = randm(4,1);\r
162                 test_ger_stuff(m,rv,cv);\r
163             }\r
164 \r
165             dlog << dlib::LINFO << "test float";\r
166             {\r
167                 matrix<float,0,0,mm,column_major_layout> m = matrix_cast<float>(randm(4,4));\r
168                 matrix<float,1,0,mm,column_major_layout> rv = matrix_cast<float>(randm(1,4));\r
169                 matrix<float,0,1,mm,column_major_layout> cv = matrix_cast<float>(randm(4,1));\r
170                 test_ger_stuff(m,rv,cv);\r
171             }\r
172 \r
173             dlog << dlib::LINFO << "test complex<double>";\r
174             {\r
175                 matrix<complex<double>,0,0,mm,column_major_layout > m = complex_matrix(randm(4,4), randm(4,4));\r
176                 matrix<complex<double>,1,0,mm,column_major_layout> rv = complex_matrix(randm(1,4), randm(1,4));\r
177                 matrix<complex<double>,0,1,mm,column_major_layout> cv = complex_matrix(randm(4,1), randm(4,1));\r
178                 test_ger_stuff(m,rv,cv);\r
179                 test_ger_stuff_conj(m,rv,cv);\r
180             }\r
181 \r
182             dlog << dlib::LINFO << "test complex<float>";\r
183             {\r
184                 matrix<complex<float>,0,0,mm,column_major_layout > m = matrix_cast<complex<float> >(complex_matrix(randm(4,4), randm(4,4)));\r
185                 matrix<complex<float>,1,0,mm,column_major_layout> rv = matrix_cast<complex<float> >(complex_matrix(randm(1,4), randm(1,4)));\r
186                 matrix<complex<float>,0,1,mm,column_major_layout> cv = matrix_cast<complex<float> >(complex_matrix(randm(4,1), randm(4,1)));\r
187                 test_ger_stuff(m,rv,cv);\r
188                 test_ger_stuff_conj(m,rv,cv);\r
189             }\r
190 \r
191 \r
192             print_spinner();\r
193         }\r
194     };\r
195 \r
196     blas_bindings_ger_tester a;\r
197 \r
198 }\r
199 \r
200 \r