matrix3.cpp 32.1 KB
Newer Older
1
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
Davis King's avatar
Davis King committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// License: Boost Software License   See LICENSE.txt for the full license.


#include <dlib/matrix.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include "../stl_checked.h"
#include "../array.h"
#include "../rand.h"

#include "tester.h"
#include <dlib/memory_manager_stateless.h>
#include <dlib/array2d.h>

namespace  
{

    using namespace test;
    using namespace dlib;
    using namespace std;

    logger dlog("test.matrix3");

28

29
    const double eps_mul = 200;
30
31
32
33
34
35
36

    template <typename T, typename U>
    void check_equal (
        const T& a,
        const U& b
    )
    {
37
38
        DLIB_TEST(a.nr() == b.nr());
        DLIB_TEST(a.nc() == b.nc());
39
40
41
42
43
44
        typedef typename T::type type;
        for (long r = 0; r < a.nr(); ++r)
        {
            for (long c = 0; c < a.nc(); ++c)
            {
                type error = std::abs(a(r,c) - b(r,c));
45
                DLIB_TEST_MSG(error < std::sqrt(std::numeric_limits<type>::epsilon())*eps_mul, "error: " << error <<
46
                             "    eps: " << std::sqrt(std::numeric_limits<type>::epsilon())*eps_mul);
47
48
49
50
51
52
53
54
55
56
            }
        }
    }

    template <typename T, typename U>
    void c_check_equal (
        const T& a,
        const U& b
    )
    {
57
58
        DLIB_TEST(a.nr() == b.nr());
        DLIB_TEST(a.nc() == b.nc());
59
60
61
62
63
64
        typedef typename T::type type;
        for (long r = 0; r < a.nr(); ++r)
        {
            for (long c = 0; c < a.nc(); ++c)
            {
                typename type::value_type error = std::abs(a(r,c) - b(r,c));
65
                DLIB_TEST_MSG(error < std::sqrt(std::numeric_limits<typename type::value_type>::epsilon())*eps_mul, "error: " << error <<
66
                             "    eps: " << std::sqrt(std::numeric_limits<typename type::value_type>::epsilon())*eps_mul);
67
68
69
70
71
72
73
74
75
76
77
            }
        }
    }

