"git@developer.sourcefind.cn:OpenDAS/ollama.git" did not exist on "c79f8c9c3934ee8c9507ab89d4ec31c16bbd0fd8"
matrix.cpp 38.8 KB
Newer Older
1
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
2
3
4
5
6
7
8
9
10
11
12
// 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"
13
#include "../rand.h"
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#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.matrix");

    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<complex<double>,2,2,MM> m;
            set_all_elements(m,complex<double>(1,2));
42
43
44
45
            DLIB_TEST((conj(m) == uniform_matrix<complex<double>,2,2>(conj(m(0,0)))));
            DLIB_TEST((real(m) == uniform_matrix<double,2,2>(1)));
            DLIB_TEST((imag(m) == uniform_matrix<double,2,2>(2)));
            DLIB_TEST_MSG((sum(abs(norm(m) - uniform_matrix<double,2,2>(5))) < 1e-10 ),norm(m));
46
47
48
49

        }

        {
50
            matrix<double,5,5,MM,column_major_layout> m(5,5);
51
52
53
54
55
56
57
58
59
60
61
62
63

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
64
65
66
67
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,5>())));
            DLIB_TEST((equal(round_zeros(m*mi,0.000001) , identity_matrix<double,5>())));
68
69
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix(m))));
            DLIB_TEST((equal(round_zeros(m*mi,0.000001) , identity_matrix(m))));
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        }
        {
            matrix<double,5,0,MM> m(5,5);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
86
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,5>())));
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        }

        {
            matrix<double,0,5,MM> m(5,5);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
104
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,5>())));
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        }


        {
            matrix<double> m(5,5);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
123
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,5>())));
124
125
        }

126
        {
127
            matrix<double,5,2,MM,column_major_layout> m;
128
129
130
131
132
133
134
135
136
137
138
139
140

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
141
142
143
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,2>())));
144
145
        }

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
        {
            matrix<double> m(5,2);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = pinv(m ); 
161
162
163
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,2>())));
164
165
        }

166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
        {
            matrix<double,5,2,MM,column_major_layout> m;

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = trans(pinv(trans(m) )); 
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,2>())));
        }

        {
            matrix<double> m(5,2);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c; 
                }
            }

            m = cos(exp(m));


            matrix<double> mi = trans(pinv(trans(m) )); 
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
            DLIB_TEST((equal(round_zeros(mi*m,0.000001) , identity_matrix<double,2>())));
        }