    template <typename T, typename U>
    void assign_no_blas (
        const T& a_,
        const U& b
    )
    {
        T& a = const_cast<T&>(a_);
78
79
        DLIB_TEST(a.nr() == b.nr());
        DLIB_TEST(a.nc() == b.nc());
80
81
82
83
84
85
86
87
88
        for (long r = 0; r < a.nr(); ++r)
        {
            for (long c = 0; c < a.nc(); ++c)
            {
                a(r,c) = b(r,c);
            }
        }
    }

89
    template <typename type>
90
    type rnd_num (dlib::rand& rnd)
91
92
93
94
    {
        return static_cast<type>(10*rnd.get_random_double());
    }

95
96
97
98
99
100
101
    template <typename type>
    void test_blas( long rows, long cols)
    {
        // The tests in this function exercise the BLAS bindings located in the matrix/matrix_blas_bindings.h file.
        // It does this by performing an assignment that is subject to BLAS bindings and comparing the
        // results directly to an unevaluated matrix_exp that should be equal.

102
        dlib::rand rnd;
103

104
        matrix<type> a(rows,cols), temp, temp2, temp3;
105

106
        for (int k = 0; k < 6; ++k)
107
108
109
110
111
        {
            for (long r= 0; r < a.nr(); ++r)
            {
                for (long c = 0; c < a.nc(); ++c)
                {
112
                    a(r,c) = rnd_num<type>(rnd);
113
114
115
116
117
                }
            }
            matrix<type> at;
            at = trans(a);

118
            matrix<complex<type> > c_a(rows,cols), c_at, c_sqr;
119
120
121
122
            for (long r= 0; r < a.nr(); ++r)
            {
                for (long c = 0; c < a.nc(); ++c)
                {
123
                    c_a(r,c) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
124
125
126
                }
            }
            c_at = trans(c_a);
127
128
129
            const int size = max(rows,cols);
            c_sqr = 10*matrix_cast<complex<type> >(complex_matrix(randm(size,size,rnd), randm(size,size,rnd)));

130
131
132
133
134
135

            matrix<complex<type> > c_temp(cols,cols), c_temp2(cols,cols);
            const complex<type> i(0,1);

            const type one = 1;
            const type two = 1;
136
137
138
            const type num1 = static_cast<type>(3.6);
            const type num2 = static_cast<type>(6.6);
            const type num3 = static_cast<type>(8.6);
139
140
141
142
143
144

            matrix<complex<type>,0,1> c_cv4(cols), c_cv3(rows);
            matrix<complex<type>,1,0> c_rv4(cols), c_rv3(rows);

            matrix<type,0,1> cv4(cols);

145
146
            for (long idx = 0; idx < cv4.size(); ++idx)
                cv4(idx) = rnd_num<type>(rnd);
147

148
149
            for (long idx = 0; idx < c_cv4.size(); ++idx)
                c_cv4(idx) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
150
151
152

            matrix<type,1,0> rv3(rows);

153
154
            for (long idx = 0; idx < rv3.size(); ++idx)
                rv3(idx) = rnd_num<type>(rnd);
155

156
157
            for (long idx = 0; idx < c_rv3.size(); ++idx)
                c_rv3(idx) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
158
159
160

            matrix<type,0,1> cv3(rows);

161
162
            for (long idx = 0; idx < cv3.size(); ++idx)
                cv3(idx) = rnd_num<type>(rnd);
163

164
165
            for (long idx = 0; idx < c_cv3.size(); ++idx)
                c_cv3(idx) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
166
167

            matrix<type,1,0> rv4(cols);
168
169
            for (long idx = 0; idx < rv4.size(); ++idx)
                rv4(idx) = rnd_num<type>(rnd);
170

171
172
            for (long idx = 0; idx < c_rv4.size(); ++idx)
                c_rv4(idx) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
173
174
175
176
177
178



            // GEMM tests
            dlog << LTRACE << "1.1";
            check_equal(tmp(at*a),   at*a);
179
180
            check_equal(tmp(trans(at*a)),   trans(at*a));
            check_equal(tmp(2.4*trans(4*trans(at*a) + at*3*a)),   2.4*trans(4*trans(at*a) + at*3*a));
181
182
            dlog << LTRACE << "1.2";
            check_equal(tmp(trans(a)*a),   trans(a)*a);
183
            check_equal(tmp(trans(trans(a)*a)),   trans(trans(a)*a));
184
185
            dlog << LTRACE << "1.3";
            check_equal(tmp(at*trans(at)),   at*trans(at));
186
            check_equal(tmp(trans(at*trans(at))),   trans(at*trans(at)));
187
188
            dlog << LTRACE << "1.4";
            check_equal(tmp(trans(at)*trans(a)),   a*at);
189
            check_equal(tmp(trans(trans(at)*trans(a))),   trans(a*at));
190
191
192
193
            dlog << LTRACE << "1.5";

            print_spinner();
            c_check_equal(tmp(conj(trans(c_a))*c_a),   trans(conj(c_a))*c_a);
194
            dlog << LTRACE << "1.5.1";
195
            c_check_equal(tmp(trans(conj(trans(c_a))*c_a)),   trans(trans(conj(c_a))*c_a));
196
197
198
199
            dlog << LTRACE << "1.5.2";
            c_check_equal(tmp((conj(trans(c_sqr))*trans(c_sqr))),   (trans(conj(c_sqr))*trans(c_sqr)));
            dlog << LTRACE << "1.5.3";
            c_check_equal(tmp(trans(conj(trans(c_sqr))*trans(c_sqr))),   trans(trans(conj(c_sqr))*trans(c_sqr)));
200
201
            dlog << LTRACE << "1.6";
            c_check_equal(tmp(c_at*trans(conj(c_at))),   c_at*conj(trans(c_at)));
202
            dlog << LTRACE << "1.6.1";
203
            c_check_equal(tmp(trans(c_at*trans(conj(c_at)))),   trans(c_at*conj(trans(c_at))));
204
205
206
207
208
209
            dlog << LTRACE << "1.6.2";
            c_check_equal(tmp((c_sqr)*trans(conj(c_sqr))),   (c_sqr)*conj(trans(c_sqr)));
            dlog << LTRACE << "1.6.2.1";
            c_check_equal(tmp(trans(c_sqr)*trans(conj(c_sqr))),   trans(c_sqr)*conj(trans(c_sqr)));
            dlog << LTRACE << "1.6.3";
            c_check_equal(tmp(trans(trans(c_sqr)*trans(conj(c_sqr)))),   trans(trans(c_sqr)*conj(trans(c_sqr))));
210
211
            dlog << LTRACE << "1.7";
            c_check_equal(tmp(conj(trans(c_at))*trans(conj(c_a))),  conj(trans(c_at))*trans(conj(c_a)));
212
            c_check_equal(tmp(trans(conj(trans(c_at))*trans(conj(c_a)))), trans(conj(trans(c_at))*trans(conj(c_a))));
213
214
215
216
217
218
            dlog << LTRACE << "1.8";

            check_equal(tmp(a*trans(rowm(a,1))) ,  a*trans(rowm(a,1)));
            check_equal(tmp(a*colm(at,1)) ,  a*colm(at,1));
            check_equal(tmp(subm(a,1,1,2,2)*subm(a,1,2,2,2)), subm(a,1,1,2,2)*subm(a,1,2,2,2));

219
220
221
222
223
224
225
            dlog << LTRACE << "1.9";
            check_equal(tmp(trans(a*trans(rowm(a,1)))) ,  trans(a*trans(rowm(a,1))));
            dlog << LTRACE << "1.10";
            check_equal(tmp(trans(a*colm(at,1))) ,  trans(a*colm(at,1)));
            dlog << LTRACE << "1.11";
            check_equal(tmp(trans(subm(a,1,1,2,2)*subm(a,1,2,2,2))), trans(subm(a,1,1,2,2)*subm(a,1,2,2,2)));
            dlog << LTRACE << "1.12";
226

227
228
229
            {
                temp = at*a;
                temp2 = temp;
230

231
232
233
                temp += 3.5*at*a;
                assign_no_blas(temp2, temp2 + 3.5*at*a);
                check_equal(temp, temp2);
234

235
236
237
                temp -= at*3.5*a;
                assign_no_blas(temp2, temp2 - at*3.5*a);
                check_equal(temp, temp2);
238

239
240
241
                temp = temp + 4*at*a;
                assign_no_blas(temp2, temp2 + 4*at*a);
                check_equal(temp, temp2);
242

243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
                temp = temp - 2.4*at*a;
                assign_no_blas(temp2, temp2 - 2.4*at*a);
                check_equal(temp, temp2);
            }
            dlog << LTRACE << "1.13";
            {
                temp = trans(at*a);
                temp2 = temp;
                temp3 = temp;

                dlog << LTRACE << "1.14";
                temp += trans(3.5*at*a);
                assign_no_blas(temp2, temp2 + trans(3.5*at*a));
                check_equal(temp, temp2);

                dlog << LTRACE << "1.15";
                temp -= trans(at*3.5*a);
                assign_no_blas(temp2, temp2 - trans(at*3.5*a));
                check_equal(temp, temp2);

                dlog << LTRACE << "1.16";
                temp = trans(temp + 4*at*a);
                assign_no_blas(temp3, trans(temp2 + 4*at*a));
                check_equal(temp, temp3);

                temp2 = temp;
                dlog << LTRACE << "1.17";
                temp = trans(temp - 2.4*at*a);
                assign_no_blas(temp3, trans(temp2 - 2.4*at*a));
                check_equal(temp, temp3);
            }

275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
            dlog << LTRACE << "1.17.1";
            {
                matrix<type> m1, m2;

                m1 = matrix_cast<type>(randm(rows, cols, rnd));
                m2 = matrix_cast<type>(randm(cols, rows + 8, rnd));
                check_equal(tmp(m1*m2), m1*m2);
                check_equal(tmp(trans(m1*m2)), trans(m1*m2));

                m1 = trans(m1);
                check_equal(tmp(trans(m1)*m2), trans(m1)*m2);
                check_equal(tmp(trans(trans(m1)*m2)), trans(trans(m1)*m2));

                m2 = trans(m2);
                check_equal(tmp(trans(m1)*trans(m2)), trans(m1)*trans(m2));
                check_equal(tmp(trans(trans(m1)*trans(m2))), trans(trans(m1)*trans(m2)));

                m1 = trans(m1);
                check_equal(tmp(m1*trans(m2)), m1*trans(m2));
                check_equal(tmp(trans(m1*trans(m2))), trans(m1*trans(m2)));
            }

            dlog << LTRACE << "1.17.5";
            {
                matrix<type,1,0> r;
                matrix<type,0,1> c;

                r = matrix_cast<type>(randm(1, rows+9, rnd));
                c = matrix_cast<type>(randm(rows, 1, rnd));

                check_equal(tmp(c*r), c*r);
                check_equal(tmp(trans(c*r)), trans(c*r));

                check_equal(tmp(trans(r)*trans(c)), trans(r)*trans(c));
                check_equal(tmp(trans(trans(r)*trans(c))), trans(trans(r)*trans(c)));
            }

312
            dlog << LTRACE << "1.18";
313
314
315

            // GEMV tests
            check_equal(tmp(a*cv4),  a*cv4);
316
            check_equal(tmp(trans(a*cv4)),  trans(a*cv4));
317
318
319
            check_equal(tmp(rv3*a),  rv3*a);
            check_equal(tmp(trans(cv4)*at),  trans(cv4)*at);
            check_equal(tmp(a*trans(rv4)),  a*trans(rv4));
320
            check_equal(tmp(trans(a*trans(rv4))),  trans(a*trans(rv4)));
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

            check_equal(tmp(trans(a)*cv3),  trans(a)*cv3);
            check_equal(tmp(rv4*trans(a)),  rv4*trans(a));
            check_equal(tmp(trans(cv3)*trans(at)),  trans(cv3)*trans(at));
            check_equal(tmp(trans(cv3)*a),  trans(cv3)*a);
            check_equal(tmp(trans(a)*trans(rv3)),  trans(a)*trans(rv3));


            c_check_equal(tmp(trans(conj(c_a))*c_cv3),  trans(conj(c_a))*c_cv3);
            c_check_equal(tmp(c_rv4*trans(conj(c_a))),  c_rv4*trans(conj(c_a)));
            c_check_equal(tmp(trans(c_cv3)*trans(conj(c_at))),  trans(c_cv3)*trans(conj(c_at)));
            c_check_equal(tmp(conj(trans(c_a))*trans(c_rv3)),  trans(conj(c_a))*trans(c_rv3));
            c_check_equal(tmp(c_rv4*conj(c_at)),  c_rv4*conj(c_at));
            c_check_equal(tmp(trans(c_cv4)*conj(c_at)),  trans(c_cv4)*conj(c_at));

336
337
338
339
340
341
342
343
344
345
            dlog << LTRACE << "2.00";

            c_check_equal(tmp(trans(trans(conj(c_a))*c_cv3)),           trans(trans(conj(c_a))*c_cv3));
            c_check_equal(tmp(trans(c_rv4*trans(conj(c_a)))),           trans(c_rv4*trans(conj(c_a))));
            c_check_equal(tmp(trans(trans(c_cv3)*trans(conj(c_at)))),   trans(trans(c_cv3)*trans(conj(c_at))));
            dlog << LTRACE << "2.20";
            c_check_equal(tmp(trans(conj(trans(c_a))*trans(c_rv3))),    trans(trans(conj(c_a))*trans(c_rv3)));
            c_check_equal(tmp(trans(c_rv4*conj(c_at))),                 trans(c_rv4*conj(c_at)));
            c_check_equal(tmp(trans(trans(c_cv4)*conj(c_at))),          trans(trans(c_cv4)*conj(c_at)));

346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403


            dlog << LTRACE << "6";
            temp = a*at;
            check_equal(temp, a*at);
            temp = temp + a*at + trans(at)*at + trans(at)*sin(at);
            check_equal(temp, a*at + a*at+ trans(at)*at + trans(at)*sin(at));

            dlog << LTRACE << "6.1";
            temp = a*at;
            check_equal(temp, a*at);
            temp = a*at + temp;
            check_equal(temp, a*at + a*at);

            print_spinner();
            dlog << LTRACE << "6.2";
            temp = a*at;
            check_equal(temp, a*at);
            dlog << LTRACE << "6.2.3";
            temp = temp - a*at;
            dlog << LTRACE << "6.2.4";
            check_equal(temp, a*at-a*at);

            dlog << LTRACE << "6.3";
            temp = a*at;
            dlog << LTRACE << "6.3.5";
            check_equal(temp, a*at);
            dlog << LTRACE << "6.3.6";
            temp = a*at - temp;
            dlog << LTRACE << "6.4";
            check_equal(temp, a*at-a*at);



            const long d = min(rows,cols);
            rectangle rect(1,1,d,d);
            temp.set_size(max(rows,cols)+4,max(rows,cols)+4);
            set_all_elements(temp,4);
            temp2 = temp;

            dlog << LTRACE << "7";
            set_subm(temp,rect) = a*at;
            assign_no_blas( set_subm(temp2,rect) , a*at);
            check_equal(temp, temp2);

            temp = a;
            temp2 = a;

            set_colm(temp,1) = a*cv4;
            assign_no_blas( set_colm(temp2,1) , a*cv4);
            check_equal(temp, temp2);

            set_rowm(temp,1) = rv3*a;
            assign_no_blas( set_rowm(temp2,1) , rv3*a);
            check_equal(temp, temp2);


            // Test BLAS GER
404
405
406
407
            {
                temp.set_size(cols,cols);
                set_all_elements(temp,3);
                temp2 = temp;
408
409


410
411
412
413
                dlog << LTRACE << "8";
                temp += cv4*rv4;
                assign_no_blas(temp2, temp2 + cv4*rv4);
                check_equal(temp, temp2);
414

415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
                dlog << LTRACE << "8.3";
                temp = temp + cv4*rv4;
                assign_no_blas(temp2, temp2 + cv4*rv4);
                check_equal(temp, temp2);
                dlog << LTRACE << "8.9";
            }
            {
                temp.set_size(cols,cols);
                set_all_elements(temp,3);
                temp2 = temp;
                temp3 = 0;

                dlog << LTRACE << "8.10";

                temp += trans(cv4*rv4);
                assign_no_blas(temp3, temp2 + trans(cv4*rv4));
                check_equal(temp, temp3);
                temp3 = 0;

                dlog << LTRACE << "8.11";
                temp2 = temp;
                temp = trans(temp + cv4*rv4);
                assign_no_blas(temp3, trans(temp2 + cv4*rv4));
                check_equal(temp, temp3);
                dlog << LTRACE << "8.12";
            }
            {
                matrix<complex<type> > temp, temp2, temp3;
                matrix<complex<type>,0,1 > cv4;
                matrix<complex<type>,1,0 > rv4;
                cv4.set_size(cols);
                rv4.set_size(cols);
                temp.set_size(cols,cols);
                set_all_elements(temp,complex<type>(3,5));
                temp(cols-1, cols-4) = 9;
                temp2 = temp;
                temp3.set_size(cols,cols);
                temp3 = 0;

                for (long i = 0; i < rv4.size(); ++i)
                {
                    rv4(i) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
                    cv4(i) = complex<type>(rnd_num<type>(rnd),rnd_num<type>(rnd));
                }

                dlog << LTRACE << "8.13";

                temp += trans(cv4*rv4);
                assign_no_blas(temp3, temp2 + trans(cv4*rv4));
                c_check_equal(temp, temp3);
                temp3 = 0;

                dlog << LTRACE << "8.14";
                temp2 = temp;
                temp = trans(temp + cv4*rv4);
                assign_no_blas(temp3, trans(temp2 + cv4*rv4));
                c_check_equal(temp, temp3);
                dlog << LTRACE << "8.15";
            }
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500




            set_all_elements(c_temp, one + num1*i);
            c_temp2 = c_temp;
            set_all_elements(c_rv4, one + num2*i);
            set_all_elements(c_cv4, two + num3*i);


            dlog << LTRACE << "9";
            c_temp += c_cv4*c_rv4;
            assign_no_blas(c_temp2, c_temp2 + c_cv4*c_rv4);
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.1";
            c_temp += c_cv4*conj(c_rv4);
            assign_no_blas(c_temp2, c_temp2 + c_cv4*conj(c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.2";
            c_temp = c_cv4*conj(c_rv4) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + c_cv4*conj(c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.3";
            c_temp = trans(c_rv4)*trans(conj(c_cv4)) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + trans(c_rv4)*trans(conj(c_cv4)));
            c_check_equal(c_temp, c_temp2);

501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519

            dlog << LTRACE << "9.4";
            c_temp += conj(c_cv4)*c_rv4;
            assign_no_blas(c_temp2, c_temp2 + conj(c_cv4)*c_rv4);
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.5";
            c_temp += conj(c_cv4)*conj(c_rv4);
            assign_no_blas(c_temp2, c_temp2 + conj(c_cv4)*conj(c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.6";
            c_temp = conj(c_cv4)*conj(c_rv4) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + conj(c_cv4)*conj(c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "9.7";
            c_temp = conj(trans(c_rv4))*trans(conj(c_cv4)) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + conj(trans(c_rv4))*trans(conj(c_cv4)));
            c_check_equal(c_temp, c_temp2);


520
            dlog << LTRACE << "10";
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
            c_temp += trans(c_cv4*c_rv4);
            assign_no_blas(c_temp2, c_temp2 + trans(c_cv4*c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.1";
            c_temp += trans(c_cv4*conj(c_rv4));
            assign_no_blas(c_temp2, c_temp2 + trans(c_cv4*conj(c_rv4)));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.2";
            c_temp = trans(c_cv4*conj(c_rv4)) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + trans(c_cv4*conj(c_rv4)));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.3";
            c_temp = trans(trans(c_rv4)*trans(conj(c_cv4))) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + trans(trans(c_rv4)*trans(conj(c_cv4))));
            c_check_equal(c_temp, c_temp2);


            dlog << LTRACE << "10.4";
            c_temp += trans(conj(c_cv4)*c_rv4);
            assign_no_blas(c_temp2, c_temp2 + trans(conj(c_cv4)*c_rv4));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.5";
            c_temp += trans(conj(c_cv4)*conj(c_rv4));
            assign_no_blas(c_temp2, c_temp2 + trans(conj(c_cv4)*conj(c_rv4)));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.6";
            c_temp = trans(conj(c_cv4)*conj(c_rv4)) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + trans(conj(c_cv4)*conj(c_rv4)));
            c_check_equal(c_temp, c_temp2);
            dlog << LTRACE << "10.7";
            c_temp = trans(conj(trans(c_rv4))*trans(conj(c_cv4))) + c_temp;
            assign_no_blas(c_temp2, c_temp2 + trans(conj(trans(c_rv4))*trans(conj(c_cv4))));
            c_check_equal(c_temp, c_temp2);

            dlog << LTRACE << "10.8";
556
557
558
559
560
561


            print_spinner();

            // Test DOT
            check_equal( tmp(rv4*cv4), rv4*cv4);
562
            check_equal( tmp(trans(rv4*cv4)), trans(rv4*cv4));
563
564
565
566
567
568
            check_equal( tmp(trans(cv4)*trans(rv4)), trans(cv4)*trans(rv4));
            check_equal( tmp(rv4*3.9*cv4), rv4*3.9*cv4);
            check_equal( tmp(trans(cv4)*3.9*trans(rv4)), trans(cv4)*3.9*trans(rv4));
            check_equal( tmp(rv4*cv4*3.9), rv4*3.9*cv4);
            check_equal( tmp(trans(cv4)*trans(rv4)*3.9), trans(cv4)*3.9*trans(rv4));

569
570
571
572
573
574
575
576
577
578

            check_equal( tmp(trans(rv4*cv4)),                    trans(rv4*cv4));
            check_equal( tmp(trans(trans(rv4*cv4))),             trans(trans(rv4*cv4)));
            check_equal( tmp(trans(trans(cv4)*trans(rv4))),      trans(trans(cv4)*trans(rv4)));
            check_equal( tmp(trans(rv4*3.9*cv4)),                trans(rv4*3.9*cv4));
            check_equal( tmp(trans(trans(cv4)*3.9*trans(rv4))),  trans(trans(cv4)*3.9*trans(rv4)));
            check_equal( tmp(trans(rv4*cv4*3.9)),                trans(rv4*3.9*cv4));
            check_equal( tmp(trans(trans(cv4)*trans(rv4)*3.9)),  trans(trans(cv4)*3.9*trans(rv4)));


579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
            temp.set_size(1,1);
            temp = 4;
            check_equal( tmp(temp + rv4*cv4), temp + rv4*cv4);
            check_equal( tmp(temp + trans(cv4)*trans(rv4)), temp + trans(cv4)*trans(rv4));

            dlog << LTRACE << "11";



            c_check_equal( tmp(conj(c_rv4)*c_cv4), conj(c_rv4)*c_cv4);
            c_check_equal( tmp(conj(trans(c_cv4))*trans(c_rv4)), trans(conj(c_cv4))*trans(c_rv4));

            c_check_equal( tmp(conj(c_rv4)*i*c_cv4), conj(c_rv4)*i*c_cv4);
            c_check_equal( tmp(conj(trans(c_cv4))*i*trans(c_rv4)), trans(conj(c_cv4))*i*trans(c_rv4));

            c_temp.set_size(1,1);
            c_temp = 4;
            c_check_equal( tmp(c_temp + conj(c_rv4)*c_cv4), c_temp + conj(c_rv4)*c_cv4);
            c_check_equal( tmp(c_temp + trans(conj(c_cv4))*trans(c_rv4)), c_temp + trans(conj(c_cv4))*trans(c_rv4));

599
            DLIB_TEST(abs((static_cast<complex<type> >(c_rv4*c_cv4) + i) - ((c_rv4*c_cv4)(0) + i)) < std::sqrt(std::numeric_limits<type>::epsilon())*eps_mul );
Davis King's avatar
Davis King committed
600
            DLIB_TEST(max(abs((rv4*cv4 + 1.0) - ((rv4*cv4)(0) + 1.0))) < std::sqrt(std::numeric_limits<type>::epsilon())*eps_mul);
601
602

        }
603
604
605
606
607
608
609
610
611

        {
            matrix<int> m(2,3), m2(6,1);

            m = 1,2,3,
                4,5,6;

            m2 = 1,2,3,4,5,6;

612
            DLIB_TEST(reshape_to_column_vector(m) == m2);
613
614
615
616
617
618
619
620
621
622
623

        }
        {
            matrix<int,2,3> m(2,3);
            matrix<int> m2(6,1);

            m = 1,2,3,
                4,5,6;

            m2 = 1,2,3,4,5,6;

624
            DLIB_TEST(reshape_to_column_vector(m) == m2);
625
626

        }
627
628
629
    }


Davis King's avatar
Davis King committed
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
    void matrix_test (
    )
    /*!
        ensures
            - runs tests on the matrix stuff compliance with the specs
    !*/
    {        
        typedef memory_manager_stateless<char>::kernel_2_2a MM;
        print_spinner();


        {
            matrix<long> m1(2,2), m2(2,2);

            m1 = 1, 2,
            3, 4;

            m2 = 4, 5,
            6, 7;


651
652
653
654
            DLIB_TEST(subm(tensor_product(m1,m2),range(0,1), range(0,1)) == 1*m2);
            DLIB_TEST(subm(tensor_product(m1,m2),range(0,1), range(2,3)) == 2*m2);
            DLIB_TEST(subm(tensor_product(m1,m2),range(2,3), range(0,1)) == 3*m2);
            DLIB_TEST(subm(tensor_product(m1,m2),range(2,3), range(2,3)) == 4*m2);
Davis King's avatar
Davis King committed
655
656
        }

657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
        {
            print_spinner();
            dlog << LTRACE << "testing blas stuff";
            dlog << LTRACE << " \nsmall double";
            test_blas<double>(3,4);
            print_spinner();
            dlog << LTRACE << " \nsmall float";
            test_blas<float>(3,4);
            print_spinner();
            dlog << LTRACE << " \nbig double";
            test_blas<double>(120,131);
            print_spinner();
            dlog << LTRACE << " \nbig float";
            test_blas<float>(120,131);
            print_spinner();
            dlog << LTRACE << "testing done";
        }

675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690

        {
            matrix<long> m(3,4), ml(3,4), mu(3,4);
            m = 1,2,3,4,
                4,5,6,7,
                7,8,9,0;

            ml = 1,0,0,0,
                 4,5,0,0,
                 7,8,9,0;

            mu = 1,2,3,4,
                 0,5,6,7,
                 0,0,9,0;


691
692
            DLIB_TEST(lowerm(m) == ml);
            DLIB_TEST(upperm(m) == mu);
693
694
695
696
697
698
699
700
701

            ml = 3,0,0,0,
                 4,3,0,0,
                 7,8,3,0;

            mu = 4,2,3,4,
                 0,4,6,7,
                 0,0,4,0;

702
703
            DLIB_TEST(lowerm(m,3) == ml);
            DLIB_TEST(upperm(m,4) == mu);
704
705
706
707
708
709
710
711
712
713
714
715

        }

        {
            matrix<long> m(3,4), row(1,3), col(2,1);
            m = 1,2,3,4,
                4,5,6,7,
                7,8,9,0;

            row = 4,5,6;
            col = 3,6;

716
717
            DLIB_TEST(rowm(m, 1, 3) == row);
            DLIB_TEST(colm(m, 2, 2) == col);
718
719
720
721

        }


722
723
724
725
        {
            std::vector<double> v(34, 8);
            std::vector<double> v2(34, 9);

726
727
            DLIB_TEST(mat(&v[0], v.size()) == mat(v));
            DLIB_TEST(mat(&v2[0], v.size()) != mat(v));
728
729
730
731
732
733
        }

        {
            std::vector<long> v(1, 3);
            std::vector<long> v2(1, 2);

734
735
            DLIB_TEST(mat(&v[0], v.size()) == mat(v));
            DLIB_TEST(mat(&v2[0], v.size()) != mat(v));
736
        }
737

738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
        {
            matrix<double> a(3,3), b(3,3);
            a = 1,   2.5, 1,
                3,   4,   5,
                0.5, 2.2, 3;

            b = 0, 1, 0,
                1, 1, 1,
                0, 1, 1;

            DLIB_TEST((a>1) == b);
            DLIB_TEST((1<a) == b);

            b = 1, 1, 1,
                1, 1, 1,
                0, 1, 1;

            DLIB_TEST((a>=1) == b);
            DLIB_TEST((1<=a) == b);

            b = 0, 0, 0,
                0, 0, 0,
                0, 1, 0;
            DLIB_TEST((a==2.2) == b);
            DLIB_TEST((a!=2.2) == (b==0));
            DLIB_TEST((2.2==a) == b);
            DLIB_TEST((2.2!=a) == (0==b));

            b = 0, 0, 0,
                0, 0, 0,
                1, 0, 0;
            DLIB_TEST((a<1) == b);
            DLIB_TEST((1>a) == b);

            b = 1, 0, 1,
                0, 0, 0,
                1, 0, 0;
            DLIB_TEST((a<=1) == b);
            DLIB_TEST((1>=a) == b);
        }

779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
        {
            matrix<double> a, b, c;
            a = randm(4,2);

            b += a;
            c -= a;

            DLIB_TEST(equal(a, b));
            DLIB_TEST(equal(-a, c));

            b += a;
            c -= a;

            DLIB_TEST(equal(2*a, b));
            DLIB_TEST(equal(-2*a, c));

            b += a + a;
            c -= a + a;

            DLIB_TEST(equal(4*a, b));
            DLIB_TEST(equal(-4*a, c));

            b.set_size(0,0);
            c.set_size(0,0);


            b += a + a;
            c -= a + a;

            DLIB_TEST(equal(2*a, b));
            DLIB_TEST(equal(-2*a, c));
        }

Davis King's avatar
Davis King committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
        {
            matrix<int> a, b, c;

            a.set_size(2, 3);
            b.set_size(2, 6);
            c.set_size(4, 3);

            a = 1, 2, 3,
                4, 5, 6;

            b = 1, 2, 3, 1, 2, 3,
                4, 5, 6, 4, 5, 6;

            c = 1, 2, 3,
                4, 5, 6,
                1, 2, 3,
                4, 5, 6;

            DLIB_TEST(join_rows(a,a) == b);
831
            DLIB_TEST(join_rows(a,abs(a)) == b);
Davis King's avatar
Davis King committed
832
833
            DLIB_TEST(join_cols(trans(a), trans(a)) == trans(b));
            DLIB_TEST(join_cols(a,a) == c)
834
            DLIB_TEST(join_cols(a,abs(a)) == c)
Davis King's avatar
Davis King committed
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
            DLIB_TEST(join_rows(trans(a),trans(a)) == trans(c))
        }

        {
            matrix<int, 2, 3> a;
            matrix<int, 2, 6> b;
            matrix<int, 4, 3> c;

            a = 1, 2, 3,
                4, 5, 6;

            b = 1, 2, 3, 1, 2, 3,
                4, 5, 6, 4, 5, 6;

            c = 1, 2, 3,
                4, 5, 6,
                1, 2, 3,
                4, 5, 6;

            DLIB_TEST(join_rows(a,a) == b);
855
            DLIB_TEST(join_rows(a,abs(a)) == b);
Davis King's avatar
Davis King committed
856
857
            DLIB_TEST(join_cols(trans(a), trans(a)) == trans(b));
            DLIB_TEST(join_cols(a,a) == c)
858
            DLIB_TEST(join_cols(a,abs(a)) == c)
Davis King's avatar
Davis King committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
            DLIB_TEST(join_rows(trans(a),trans(a)) == trans(c))
        }

        {
            matrix<int, 2, 3> a;
            matrix<int> a2;
            matrix<int, 2, 6> b;
            matrix<int, 4, 3> c;

            a = 1, 2, 3,
                4, 5, 6;

            a2 = a;

            b = 1, 2, 3, 1, 2, 3,
                4, 5, 6, 4, 5, 6;

            c = 1, 2, 3,
                4, 5, 6,
                1, 2, 3,
                4, 5, 6;

            DLIB_TEST(join_rows(a,a2) == b);
            DLIB_TEST(join_rows(a2,a) == b);
            DLIB_TEST(join_cols(trans(a2), trans(a)) == trans(b));
            DLIB_TEST(join_cols(a2,a) == c)
            DLIB_TEST(join_cols(a,a2) == c)
            DLIB_TEST(join_rows(trans(a2),trans(a)) == trans(c))
        }

889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
        {
            matrix<int> a, b;

            a.set_size(2,3);

            a = 1, 2, 3,
                4, 5, 6;

            b.set_size(3,2);
            b = 1, 2,
                3, 4,
                5, 6;

            DLIB_TEST(reshape(a, 3, 2) == b);

            b.set_size(2,3);
            b = 1, 4, 2,
                5, 3, 6;

            DLIB_TEST(reshape(trans(a), 2, 3) == b);

        }

        {
            matrix<int,2,3> a;
            matrix<int> b;

            a = 1, 2, 3,
                4, 5, 6;

            b.set_size(3,2);
            b = 1, 2,
                3, 4,
                5, 6;

            DLIB_TEST(reshape(a, 3, 2) == b);

            b.set_size(2,3);
            b = 1, 4, 2,
                5, 3, 6;

            DLIB_TEST(reshape(trans(a), 2, 3) == b);

        }

        {
            std::vector<int> v(6);
            for (unsigned long i = 0; i < v.size(); ++i)
                v[i] = i;

            matrix<int,2,3> a;
            a = 0, 1, 2, 
                3, 4, 5;

943
            DLIB_TEST(mat(&v[0], 2, 3) == a);
944
        }
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967

        {
            matrix<int> a(3,4);
            matrix<int> b(3,1), c(1,4);

            a = 1, 2, 3, 6,
                4, 5, 6, 9,
                1, 1, 1, 3;

            b(0) = sum(rowm(a,0));
            b(1) = sum(rowm(a,1));
            b(2) = sum(rowm(a,2));

            c(0) = sum(colm(a,0));
            c(1) = sum(colm(a,1));
            c(2) = sum(colm(a,2));
            c(3) = sum(colm(a,3));

            DLIB_TEST(sum_cols(a) == b);
            DLIB_TEST(sum_rows(a) == c);

        }

Davis King's avatar
Davis King committed
968
969
970
971
972
973
974
975
976
977
978
979
        {
            matrix<int> m(3,3);

            m = 1, 2, 3,
                4, 5, 6,
                7, 8, 9;

            DLIB_TEST(make_symmetric(m) == trans(make_symmetric(m)));
            DLIB_TEST(lowerm(make_symmetric(m)) == lowerm(m));
            DLIB_TEST(upperm(make_symmetric(m)) == trans(lowerm(m)));
        }

980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        {
            matrix<int,3,4> a;
            matrix<int> b(3,1), c(1,4);

            a = 1, 2, 3, 6,
                4, 5, 6, 9,
                1, 1, 1, 3;

            b(0) = sum(rowm(a,0));
            b(1) = sum(rowm(a,1));
            b(2) = sum(rowm(a,2));

            c(0) = sum(colm(a,0));
            c(1) = sum(colm(a,1));
            c(2) = sum(colm(a,2));
            c(3) = sum(colm(a,3));

            DLIB_TEST(sum_cols(a) == b);
            DLIB_TEST(sum_rows(a) == c);

        }
Davis King's avatar
Davis King committed
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
    }






    class matrix_tester : public tester
    {
    public:
        matrix_tester (
        ) :
            tester ("test_matrix3",
                    "Runs tests on the matrix component.")
        {}

        void perform_test (
        )
        {
            matrix_test();
        }
    } a;

}