206
207
        {
            matrix<long> a1(5,1);
208
            matrix<long,0,0,MM,column_major_layout> a2(1,5);
209
210
211
212
            matrix<long,5,1> b1(5,1);
            matrix<long,1,5> b2(1,5);
            matrix<long,0,1> c1(5,1);
            matrix<long,1,0> c2(1,5);
213
            matrix<long,0,1,MM,column_major_layout> d1(5,1);
214
215
216
217
218
219
220
221
222
223
224
225
226
227
            matrix<long,1,0,MM> d2(1,5);

            for (long i = 0; i < 5; ++i)
            {
                a1(i) = i;
                a2(i) = i;
                b1(i) = i;
                b2(i) = i;
                c1(i) = i;
                c2(i) = i;
                d1(i) = i;
                d2(i) = i;
            }

228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
            DLIB_TEST(a1 == trans(a2));
            DLIB_TEST(a1 == trans(b2));
            DLIB_TEST(a1 == trans(c2));
            DLIB_TEST(a1 == trans(d2));

            DLIB_TEST(a1 == b1);
            DLIB_TEST(a1 == c1);
            DLIB_TEST(a1 == d1);

            DLIB_TEST(trans(a1) == c2);
            DLIB_TEST(trans(b1) == c2);
            DLIB_TEST(trans(c1) == c2);
            DLIB_TEST(trans(d1) == c2);

            DLIB_TEST(sum(a1) == 10);
            DLIB_TEST(sum(a2) == 10);
            DLIB_TEST(sum(b1) == 10);
            DLIB_TEST(sum(b2) == 10);
            DLIB_TEST(sum(c1) == 10);
            DLIB_TEST(sum(c2) == 10);
            DLIB_TEST(sum(d1) == 10);
            DLIB_TEST(sum(d2) == 10);
250
251
252
253
254
255
256
257
258
259
260
261
262
263

            const matrix<long> orig1 = a1;
            const matrix<long> orig2 = a2;

            ostringstream sout;
            serialize(a1,sout);
            serialize(a2,sout);
            serialize(b1,sout);
            serialize(b2,sout);
            serialize(c1,sout);
            serialize(c2,sout);
            serialize(d1,sout);
            serialize(d2,sout);

264
265
266
267
268
269
270
271
            DLIB_TEST(a1 == orig1);
            DLIB_TEST(a2 == orig2);
            DLIB_TEST(b1 == orig1);
            DLIB_TEST(b2 == orig2);
            DLIB_TEST(c1 == orig1);
            DLIB_TEST(c2 == orig2);
            DLIB_TEST(d1 == orig1);
            DLIB_TEST(d2 == orig2);
272
273
274
275
276
277
278
279
280
281

            set_all_elements(a1,99);
            set_all_elements(a2,99);
            set_all_elements(b1,99);
            set_all_elements(b2,99);
            set_all_elements(c1,99);
            set_all_elements(c2,99);
            set_all_elements(d1,99);
            set_all_elements(d2,99);

282
283
284
285
286
287
288
289
            DLIB_TEST(a1 != orig1);
            DLIB_TEST(a2 != orig2);
            DLIB_TEST(b1 != orig1);
            DLIB_TEST(b2 != orig2);
            DLIB_TEST(c1 != orig1);
            DLIB_TEST(c2 != orig2);
            DLIB_TEST(d1 != orig1);
            DLIB_TEST(d2 != orig2);
290
291
292
293
294
295
296
297
298
299
300
301

            istringstream sin(sout.str());

            deserialize(a1,sin);
            deserialize(a2,sin);
            deserialize(b1,sin);
            deserialize(b2,sin);
            deserialize(c1,sin);
            deserialize(c2,sin);
            deserialize(d1,sin);
            deserialize(d2,sin);

302
303
304
305
306
307
308
309
            DLIB_TEST(a1 == orig1);
            DLIB_TEST(a2 == orig2);
            DLIB_TEST(b1 == orig1);
            DLIB_TEST(b2 == orig2);
            DLIB_TEST(c1 == orig1);
            DLIB_TEST(c2 == orig2);
            DLIB_TEST(d1 == orig1);
            DLIB_TEST(d2 == orig2);
310
311
312
313
314
315
316
317
318


        }

        {
            matrix<double,1,0> a(5);
            matrix<double,0,1> b(5);
            matrix<double,1,5> c(5);
            matrix<double,5,1> d(5);
319
320
321
322
323
324
325
326
327
            DLIB_TEST(a.nr() == 1);
            DLIB_TEST(a.nc() == 5);
            DLIB_TEST(c.nr() == 1);
            DLIB_TEST(c.nc() == 5);

            DLIB_TEST(b.nc() == 1);
            DLIB_TEST(b.nr() == 5);
            DLIB_TEST(d.nc() == 1);
            DLIB_TEST(d.nr() == 5);
328
329
330
331
332
333
334
335
336
337
338
339
340
        }

        {
            matrix<double,1,0> a;
            matrix<double,0,1> b;
            matrix<double,1,5> c;
            matrix<double,5,1> d;

            a.set_size(5);
            b.set_size(5);
            c.set_size(5);
            d.set_size(5);

341
342
343
344
            DLIB_TEST(a.nr() == 1);
            DLIB_TEST(a.nc() == 5);
            DLIB_TEST(c.nr() == 1);
            DLIB_TEST(c.nc() == 5);
345

346
347
348
349
            DLIB_TEST(b.nc() == 1);
            DLIB_TEST(b.nr() == 5);
            DLIB_TEST(d.nc() == 1);
            DLIB_TEST(d.nr() == 5);
350
351
352
353
354
355
356
357
358
359
360
361
        }

        {
            matrix<double> a(1,5);
            matrix<double> b(5,1);

            set_all_elements(a,1);
            set_all_elements(b,1);


            a = a*b;

362
            DLIB_TEST(a(0) == 5);
363
364
365
        }

        {
366
            matrix<double,0,0,MM,column_major_layout> a(6,7);
367
368
369
370
371
372
373
374
375
376
377

            for (long r = 0; r < a.nr(); ++r)
            {
                for (long c = 0; c < a.nc(); ++c)
                {
                    a(r,c) = r*a.nc() + c;
                }
            }



378
379
380
381
            DLIB_TEST(rowm(a,1).nr() == 1);
            DLIB_TEST(rowm(a,1).nc() == a.nc());
            DLIB_TEST(colm(a,1).nr() == a.nr());
            DLIB_TEST(colm(a,1).nc() == 1);
382
383
384

            for (long c = 0; c < a.nc(); ++c)
            {
385
                DLIB_TEST( rowm(a,1)(c) == 1*a.nc() + c);
386
387
388
389
            }

            for (long r = 0; r < a.nr(); ++r)
            {
390
                DLIB_TEST( colm(a,1)(r) == r*a.nc() + 1);
391
392
393
            }

            rectangle rect(2, 1, 3+2-1, 2+1-1);
394
395
            DLIB_TEST(get_rect(a).contains(get_rect(a)));
            DLIB_TEST(get_rect(a).contains(rect));
396
397
398
399
            for (long r = 0; r < 2; ++r)
            {
                for (long c = 0; c < 3; ++c)
                {
400
401
                    DLIB_TEST(subm(a,1,2,2,3)(r,c) == (r+1)*a.nc() + c+2);
                    DLIB_TEST(subm(a,1,2,2,3) == subm(a,rect));
402
403
404
                }
            }

405
406
            DLIB_TEST(subm(a,rectangle()).nr() == 0);
            DLIB_TEST(subm(a,rectangle()).nc() == 0);
407
408
409
410

        }

        {
411
            array2d<double> a;
412
413
414
415
416
417
418
419
420
421
422
423
424
            a.set_size(6,7);


            for (long r = 0; r < a.nr(); ++r)
            {
                for (long c = 0; c < a.nc(); ++c)
                {
                    a[r][c] = r*a.nc() + c;
                }
            }



425
426
427
428
            DLIB_TEST(rowm(mat(a),1).nr() == 1);
            DLIB_TEST(rowm(mat(a),1).nc() == a.nc());
            DLIB_TEST(colm(mat(a),1).nr() == a.nr());
            DLIB_TEST(colm(mat(a),1).nc() == 1);
429
430
431

            for (long c = 0; c < a.nc(); ++c)
            {
432
                DLIB_TEST( rowm(mat(a),1)(c) == 1*a.nc() + c);
433
434
435
436
            }

            for (long r = 0; r < a.nr(); ++r)
            {
437
                DLIB_TEST( colm(mat(a),1)(r) == r*a.nc() + 1);
438
439
440
441
442
443
            }

            for (long r = 0; r < 2; ++r)
            {
                for (long c = 0; c < 3; ++c)
                {
444
                    DLIB_TEST(subm(mat(a),1,2,2,3)(r,c) == (r+1)*a.nc() + c+2);
445
446
447
448
449
450
451
                }
            }


        }

        {
452
            array2d<double> m;
453
454
455
456
457
458
459
460
461
462
463
            m.set_size(5,5);

            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m[r][c] = r*c; 
                }
            }


464
            matrix<double> mi = pinv(cos(exp(mat(m))) ); 
465
466
            DLIB_TEST(mi.nr() == m.nc());
            DLIB_TEST(mi.nc() == m.nr());
467
468
            DLIB_TEST((equal(round_zeros(mi*cos(exp(mat(m))),0.000001) , identity_matrix<double,5>())));
            DLIB_TEST((equal(round_zeros(cos(exp(mat(m)))*mi,0.000001) , identity_matrix<double,5>())));
469
470
        }

471
        {
472
            matrix<long,5,5,MM,column_major_layout> m1, res;
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
            matrix<long,2,2> m2;

            set_all_elements(m1,0);


            long res_vals[] = {
                9, 9, 9, 9, 9,
                0, 1, 1, 0, 0,
                0, 1, 1, 0, 2,
                0, 0, 2, 2, 2,
                0, 0, 2, 2, 0
            };

            res = res_vals;

            set_all_elements(m2, 1);
            set_subm(m1, range(1,2), range(1,2)) = subm(m2,0,0,2,2);
            set_all_elements(m2, 2);
            set_subm(m1, 3,2,2,2) = m2;

            set_colm(m1,4) = trans(rowm(m1,4));
            set_rowm(m1,0) = 9;

496
            DLIB_TEST_MSG(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
497
498

            set_subm(m1,0,0,5,5) = m1*m1;
499
            DLIB_TEST_MSG(m1 == res*res, "m1: \n" << m1 << "\nres*res: \n" << res*res);
500
501
502
503
504
505
506
507
508
509
510
511
512

            m1 = res;
            set_subm(m1,1,1,2,2) = subm(m1,0,0,2,2);

            long res_vals2[] = {
                9, 9, 9, 9, 9,
                0, 9, 9, 0, 0,
                0, 0, 1, 0, 2,
                0, 0, 2, 2, 2,
                0, 0, 2, 2, 0
            };

            res = res_vals2;
513
            DLIB_TEST_MSG(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
514
515
516


        }
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542

        {
            matrix<long,5,5> m1, res;
            matrix<long,2,2> m2;

            set_all_elements(m1,0);


            long res_vals[] = {
                9, 9, 9, 9, 9,
                0, 1, 1, 0, 0,
                0, 1, 1, 0, 2,
                0, 0, 2, 2, 2,
                0, 0, 2, 2, 0
            };

            res = res_vals;

            set_all_elements(m2, 1);
            set_subm(m1, rectangle(1,1,2,2)) = subm(m2,0,0,2,2);
            set_all_elements(m2, 2);
            set_subm(m1, 3,2,2,2) = m2;

            set_colm(m1,4) = trans(rowm(m1,4));
            set_rowm(m1,0) = 9;

543
            DLIB_TEST_MSG(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
544

545
            set_subm(m1,0,0,5,5) = m1*m1;
546
            DLIB_TEST_MSG(m1 == res*res, "m1: \n" << m1 << "\nres*res: \n" << res*res);
547
548
549
550
551
552
553
554
555
556
557
558
559

            m1 = res;
            set_subm(m1,1,1,2,2) = subm(m1,0,0,2,2);

            long res_vals2[] = {
                9, 9, 9, 9, 9,
                0, 9, 9, 0, 0,
                0, 0, 1, 0, 2,
                0, 0, 2, 2, 2,
                0, 0, 2, 2, 0
            };

            res = res_vals2;
560
            DLIB_TEST_MSG(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
561
562


563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
        }

        {
            matrix<long,5,5> m1, res;
            matrix<long,2,2> m2;

            set_all_elements(m1,0);


            long res_vals[] = {
                9, 0, 3, 3, 0,
                9, 2, 2, 2, 0,
                9, 2, 2, 2, 0,
                4, 4, 4, 4, 4,
                9, 0, 3, 3, 0
            };
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
            long res_vals_c3[] = {
                9, 0, 3, 0,
                9, 2, 2, 0,
                9, 2, 2, 0,
                4, 4, 4, 4,
                9, 0, 3, 0
            };
            long res_vals_r2[] = {
                9, 0, 3, 3, 0,
                9, 2, 2, 2, 0,
                4, 4, 4, 4, 4,
                9, 0, 3, 3, 0
            };

            matrix<long> temp;
594
595
596

            res = res_vals;

597
            temp = matrix<long,4,5>(res_vals_r2);
598
599
600
601
602
603
604
605
            DLIB_TEST(remove_row<2>(res) == temp);
            DLIB_TEST(remove_row<2>(res)(3,3) == 3);
            DLIB_TEST(remove_row<2>(res).nr() == 4);
            DLIB_TEST(remove_row<2>(res).nc() == 5);
            DLIB_TEST(remove_row(res,2) == temp);
            DLIB_TEST(remove_row(res,2)(3,3) == 3);
            DLIB_TEST(remove_row(res,2).nr() == 4);
            DLIB_TEST(remove_row(res,2).nc() == 5);
606

607
608
            temp = matrix<long,5,5>(res_vals);
            temp = remove_row(res,2);
609
            DLIB_TEST((temp == matrix<long,4,5>(res_vals_r2)));
610
611
            temp = matrix<long,5,5>(res_vals);
            temp = remove_col(res,3);
612
            DLIB_TEST((temp == matrix<long,5,4>(res_vals_c3)));
613
614
615
616
617

            matrix<long,3,1> vect;
            set_all_elements(vect,1);
            temp = identity_matrix<long>(3);
            temp = temp*vect;
618
            DLIB_TEST(temp == vect);
619

620
            temp = matrix<long,5,4>(res_vals_c3);
621
622
623
624
            DLIB_TEST(remove_col(res,3) == temp);
            DLIB_TEST(remove_col(res,3)(2,3) == 0);
            DLIB_TEST(remove_col(res,3).nr() == 5);
            DLIB_TEST(remove_col(res,3).nc() == 4);
625

626
627
628
629
630
631
632
633
634
            set_all_elements(m2, 1);
            set_subm(m1, rectangle(1,1,3,2)) = 2;
            set_all_elements(m2, 2);
            set_subm(m1, 3,2,2,2) = 3;

            set_colm(m1,0) = 9;
            set_rowm(m1,0) = rowm(m1,4);
            set_rowm(m1,3) = 4;

635
            DLIB_TEST_MSG(m1 == res, "m1: \n" << m1 << "\nres: \n" << res);
636
637
638

        }

639
640
641
642
643
644
645
646
647
648
649
650
651

        {

            const double stuff[] = { 
                1, 2, 3,
                6, 3, 3, 
                7, 3, 9};

            matrix<double,3,3> m(stuff);

            // make m be symmetric
            m = m*trans(m);

652
            matrix<double> L = chol(m);
653
            DLIB_TEST(equal(L*trans(L), m));
654

655
656
657
            DLIB_TEST_MSG(equal(inv(m), inv_upper_triangular(trans(L))*inv_lower_triangular((L))), "") 
            DLIB_TEST(equal(round_zeros(inv_upper_triangular(trans(L))*trans(L),1e-10), identity_matrix<double>(3), 1e-10)); 
            DLIB_TEST(equal(round_zeros(inv_lower_triangular((L))*(L),1e-10) ,identity_matrix<double>(3),1e-10)); 
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

        }

        {

            const double stuff[] = { 
                1, 2, 3, 6, 3, 4,
                6, 3, 3, 1, 2, 3,
                7, 3, 9, 54.3, 5, 3,
                -6, 3, -3, 1, 2, 3,
                1, 2, 3, 5, -3, 3,
                7, 3, -9, 54.3, 5, 3
                };

            matrix<double,6,6> m(stuff);

            // make m be symmetric
            m = m*trans(m);

677
            matrix<double> L = chol(m);
678
            DLIB_TEST_MSG(equal(L*trans(L), m, 1e-10), L*trans(L)-m);
679

680
681
682
683
            DLIB_TEST_MSG(equal(inv(m), inv_upper_triangular(trans(L))*inv_lower_triangular((L))), "") 
            DLIB_TEST_MSG(equal(inv(m), trans(inv_lower_triangular(L))*inv_lower_triangular((L))), "") 
            DLIB_TEST_MSG(equal(inv(m), trans(inv_lower_triangular(L))*trans(inv_upper_triangular(trans(L)))), "") 
            DLIB_TEST_MSG(equal(round_zeros(inv_upper_triangular(trans(L))*trans(L),1e-10) , identity_matrix<double>(6), 1e-10),
684
                         round_zeros(inv_upper_triangular(trans(L))*trans(L),1e-10)); 
685
            DLIB_TEST_MSG(equal(round_zeros(inv_lower_triangular((L))*(L),1e-10) ,identity_matrix<double>(6), 1e-10),
686
687
688
689
                         round_zeros(inv_lower_triangular((L))*(L),1e-10)); 

        }

690
691
692
693
694
695
        {
            matrix<int> m(3,4), m2;
            m = 1,2,3,4,
                4,5,6,6,
                6,1,8,0;
            m2 = m;
696
697
            DLIB_TEST(round(m) == m2);
            DLIB_TEST(round_zeros(m) == m2);
698
699
700
701
702

            m2 = 0,2,3,4,
                 4,5,6,6,
                 6,0,8,0;

703
            DLIB_TEST(round_zeros(m,2) == m2);
704
705
706
        }


707
708
709
710
        {

            matrix<double,6,6> m(identity_matrix<double>(6)*4.5);

711
            matrix<double> L = chol(m);
712
            DLIB_TEST_MSG(equal(L*trans(L), m, 1e-10), L*trans(L)-m);
713

714
715
            DLIB_TEST_MSG(equal(inv(m), inv_upper_triangular(trans(L))*inv_lower_triangular((L))), "") 
            DLIB_TEST_MSG(equal(round_zeros(inv_upper_triangular(trans(L))*trans(L),1e-10) , identity_matrix<double>(6), 1e-10),
716
                         round_zeros(inv_upper_triangular(trans(L))*trans(L),1e-10)); 
717
            DLIB_TEST_MSG(equal(round_zeros(inv_lower_triangular((L))*(L),1e-10) ,identity_matrix<double>(6), 1e-10),
718
719
720
721
722
723
724
725
726
                         round_zeros(inv_lower_triangular((L))*(L),1e-10)); 

        }

        {

            matrix<double,6,6> m(identity_matrix<double>(6)*4.5);
            m(1,4) = 2;

727
728
            DLIB_TEST_MSG(dlib::equal(inv_upper_triangular(m), inv(m),1e-10), inv_upper_triangular(m)-inv(m));
            DLIB_TEST_MSG(dlib::equal(inv_lower_triangular(trans(m)), inv(trans(m)),1e-10), inv_lower_triangular(trans(m))-inv(trans(m)));
729
730
731

        }

732
733
734
735
736
737
738
        {
            matrix<double> a;
            matrix<float> b;
            matrix<int> i;
            a.set_size(1000,10);
            b.set_size(1000,10);
            i.set_size(1000,10);
739
            dlib::rand rnd;
740
741
742
743
744
745
746
747
748
749
750
            for (long r = 0; r < a.nr(); ++r)
            {
                for (long c = 0; c < a.nc(); ++c)
                {
                    a(r,c) = rnd.get_random_double();
                    b(r,c) = rnd.get_random_float();
                    i(r,c) = r+c*r;
                }
            }

            // make sure the multiply optimizations aren't messing things up
751
752
753
            DLIB_TEST(trans(i)*i == tmp(trans(i)*i));
            DLIB_TEST_MSG(equal(trans(a)*a , tmp(trans(a)*a), 1e-11),max(abs(trans(a)*a - tmp(trans(a)*a))));
            DLIB_TEST_MSG(equal(trans(b)*b , tmp(trans(b)*b), 1e-3f),max(abs(trans(b)*b - tmp(trans(b)*b))));
754
755
        }

Davis King's avatar
Davis King committed
756
757
758
759
760
761
762
763
764
765
766
767
768
        {
            matrix<int,4> i(4,1);
            i(0) = 1;
            i(1) = 2;
            i(2) = 3;
            i(3) = 4;
            matrix<int,4,4> m;
            set_all_elements(m,0);
            m(0,0) = 1;
            m(1,1) = 2;
            m(2,2) = 3;
            m(3,3) = 4;

769
            DLIB_TEST(diagm(i) == m);
Davis King's avatar
Davis King committed
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
        }

        {
            matrix<int,1,4> i;
            i(0) = 1;
            i(1) = 2;
            i(2) = 3;
            i(3) = 4;
            matrix<int,4,4> m;
            set_all_elements(m,0);
            m(0,0) = 1;
            m(1,1) = 2;
            m(2,2) = 3;
            m(3,3) = 4;

785
            DLIB_TEST(diagm(i) == m);
Davis King's avatar
Davis King committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
        }

        {
            matrix<int> i(4,1);
            i(0) = 1;
            i(1) = 2;
            i(2) = 3;
            i(3) = 4;
            matrix<int> m(4,4);
            set_all_elements(m,0);
            m(0,0) = 1;
            m(1,1) = 2;
            m(2,2) = 3;
            m(3,3) = 4;

801
            DLIB_TEST(diagm(i) == m);
Davis King's avatar
Davis King committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
        }

        {
            matrix<int> i(1,4);
            i(0) = 1;
            i(1) = 2;
            i(2) = 3;
            i(3) = 4;
            matrix<int> m(4,4);
            set_all_elements(m,0);
            m(0,0) = 1;
            m(1,1) = 2;
            m(2,2) = 3;
            m(3,3) = 4;

817
            DLIB_TEST(diagm(i) == m);
Davis King's avatar
Davis King committed
818
819
        }

820
        {
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
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
            DLIB_TEST(range(0,5).nc() == 6);
            DLIB_TEST(range(1,5).nc() == 5);
            DLIB_TEST(range(0,5).nr() == 1);
            DLIB_TEST(range(1,5).nr() == 1);
            DLIB_TEST(trans(range(0,5)).nr() == 6);
            DLIB_TEST(trans(range(1,5)).nr() == 5);
            DLIB_TEST(trans(range(0,5)).nc() == 1);
            DLIB_TEST(trans(range(1,5)).nc() == 1);

            DLIB_TEST(range(0,2,5).nc() == 3);
            DLIB_TEST(range(1,2,5).nc() == 3);
            DLIB_TEST(range(0,2,5).nr() == 1);
            DLIB_TEST(range(1,2,5).nr() == 1);
            DLIB_TEST(trans(range(0,2,5)).nr() == 3);
            DLIB_TEST(trans(range(1,2,5)).nr() == 3);
            DLIB_TEST(trans(range(0,2,5)).nc() == 1);
            DLIB_TEST(trans(range(1,2,5)).nc() == 1);

            DLIB_TEST(range(0,3,6).nc() == 3);
            DLIB_TEST(range(1,3,5).nc() == 2);
            DLIB_TEST(range(0,3,5).nr() == 1);
            DLIB_TEST(range(1,3,5).nr() == 1);
            DLIB_TEST(trans(range(0,3,6)).nr() == 3);
            DLIB_TEST(trans(range(1,3,5)).nr() == 2);
            DLIB_TEST(trans(range(0,3,5)).nc() == 1);
            DLIB_TEST(trans(range(1,3,5)).nc() == 1);

            DLIB_TEST(range(1,9,5).nc() == 1);
            DLIB_TEST(range(1,9,5).nr() == 1);

            DLIB_TEST(range(0,0).nc() == 1);
            DLIB_TEST(range(0,0).nr() == 1);

            DLIB_TEST(range(1,1)(0) == 1);

            DLIB_TEST(range(0,5)(0) == 0 && range(0,5)(1) == 1 && range(0,5)(5) == 5);
            DLIB_TEST(range(1,2,5)(0) == 1 && range(1,2,5)(1) == 3 && range(1,2,5)(2) == 5);
            DLIB_TEST((range<0,5>()(0) == 0 && range<0,5>()(1) == 1 && range<0,5>()(5) == 5));
            DLIB_TEST((range<1,2,5>()(0) == 1 && range<1,2,5>()(1) == 3 && range<1,2,5>()(2) == 5));


            DLIB_TEST((range<0,5>().nc() == 6));
            DLIB_TEST((range<1,5>().nc() == 5));
            DLIB_TEST((range<0,5>().nr() == 1));
            DLIB_TEST((range<1,5>().nr() == 1));
            DLIB_TEST((trans(range<0,5>()).nr() == 6));
            DLIB_TEST((trans(range<1,5>()).nr() == 5));
            DLIB_TEST((trans(range<0,5>()).nc() == 1));
            DLIB_TEST((trans(range<1,5>()).nc() == 1));

            DLIB_TEST((range<0,2,5>().nc() == 3));
            DLIB_TEST((range<1,2,5>().nc() == 3));
            DLIB_TEST((range<0,2,5>().nr() == 1));
            DLIB_TEST((range<1,2,5>().nr() == 1));
            DLIB_TEST((trans(range<0,2,5>()).nr() == 3));
            DLIB_TEST((trans(range<1,2,5>()).nr() == 3));
            DLIB_TEST((trans(range<0,2,5>()).nc() == 1));
            DLIB_TEST((trans(range<1,2,5>()).nc() == 1));

            DLIB_TEST((range<0,3,6>().nc() == 3));
            DLIB_TEST((range<1,3,5>().nc() == 2));
            DLIB_TEST((range<0,3,5>().nr() == 1));
            DLIB_TEST((range<1,3,5>().nr() == 1));
            DLIB_TEST((trans(range<0,3,6>()).nr() == 3));
            DLIB_TEST((trans(range<1,3,5>()).nr() == 2));
            DLIB_TEST((trans(range<0,3,5>()).nc() == 1));
            DLIB_TEST((trans(range<1,3,5>()).nc() == 1));
888
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
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
            DLIB_TEST(range(5,0).nc() == 6);
            DLIB_TEST(range(5,1).nc() == 5);
            DLIB_TEST(range(5,0).nr() == 1);
            DLIB_TEST(range(5,1).nr() == 1);
            DLIB_TEST(trans(range(5,0)).nr() == 6);
            DLIB_TEST(trans(range(5,1)).nr() == 5);
            DLIB_TEST(trans(range(5,0)).nc() == 1);
            DLIB_TEST(trans(range(5,1)).nc() == 1);

            DLIB_TEST(range(5,2,0).nc() == 3);
            DLIB_TEST(range(5,2,1).nc() == 3);
            DLIB_TEST(range(5,2,0).nr() == 1);
            DLIB_TEST(range(5,2,1).nr() == 1);
            DLIB_TEST(trans(range(5,2,0)).nr() == 3);
            DLIB_TEST(trans(range(5,2,1)).nr() == 3);
            DLIB_TEST(trans(range(5,2,0)).nc() == 1);
            DLIB_TEST(trans(range(5,2,1)).nc() == 1);

            DLIB_TEST(range(6,3,0).nc() == 3);
            DLIB_TEST(range(5,3,1).nc() == 2);
            DLIB_TEST(range(5,3,0).nr() == 1);
            DLIB_TEST(range(5,3,1).nr() == 1);
            DLIB_TEST(trans(range(6,3,0)).nr() == 3);
            DLIB_TEST(trans(range(5,3,1)).nr() == 2);
            DLIB_TEST(trans(range(5,3,0)).nc() == 1);
            DLIB_TEST(trans(range(5,3,1)).nc() == 1);

            DLIB_TEST(range(5,9,1).nc() == 1);
            DLIB_TEST(range(5,9,1).nr() == 1);

            DLIB_TEST(range(0,0).nc() == 1);
            DLIB_TEST(range(0,0).nr() == 1);

            DLIB_TEST(range(1,1)(0) == 1);

            DLIB_TEST(range(5,0)(0) == 5 && range(5,0)(1) == 4 && range(5,0)(5) == 0);
            DLIB_TEST(range(5,2,1)(0) == 5 && range(5,2,1)(1) == 3 && range(5,2,1)(2) == 1);
            DLIB_TEST((range<5,0>()(0) == 5 && range<5,0>()(1) == 4 && range<5,0>()(5) == 0));
            DLIB_TEST((range<5,2,1>()(0) == 5 && range<5,2,1>()(1) == 3 && range<5,2,1>()(2) == 1));


            DLIB_TEST((range<5,0>().nc() == 6));
            DLIB_TEST((range<5,1>().nc() == 5));
            DLIB_TEST((range<5,0>().nr() == 1));
            DLIB_TEST((range<5,1>().nr() == 1));
            DLIB_TEST((trans(range<5,0>()).nr() == 6));
            DLIB_TEST((trans(range<5,1>()).nr() == 5));
            DLIB_TEST((trans(range<5,0>()).nc() == 1));
            DLIB_TEST((trans(range<5,1>()).nc() == 1));

            DLIB_TEST((range<5,2,0>().nc() == 3));
            DLIB_TEST((range<5,2,1>().nc() == 3));
            DLIB_TEST((range<5,2,0>().nr() == 1));
            DLIB_TEST((range<5,2,1>().nr() == 1));
            DLIB_TEST((trans(range<5,2,0>()).nr() == 3));
            DLIB_TEST((trans(range<5,2,1>()).nr() == 3));
            DLIB_TEST((trans(range<5,2,0>()).nc() == 1));
            DLIB_TEST((trans(range<5,2,1>()).nc() == 1));

            DLIB_TEST((range<6,3,0>().nc() == 3));
            DLIB_TEST((range<5,3,1>().nc() == 2));
            DLIB_TEST((range<5,3,0>().nr() == 1));
            DLIB_TEST((range<5,3,1>().nr() == 1));
            DLIB_TEST((trans(range<6,3,0>()).nr() == 3));
            DLIB_TEST((trans(range<5,3,1>()).nr() == 2));
            DLIB_TEST((trans(range<5,3,0>()).nc() == 1));
            DLIB_TEST((trans(range<5,3,1>()).nc() == 1));
958
959
        }

960
961
962
963
964
965
966
967
968
969
        {
            matrix<double> m(4,3);
            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c;
                }
            }

970
971
972
            DLIB_TEST(subm(m,range(0,3),range(0,0)) == colm(m,0));
            DLIB_TEST(subm(m,range(0,3),range(1,1)) == colm(m,1));
            DLIB_TEST(subm(m,range(0,3),range(2,2)) == colm(m,2));
973

974
975
976
977
            DLIB_TEST(subm(m,range(0,0),range(0,2)) == rowm(m,0));
            DLIB_TEST(subm(m,range(1,1),range(0,2)) == rowm(m,1));
            DLIB_TEST(subm(m,range(2,2),range(0,2)) == rowm(m,2));
            DLIB_TEST(subm(m,range(3,3),range(0,2)) == rowm(m,3));
978

979
980
            DLIB_TEST(subm(m,0,0,2,2) == subm(m,range(0,1),range(0,1)));
            DLIB_TEST(subm(m,1,1,2,2) == subm(m,range(1,2),range(1,2)));
981
982
983

            matrix<double,2,2> m2 = subm(m,range(0,2,2),range(0,2,2));

984
985
986
987
            DLIB_TEST(m2(0,0) == m(0,0));
            DLIB_TEST(m2(0,1) == m(0,2));
            DLIB_TEST(m2(1,0) == m(2,0));
            DLIB_TEST(m2(1,1) == m(2,2));
988
989
990
991
992
993
994
995
996
997
998
999
1000


        }
        {
            matrix<double,4,3> m(4,3);
            for (long r = 0; r < m.nr(); ++r)
            {
                for (long c = 0; c < m.nc(); ++c)
                {
                    m(r,c) = r*c;
                }
            }

1001
1002
1003
            DLIB_TEST(subm(m,range<0,3>(),range<0,0>()) == colm(m,0));
            DLIB_TEST(subm(m,range<0,3>(),range<1,1>()) == colm(m,1));
            DLIB_TEST(subm(m,range<0,3>(),range<2,2>()) == colm(m,2));
1004

1005
1006
1007
1008
            DLIB_TEST(subm(m,range<0,0>(),range<0,2>()) == rowm(m,0));
            DLIB_TEST(subm(m,range<1,1>(),range<0,2>()) == rowm(m,1));
            DLIB_TEST(subm(m,range<2,2>(),range<0,2>()) == rowm(m,2));
            DLIB_TEST(subm(m,range<3,3>(),range<0,2>()) == rowm(m,3));
1009

1010
1011
            DLIB_TEST(subm(m,0,0,2,2) == subm(m,range<0,1>(),range<0,1>()));
            DLIB_TEST(subm(m,1,1,2,2) == subm(m,range<1,2>(),range<1,2>()));
1012
1013
1014

            matrix<double,2,2> m2 = subm(m,range<0,2,2>(),range<0,2,2>());

1015
1016
1017
1018
            DLIB_TEST(m2(0,0) == m(0,0));
            DLIB_TEST(m2(0,1) == m(0,2));
            DLIB_TEST(m2(1,0) == m(2,0));
            DLIB_TEST(m2(1,1) == m(2,2));
1019
1020
1021
1022


        }

1023
1024
1025
        {
            matrix<double,4,5> m;
            set_subm(m, range(0,3), range(0,4)) = 4;
1026
            DLIB_TEST(min(m) == max(m) && min(m) == 4);
1027
1028

            set_subm(m,range(1,1),range(0,4)) = 7;
1029
1030
1031
1032
            DLIB_TEST((rowm(m,0) == uniform_matrix<double>(1,5, 4)));
            DLIB_TEST((rowm(m,1) == uniform_matrix<double>(1,5, 7)));
            DLIB_TEST((rowm(m,2) == uniform_matrix<double>(1,5, 4)));
            DLIB_TEST((rowm(m,3) == uniform_matrix<double>(1,5, 4)));
1033
1034
1035
1036
1037


            set_subm(m, range(0,2,3), range(0,2,4)) = trans(subm(m,0,0,3,2));


1038
1039
            DLIB_TEST(m(0,2) == 7);
            DLIB_TEST(m(2,2) == 7);
1040

1041
            DLIB_TEST(sum(m) == 7*5+ 7+7 +  4*(4*5 - 7));
1042
1043
1044

        }

1045
        {
1046
            matrix<double> mat(4,5);
1047
            DLIB_TEST((uniform_matrix<double>(4,5,1) == ones_matrix<double>(4,5)));
1048
            DLIB_TEST((uniform_matrix<double>(4,5,1) == ones_matrix(mat)));
1049
            DLIB_TEST((uniform_matrix<double>(4,5,0) == zeros_matrix<double>(4,5)));
1050
            DLIB_TEST((uniform_matrix<double>(4,5,0) == zeros_matrix(mat)));
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
            DLIB_TEST((uniform_matrix<float>(4,5,1) == ones_matrix<float>(4,5)));
            DLIB_TEST((uniform_matrix<float>(4,5,0) == zeros_matrix<float>(4,5)));
            DLIB_TEST((uniform_matrix<complex<double> >(4,5,1) == ones_matrix<complex<double> >(4,5)));
            DLIB_TEST((uniform_matrix<complex<double> >(4,5,0) == zeros_matrix<complex<double> >(4,5)));
            DLIB_TEST((uniform_matrix<complex<float> >(4,5,1) == ones_matrix<complex<float> >(4,5)));
            DLIB_TEST((uniform_matrix<complex<float> >(4,5,0) == zeros_matrix<complex<float> >(4,5)));
            DLIB_TEST((complex_matrix(ones_matrix<double>(3,3), zeros_matrix<double>(3,3)) == complex_matrix(ones_matrix<double>(3,3))));
            DLIB_TEST((pointwise_multiply(complex_matrix(ones_matrix<double>(3,3)), ones_matrix<double>(3,3)*2) ==
                       complex_matrix(2*ones_matrix<double>(3,3))));
        }

Davis King's avatar
Davis King committed
1062
        {
1063
1064
            DLIB_TEST(( uniform_matrix<double>(303,303, 3)*identity_matrix<double>(303) == uniform_matrix<double,303,303>(3) ) );
            DLIB_TEST(( uniform_matrix<double,303,303>(3)*identity_matrix<double,303>() == uniform_matrix<double,303,303>(3) ));
Davis King's avatar
Davis King committed
1065
1066
        }

Davis King's avatar
Davis King committed
1067
1068
        {
            matrix<double> m(2,3);
1069
1070
            m = 1,2,3,
                5,6,7;
Davis King's avatar
Davis King committed
1071

1072
            DLIB_TEST_MSG(m(0,0) == 1 && m(0,1) == 2 && m(0,2) == 3 &&
Davis King's avatar
Davis King committed
1073
1074
                         m(1,0) == 5 && m(1,1) == 6 && m(1,2) == 7,"");

1075
            m = 4;
1076
            DLIB_TEST((m == uniform_matrix<double,2,3>(4)));
1077

Davis King's avatar
Davis King committed
1078
            matrix<double,2,3> m2;
1079
1080
            m2 = 1,2,3,
                 5,6,7;
1081
            DLIB_TEST_MSG(m2(0,0) == 1 && m2(0,1) == 2 && m2(0,2) == 3 &&
Davis King's avatar
Davis King committed
1082
1083
1084
                         m2(1,0) == 5 && m2(1,1) == 6 && m2(1,2) == 7,"");

            matrix<double,2,1> m3;
1085
1086
            m3 = 1,
                 5;
1087
            DLIB_TEST(m3(0) == 1 && m3(1) == 5 );
Davis King's avatar
Davis King committed
1088
1089

            matrix<double,1,2> m4;
1090
            m4 = 1, 5;
1091
            DLIB_TEST(m3(0) == 1 && m3(1) == 5 );
Davis King's avatar
Davis King committed
1092
1093
        }

1094
1095
1096
        {
            matrix<double> m(4,1);
            m = 3, 1, 5, 2;
1097
1098
1099
1100
            DLIB_TEST(index_of_min(m) == 1);
            DLIB_TEST(index_of_max(m) == 2);
            DLIB_TEST(index_of_min(trans(m)) == 1);
            DLIB_TEST(index_of_max(trans(m)) == 2);
1101
        }
Davis King's avatar
Davis King committed
1102

1103
1104
1105
1106
1107
1108
        {
            matrix<double> m1(1,5), m2;

            m1 = 3.0000,  3.7500,  4.5000,  5.2500,  6.0000; 
            m2 = linspace(3, 6, 5);

1109
            DLIB_TEST(equal(m1, m2));
1110
1111
1112
1113
            
            m1 = pow(10, m1);
            m2 = logspace(3, 6, 5);

1114
            DLIB_TEST(equal(m1, m2));
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
        }

        {
            matrix<long> m = cartesian_product(range(1,3), range(0,1));

            matrix<long,2,1> c0, c1, c2, c3, c4, c5;
            c0 = 1, 0;
            c1 = 1, 1;
            c2 = 2, 0;
            c3 = 2, 1;
            c4 = 3, 0;
            c5 = 3, 1;

1128
1129
1130
1131
1132
1133
            DLIB_TEST_MSG(colm(m,0) == c0, colm(m,0) << "\n\n" << c0);
            DLIB_TEST(colm(m,1) == c1);
            DLIB_TEST(colm(m,2) == c2);
            DLIB_TEST(colm(m,3) == c3);
            DLIB_TEST(colm(m,4) == c4);
            DLIB_TEST(colm(m,5) == c5);
1134
1135
        }

1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153

        {
            matrix<double> m(2,2), mr(2,2), mr_max(2,2);

            m = 1, 2,
                0, 4;

            mr = 1, 1.0/2.0,
                0,  1.0/4.0;

            mr_max = 1, 1.0/2.0,
                     std::numeric_limits<double>::max(),  1.0/4.0;

            DLIB_TEST(equal(reciprocal(m), mr));
            DLIB_TEST(equal(reciprocal_max(m), mr_max));

        }

1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
        {
            matrix<double> m1, m2;
            m1.set_size(3,1);
            m2.set_size(1,3);

            m1 = 1,2,3;
            m2 = 4,5,6;
            DLIB_TEST(dot(m1, m2)               == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(m1, trans(m2))        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), m2)        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), trans(m2)) == 1*4 + 2*5 + 3*6);
        }

        {
            matrix<double,3,1> m1, m2;
            m1.set_size(3,1);
            m2.set_size(3,1);

            m1 = 1,2,3;
            m2 = 4,5,6;
            DLIB_TEST(dot(m1, m2)               == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(m1, trans(m2))        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), m2)        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), trans(m2)) == 1*4 + 2*5 + 3*6);
        }
        {
            matrix<double,1,3> m1, m2;
            m1.set_size(1,3);
            m2.set_size(1,3);

            m1 = 1,2,3;
            m2 = 4,5,6;
            DLIB_TEST(dot(m1, m2)               == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(m1, trans(m2))        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), m2)        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), trans(m2)) == 1*4 + 2*5 + 3*6);
        }
        {
            matrix<double,1,3> m1;
            matrix<double> m2;
            m1.set_size(1,3);
            m2.set_size(3,1);

            m1 = 1,2,3;
            m2 = 4,5,6;
            DLIB_TEST(dot(m1, m2)               == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(m1, trans(m2))        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), m2)        == 1*4 + 2*5 + 3*6);
            DLIB_TEST(dot(trans(m1), trans(m2)) == 1*4 + 2*5 + 3*6);
        }
1204
1205
1206
1207
1208
1209
1210

        {
            matrix<double> m1(3,3), m2(3,3);

            m1 = 1;
            m2 = 1;
            m1 = m1*subm(m2,0,0,3,3);
1211
            DLIB_TEST(is_finite(m1));
1212
1213
1214
1215
1216
1217
1218
1219
1220
        }
        {
            matrix<double,3,1> m1;
            matrix<double> m2(3,3);

            m1 = 1;
            m2 = 1;
            m1 = subm(m2,0,0,3,3)*m1;
        }
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256

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

            m = 3,3;
            m /= m(0);

            DLIB_TEST(m(0) == 1);
            DLIB_TEST(m(1) == 1);
        }
        {
            matrix<int> m(2,1);

            m = 3,3;
            m *= m(0);

            DLIB_TEST(m(0) == 9);
            DLIB_TEST(m(1) == 9);
        }
        {
            matrix<int> m(2,1);

            m = 3,3;
            m -= m(0);

            DLIB_TEST(m(0) == 0);
            DLIB_TEST(m(1) == 0);
        }
        {
            matrix<int> m(2,1);

            m = 3,3;
            m += m(0);

            DLIB_TEST(m(0) == 6);
            DLIB_TEST(m(1) == 6);
1257
            DLIB_TEST(is_finite(m));
1258
        }
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271


        {
            matrix<double> m(3,3);
            m = 3;
            m(1,1) = std::numeric_limits<double>::infinity();
            DLIB_TEST(is_finite(m) == false);
            m(1,1) = -std::numeric_limits<double>::infinity();
            DLIB_TEST(is_finite(m) == false);
            m(1,1) = 2;
            DLIB_TEST(is_finite(m));
        }

1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
    }






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

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

}